3
0

index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. const constructKeys = function (keyDescriptions) {
  24. const keys = [];
  25. function fillKeys (keyDescriptions) {
  26. keyDescriptions.forEach(each => {
  27. if (typeof each === 'number' || isMapKey(each)) keys.push(each);
  28. else if (Array.isArray(each)) fillKeys(each);
  29. else keys.push(...each.toString().split('.'));
  30. });
  31. }
  32. fillKeys(keyDescriptions);
  33. return keys;
  34. };
  35. const getKey = (x, key) => x == null ? undefined : isMapKey(key) ? x.get(key.key) : x[key];
  36. const getKeyWithCheck = (x, key) =>
  37. x == null ? null :
  38. isMapKey(key) ? x.has(key.key) ? {value: x.get(key.key)} : null :
  39. x.hasOwnProperty(key) ? {value: x[key]} : null;
  40. export const deepGetOrNil = (...keyDescriptions) => {
  41. const keys = constructKeys(keyDescriptions);
  42. return obj => keys.reduce(getKey, obj);
  43. };
  44. export const deget = deepGetOrNil;
  45. const chain = (list, stop, step) => (function worker (index) {
  46. return index >= list.length ? stop : step(list[index], worker(index + 1));
  47. })(0);
  48. const copyOnModification = (obj, key, value, modifierFn) => {
  49. const modified = modifierFn(value);
  50. return value === modified ? obj : copyWith(key, modified)(obj);
  51. };
  52. export const deepCopyOnWrite = (...keyDescriptions) => {
  53. const keys = constructKeys(keyDescriptions);
  54. return val => chain(keys, () => val, (key, next) => x =>
  55. copyOnModification(x, key, getKey(x, key), next)
  56. );
  57. };
  58. export const decow = deepCopyOnWrite;
  59. export const deepCopyOnMap = (...keyDescriptions) => {
  60. const keys = constructKeys(keyDescriptions);
  61. return fn => chain(keys, fn, (key, next) => x => {
  62. const valueWithCheck = getKeyWithCheck(x, key);
  63. if (valueWithCheck == null) return x;
  64. return copyOnModification(x, key, valueWithCheck.value, next);
  65. });
  66. };
  67. export const decomap = deepCopyOnMap;
  68. export const cowWorkshop = (keys, fn = x => x) => (obj, {result = obj, resultKeys = keys, diff} = {}) => {
  69. keys.forEach((key, index) => {
  70. const value = fn(deget(key)(obj));
  71. if (typeof value === "undefined") return;
  72. const modifier = decow(resultKeys[index])(value);
  73. const oldResult = result;
  74. result = modifier(oldResult);
  75. if (result !== oldResult) {
  76. diff = modifier(diff);
  77. }
  78. });
  79. return {result, diff};
  80. };