index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const copyArrayWith = (key, value) => obj => {
  2. const result = obj == null ? [] : [...obj];
  3. result[key] = value;
  4. return result;
  5. };
  6. const copyMapWith = (key, value) => obj => {
  7. const result = obj == null ? new Map() : new Map(obj);
  8. result.set(key, value);
  9. return result;
  10. };
  11. const copyObjectWith = (key, value) => obj => {
  12. const result = obj == null ? {} : {...obj};
  13. result[key] = value;
  14. return result;
  15. };
  16. const isMapKey = key => typeof key === 'object' && key.isKeyInMap === true;
  17. export const keyInMap = key => ({isKeyInMap: true, key});
  18. export const kim = keyInMap;
  19. const copyWith = (key, value) =>
  20. typeof key === 'number' ? copyArrayWith(key, value) :
  21. isMapKey(key) ? copyMapWith(key.key, value) :
  22. copyObjectWith(key, value);
  23. function parseKeysInto (keyDescriptions, keys) {
  24. keyDescriptions.forEach(each => {
  25. if (typeof each === 'number' || isMapKey(each)) keys.push(each);
  26. else if (Array.isArray(each)) fillKeys(each);
  27. else keys.push(...each.toString().split('.'));
  28. });
  29. return keys;
  30. }
  31. const getKey = (x, key) => x == null ? undefined : isMapKey(key) ? x.get(key.key) : x[key];
  32. const getKeyWithCheck = (x, key) =>
  33. x == null ? null :
  34. isMapKey(key) ? x.has(key.key) ? {value: x.get(key.key)} : null :
  35. x.hasOwnProperty(key) ? {value: x[key]} : null;
  36. const chain = (list, stop, step) => (function worker (index) {
  37. return index >= list.length ? stop : step(list[index], worker(index + 1));
  38. })(0);
  39. const copyOnModification = (obj, key, value, modifierFn) => {
  40. const modified = modifierFn(value);
  41. return value === modified ? obj : copyWith(key, modified)(obj);
  42. };
  43. const atPathBeginningWith = prefix => (...keyDescriptions) => {
  44. const keys = parseKeysInto(keyDescriptions, [...prefix]);
  45. return Object.assign(atPathBeginningWith(keys), {
  46. get: obj => keys.reduce(getKey, obj),
  47. put: val => chain(keys, () => val, (key, next) => x =>
  48. copyOnModification(x, key, getKey(x, key), next)
  49. ),
  50. map: fn => chain(keys, fn, (key, next) => x => {
  51. const valueWithCheck = getKeyWithCheck(x, key);
  52. if (valueWithCheck == null) return x;
  53. return copyOnModification(x, key, valueWithCheck.value, next);
  54. })
  55. });
  56. };
  57. export const atPath = atPathBeginningWith([]);
  58. export const cowWorkshop = (keys, fn = x => x) => (obj, {result = obj, resultKeys = keys, diff} = {}) => {
  59. keys.forEach((key, index) => {
  60. const value = fn(deget(key)(obj));
  61. if (typeof value === "undefined") return;
  62. const modifier = decow(resultKeys[index])(value);
  63. const oldResult = result;
  64. result = modifier(oldResult);
  65. if (result !== oldResult) {
  66. diff = modifier(diff);
  67. }
  68. });
  69. return {result, diff};
  70. };