cow-value-model.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
  6. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
  7. function copyWith(obj, key, value) {
  8. var result = typeof key === 'number' ? [].concat(_toConsumableArray(obj)) : _extends({}, obj);
  9. result[key] = value;
  10. return result;
  11. }
  12. var cowValueModel = exports.cowValueModel = function cowValueModel(key) {
  13. var keys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : key.split('.');
  14. function setField() {
  15. var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  16. var index = arguments[1];
  17. var val = arguments[2];
  18. if (index >= keys.length) return val;
  19. var key = keys[index],
  20. value = x[key],
  21. modified = setField(value, index + 1, val);
  22. return value === modified ? x : copyWith(x, key, modified);
  23. }
  24. var GET_SENTINEL = {};
  25. return function (obj) {
  26. var val = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : GET_SENTINEL;
  27. return val === GET_SENTINEL ? keys.reduce(function () {
  28. var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  29. var key = arguments[1];
  30. return x[key];
  31. }, obj) : setField(obj, 0, val);
  32. };
  33. };