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