BVB Source Codes

reactide Show core.dict.js Source code

Return Download reactide: download core.dict.js Source code - Download reactide Source code - Type:.js
  1. 'use strict';
  2. var ctx            = require('./_ctx')
  3.   , $export        = require('./_export')
  4.   , createDesc     = require('./_property-desc')
  5.   , assign         = require('./_object-assign')
  6.   , create         = require('./_object-create')
  7.   , getPrototypeOf = require('./_object-gpo')
  8.   , getKeys        = require('./_object-keys')
  9.   , dP             = require('./_object-dp')
  10.   , keyOf          = require('./_keyof')
  11.   , aFunction      = require('./_a-function')
  12.   , forOf          = require('./_for-of')
  13.   , isIterable     = require('./core.is-iterable')
  14.   , $iterCreate    = require('./_iter-create')
  15.   , step           = require('./_iter-step')
  16.   , isObject       = require('./_is-object')
  17.   , toIObject      = require('./_to-iobject')
  18.   , DESCRIPTORS    = require('./_descriptors')
  19.   , 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
  29. var createDictMethod = function(TYPE){
  30.   var IS_MAP   = TYPE == 1
  31.     , IS_EVERY = TYPE == 4;
  32.   return function(object, callbackfn, that /* = undefined */){
  33.     var f      = ctx(callbackfn, that, 3)
  34.       , O      = toIObject(object)
  35.       , result = IS_MAP || TYPE == 7 || TYPE == 2
  36.           ? new (typeof this == 'function' ? this : Dict) : undefined
  37.       , 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. };
  55. var findKey = createDictMethod(6);
  56.  
  57. var createDictIter = function(kind){
  58.   return function(it){
  59.     return new DictIterator(it, kind);
  60.   };
  61. };
  62. var 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.     , O    = that._t
  71.     , keys = that._a
  72.     , kind = that._k
  73.     , 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.  
  85. function 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. }
  96. Dict.prototype = null;
  97.  
  98. function reduce(object, mapfn, init){
  99.   aFunction(mapfn);
  100.   var O      = toIObject(object)
  101.     , keys   = getKeys(O)
  102.     , length = keys.length
  103.     , i      = 0
  104.     , 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.  
  115. function includes(object, el){
  116.   return (el == el ? keyOf(object, el) : findKey(object, function(it){
  117.     return it != it;
  118.   })) !== undefined;
  119. }
  120.  
  121. function get(object, key){
  122.   if(has(object, key))return object[key];
  123. }
  124. function set(object, key, value){
  125.   if(DESCRIPTORS && key in Object)dP.f(object, key, createDesc(0, value));
  126.   else object[key] = value;
  127.   return object;
  128. }
  129.  
  130. function isDict(it){
  131.   return isObject(it) && getPrototypeOf(it) === Dict.prototype;
  132. }
  133.  
  134. $export($export.G + $export.F, {Dict: Dict});
  135.  
  136. $export($export.S, 'Dict', {
  137.   keys:     createDictIter('keys'),
  138.   values:   createDictIter('values'),
  139.   entries:  createDictIter('entries'),
  140.   forEach:  createDictMethod(0),
  141.   map:      createDictMethod(1),
  142.   filter:   createDictMethod(2),
  143.   some:     createDictMethod(3),
  144.   every:    createDictMethod(4),
  145.   find:     createDictMethod(5),
  146.   findKey:  findKey,
  147.   mapPairs: createDictMethod(7),
  148.   reduce:   reduce,
  149.   keyOf:    keyOf,
  150.   includes: includes,
  151.   has:      has,
  152.   get:      get,
  153.   set:      set,
  154.   isDict:   isDict
  155. });
downloadcore.dict.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top