PageRenderTime 34ms CodeModel.GetById 13ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/babel-polyfill/node_modules/core-js/library/modules/core.dict.js

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