core.dict.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. var ctx = require('./_ctx');
  3. var $export = require('./_export');
  4. var createDesc = require('./_property-desc');
  5. var assign = require('./_object-assign');
  6. var create = require('./_object-create');
  7. var getPrototypeOf = require('./_object-gpo');
  8. var getKeys = require('./_object-keys');
  9. var dP = require('./_object-dp');
  10. var keyOf = require('./_keyof');
  11. var aFunction = require('./_a-function');
  12. var forOf = require('./_for-of');
  13. var isIterable = require('./core.is-iterable');
  14. var $iterCreate = require('./_iter-create');
  15. var step = require('./_iter-step');
  16. var isObject = require('./_is-object');
  17. var toIObject = require('./_to-iobject');
  18. var DESCRIPTORS = require('./_descriptors');
  19. var has = require('./_has');
  20. // 0 -> Dict.forEach
  21. // 1 -> Dict.map
  22. // 2 -> Dict.filter
  23. // 3 -> Dict.some
  24. // 4 -> Dict.every
  25. // 5 -> Dict.find
  26. // 6 -> Dict.findKey
  27. // 7 -> Dict.mapPairs
  28. var createDictMethod = function (TYPE) {
  29. var IS_MAP = TYPE == 1;
  30. var IS_EVERY = TYPE == 4;
  31. return function (object, callbackfn, that /* = undefined */) {
  32. var f = ctx(callbackfn, that, 3);
  33. var O = toIObject(object);
  34. var result = IS_MAP || TYPE == 7 || TYPE == 2
  35. ? new (typeof this == 'function' ? this : Dict)() : undefined;
  36. var key, val, res;
  37. for (key in O) if (has(O, key)) {
  38. val = O[key];
  39. res = f(val, key, object);
  40. if (TYPE) {
  41. if (IS_MAP) result[key] = res; // map
  42. else if (res) switch (TYPE) {
  43. case 2: result[key] = val; break; // filter
  44. case 3: return true; // some
  45. case 5: return val; // find
  46. case 6: return key; // findKey
  47. case 7: result[res[0]] = res[1]; // mapPairs
  48. } else if (IS_EVERY) return false; // every
  49. }
  50. }
  51. return TYPE == 3 || IS_EVERY ? IS_EVERY : result;
  52. };
  53. };
  54. var findKey = createDictMethod(6);
  55. var createDictIter = function (kind) {
  56. return function (it) {
  57. return new DictIterator(it, kind);
  58. };
  59. };
  60. var DictIterator = function (iterated, kind) {
  61. this._t = toIObject(iterated); // target
  62. this._a = getKeys(iterated); // keys
  63. this._i = 0; // next index
  64. this._k = kind; // kind
  65. };
  66. $iterCreate(DictIterator, 'Dict', function () {
  67. var that = this;
  68. var O = that._t;
  69. var keys = that._a;
  70. var kind = that._k;
  71. var key;
  72. do {
  73. if (that._i >= keys.length) {
  74. that._t = undefined;
  75. return step(1);
  76. }
  77. } while (!has(O, key = keys[that._i++]));
  78. if (kind == 'keys') return step(0, key);
  79. if (kind == 'values') return step(0, O[key]);
  80. return step(0, [key, O[key]]);
  81. });
  82. function Dict(iterable) {
  83. var dict = create(null);
  84. if (iterable != undefined) {
  85. if (isIterable(iterable)) {
  86. forOf(iterable, true, function (key, value) {
  87. dict[key] = value;
  88. });
  89. } else assign(dict, iterable);
  90. }
  91. return dict;
  92. }
  93. Dict.prototype = null;
  94. function reduce(object, mapfn, init) {
  95. aFunction(mapfn);
  96. var O = toIObject(object);
  97. var keys = getKeys(O);
  98. var length = keys.length;
  99. var i = 0;
  100. var memo, key;
  101. if (arguments.length < 3) {
  102. if (!length) throw TypeError('Reduce of empty object with no initial value');
  103. memo = O[keys[i++]];
  104. } else memo = Object(init);
  105. while (length > i) if (has(O, key = keys[i++])) {
  106. memo = mapfn(memo, O[key], key, object);
  107. }
  108. return memo;
  109. }
  110. function includes(object, el) {
  111. // eslint-disable-next-line no-self-compare
  112. return (el == el ? keyOf(object, el) : findKey(object, function (it) {
  113. // eslint-disable-next-line no-self-compare
  114. return it != it;
  115. })) !== undefined;
  116. }
  117. function get(object, key) {
  118. if (has(object, key)) return object[key];
  119. }
  120. function set(object, key, value) {
  121. if (DESCRIPTORS && key in Object) dP.f(object, key, createDesc(0, value));
  122. else object[key] = value;
  123. return object;
  124. }
  125. function isDict(it) {
  126. return isObject(it) && getPrototypeOf(it) === Dict.prototype;
  127. }
  128. $export($export.G + $export.F, { Dict: Dict });
  129. $export($export.S, 'Dict', {
  130. keys: createDictIter('keys'),
  131. values: createDictIter('values'),
  132. entries: createDictIter('entries'),
  133. forEach: createDictMethod(0),
  134. map: createDictMethod(1),
  135. filter: createDictMethod(2),
  136. some: createDictMethod(3),
  137. every: createDictMethod(4),
  138. find: createDictMethod(5),
  139. findKey: findKey,
  140. mapPairs: createDictMethod(7),
  141. reduce: reduce,
  142. keyOf: keyOf,
  143. includes: includes,
  144. has: has,
  145. get: get,
  146. set: set,
  147. isDict: isDict
  148. });