Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. const isObject = value => typeof value === 'object' && value !== null;
  3. // Customized for this use-case
  4. const isObjectCustom = value =>
  5. isObject(value) &&
  6. !(value instanceof RegExp) &&
  7. !(value instanceof Error) &&
  8. !(value instanceof Date);
  9. const mapObject = (object, mapper, options, isSeen = new WeakMap()) => {
  10. options = {
  11. deep: false,
  12. target: {},
  13. ...options
  14. };
  15. if (isSeen.has(object)) {
  16. return isSeen.get(object);
  17. }
  18. isSeen.set(object, options.target);
  19. const {target} = options;
  20. delete options.target;
  21. const mapArray = array => array.map(element => isObjectCustom(element) ? mapObject(element, mapper, options, isSeen) : element);
  22. if (Array.isArray(object)) {
  23. return mapArray(object);
  24. }
  25. for (const [key, value] of Object.entries(object)) {
  26. let [newKey, newValue, {shouldRecurse = true} = {}] = mapper(key, value, object);
  27. // Drop `__proto__` keys.
  28. if (newKey === '__proto__') {
  29. continue;
  30. }
  31. if (options.deep && shouldRecurse && isObjectCustom(newValue)) {
  32. newValue = Array.isArray(newValue) ?
  33. mapArray(newValue) :
  34. mapObject(newValue, mapper, options, isSeen);
  35. }
  36. target[newKey] = newValue;
  37. }
  38. return target;
  39. };
  40. module.exports = (object, mapper, options) => {
  41. if (!isObject(object)) {
  42. throw new TypeError(`Expected an object, got \`${object}\` (${typeof object})`);
  43. }
  44. return mapObject(object, mapper, options);
  45. };