Skip to content

Commit

Permalink
optimize code for current minification rules - use named functions on…
Browse files Browse the repository at this point in the history
…ly for exported functions
  • Loading branch information
zloirock committed Jul 31, 2015
1 parent 9f5fa6a commit cacbf43
Show file tree
Hide file tree
Showing 40 changed files with 291 additions and 299 deletions.
8 changes: 4 additions & 4 deletions library/modules/$.collection-strong.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var $ = require('./$')
, SIZE = SUPPORT_DESC ? '_s' : 'size'
, id = 0;

function fastKey(it, create){
var fastKey = function(it, create){
// return primitive with prefix
if(!isObject(it))return typeof it == 'symbol' ? it : (typeof it == 'string' ? 'S' : 'P') + it;
if(!$has(it, ID)){
Expand All @@ -27,17 +27,17 @@ function fastKey(it, create){
hide(it, ID, ++id);
// return object id with prefix
} return 'O' + it[ID];
}
};

function getEntry(that, key){
var getEntry = function(that, key){
// fast case
var index = fastKey(key), entry;
if(index !== 'F')return that._i[index];
// frozen object case
for(entry = that._f; entry; entry = entry.n){
if(entry.k == key)return entry;
}
}
};

module.exports = {
getConstructor: function(wrapper, NAME, IS_MAP, ADDER){
Expand Down
18 changes: 9 additions & 9 deletions library/modules/$.collection-weak.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ var hide = require('./$.hide')
, id = 0;

// fallback for frozen keys
function frozenStore(that){
var frozenStore = function(that){
return that._l || (that._l = new FrozenStore);
}
function FrozenStore(){
};
var FrozenStore = function(){
this.a = [];
}
};
var findFrozen = function(store, key){
return find(store.a, function(it){
return it[0] === key;
});
};
FrozenStore.prototype = {
get: function(key){
var entry = findFrozen(this, key);
Expand All @@ -40,11 +45,6 @@ FrozenStore.prototype = {
return !!~index;
}
};
function findFrozen(store, key){
return find(store.a, function(it){
return it[0] === key;
});
}

module.exports = {
getConstructor: function(wrapper, NAME, IS_MAP, ADDER){
Expand Down
22 changes: 11 additions & 11 deletions library/modules/$.def.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
var global = require('./$.global')
, core = require('./$.core')
, PROTOTYPE = 'prototype';
function ctx(fn, that){
var ctx = function(fn, that){
return function(){
return fn.apply(that, arguments);
};
}
// type bitmap
$def.F = 1; // forced
$def.G = 2; // global
$def.S = 4; // static
$def.P = 8; // proto
$def.B = 16; // bind
$def.W = 32; // wrap
function $def(type, name, source){
};
var $def = function(type, name, source){
var key, own, out, exp
, isGlobal = type & $def.G
, isProto = type & $def.P
Expand Down Expand Up @@ -43,5 +36,12 @@ function $def(type, name, source){
exports[key] = exp;
if(isProto)(exports[PROTOTYPE] || (exports[PROTOTYPE] = {}))[key] = out;
}
}
};
// type bitmap
$def.F = 1; // forced
$def.G = 2; // global
$def.S = 4; // static
$def.P = 8; // proto
$def.B = 16; // bind
$def.W = 32; // wrap
module.exports = $def;
2 changes: 1 addition & 1 deletion library/modules/$.flags.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var anObject = require('./$.an-object');
module.exports = function flags(){
module.exports = function(){
var that = anObject(this)
, result = '';
if(that.global)result += 'g';
Expand Down
4 changes: 2 additions & 2 deletions library/modules/$.get-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ var toString = {}.toString
var windowNames = typeof window == 'object' && Object.getOwnPropertyNames
? Object.getOwnPropertyNames(window) : [];

function getWindowNames(it){
var getWindowNames = function(it){
try {
return getNames(it);
} catch(e){
return windowNames.slice();
}
}
};

module.exports.get = function getOwnPropertyNames(it){
if(windowNames && toString.call(it) == '[object Window]')return getWindowNames(it);
Expand Down
4 changes: 2 additions & 2 deletions library/modules/$.iter-call.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var anObject = require('./$.an-object');
function close(iterator){
var close = function(iterator){
var ret = iterator['return'];
if(ret !== undefined)anObject(ret.call(iterator));
}
};
module.exports = function(iterator, fn, value, entries){
try {
return entries ? fn(anObject(value)[0], value[1]) : fn(value);
Expand Down
6 changes: 3 additions & 3 deletions library/modules/$.iter-define.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ var LIBRARY = require('./$.library')
, FF_ITERATOR = '@@iterator'
, KEYS = 'keys'
, VALUES = 'values';
function returnThis(){ return this; }
var returnThis = function(){ return this; };
module.exports = function(Base, NAME, Constructor, next, DEFAULT, IS_SET, FORCE){
require('./$.iter-create')(Constructor, NAME, next);
function createMethod(kind){
var createMethod = function(kind){
switch(kind){
case KEYS: return function keys(){ return new Constructor(this, kind); };
case VALUES: return function values(){ return new Constructor(this, kind); };
} return function entries(){ return new Constructor(this, kind); };
}
};
var TAG = NAME + ' Iterator'
, proto = Base.prototype
, _native = proto[SYMBOL_ITERATOR] || proto[FF_ITERATOR] || DEFAULT && proto[DEFAULT]
Expand Down
4 changes: 2 additions & 2 deletions library/modules/$.set-proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
var getDesc = require('./$').getDesc
, isObject = require('./$.is-object')
, anObject = require('./$.an-object');
function check(O, proto){
var check = function(O, proto){
anObject(O);
if(!isObject(proto) && proto !== null)throw TypeError(proto + ": can't set as prototype!");
}
};
module.exports = {
set: Object.setPrototypeOf || ('__proto__' in {} // eslint-disable-line
? function(buggy, set){
Expand Down
8 changes: 4 additions & 4 deletions library/modules/$.task.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ var ctx = require('./$.ctx')
, queue = {}
, ONREADYSTATECHANGE = 'onreadystatechange'
, defer, channel, port;
function run(){
var run = function(){
var id = +this;
if(queue.hasOwnProperty(id)){
var fn = queue[id];
delete queue[id];
fn();
}
}
function listner(event){
};
var listner = function(event){
run.call(event.data);
}
};
// Node.js 0.9+ & IE10+ has setImmediate, otherwise:
if(!setTask || !clearTask){
setTask = function setImmediate(fn){
Expand Down
95 changes: 46 additions & 49 deletions library/modules/core.dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,7 @@ var $ = require('./$')
, toObject = require('./$.to-object')
, SUPPORT_DESC = require('./$.support-desc')
, has = require('./$.has')
, getKeys = $.getKeys
, findKey = createDictMethod(6);

function Dict(iterable){
var dict = $.create(null);
if(iterable != undefined){
if(isIterable(iterable)){
forOf(iterable, true, function(key, value){
dict[key] = value;
});
} else assign(dict, iterable);
}
return dict;
}
Dict.prototype = null;

function DictIterator(iterated, kind){
this._t = toObject(iterated); // target
this._a = getKeys(iterated); // keys
this._i = 0; // next index
this._k = kind; // kind
}
require('./$.iter-create')(DictIterator, 'Dict', function(){
var that = this
, O = that._t
, keys = that._a
, kind = that._k
, key;
do {
if(that._i >= keys.length){
that._t = undefined;
return step(1);
}
} while(!has(O, key = keys[that._i++]));
if(kind == 'keys' )return step(0, key);
if(kind == 'values')return step(0, O[key]);
return step(0, [key, O[key]]);
});
function createDictIter(kind){
return function(it){
return new DictIterator(it, kind);
};
}
function generic(A, B){
// strange IE quirks mode bug -> use typeof instead of isFunction
return typeof A == 'function' ? A : B;
}
, getKeys = $.getKeys;

// 0 -> Dict.forEach
// 1 -> Dict.map
Expand All @@ -68,13 +22,14 @@ function generic(A, B){
// 5 -> Dict.find
// 6 -> Dict.findKey
// 7 -> Dict.mapPairs
function createDictMethod(TYPE){
var createDictMethod = function(TYPE){
var IS_MAP = TYPE == 1
, IS_EVERY = TYPE == 4;
return function(object, callbackfn, that /* = undefined */){
var f = ctx(callbackfn, that, 3)
, O = toObject(object)
, result = IS_MAP || TYPE == 7 || TYPE == 2 ? new (generic(this, Dict)) : undefined
, result = IS_MAP || TYPE == 7 || TYPE == 2
? new (typeof this == 'function' ? this : Dict) : undefined
, key, val, res;
for(key in O)if(has(O, key)){
val = O[key];
Expand All @@ -92,7 +47,49 @@ function createDictMethod(TYPE){
}
return TYPE == 3 || IS_EVERY ? IS_EVERY : result;
};
};
var findKey = createDictMethod(6);

var createDictIter = function(kind){
return function(it){
return new DictIterator(it, kind);
};
};
var DictIterator = function(iterated, kind){
this._t = toObject(iterated); // target
this._a = getKeys(iterated); // keys
this._i = 0; // next index
this._k = kind; // kind
};
require('./$.iter-create')(DictIterator, 'Dict', function(){
var that = this
, O = that._t
, keys = that._a
, kind = that._k
, key;
do {
if(that._i >= keys.length){
that._t = undefined;
return step(1);
}
} while(!has(O, key = keys[that._i++]));
if(kind == 'keys' )return step(0, key);
if(kind == 'values')return step(0, O[key]);
return step(0, [key, O[key]]);
});

function Dict(iterable){
var dict = $.create(null);
if(iterable != undefined){
if(isIterable(iterable)){
forOf(iterable, true, function(key, value){
dict[key] = value;
});
} else assign(dict, iterable);
}
return dict;
}
Dict.prototype = null;

function reduce(object, mapfn, init){
aFunction(mapfn);
Expand Down
22 changes: 11 additions & 11 deletions library/modules/es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ var createDict = function(){
while(i--)delete createDict.prototype[keys1[i]];
return createDict();
};
function createGetKeys(names, length){
var createGetKeys = function(names, length){
return function(object){
var O = toObject(object)
, i = 0
Expand All @@ -113,8 +113,8 @@ function createGetKeys(names, length){
}
return result;
};
}
function Empty(){}
};
var Empty = function(){};
$def($def.S, 'Object', {
// 19.1.2.9 / 15.2.3.2 Object.getPrototypeOf(O)
getPrototypeOf: $.getProto = $.getProto || function(O){
Expand Down Expand Up @@ -166,23 +166,23 @@ $def($def.S, 'Object', {
}
});

function construct(F, len, args){
var construct = function(F, len, args){
if(!(len in factories)){
for(var n = [], i = 0; i < len; i++)n[i] = 'a[' + i + ']';
factories[len] = Function('F,a', 'return new F(' + n.join(',') + ')');
}
return factories[len](F, args);
}
};

// 19.2.3.2 / 15.3.4.5 Function.prototype.bind(thisArg, args...)
$def($def.P, 'Function', {
bind: function(that /*, args... */){
var fn = aFunction(this)
, partArgs = _slice.call(arguments, 1);
function bound(/* args... */){
var bound = function(/* args... */){
var args = partArgs.concat(_slice.call(arguments));
return this instanceof bound ? construct(fn, args.length, args) : invoke(fn, args, that);
}
};
if(isObject(fn.prototype))bound.prototype = fn.prototype;
return bound;
}
Expand Down Expand Up @@ -221,7 +221,7 @@ $def($def.P + $def.F * (ES5Object != Object), 'Array', {
// 22.1.2.2 / 15.4.3.2 Array.isArray(arg)
$def($def.S, 'Array', {isArray: function(arg){ return cof(arg) == 'Array'; }});

function createArrayReduce(isRight){
var createArrayReduce = function(isRight){
return function(callbackfn, memo){
aFunction(callbackfn);
var O = toObject(this)
Expand All @@ -244,7 +244,7 @@ function createArrayReduce(isRight){
}
return memo;
};
}
};
$def($def.P, 'Array', {
// 22.1.3.10 / 15.4.4.18 Array.prototype.forEach(callbackfn [, thisArg])
forEach: $.each = $.each || function forEach(callbackfn/*, that = undefined */){
Expand Down Expand Up @@ -292,9 +292,9 @@ $def($def.P, 'String', {trim: function trim(){ return $trim(this); }});
// 20.3.3.1 / 15.9.4.4 Date.now()
$def($def.S, 'Date', {now: function now(){ return +new Date; }});

function lz(num){
var lz = function(num){
return num > 9 ? num : '0' + num;
}
};

// 20.3.4.36 / 15.9.5.43 Date.prototype.toISOString()
// PhantomJS and old webkit had a broken Date implementation.
Expand Down
Loading

0 comments on commit cacbf43

Please sign in to comment.