cow-value-model.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. export const deepGetOrNil = (...keyDescriptions) => {
  37. const keys = constructKeys(keyDescriptions);
  38. return obj => keys.reduce(getKey, obj);
  39. };
  40. export const deget = deepGetOrNil;
  41. export const deepCopyOnWrite = (...keyDescriptions) => {
  42. const keys = constructKeys(keyDescriptions);
  43. function setVal (x, index, val) {
  44. if (index >= keys.length) return val;
  45. const key = keys[index],
  46. value = getKey(x, key),
  47. modified = setVal(value, index + 1, val);
  48. return value === modified ? x : copyWith(x, key, modified);
  49. }
  50. return val => obj => setVal(obj, 0, val);
  51. };
  52. export const decow = deepCopyOnWrite;
  53. export const cowValueModel = (...keyDescriptions) => {
  54. const GET_SENTINEL = {};
  55. return (obj, val = GET_SENTINEL) =>
  56. (val === GET_SENTINEL ?
  57. deget(...keyDescriptions) :
  58. decow(...keyDescriptions)(val))
  59. (obj);
  60. };
  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. };