Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

234 rindas
9.6 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var invariant = require("invariant");
  4. var hasOwnProperty = Object.prototype.hasOwnProperty;
  5. var splice = Array.prototype.splice;
  6. var toString = Object.prototype.toString;
  7. function type(obj) {
  8. return toString.call(obj).slice(8, -1);
  9. }
  10. var assign = Object.assign || /* istanbul ignore next */ (function (target, source) {
  11. getAllKeys(source).forEach(function (key) {
  12. if (hasOwnProperty.call(source, key)) {
  13. target[key] = source[key];
  14. }
  15. });
  16. return target;
  17. });
  18. var getAllKeys = typeof Object.getOwnPropertySymbols === 'function'
  19. ? function (obj) { return Object.keys(obj).concat(Object.getOwnPropertySymbols(obj)); }
  20. /* istanbul ignore next */
  21. : function (obj) { return Object.keys(obj); };
  22. function copy(object) {
  23. return Array.isArray(object)
  24. ? assign(object.constructor(object.length), object)
  25. : (type(object) === 'Map')
  26. ? new Map(object)
  27. : (type(object) === 'Set')
  28. ? new Set(object)
  29. : (object && typeof object === 'object')
  30. ? assign(Object.create(Object.getPrototypeOf(object)), object)
  31. /* istanbul ignore next */
  32. : object;
  33. }
  34. var Context = /** @class */ (function () {
  35. function Context() {
  36. this.commands = assign({}, defaultCommands);
  37. this.update = this.update.bind(this);
  38. // Deprecated: update.extend, update.isEquals and update.newContext
  39. this.update.extend = this.extend = this.extend.bind(this);
  40. this.update.isEquals = function (x, y) { return x === y; };
  41. this.update.newContext = function () { return new Context().update; };
  42. }
  43. Object.defineProperty(Context.prototype, "isEquals", {
  44. get: function () {
  45. return this.update.isEquals;
  46. },
  47. set: function (value) {
  48. this.update.isEquals = value;
  49. },
  50. enumerable: true,
  51. configurable: true
  52. });
  53. Context.prototype.extend = function (directive, fn) {
  54. this.commands[directive] = fn;
  55. };
  56. Context.prototype.update = function (object, $spec) {
  57. var _this = this;
  58. var spec = (typeof $spec === 'function') ? { $apply: $spec } : $spec;
  59. if (!(Array.isArray(object) && Array.isArray(spec))) {
  60. invariant(!Array.isArray(spec), 'update(): You provided an invalid spec to update(). The spec may ' +
  61. 'not contain an array except as the value of $set, $push, $unshift, ' +
  62. '$splice or any custom command allowing an array value.');
  63. }
  64. invariant(typeof spec === 'object' && spec !== null, 'update(): You provided an invalid spec to update(). The spec and ' +
  65. 'every included key path must be plain objects containing one of the ' +
  66. 'following commands: %s.', Object.keys(this.commands).join(', '));
  67. var nextObject = object;
  68. getAllKeys(spec).forEach(function (key) {
  69. if (hasOwnProperty.call(_this.commands, key)) {
  70. var objectWasNextObject = object === nextObject;
  71. nextObject = _this.commands[key](spec[key], nextObject, spec, object);
  72. if (objectWasNextObject && _this.isEquals(nextObject, object)) {
  73. nextObject = object;
  74. }
  75. }
  76. else {
  77. var nextValueForKey = type(object) === 'Map'
  78. ? _this.update(object.get(key), spec[key])
  79. : _this.update(object[key], spec[key]);
  80. var nextObjectValue = type(nextObject) === 'Map'
  81. ? nextObject.get(key)
  82. : nextObject[key];
  83. if (!_this.isEquals(nextValueForKey, nextObjectValue)
  84. || typeof nextValueForKey === 'undefined'
  85. && !hasOwnProperty.call(object, key)) {
  86. if (nextObject === object) {
  87. nextObject = copy(object);
  88. }
  89. if (type(nextObject) === 'Map') {
  90. nextObject.set(key, nextValueForKey);
  91. }
  92. else {
  93. nextObject[key] = nextValueForKey;
  94. }
  95. }
  96. }
  97. });
  98. return nextObject;
  99. };
  100. return Context;
  101. }());
  102. exports.Context = Context;
  103. var defaultCommands = {
  104. $push: function (value, nextObject, spec) {
  105. invariantPushAndUnshift(nextObject, spec, '$push');
  106. return value.length ? nextObject.concat(value) : nextObject;
  107. },
  108. $unshift: function (value, nextObject, spec) {
  109. invariantPushAndUnshift(nextObject, spec, '$unshift');
  110. return value.length ? value.concat(nextObject) : nextObject;
  111. },
  112. $splice: function (value, nextObject, spec, originalObject) {
  113. invariantSplices(nextObject, spec);
  114. value.forEach(function (args) {
  115. invariantSplice(args);
  116. if (nextObject === originalObject && args.length) {
  117. nextObject = copy(originalObject);
  118. }
  119. splice.apply(nextObject, args);
  120. });
  121. return nextObject;
  122. },
  123. $set: function (value, _nextObject, spec) {
  124. invariantSet(spec);
  125. return value;
  126. },
  127. $toggle: function (targets, nextObject) {
  128. invariantSpecArray(targets, '$toggle');
  129. var nextObjectCopy = targets.length ? copy(nextObject) : nextObject;
  130. targets.forEach(function (target) {
  131. nextObjectCopy[target] = !nextObject[target];
  132. });
  133. return nextObjectCopy;
  134. },
  135. $unset: function (value, nextObject, _spec, originalObject) {
  136. invariantSpecArray(value, '$unset');
  137. value.forEach(function (key) {
  138. if (Object.hasOwnProperty.call(nextObject, key)) {
  139. if (nextObject === originalObject) {
  140. nextObject = copy(originalObject);
  141. }
  142. delete nextObject[key];
  143. }
  144. });
  145. return nextObject;
  146. },
  147. $add: function (values, nextObject, _spec, originalObject) {
  148. invariantMapOrSet(nextObject, '$add');
  149. invariantSpecArray(values, '$add');
  150. if (type(nextObject) === 'Map') {
  151. values.forEach(function (_a) {
  152. var key = _a[0], value = _a[1];
  153. if (nextObject === originalObject && nextObject.get(key) !== value) {
  154. nextObject = copy(originalObject);
  155. }
  156. nextObject.set(key, value);
  157. });
  158. }
  159. else {
  160. values.forEach(function (value) {
  161. if (nextObject === originalObject && !nextObject.has(value)) {
  162. nextObject = copy(originalObject);
  163. }
  164. nextObject.add(value);
  165. });
  166. }
  167. return nextObject;
  168. },
  169. $remove: function (value, nextObject, _spec, originalObject) {
  170. invariantMapOrSet(nextObject, '$remove');
  171. invariantSpecArray(value, '$remove');
  172. value.forEach(function (key) {
  173. if (nextObject === originalObject && nextObject.has(key)) {
  174. nextObject = copy(originalObject);
  175. }
  176. nextObject.delete(key);
  177. });
  178. return nextObject;
  179. },
  180. $merge: function (value, nextObject, _spec, originalObject) {
  181. invariantMerge(nextObject, value);
  182. getAllKeys(value).forEach(function (key) {
  183. if (value[key] !== nextObject[key]) {
  184. if (nextObject === originalObject) {
  185. nextObject = copy(originalObject);
  186. }
  187. nextObject[key] = value[key];
  188. }
  189. });
  190. return nextObject;
  191. },
  192. $apply: function (value, original) {
  193. invariantApply(value);
  194. return value(original);
  195. },
  196. };
  197. var defaultContext = new Context();
  198. exports.isEquals = defaultContext.update.isEquals;
  199. exports.extend = defaultContext.extend;
  200. exports.default = defaultContext.update;
  201. // @ts-ignore
  202. exports.default.default = module.exports = assign(exports.default, exports);
  203. // invariants
  204. function invariantPushAndUnshift(value, spec, command) {
  205. invariant(Array.isArray(value), 'update(): expected target of %s to be an array; got %s.', command, value);
  206. invariantSpecArray(spec[command], command);
  207. }
  208. function invariantSpecArray(spec, command) {
  209. invariant(Array.isArray(spec), 'update(): expected spec of %s to be an array; got %s. ' +
  210. 'Did you forget to wrap your parameter in an array?', command, spec);
  211. }
  212. function invariantSplices(value, spec) {
  213. invariant(Array.isArray(value), 'Expected $splice target to be an array; got %s', value);
  214. invariantSplice(spec.$splice);
  215. }
  216. function invariantSplice(value) {
  217. invariant(Array.isArray(value), 'update(): expected spec of $splice to be an array of arrays; got %s. ' +
  218. 'Did you forget to wrap your parameters in an array?', value);
  219. }
  220. function invariantApply(fn) {
  221. invariant(typeof fn === 'function', 'update(): expected spec of $apply to be a function; got %s.', fn);
  222. }
  223. function invariantSet(spec) {
  224. invariant(Object.keys(spec).length === 1, 'Cannot have more than one key in an object with $set');
  225. }
  226. function invariantMerge(target, specValue) {
  227. invariant(specValue && typeof specValue === 'object', 'update(): $merge expects a spec of type \'object\'; got %s', specValue);
  228. invariant(target && typeof target === 'object', 'update(): $merge expects a target of type \'object\'; got %s', target);
  229. }
  230. function invariantMapOrSet(target, command) {
  231. var typeOfTarget = type(target);
  232. invariant(typeOfTarget === 'Map' || typeOfTarget === 'Set', 'update(): %s expects a target of type Set or Map; got %s', command, typeOfTarget);
  233. }