index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 => step => stop => (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. keyChain = chain(keys),
  46. putChain = keyChain((key, next) => x =>
  47. copyOnModification(x, key, getKey(x, key), next)
  48. ),
  49. mapChain = keyChain((key, next) => x => {
  50. const valueWithCheck = getKeyWithCheck(x, key);
  51. if (valueWithCheck == null) return x;
  52. return copyOnModification(x, key, valueWithCheck.value, next);
  53. });
  54. return Object.assign(atPathBeginningWith(keys), {
  55. get: obj => keys.reduce(getKey, obj),
  56. put: val => putChain(() => val),
  57. map: mapChain
  58. });
  59. };
  60. export const atPath = atPathBeginningWith([]);
  61. export const cowWorkshop = (keys, fn = x => x) => (obj, {result = obj, resultKeys = keys, diff} = {}) => {
  62. keys.forEach((key, index) => {
  63. const value = fn(deget(key)(obj));
  64. if (typeof value === "undefined") return;
  65. const modifier = decow(resultKeys[index])(value);
  66. const oldResult = result;
  67. result = modifier(oldResult);
  68. if (result !== oldResult) {
  69. diff = modifier(diff);
  70. }
  71. });
  72. return {result, diff};
  73. };