Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
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.

45 lines
853 B

  1. 'use strict';
  2. // customized for this use-case
  3. const isObject = x =>
  4. typeof x === 'object' &&
  5. x !== null &&
  6. !(x instanceof RegExp) &&
  7. !(x instanceof Error) &&
  8. !(x instanceof Date);
  9. module.exports = function mapObj(obj, fn, opts, seen) {
  10. opts = Object.assign({
  11. deep: false,
  12. target: {}
  13. }, opts);
  14. seen = seen || new WeakMap();
  15. if (seen.has(obj)) {
  16. return seen.get(obj);
  17. }
  18. seen.set(obj, opts.target);
  19. const target = opts.target;
  20. delete opts.target;
  21. for (const key of Object.keys(obj)) {
  22. const val = obj[key];
  23. const res = fn(key, val, obj);
  24. let newVal = res[1];
  25. if (opts.deep && isObject(newVal)) {
  26. if (Array.isArray(newVal)) {
  27. newVal = newVal.map(x => isObject(x) ? mapObj(x, fn, opts, seen) : x);
  28. } else {
  29. newVal = mapObj(newVal, fn, opts, seen);
  30. }
  31. }
  32. target[res[0]] = newVal;
  33. }
  34. return target;
  35. };