cow-value-model.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function copyArrayWith (obj, key, value) {
  2. const result = obj == null ? [] : [...obj];
  3. result[key] = value;
  4. return result;
  5. }
  6. function copyMapWith (obj, key, value) {
  7. const result = obj == null ? new Map() : new Map(obj);
  8. result.set(key, value);
  9. return result;
  10. }
  11. function copyObjectWith (obj, key, value) {
  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 = (obj, key, value) =>
  20. typeof key === 'number' ? copyArrayWith(obj, key, value) :
  21. isMapKey(key) ? copyMapWith(obj, key.key, value) :
  22. copyObjectWith(obj, 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(obj, key, modified);
  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 cowValueModel = (...keyDescriptions) => {
  69. const GET_SENTINEL = {};
  70. return (obj, val = GET_SENTINEL) =>
  71. (val === GET_SENTINEL ?
  72. deget(...keyDescriptions) :
  73. decow(...keyDescriptions)(val))
  74. (obj);
  75. };
  76. export const cowWorkshop = (keys, fn = x => x) => (obj, {result = obj, resultKeys = keys, diff} = {}) => {
  77. keys.forEach((key, index) => {
  78. const value = fn(deget(key)(obj));
  79. if (typeof value === "undefined") return;
  80. const modifier = decow(resultKeys[index])(value);
  81. const oldResult = result;
  82. result = modifier(oldResult);
  83. if (result !== oldResult) {
  84. diff = modifier(diff);
  85. }
  86. });
  87. return {result, diff};
  88. };