cow-value-model.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  7. var cowValueModel = exports.cowValueModel = function cowValueModel(key) {
  8. var keys = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : key.split('.');
  9. function setField() {
  10. var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  11. var index = arguments[1];
  12. var val = arguments[2];
  13. if (index >= keys.length) return val;
  14. var key = keys[index],
  15. value = x[key],
  16. modified = setField(value, index + 1, val);
  17. return value === modified ? x : _extends({}, x, _defineProperty({}, key, modified));
  18. }
  19. return {
  20. key: key,
  21. get: function get(obj) {
  22. return keys.reduce(function () {
  23. var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  24. var key = arguments[1];
  25. return x[key];
  26. }, obj);
  27. },
  28. set: function set(obj, val) {
  29. return setField(obj, 0, val);
  30. }
  31. };
  32. };