Can build as library
Change callback args {Array::, Array, Object, Map, Set}reduceTo
Object::tie additional args & placeholder
add Function::by ???
This commit is contained in:
zloirock
2014-02-27 14:18:01 +07:00
parent 76eaee0ed5
commit e5ff47cc43
35 changed files with 3270 additions and 528 deletions
+9 -22
View File
@@ -1,23 +1,3 @@
/*
option 'all' => 'enable all modules'
option 'node' => 'enable all modules for node.js'
option 'es5' => 'enable ES5 polyfill'
option 'es6' => 'enable ES6 polyfill'
option 'es6c' => 'enable ES6 collections (Map and Set) polyfill'
option 'timers' => 'enable timers polyfill'
option 'global' => '`global` is one reference for global object on node and browser'
option 'iz' => 'enable iz'
option 'function' => 'enable extend Function and Function.prototype'
option 'object' => 'enable extend Object'
option 'array' => 'enable extend Array and Array.prototype'
option 'number' => 'enable extend Number and Number.prototype'
option 'string' => 'enable extend String.prototype'
option 'regexp' => 'enable extend RegExp and RegExp.prototype'
option 'date' => 'enable extend Date and Date.prototype'
option 'events' => 'enable EventEmitter'
option 'async' => 'enable async'
option 'console' => 'enable console'
*/
require! <[./core-stab ./build fs ./config]>
module.exports = (grunt)->
require \load-grunt-tasks <| grunt
@@ -43,6 +23,13 @@ module.exports = (grunt)->
options .= split \, .reduceTo -> @[it] = on
done = @async!
js <- build options
<- fs.writeFile if options.nodePath => './index.js' else './core.js', js
<- fs.writeFile grunt.option(\path) || './core.js', js
done!
grunt.registerTask \default <[build:all build:node,nodePath uglify]>
grunt.registerTask \all <[build:all]>
grunt.registerTask \node ->
grunt.option \path './index.js'
grunt.task.run <[build:node]>
grunt.registerTask \library ->
grunt.option \path './library.js'
grunt.task.run <[build:all,library]>
grunt.registerTask \default <[all node library uglify]>
+1 -1
View File
@@ -1,6 +1,6 @@
{banner} = require './config'
{readFile} = require \fs
modules = <[global init framework library es5 resume immediateInternal es6 es6c promise symbol extendedObjectAPI timers immediate function binding object array arrayStatics number string date extendCollections console]>
modules = <[init framework library es5 resume global immediateInternal es6 es6c promise symbol extendedObjectAPI timers immediate function binding object array arrayStatics number string date extendCollections console]>
required = <[init resume]>
module.exports = (opt, next)-> let @ = opt
import {+global, +es5, +timers, +node} if @all
+172 -133
View File
@@ -6,10 +6,6 @@
*/
!function(global, undefined){
'use strict';
/**
* Module : global
*/
global.global = global;
/**
* Module : init
*/
@@ -21,7 +17,6 @@ var prototype = 'prototype'
, String = global.String
, Number = global.Number
, RegExp = global.RegExp
, Date = global.Date
, Math = global.Math
, setTimeout = global.setTimeout
, clearTimeout = global.clearTimeout
@@ -31,9 +26,7 @@ var prototype = 'prototype'
, document = global.document
, Infinity = 1 / 0
, $Array = Array[prototype]
, $Number = Number[prototype]
, $Object = Object[prototype]
, $String = String[prototype]
, $Function = Function[prototype]
, console = global.console || {log: $Function};
@@ -64,40 +57,40 @@ function classof(it){
// Function:
var apply = $Function.apply
, call = $Function.call;
// unbind method from context
// foo.fn(arg1, arg2, ...) => fn(foo, arg1, arg2, ...)
function unbind(that){
return tie.call(that, 'call');
}
// simple bind context
function tie(key){
var that = this
, fn = that[key];
assertFunction(fn);
return function(){
return fn.apply(that, arguments);
}
}
// placeholder for partial apply
var _ = {};
// partial apply
function part(/*args...*/){
var fn = this
, lengthPart = arguments.length
, argsPart = Array(lengthPart)
, i = 0
var length = arguments.length
, args = Array(length)
, i = 0
, placeholder = false;
while(length > i)if((args[i] = arguments[i++]) === _)placeholder = true;
return createPartialApplication(this, args, length, placeholder, false);
}
function ctx(fn, that){
return function(){
return fn.apply(that, arguments);
}
}
function createPartialApplication(fn, argsPart, lengthPart, placeholder, bind, context){
assertFunction(fn);
while(lengthPart > i)if((argsPart[i] = arguments[i++]) === _)placeholder = true;
return function(/*args...*/){
var length = arguments.length
, i, j, args;
if(!placeholder && length === 0)return fn.apply(this, argsPart);
var that = bind ? context : this
, length = arguments.length
, i = 0, j = 0, args;
if(!placeholder && length == 0)return fn.apply(that, argsPart);
args = argsPart.slice();
i = j = 0;
if(placeholder)for(;lengthPart > i; i++)if(args[i] === _)args[i] = arguments[j++]
while(length > j)args.push(arguments[j++]);
return fn.apply(this, args);
return fn.apply(that, args);
}
}
// unbind method from context
// foo.fn(arg1, arg2, ...) => fn(foo, arg1, arg2, ...)
function unbind(that){
return function(){
return call.apply(that, arguments);
}
}
// add `this` as first argument
@@ -121,7 +114,7 @@ function has(object, key){
}
var isEnumerable = $Object.propertyIsEnumerable
, defineProperty = Object.defineProperty
, PROTO = '__proto__' in $Object
, __PROTO__ = '__proto__' in $Object
, DESCRIPTORS = 1;
// http://wiki.ecmascript.org/doku.php?id=strawman:extended_object_api
function getOwnPropertyDescriptors(object){
@@ -173,7 +166,11 @@ function reduceTo(target, callbackfn){
callbackfn = target;
target = {};
} else target = Object(target);
forEach.call(this, callbackfn, target);
assertFunction(callbackfn);
var self = ES5Object(this)
, length = toLength(self.length)
, i = 0;
for(;length > i; i++)i in self && callbackfn(target, self[i], i, this);
return target;
}
@@ -223,17 +220,21 @@ function descriptor(bitmap, value){
function hidden(object, key, value){
return defineProperty(object, key, descriptor(6, value));
}
var GLOBAL = 0
, STATIC = 1
, PROTO = 2;
/**
* Module : framework
* Module : library
*/
function extendBuiltInObject(target, source, forced /* = false */){
if(target)for(var key in source){
try {
has(source, key)
&& (forced || !has(target, key) || !isNative(target[key]))
&& delete target[key]
&& hidden(target, key, source[key]);
} catch(e){}
var $exports = typeof exports != 'undefined' ? exports : (global.C = {});
function $define(type, name, source, forced /* = false */){
var old = type == GLOBAL ? global : type == STATIC ? global[name] || {} : (global[name] || {})[prototype] || {}
, target = type == GLOBAL ? $exports : $exports[name] || ($exports[name] = {})
, key, prop;
for(key in source)if(has(source, key)){
prop = !forced && old && isNative(old[key]) ? old[key] : source[key];
target[key] = type == PROTO && isFunction(prop) ? unbind(prop) : prop;
}
}
/**
@@ -259,7 +260,7 @@ function extendBuiltInObject(target, source, forced /* = false */){
, nativeSlice = slice
, nativeJoin = $Array.join
// Create object with null as it's prototype
, createNullProtoObject = PROTO
, createNullProtoObject = __PROTO__
? function(){
return {__proto__: null};
}
@@ -332,7 +333,7 @@ function extendBuiltInObject(target, source, forced /* = false */){
return O;
}
}
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
/**
* 15.2.3.2 Object.getPrototypeOf ( O )
* http://es5.github.io/#x15.2.3.2
@@ -358,7 +359,7 @@ function extendBuiltInObject(target, source, forced /* = false */){
var result = new Empty();
if(Properties)defineProperties(result, Properties);
// add __proto__ for Object.getPrototypeOf shim
PROTO || result.constructor[prototype] === O || (result.__proto__ = O);
__PROTO__ || result.constructor[prototype] === O || (result.__proto__ = O);
return result;
},
/**
@@ -384,7 +385,7 @@ function extendBuiltInObject(target, source, forced /* = false */){
* 15.3.4.5 Function.prototype.bind (thisArg [, arg1 [, arg2, …]])
* http://es5.github.io/#x15.3.4.5
*/
extendBuiltInObject($Function, {
$define(PROTO, 'Function', {
bind: function(scope /*, args...*/){
var fn = this
, args = $slice(arguments, 1);
@@ -407,7 +408,7 @@ function extendBuiltInObject(target, source, forced /* = false */){
* http://api.jquery.com/jQuery.isArray/
* http://docs.angularjs.org/api/angular.isArray
*/
extendBuiltInObject(Array, {isArray: function(it){
$define(STATIC, 'Array', {isArray: function(it){
return classof(it) == 'Array'
}});
function forEach(callbackfn, thisArg /* = undefined */){
@@ -417,7 +418,7 @@ function extendBuiltInObject(target, source, forced /* = false */){
, i = 0;
for(;length > i; i++)i in self && callbackfn.call(thisArg, self[i], i, this);
}
extendBuiltInObject($Array, {
$define(PROTO, 'Array', {
/**
* 15.4.4.14 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
* http://es5.github.io/#x15.4.4.14
@@ -542,14 +543,14 @@ function extendBuiltInObject(target, source, forced /* = false */){
* 15.5.4.20 String.prototype.trim ( )
* http://es5.github.io/#x15.5.4.20
*/
extendBuiltInObject($String, {trim: function(){
$define(PROTO, 'String', {trim: function(){
return String(this).replace(trimRegExp, '');
}});
/**
* 15.9.4.4 Date.now ( )
* http://es5.github.io/#x15.9.4.4
*/
extendBuiltInObject(Date, {now: function(){
$define(STATIC, 'Date', {now: function(){
return +new Date;
}});
if(isFunction(trimRegExp))isFunction = function(it){
@@ -568,6 +569,10 @@ var create = Object.create
, forEach = $Array.forEach
, isArray = Array.isArray
, map = $Array.map;
/**
* Module : global
*/
$define(GLOBAL, undefined, {global: global});
/**
* Module : immediateInternal
*/
@@ -625,7 +630,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
} else if(isFunction(MessageChannel)){
channel = new MessageChannel();
channel.port1.onmessage = listner;
defer = tie.call(channel.port2, 'postMessage');
defer = ctx(channel.port2.postMessage, channel.port2);
// IE8-
// always run before timers, like nextTick => some problems with recursive call
} else if(document && onreadystatechange in document.createElement('script')){
@@ -658,7 +663,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
function sign(it){
return (it = +it) == 0 || it != it ? it : it < 0 ? -1 : 1;
}
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
/**
* The assign function is used to copy the values of all of the enumerable
* own properties from a source object to a target object.
@@ -682,7 +687,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://kangax.github.io/es5-compat-table/es6/#Object.setPrototypeOf
* work only if browser support __proto__, don't work with null proto objects
*/
PROTO && extendBuiltInObject(Object, {
__PROTO__ && $define(STATIC, 'Object', {
setPrototypeOf: function(O, proto){
assertObject(O);
assert(isObject(proto) || proto === null, "Can't set", proto, 'as prototype');
@@ -690,7 +695,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
return O;
}
});
extendBuiltInObject(Number, {
$define(STATIC, 'Number', {
/**
* 20.1.2.1 Number.EPSILON
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.epsilon
@@ -757,7 +762,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
, exp = Math.exp
, log = Math.log
, sqrt = Math.sqrt;
extendBuiltInObject(Math, {
$define(STATIC, 'Math', {
/**
* Returns an implementation-dependent approximation to the inverse hyperbolic cosine of x.
* 20.2.2.3 Math.acosh(x)
@@ -919,7 +924,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
}
});
/**
extendBuiltInObject(String, {
$define(STATIC, 'String', {
* 21.1.2.2 String.fromCodePoint ( ...codePoints)
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.fromcodepoint
* http://kangax.github.io/es5-compat-table/es6/#String.fromCodePoint
@@ -929,7 +934,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
raw: function(){ TODO }
});
*/
extendBuiltInObject($String, {
$define(PROTO, 'String', {
/**
* 21.1.3.3 String.prototype.codePointAt (pos)
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.codepointat
@@ -980,7 +985,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
return String(this).slice(position, position + searchString.length) === searchString;
}
});
extendBuiltInObject(Array, {
$define(STATIC, 'Array', {
/**
* 22.1.2.1 Array.from ( arrayLike , mapfn=undefined, thisArg=undefined )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
@@ -1021,7 +1026,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
}
return -1;
}
extendBuiltInObject($Array, {
$define(PROTO, 'Array', {
/**
* 22.1.3.3 Array.prototype.copyWithin (target, start, end = this.length)
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.copywithin
@@ -1152,8 +1157,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-map-objects
*/
if(!isNative(Map) || !has(Map[prototype], 'forEach')){
global.Map = Map = createCollectionConstructor('Map');
extendBuiltInObject(Map[prototype], {
Map = createCollectionConstructor('Map');
assign(Map[prototype], {
/**
* 23.1.3.1 Map.prototype.clear ()
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-map.prototype.clear
@@ -1220,8 +1225,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-set-objects
*/
if(!isNative(Set) || !has(Set[prototype], 'forEach')){
global.Set = Set = createCollectionConstructor('Set', 1);
extendBuiltInObject(Set[prototype], {
Set = createCollectionConstructor('Set', 1);
assign(Set[prototype], {
/**
* 23.2.3.1 Set.prototype.add ( value )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-set.prototype.add
@@ -1272,7 +1277,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
defineProperties(Set[prototype], sizeGetter);
} else {
// IE 11 fix
if(new Set([1]).size != 1)global.Set = fixCollectionConstructor(Set, 'Set');
if(new Set([1]).size != 1)Set = fixCollectionConstructor(Set, 'Set');
fixAdd(Set, 'add');
}
function getWeakData(it){
@@ -1312,8 +1317,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakmap-objects
*/
if(!isNative(WeakMap) || !has(WeakMap[prototype], 'clear')){
global.WeakMap = WeakMap = createCollectionConstructor('WeakMap');
extendBuiltInObject(WeakMap[prototype], assign({
WeakMap = createCollectionConstructor('WeakMap');
assign(WeakMap[prototype], assign({
/**
* 23.3.3.4 WeakMap.prototype.get ( key )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakmap.prototype.get
@@ -1337,8 +1342,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakset-objects
*/
if(!isNative(WeakSet)){
global.WeakSet = WeakSet = createCollectionConstructor('WeakSet', 1);
extendBuiltInObject(WeakSet[prototype], assign({
WeakSet = createCollectionConstructor('WeakSet', 1);
assign(WeakSet[prototype], assign({
/**
* 23.4.3.1 WeakSet.prototype.add (value )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakset.prototype.add
@@ -1351,9 +1356,15 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
}, commonWeakCollection));
} else {
// v8 fix
if(!new WeakSet([tmp]).has(tmp))global.WeakSet = fixCollectionConstructor(WeakSet, 'WeakSet');
if(!new WeakSet([tmp]).has(tmp))WeakSet = fixCollectionConstructor(WeakSet, 'WeakSet');
fixAdd(WeakSet, 'add');
}
$define(GLOBAL, undefined, {
Map: Map,
Set: Set,
WeakMap: WeakMap,
WeakSet: WeakSet
}, 1);
}(global.Map, global.Set, global.WeakMap, global.WeakSet);
/**
* Module : promise
@@ -1395,7 +1406,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
, DETAIL = symbol('detail')
, ITERABLE_ERROR = 'You must pass an array to race or all';
// https://github.com/domenic/promises-unwrapping#the-promise-constructor
function Promise(resolver){
Promise = function(resolver){
var promise = this
, rejectPromise = part.call(handle, promise, REJECTED);
assertInstance(promise, Promise, 'Promise');
@@ -1407,7 +1418,6 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
rejectPromise(e);
}
}
global.Promise = Promise;
function invokeCallback(settled, promise, callback, detail){
var hasCallback = isFunction(callback)
, value, error, succeeded, failed;
@@ -1553,6 +1563,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
}
}
}();
$define(GLOBAL, undefined, {Promise: Promise});
}(global.Promise);
/**
* Module : symbol
@@ -1565,8 +1576,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* https://github.com/component/symbol
* https://github.com/anthonyshort/symbol
*/
if(!isNative(global.Symbol)){
global.Symbol = function(description){
$define(GLOBAL, undefined, {
Symbol: function(description){
var tag = symbol(description);
defineProperty($Object, tag, {set: function(value){
hidden(this, tag, value);
@@ -1574,8 +1585,8 @@ if(!isNative(global.Symbol)){
return {toString: function(){
return tag;
}};
};
};
}
});
/**
* Module : extendedObjectAPI
*/
@@ -1584,7 +1595,7 @@ if(!isNative(global.Symbol)){
* http://wiki.ecmascript.org/doku.php?id=harmony:extended_object_api
* http://wiki.ecmascript.org/doku.php?id=strawman:extended_object_api
*/
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
getPropertyDescriptor: function(object, key){
if(key in object)do {
if(has(object, key))return getOwnPropertyDescriptor(object, key);
@@ -1642,20 +1653,19 @@ extendBuiltInObject(Object, {
* https://github.com/NobleJS/setImmediate
* https://github.com/calvinmetcalf/immediate
*/
if(!isSetImmediate){
global.setImmediate = setImmediate;
global.clearImmediate = clearImmediate;
}
$define(GLOBAL, undefined, {
setImmediate: setImmediate,
clearImmediate: clearImmediate
});
/**
* Module : function
*/
function inherits(parent){
assertFunction(this);
assertFunction(parent);
assertFunction(this), assertFunction(parent);
this[prototype] = create(parent[prototype], getOwnPropertyDescriptors(this[prototype]));
return this;
}
extendBuiltInObject(Function, {
$define(STATIC, 'Function', {
/**
* Alternatives:
* http://underscorejs.org/#isFunction
@@ -1669,7 +1679,7 @@ extendBuiltInObject(Function, {
inherits: unbind(inherits),
_: _
});
extendBuiltInObject($Function, {
$define(PROTO, 'Function', {
invoke: function(args){
assertFunction(this);
var instance = create(this[prototype])
@@ -1717,7 +1727,7 @@ function createDeferred(set, clear, args){
},
run: function(){
id && clear(id);
id = apply.call(set, global, args);
id = apply.call(set, undefined, args);
return deferred;
}
}, id;
@@ -1726,7 +1736,19 @@ function createDeferred(set, clear, args){
/**
* Module : binding
*/
extendBuiltInObject($Function, {
function tie(key){
var that = this
, i = 1
, placeholder = false
, length, args;
assertObject(that);
length = arguments.length;
if(length < 2)return ctx(that[key], that);
args = Array(length - 1)
while(length > i)if((args[i - 1] = arguments[i++]) === _)placeholder = true;
return createPartialApplication(that[key], args, length, placeholder, true, that);
}
$define(PROTO, 'Function', {
tie: tie,
/**
* Partial apply.
@@ -1737,6 +1759,17 @@ extendBuiltInObject($Function, {
* http://fitzgen.github.io/wu.js/#wu-partial
*/
part: part,
by: function(that){
var fn = this
, length = arguments.length
, i = 1
, placeholder = false
, args;
if(length < 2)return ctx(fn, that);
args = Array(length - 1);
while(length > i)if((args[i - 1] = arguments[i++]) === _)placeholder = true;
return createPartialApplication(fn, args, length, placeholder, true, that);
},
/**
* function -> method
* Alternatives:
@@ -1744,9 +1777,9 @@ extendBuiltInObject($Function, {
*/
methodize: methodize
});
extendBuiltInObject($Array, {tie: tie});
extendBuiltInObject(RegExp[prototype], {tie: tie});
extendBuiltInObject(Object, {
$define(PROTO, 'Array', {tie: tie});
$define(PROTO, 'RegExp', {tie: tie});
$define(STATIC, 'Object', {
/**
* Alternatives:
* http://www.2ality.com/2013/06/auto-binding.html
@@ -1754,14 +1787,17 @@ extendBuiltInObject(Object, {
* http://lodash.com/docs#bindKey
*/
tie: unbind(tie),
useTie: part.call(extendBuiltInObject, $Object, {tie: tie})
useTie: part.call($define, PROTO, 'Object', {tie: tie})
});
/**
* Module : object
*/
!function(){
function make(proto, props){
return create(proto, props ? getOwnPropertyDescriptors(props) : undefined);
function mixin(target, source){
return defineProperties(target, getOwnPropertyDescriptors(source));
}
function make(proto, props, desc){
return props ? (desc ? mixin : assign)(create(proto), props) : create(proto);
}
function merge(target, source, deep /* = false */, reverse /* = false */, desc /* = false */, stackA, stackB){
if(isObject(target) && isObject(source)){
@@ -1853,13 +1889,13 @@ extendBuiltInObject(Object, {
StackA = StackA.concat([a]);
StackB = StackB.concat([b]);
switch(type){
case'Boolean' :
case'String' :
case'Number' : return a.valueOf() == b.valueOf();
case'RegExp' : return '' + a == '' + b;
case'Error' : return a.message == b.message;/*
case'Array' :
case'Arguments' :
case 'Boolean' :
case 'String' :
case 'Number' : return a.valueOf() == b.valueOf();
case 'RegExp' : return '' + a == '' + b;
case 'Error' : return a.message == b.message;/*
case 'Array' :
case 'Arguments' :
length = toLength(a.length);
if(length != b.length)return false;
while(length--){
@@ -1881,16 +1917,6 @@ extendBuiltInObject(Object, {
}
return true;
}
function forOwnKeys(object, fn, that /* = undefined */){
assertFunction(fn);
var O = ES5Object(object)
, props = keys(O)
, length = props.length
, i = 0
, key;
while(length > i)fn.call(that, O[key = props[i++]], key, object);
return object;
}
function findIndex(object, fn, that /* = undefined */){
assertFunction(fn);
var O = ES5Object(object)
@@ -1902,7 +1928,7 @@ extendBuiltInObject(Object, {
if(fn.call(that, O[key = props[i++]], key, object))return key;
}
}
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
/**
* Alternatives:
* http://underscorejs.org/#has
@@ -1933,9 +1959,7 @@ extendBuiltInObject(Object, {
* 19.1.3.15 Object.mixin ( target, source ) <= Removed in Draft Rev 22, January 20, 2014, http://esdiscuss.org/topic/november-19-2013-meeting-notes#content-1
* TODO: rename
*/
mixin: function(target, source){
return defineProperties(target, getOwnPropertyDescriptors(source));
},
mixin: mixin,
/**
* Alternatives:
* http://underscorejs.org/#clone
@@ -2042,7 +2066,16 @@ extendBuiltInObject(Object, {
* http://api.jquery.com/jQuery.each/
* http://docs.angularjs.org/api/angular.forEach
*/
forEach: forOwnKeys,
forEach: function(object, fn, that /* = undefined */){
assertFunction(fn);
var O = ES5Object(object)
, props = keys(O)
, length = props.length
, i = 0
, key;
while(length > i)fn.call(that, O[key = props[i++]], key, object);
return object;
},
/**
* Alternatives:
* http://mootools.net/docs/core/Types/Object#Object:Object-keyOf
@@ -2133,7 +2166,13 @@ extendBuiltInObject(Object, {
target = create(null);
}
else target = Object(target);
forOwnKeys(object, callbackfn, target);
assertFunction(callbackfn);
var O = ES5Object(object)
, props = keys(O)
, length = props.length
, i = 0
, key;
while(length > i)callbackfn(target, O[key = props[i++]], key, object);
return target;
},
/**
@@ -2158,7 +2197,7 @@ extendBuiltInObject(Object, {
/**
* Module : array
*/
extendBuiltInObject($Array, {
$define(PROTO, 'Array', {
/**
* Alternatives:
* http://sugarjs.com/api/Array/at
@@ -2209,7 +2248,7 @@ extendBuiltInObject($Array, {
* https://github.com/plusdude/array-generics
* http://mootools.net/docs/core/Core/Core#Type:generics
*/
extendBuiltInObject(Array, reduceTo.call(
$define(STATIC, 'Array', reduceTo.call(
// IE... getOwnPropertyNames($Array),
array(
// ES3:
@@ -2222,21 +2261,21 @@ extendBuiltInObject(Array, reduceTo.call(
// Core.js:
'at,pluck,reduceTo,merge'
),
function(key){
if(key in $Array)this[key] = unbind($Array[key]);
function(memo, key){
if(key in $Array)memo[key] = unbind($Array[key]);
}
));
/**
* Module : number
*/
extendBuiltInObject(Number, {
$define(STATIC, 'Number', {
/**
* Alternatives:
* http://mootools.net/docs/core/Types/Number#Number:toInt
*/
toInteger: toInteger
});
extendBuiltInObject($Number, {
$define(PROTO, 'Number', {
/**
* Invoke function @ times and return array of results
* Alternatives:
@@ -2277,7 +2316,7 @@ extendBuiltInObject($Number, {
* http://sugarjs.com/api/Number/math
* http://mootools.net/docs/core/Types/Number#Number-Math
*/
extendBuiltInObject($Number, reduceTo.call(
$define(PROTO, 'Number', reduceTo.call(
// IE... getOwnPropertyNames(Math)
array(
// ES3
@@ -2285,8 +2324,8 @@ extendBuiltInObject($Number, reduceTo.call(
// ES6
'acosh,asinh,atanh,cbrt,cosh,expm1,hypot,imul,log1p,log10,log2,sign,sinh,tanh,trunc'
),
function(key){
if(key in Math)this[key] = methodize.call(Math[key]);
function(memo, key){
if(key in Math)memo[key] = methodize.call(Math[key]);
}
));
/**
@@ -2305,7 +2344,7 @@ extendBuiltInObject($Number, reduceTo.call(
, RegExpEscapeHTML = RegExp('[' + keys(dictionaryEscapeHTML).join('') + ']', 'g')
, RegExpUnescapeHTML = RegExp('(' + keys(dictionaryUnescapeHTML).join('|') + ')', 'g')
, RegExpEscapeRegExp = /([\\\/'*+?|()\[\]{}.^$])/g;
extendBuiltInObject($String, {
$define(PROTO, 'String', {
/**
* Alternatives:
* http://sugarjs.com/api/String/at
@@ -2419,14 +2458,14 @@ extendBuiltInObject($Number, reduceTo.call(
});
}
}
extendBuiltInObject(Date, {
$define(STATIC, 'Date', {
locale: function(locale){
if(has(locales, locale))current = locale;
return current;
},
addLocale: addLocale
});
extendBuiltInObject(Date[prototype], {format: format});
$define(PROTO, 'Date', {format: format});
addLocale('en', {
w: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
M: 'January,February,March,April,May,June,July,August,September,October,November,December'
@@ -2506,11 +2545,11 @@ var extendCollections = {
fn = target;
target = create(null);
} else target = Object(target);
this.forEach(fn, target);
this.forEach(part.call(fn, target));
return target;
}
};
extendBuiltInObject(Map[prototype], assign({
$define(PROTO, 'Map', assign({
map: function(fn, that){
assertFunction(fn);
var result = new Map;
@@ -2548,7 +2587,7 @@ extendBuiltInObject(Map[prototype], assign({
return result;
}
}, extendCollections));
extendBuiltInObject(Set[prototype], assign({
$define(PROTO, 'Set', assign({
map: function(fn, that){
assertFunction(fn);
var result = new Set;
@@ -2581,8 +2620,8 @@ var $console = reduceTo.call(
'group,groupCollapsed,groupEnd,info,log,table,trace,warn,' +
'markTimeline,profile,profileEnd,time,timeEnd,timeStamp'),
{enabled: true},
function(key){
this[key] = function(){
function(memo, key){
memo[key] = function(){
return console[key] && $console.enabled ? apply.call(console[key], console, arguments) : undefined;
};
}
@@ -2590,5 +2629,5 @@ var $console = reduceTo.call(
try {
delete global.console;
} catch(e){}
$console = global.console = assign($console.log, $console);
$define(GLOBAL, undefined, {console: $console = assign($console.log, $console)});
}(typeof window != 'undefined' ? window : global);
Vendored
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+154 -114
View File
@@ -17,7 +17,6 @@ var prototype = 'prototype'
, String = global.String
, Number = global.Number
, RegExp = global.RegExp
, Date = global.Date
, Math = global.Math
, setTimeout = global.setTimeout
, clearTimeout = global.clearTimeout
@@ -27,9 +26,7 @@ var prototype = 'prototype'
, document = global.document
, Infinity = 1 / 0
, $Array = Array[prototype]
, $Number = Number[prototype]
, $Object = Object[prototype]
, $String = String[prototype]
, $Function = Function[prototype]
, console = global.console || {log: $Function};
@@ -60,40 +57,40 @@ function classof(it){
// Function:
var apply = $Function.apply
, call = $Function.call;
// unbind method from context
// foo.fn(arg1, arg2, ...) => fn(foo, arg1, arg2, ...)
function unbind(that){
return tie.call(that, 'call');
}
// simple bind context
function tie(key){
var that = this
, fn = that[key];
assertFunction(fn);
return function(){
return fn.apply(that, arguments);
}
}
// placeholder for partial apply
var _ = {};
// partial apply
function part(/*args...*/){
var fn = this
, lengthPart = arguments.length
, argsPart = Array(lengthPart)
, i = 0
var length = arguments.length
, args = Array(length)
, i = 0
, placeholder = false;
while(length > i)if((args[i] = arguments[i++]) === _)placeholder = true;
return createPartialApplication(this, args, length, placeholder, false);
}
function ctx(fn, that){
return function(){
return fn.apply(that, arguments);
}
}
function createPartialApplication(fn, argsPart, lengthPart, placeholder, bind, context){
assertFunction(fn);
while(lengthPart > i)if((argsPart[i] = arguments[i++]) === _)placeholder = true;
return function(/*args...*/){
var length = arguments.length
, i, j, args;
if(!placeholder && length === 0)return fn.apply(this, argsPart);
var that = bind ? context : this
, length = arguments.length
, i = 0, j = 0, args;
if(!placeholder && length == 0)return fn.apply(that, argsPart);
args = argsPart.slice();
i = j = 0;
if(placeholder)for(;lengthPart > i; i++)if(args[i] === _)args[i] = arguments[j++]
while(length > j)args.push(arguments[j++]);
return fn.apply(this, args);
return fn.apply(that, args);
}
}
// unbind method from context
// foo.fn(arg1, arg2, ...) => fn(foo, arg1, arg2, ...)
function unbind(that){
return function(){
return call.apply(that, arguments);
}
}
// add `this` as first argument
@@ -117,7 +114,7 @@ function has(object, key){
}
var isEnumerable = $Object.propertyIsEnumerable
, defineProperty = Object.defineProperty
, PROTO = '__proto__' in $Object
, __PROTO__ = '__proto__' in $Object
, DESCRIPTORS = 1;
// http://wiki.ecmascript.org/doku.php?id=strawman:extended_object_api
function getOwnPropertyDescriptors(object){
@@ -169,7 +166,11 @@ function reduceTo(target, callbackfn){
callbackfn = target;
target = {};
} else target = Object(target);
forEach.call(this, callbackfn, target);
assertFunction(callbackfn);
var self = ES5Object(this)
, length = toLength(self.length)
, i = 0;
for(;length > i; i++)i in self && callbackfn(target, self[i], i, this);
return target;
}
@@ -219,11 +220,16 @@ function descriptor(bitmap, value){
function hidden(object, key, value){
return defineProperty(object, key, descriptor(6, value));
}
var GLOBAL = 0
, STATIC = 1
, PROTO = 2;
/**
* Module : framework
*/
function extendBuiltInObject(target, source, forced /* = false */){
if(target)for(var key in source){
function $define(type, name, source, forced /* = false */){
var target = type == GLOBAL ? global : type == STATIC ? global[name] : global[name][prototype];
for(var key in source){
try {
has(source, key)
&& (forced || !has(target, key) || !isNative(target[key]))
@@ -301,7 +307,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
} else if(isFunction(MessageChannel)){
channel = new MessageChannel();
channel.port1.onmessage = listner;
defer = tie.call(channel.port2, 'postMessage');
defer = ctx(channel.port2.postMessage, channel.port2);
// IE8-
// always run before timers, like nextTick => some problems with recursive call
} else if(document && onreadystatechange in document.createElement('script')){
@@ -334,7 +340,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
function sign(it){
return (it = +it) == 0 || it != it ? it : it < 0 ? -1 : 1;
}
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
/**
* The assign function is used to copy the values of all of the enumerable
* own properties from a source object to a target object.
@@ -358,7 +364,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://kangax.github.io/es5-compat-table/es6/#Object.setPrototypeOf
* work only if browser support __proto__, don't work with null proto objects
*/
PROTO && extendBuiltInObject(Object, {
__PROTO__ && $define(STATIC, 'Object', {
setPrototypeOf: function(O, proto){
assertObject(O);
assert(isObject(proto) || proto === null, "Can't set", proto, 'as prototype');
@@ -366,7 +372,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
return O;
}
});
extendBuiltInObject(Number, {
$define(STATIC, 'Number', {
/**
* 20.1.2.1 Number.EPSILON
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.epsilon
@@ -433,7 +439,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
, exp = Math.exp
, log = Math.log
, sqrt = Math.sqrt;
extendBuiltInObject(Math, {
$define(STATIC, 'Math', {
/**
* Returns an implementation-dependent approximation to the inverse hyperbolic cosine of x.
* 20.2.2.3 Math.acosh(x)
@@ -595,7 +601,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
}
});
/**
extendBuiltInObject(String, {
$define(STATIC, 'String', {
* 21.1.2.2 String.fromCodePoint ( ...codePoints)
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.fromcodepoint
* http://kangax.github.io/es5-compat-table/es6/#String.fromCodePoint
@@ -605,7 +611,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
raw: function(){ TODO }
});
*/
extendBuiltInObject($String, {
$define(PROTO, 'String', {
/**
* 21.1.3.3 String.prototype.codePointAt (pos)
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.codepointat
@@ -656,7 +662,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
return String(this).slice(position, position + searchString.length) === searchString;
}
});
extendBuiltInObject(Array, {
$define(STATIC, 'Array', {
/**
* 22.1.2.1 Array.from ( arrayLike , mapfn=undefined, thisArg=undefined )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
@@ -697,7 +703,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
}
return -1;
}
extendBuiltInObject($Array, {
$define(PROTO, 'Array', {
/**
* 22.1.3.3 Array.prototype.copyWithin (target, start, end = this.length)
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.copywithin
@@ -828,8 +834,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-map-objects
*/
if(!isNative(Map) || !has(Map[prototype], 'forEach')){
global.Map = Map = createCollectionConstructor('Map');
extendBuiltInObject(Map[prototype], {
Map = createCollectionConstructor('Map');
assign(Map[prototype], {
/**
* 23.1.3.1 Map.prototype.clear ()
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-map.prototype.clear
@@ -896,8 +902,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-set-objects
*/
if(!isNative(Set) || !has(Set[prototype], 'forEach')){
global.Set = Set = createCollectionConstructor('Set', 1);
extendBuiltInObject(Set[prototype], {
Set = createCollectionConstructor('Set', 1);
assign(Set[prototype], {
/**
* 23.2.3.1 Set.prototype.add ( value )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-set.prototype.add
@@ -948,7 +954,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
defineProperties(Set[prototype], sizeGetter);
} else {
// IE 11 fix
if(new Set([1]).size != 1)global.Set = fixCollectionConstructor(Set, 'Set');
if(new Set([1]).size != 1)Set = fixCollectionConstructor(Set, 'Set');
fixAdd(Set, 'add');
}
function getWeakData(it){
@@ -988,8 +994,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakmap-objects
*/
if(!isNative(WeakMap) || !has(WeakMap[prototype], 'clear')){
global.WeakMap = WeakMap = createCollectionConstructor('WeakMap');
extendBuiltInObject(WeakMap[prototype], assign({
WeakMap = createCollectionConstructor('WeakMap');
assign(WeakMap[prototype], assign({
/**
* 23.3.3.4 WeakMap.prototype.get ( key )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakmap.prototype.get
@@ -1013,8 +1019,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakset-objects
*/
if(!isNative(WeakSet)){
global.WeakSet = WeakSet = createCollectionConstructor('WeakSet', 1);
extendBuiltInObject(WeakSet[prototype], assign({
WeakSet = createCollectionConstructor('WeakSet', 1);
assign(WeakSet[prototype], assign({
/**
* 23.4.3.1 WeakSet.prototype.add (value )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakset.prototype.add
@@ -1027,9 +1033,15 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
}, commonWeakCollection));
} else {
// v8 fix
if(!new WeakSet([tmp]).has(tmp))global.WeakSet = fixCollectionConstructor(WeakSet, 'WeakSet');
if(!new WeakSet([tmp]).has(tmp))WeakSet = fixCollectionConstructor(WeakSet, 'WeakSet');
fixAdd(WeakSet, 'add');
}
$define(GLOBAL, undefined, {
Map: Map,
Set: Set,
WeakMap: WeakMap,
WeakSet: WeakSet
}, 1);
}(global.Map, global.Set, global.WeakMap, global.WeakSet);
/**
* Module : promise
@@ -1071,7 +1083,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
, DETAIL = symbol('detail')
, ITERABLE_ERROR = 'You must pass an array to race or all';
// https://github.com/domenic/promises-unwrapping#the-promise-constructor
function Promise(resolver){
Promise = function(resolver){
var promise = this
, rejectPromise = part.call(handle, promise, REJECTED);
assertInstance(promise, Promise, 'Promise');
@@ -1083,7 +1095,6 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
rejectPromise(e);
}
}
global.Promise = Promise;
function invokeCallback(settled, promise, callback, detail){
var hasCallback = isFunction(callback)
, value, error, succeeded, failed;
@@ -1229,6 +1240,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
}
}
}();
$define(GLOBAL, undefined, {Promise: Promise});
}(global.Promise);
/**
* Module : symbol
@@ -1241,8 +1253,8 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
* https://github.com/component/symbol
* https://github.com/anthonyshort/symbol
*/
if(!isNative(global.Symbol)){
global.Symbol = function(description){
$define(GLOBAL, undefined, {
Symbol: function(description){
var tag = symbol(description);
defineProperty($Object, tag, {set: function(value){
hidden(this, tag, value);
@@ -1250,8 +1262,8 @@ if(!isNative(global.Symbol)){
return {toString: function(){
return tag;
}};
};
};
}
});
/**
* Module : extendedObjectAPI
*/
@@ -1260,7 +1272,7 @@ if(!isNative(global.Symbol)){
* http://wiki.ecmascript.org/doku.php?id=harmony:extended_object_api
* http://wiki.ecmascript.org/doku.php?id=strawman:extended_object_api
*/
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
getPropertyDescriptor: function(object, key){
if(key in object)do {
if(has(object, key))return getOwnPropertyDescriptor(object, key);
@@ -1295,20 +1307,19 @@ extendBuiltInObject(Object, {
* https://github.com/NobleJS/setImmediate
* https://github.com/calvinmetcalf/immediate
*/
if(!isSetImmediate){
global.setImmediate = setImmediate;
global.clearImmediate = clearImmediate;
}
$define(GLOBAL, undefined, {
setImmediate: setImmediate,
clearImmediate: clearImmediate
});
/**
* Module : function
*/
function inherits(parent){
assertFunction(this);
assertFunction(parent);
assertFunction(this), assertFunction(parent);
this[prototype] = create(parent[prototype], getOwnPropertyDescriptors(this[prototype]));
return this;
}
extendBuiltInObject(Function, {
$define(STATIC, 'Function', {
/**
* Alternatives:
* http://underscorejs.org/#isFunction
@@ -1322,7 +1333,7 @@ extendBuiltInObject(Function, {
inherits: unbind(inherits),
_: _
});
extendBuiltInObject($Function, {
$define(PROTO, 'Function', {
invoke: function(args){
assertFunction(this);
var instance = create(this[prototype])
@@ -1370,7 +1381,7 @@ function createDeferred(set, clear, args){
},
run: function(){
id && clear(id);
id = apply.call(set, global, args);
id = apply.call(set, undefined, args);
return deferred;
}
}, id;
@@ -1379,7 +1390,19 @@ function createDeferred(set, clear, args){
/**
* Module : binding
*/
extendBuiltInObject($Function, {
function tie(key){
var that = this
, i = 1
, placeholder = false
, length, args;
assertObject(that);
length = arguments.length;
if(length < 2)return ctx(that[key], that);
args = Array(length - 1)
while(length > i)if((args[i - 1] = arguments[i++]) === _)placeholder = true;
return createPartialApplication(that[key], args, length, placeholder, true, that);
}
$define(PROTO, 'Function', {
tie: tie,
/**
* Partial apply.
@@ -1390,6 +1413,17 @@ extendBuiltInObject($Function, {
* http://fitzgen.github.io/wu.js/#wu-partial
*/
part: part,
by: function(that){
var fn = this
, length = arguments.length
, i = 1
, placeholder = false
, args;
if(length < 2)return ctx(fn, that);
args = Array(length - 1);
while(length > i)if((args[i - 1] = arguments[i++]) === _)placeholder = true;
return createPartialApplication(fn, args, length, placeholder, true, that);
},
/**
* function -> method
* Alternatives:
@@ -1397,9 +1431,9 @@ extendBuiltInObject($Function, {
*/
methodize: methodize
});
extendBuiltInObject($Array, {tie: tie});
extendBuiltInObject(RegExp[prototype], {tie: tie});
extendBuiltInObject(Object, {
$define(PROTO, 'Array', {tie: tie});
$define(PROTO, 'RegExp', {tie: tie});
$define(STATIC, 'Object', {
/**
* Alternatives:
* http://www.2ality.com/2013/06/auto-binding.html
@@ -1407,14 +1441,17 @@ extendBuiltInObject(Object, {
* http://lodash.com/docs#bindKey
*/
tie: unbind(tie),
useTie: part.call(extendBuiltInObject, $Object, {tie: tie})
useTie: part.call($define, PROTO, 'Object', {tie: tie})
});
/**
* Module : object
*/
!function(){
function make(proto, props){
return create(proto, props ? getOwnPropertyDescriptors(props) : undefined);
function mixin(target, source){
return defineProperties(target, getOwnPropertyDescriptors(source));
}
function make(proto, props, desc){
return props ? (desc ? mixin : assign)(create(proto), props) : create(proto);
}
function merge(target, source, deep /* = false */, reverse /* = false */, desc /* = false */, stackA, stackB){
if(isObject(target) && isObject(source)){
@@ -1506,13 +1543,13 @@ extendBuiltInObject(Object, {
StackA = StackA.concat([a]);
StackB = StackB.concat([b]);
switch(type){
case'Boolean' :
case'String' :
case'Number' : return a.valueOf() == b.valueOf();
case'RegExp' : return '' + a == '' + b;
case'Error' : return a.message == b.message;/*
case'Array' :
case'Arguments' :
case 'Boolean' :
case 'String' :
case 'Number' : return a.valueOf() == b.valueOf();
case 'RegExp' : return '' + a == '' + b;
case 'Error' : return a.message == b.message;/*
case 'Array' :
case 'Arguments' :
length = toLength(a.length);
if(length != b.length)return false;
while(length--){
@@ -1534,16 +1571,6 @@ extendBuiltInObject(Object, {
}
return true;
}
function forOwnKeys(object, fn, that /* = undefined */){
assertFunction(fn);
var O = ES5Object(object)
, props = keys(O)
, length = props.length
, i = 0
, key;
while(length > i)fn.call(that, O[key = props[i++]], key, object);
return object;
}
function findIndex(object, fn, that /* = undefined */){
assertFunction(fn);
var O = ES5Object(object)
@@ -1555,7 +1582,7 @@ extendBuiltInObject(Object, {
if(fn.call(that, O[key = props[i++]], key, object))return key;
}
}
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
/**
* Alternatives:
* http://underscorejs.org/#has
@@ -1586,9 +1613,7 @@ extendBuiltInObject(Object, {
* 19.1.3.15 Object.mixin ( target, source ) <= Removed in Draft Rev 22, January 20, 2014, http://esdiscuss.org/topic/november-19-2013-meeting-notes#content-1
* TODO: rename
*/
mixin: function(target, source){
return defineProperties(target, getOwnPropertyDescriptors(source));
},
mixin: mixin,
/**
* Alternatives:
* http://underscorejs.org/#clone
@@ -1695,7 +1720,16 @@ extendBuiltInObject(Object, {
* http://api.jquery.com/jQuery.each/
* http://docs.angularjs.org/api/angular.forEach
*/
forEach: forOwnKeys,
forEach: function(object, fn, that /* = undefined */){
assertFunction(fn);
var O = ES5Object(object)
, props = keys(O)
, length = props.length
, i = 0
, key;
while(length > i)fn.call(that, O[key = props[i++]], key, object);
return object;
},
/**
* Alternatives:
* http://mootools.net/docs/core/Types/Object#Object:Object-keyOf
@@ -1786,7 +1820,13 @@ extendBuiltInObject(Object, {
target = create(null);
}
else target = Object(target);
forOwnKeys(object, callbackfn, target);
assertFunction(callbackfn);
var O = ES5Object(object)
, props = keys(O)
, length = props.length
, i = 0
, key;
while(length > i)callbackfn(target, O[key = props[i++]], key, object);
return target;
},
/**
@@ -1811,7 +1851,7 @@ extendBuiltInObject(Object, {
/**
* Module : array
*/
extendBuiltInObject($Array, {
$define(PROTO, 'Array', {
/**
* Alternatives:
* http://sugarjs.com/api/Array/at
@@ -1862,7 +1902,7 @@ extendBuiltInObject($Array, {
* https://github.com/plusdude/array-generics
* http://mootools.net/docs/core/Core/Core#Type:generics
*/
extendBuiltInObject(Array, reduceTo.call(
$define(STATIC, 'Array', reduceTo.call(
// IE... getOwnPropertyNames($Array),
array(
// ES3:
@@ -1875,21 +1915,21 @@ extendBuiltInObject(Array, reduceTo.call(
// Core.js:
'at,pluck,reduceTo,merge'
),
function(key){
if(key in $Array)this[key] = unbind($Array[key]);
function(memo, key){
if(key in $Array)memo[key] = unbind($Array[key]);
}
));
/**
* Module : number
*/
extendBuiltInObject(Number, {
$define(STATIC, 'Number', {
/**
* Alternatives:
* http://mootools.net/docs/core/Types/Number#Number:toInt
*/
toInteger: toInteger
});
extendBuiltInObject($Number, {
$define(PROTO, 'Number', {
/**
* Invoke function @ times and return array of results
* Alternatives:
@@ -1930,7 +1970,7 @@ extendBuiltInObject($Number, {
* http://sugarjs.com/api/Number/math
* http://mootools.net/docs/core/Types/Number#Number-Math
*/
extendBuiltInObject($Number, reduceTo.call(
$define(PROTO, 'Number', reduceTo.call(
// IE... getOwnPropertyNames(Math)
array(
// ES3
@@ -1938,8 +1978,8 @@ extendBuiltInObject($Number, reduceTo.call(
// ES6
'acosh,asinh,atanh,cbrt,cosh,expm1,hypot,imul,log1p,log10,log2,sign,sinh,tanh,trunc'
),
function(key){
if(key in Math)this[key] = methodize.call(Math[key]);
function(memo, key){
if(key in Math)memo[key] = methodize.call(Math[key]);
}
));
/**
@@ -1958,7 +1998,7 @@ extendBuiltInObject($Number, reduceTo.call(
, RegExpEscapeHTML = RegExp('[' + keys(dictionaryEscapeHTML).join('') + ']', 'g')
, RegExpUnescapeHTML = RegExp('(' + keys(dictionaryUnescapeHTML).join('|') + ')', 'g')
, RegExpEscapeRegExp = /([\\\/'*+?|()\[\]{}.^$])/g;
extendBuiltInObject($String, {
$define(PROTO, 'String', {
/**
* Alternatives:
* http://sugarjs.com/api/String/at
@@ -2072,14 +2112,14 @@ extendBuiltInObject($Number, reduceTo.call(
});
}
}
extendBuiltInObject(Date, {
$define(STATIC, 'Date', {
locale: function(locale){
if(has(locales, locale))current = locale;
return current;
},
addLocale: addLocale
});
extendBuiltInObject(Date[prototype], {format: format});
$define(PROTO, 'Date', {format: format});
addLocale('en', {
w: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
M: 'January,February,March,April,May,June,July,August,September,October,November,December'
@@ -2159,11 +2199,11 @@ var extendCollections = {
fn = target;
target = create(null);
} else target = Object(target);
this.forEach(fn, target);
this.forEach(part.call(fn, target));
return target;
}
};
extendBuiltInObject(Map[prototype], assign({
$define(PROTO, 'Map', assign({
map: function(fn, that){
assertFunction(fn);
var result = new Map;
@@ -2201,7 +2241,7 @@ extendBuiltInObject(Map[prototype], assign({
return result;
}
}, extendCollections));
extendBuiltInObject(Set[prototype], assign({
$define(PROTO, 'Set', assign({
map: function(fn, that){
assertFunction(fn);
var result = new Set;
@@ -2234,8 +2274,8 @@ var $console = reduceTo.call(
'group,groupCollapsed,groupEnd,info,log,table,trace,warn,' +
'markTimeline,profile,profileEnd,time,timeEnd,timeStamp'),
{enabled: true},
function(key){
this[key] = function(){
function(memo, key){
memo[key] = function(){
return console[key] && $console.enabled ? apply.call(console[key], console, arguments) : undefined;
};
}
@@ -2243,5 +2283,5 @@ var $console = reduceTo.call(
try {
delete global.console;
} catch(e){}
$console = global.console = assign($console.log, $console);
$define(GLOBAL, undefined, {console: $console = assign($console.log, $console)});
}(typeof window != 'undefined' ? window : global);
+2633
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,4 +1,4 @@
extendBuiltInObject($Array, {
$define(PROTO, 'Array', {
/**
* Alternatives:
* http://sugarjs.com/api/Array/at
+3 -3
View File
@@ -8,7 +8,7 @@
* https://github.com/plusdude/array-generics
* http://mootools.net/docs/core/Core/Core#Type:generics
*/
extendBuiltInObject(Array, reduceTo.call(
$define(STATIC, 'Array', reduceTo.call(
// IE... getOwnPropertyNames($Array),
array(
// ES3:
@@ -21,7 +21,7 @@ extendBuiltInObject(Array, reduceTo.call(
// Core.js:
'at,pluck,reduceTo,merge'
),
function(key){
if(key in $Array)this[key] = unbind($Array[key]);
function(memo, key){
if(key in $Array)memo[key] = unbind($Array[key]);
}
));
+28 -5
View File
@@ -1,4 +1,16 @@
extendBuiltInObject($Function, {
function tie(key){
var that = this
, i = 1
, placeholder = false
, length, args;
assertObject(that);
length = arguments.length;
if(length < 2)return ctx(that[key], that);
args = Array(length - 1)
while(length > i)if((args[i - 1] = arguments[i++]) === _)placeholder = true;
return createPartialApplication(that[key], args, length, placeholder, true, that);
}
$define(PROTO, 'Function', {
tie: tie,
/**
* Partial apply.
@@ -9,6 +21,17 @@ extendBuiltInObject($Function, {
* http://fitzgen.github.io/wu.js/#wu-partial
*/
part: part,
by: function(that){
var fn = this
, length = arguments.length
, i = 1
, placeholder = false
, args;
if(length < 2)return ctx(fn, that);
args = Array(length - 1);
while(length > i)if((args[i - 1] = arguments[i++]) === _)placeholder = true;
return createPartialApplication(fn, args, length, placeholder, true, that);
},
/**
* function -> method
* Alternatives:
@@ -16,9 +39,9 @@ extendBuiltInObject($Function, {
*/
methodize: methodize
});
extendBuiltInObject($Array, {tie: tie});
extendBuiltInObject(RegExp[prototype], {tie: tie});
extendBuiltInObject(Object, {
$define(PROTO, 'Array', {tie: tie});
$define(PROTO, 'RegExp', {tie: tie});
$define(STATIC, 'Object', {
/**
* Alternatives:
* http://www.2ality.com/2013/06/auto-binding.html
@@ -26,5 +49,5 @@ extendBuiltInObject(Object, {
* http://lodash.com/docs#bindKey
*/
tie: unbind(tie),
useTie: part.call(extendBuiltInObject, $Object, {tie: tie})
useTie: part.call($define, PROTO, 'Object', {tie: tie})
});
+3 -3
View File
@@ -10,8 +10,8 @@ var $console = reduceTo.call(
'group,groupCollapsed,groupEnd,info,log,table,trace,warn,' +
'markTimeline,profile,profileEnd,time,timeEnd,timeStamp'),
{enabled: true},
function(key){
this[key] = function(){
function(memo, key){
memo[key] = function(){
return console[key] && $console.enabled ? apply.call(console[key], console, arguments) : undefined;
};
}
@@ -19,4 +19,4 @@ var $console = reduceTo.call(
try {
delete global.console;
} catch(e){}
$console = global.console = assign($console.log, $console);
$define(GLOBAL, undefined, {console: $console = assign($console.log, $console)});
+2 -2
View File
@@ -54,14 +54,14 @@
});
}
}
extendBuiltInObject(Date, {
$define(STATIC, 'Date', {
locale: function(locale){
if(has(locales, locale))current = locale;
return current;
},
addLocale: addLocale
});
extendBuiltInObject(Date[prototype], {format: format});
$define(PROTO, 'Date', {format: format});
addLocale('en', {
w: 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
M: 'January,February,March,April,May,June,July,August,September,October,November,December'
+8 -8
View File
@@ -18,7 +18,7 @@
, nativeSlice = slice
, nativeJoin = $Array.join
// Create object with null as it's prototype
, createNullProtoObject = PROTO
, createNullProtoObject = __PROTO__
? function(){
return {__proto__: null};
}
@@ -91,7 +91,7 @@
return O;
}
}
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
/**
* 15.2.3.2 Object.getPrototypeOf ( O )
* http://es5.github.io/#x15.2.3.2
@@ -117,7 +117,7 @@
var result = new Empty();
if(Properties)defineProperties(result, Properties);
// add __proto__ for Object.getPrototypeOf shim
PROTO || result.constructor[prototype] === O || (result.__proto__ = O);
__PROTO__ || result.constructor[prototype] === O || (result.__proto__ = O);
return result;
},
/**
@@ -143,7 +143,7 @@
* 15.3.4.5 Function.prototype.bind (thisArg [, arg1 [, arg2, …]])
* http://es5.github.io/#x15.3.4.5
*/
extendBuiltInObject($Function, {
$define(PROTO, 'Function', {
bind: function(scope /*, args...*/){
var fn = this
, args = $slice(arguments, 1);
@@ -166,7 +166,7 @@
* http://api.jquery.com/jQuery.isArray/
* http://docs.angularjs.org/api/angular.isArray
*/
extendBuiltInObject(Array, {isArray: function(it){
$define(STATIC, 'Array', {isArray: function(it){
return classof(it) == 'Array'
}});
function forEach(callbackfn, thisArg /* = undefined */){
@@ -176,7 +176,7 @@
, i = 0;
for(;length > i; i++)i in self && callbackfn.call(thisArg, self[i], i, this);
}
extendBuiltInObject($Array, {
$define(PROTO, 'Array', {
/**
* 15.4.4.14 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
* http://es5.github.io/#x15.4.4.14
@@ -301,14 +301,14 @@
* 15.5.4.20 String.prototype.trim ( )
* http://es5.github.io/#x15.5.4.20
*/
extendBuiltInObject($String, {trim: function(){
$define(PROTO, 'String', {trim: function(){
return String(this).replace(trimRegExp, '');
}});
/**
* 15.9.4.4 Date.now ( )
* http://es5.github.io/#x15.9.4.4
*/
extendBuiltInObject(Date, {now: function(){
$define(STATIC, 'Date', {now: function(){
return +new Date;
}});
if(isFunction(trimRegExp))isFunction = function(it){
+8 -8
View File
@@ -11,7 +11,7 @@
function sign(it){
return (it = +it) == 0 || it != it ? it : it < 0 ? -1 : 1;
}
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
/**
* The assign function is used to copy the values of all of the enumerable
* own properties from a source object to a target object.
@@ -35,7 +35,7 @@
* http://kangax.github.io/es5-compat-table/es6/#Object.setPrototypeOf
* work only if browser support __proto__, don't work with null proto objects
*/
PROTO && extendBuiltInObject(Object, {
__PROTO__ && $define(STATIC, 'Object', {
setPrototypeOf: function(O, proto){
assertObject(O);
assert(isObject(proto) || proto === null, "Can't set", proto, 'as prototype');
@@ -43,7 +43,7 @@
return O;
}
});
extendBuiltInObject(Number, {
$define(STATIC, 'Number', {
/**
* 20.1.2.1 Number.EPSILON
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-number.epsilon
@@ -110,7 +110,7 @@
, exp = Math.exp
, log = Math.log
, sqrt = Math.sqrt;
extendBuiltInObject(Math, {
$define(STATIC, 'Math', {
/**
* Returns an implementation-dependent approximation to the inverse hyperbolic cosine of x.
* 20.2.2.3 Math.acosh(x)
@@ -272,7 +272,7 @@
}
});
/**
extendBuiltInObject(String, {
$define(STATIC, 'String', {
* 21.1.2.2 String.fromCodePoint ( ...codePoints)
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.fromcodepoint
* http://kangax.github.io/es5-compat-table/es6/#String.fromCodePoint
@@ -282,7 +282,7 @@
raw: function(){ TODO }
});
*/
extendBuiltInObject($String, {
$define(PROTO, 'String', {
/**
* 21.1.3.3 String.prototype.codePointAt (pos)
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.codepointat
@@ -333,7 +333,7 @@
return String(this).slice(position, position + searchString.length) === searchString;
}
});
extendBuiltInObject(Array, {
$define(STATIC, 'Array', {
/**
* 22.1.2.1 Array.from ( arrayLike , mapfn=undefined, thisArg=undefined )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
@@ -374,7 +374,7 @@
}
return -1;
}
extendBuiltInObject($Array, {
$define(PROTO, 'Array', {
/**
* 22.1.3.3 Array.prototype.copyWithin (target, start, end = this.length)
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.prototype.copywithin
+16 -10
View File
@@ -92,8 +92,8 @@
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-map-objects
*/
if(!isNative(Map) || !has(Map[prototype], 'forEach')){
global.Map = Map = createCollectionConstructor('Map');
extendBuiltInObject(Map[prototype], {
Map = createCollectionConstructor('Map');
assign(Map[prototype], {
/**
* 23.1.3.1 Map.prototype.clear ()
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-map.prototype.clear
@@ -160,8 +160,8 @@
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-set-objects
*/
if(!isNative(Set) || !has(Set[prototype], 'forEach')){
global.Set = Set = createCollectionConstructor('Set', 1);
extendBuiltInObject(Set[prototype], {
Set = createCollectionConstructor('Set', 1);
assign(Set[prototype], {
/**
* 23.2.3.1 Set.prototype.add ( value )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-set.prototype.add
@@ -212,7 +212,7 @@
defineProperties(Set[prototype], sizeGetter);
} else {
// IE 11 fix
if(new Set([1]).size != 1)global.Set = fixCollectionConstructor(Set, 'Set');
if(new Set([1]).size != 1)Set = fixCollectionConstructor(Set, 'Set');
fixAdd(Set, 'add');
}
function getWeakData(it){
@@ -252,8 +252,8 @@
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakmap-objects
*/
if(!isNative(WeakMap) || !has(WeakMap[prototype], 'clear')){
global.WeakMap = WeakMap = createCollectionConstructor('WeakMap');
extendBuiltInObject(WeakMap[prototype], assign({
WeakMap = createCollectionConstructor('WeakMap');
assign(WeakMap[prototype], assign({
/**
* 23.3.3.4 WeakMap.prototype.get ( key )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakmap.prototype.get
@@ -277,8 +277,8 @@
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakset-objects
*/
if(!isNative(WeakSet)){
global.WeakSet = WeakSet = createCollectionConstructor('WeakSet', 1);
extendBuiltInObject(WeakSet[prototype], assign({
WeakSet = createCollectionConstructor('WeakSet', 1);
assign(WeakSet[prototype], assign({
/**
* 23.4.3.1 WeakSet.prototype.add (value )
* http://people.mozilla.org/~jorendorff/es6-draft.html#sec-weakset.prototype.add
@@ -291,7 +291,13 @@
}, commonWeakCollection));
} else {
// v8 fix
if(!new WeakSet([tmp]).has(tmp))global.WeakSet = fixCollectionConstructor(WeakSet, 'WeakSet');
if(!new WeakSet([tmp]).has(tmp))WeakSet = fixCollectionConstructor(WeakSet, 'WeakSet');
fixAdd(WeakSet, 'add');
}
$define(GLOBAL, undefined, {
Map: Map,
Set: Set,
WeakMap: WeakMap,
WeakSet: WeakSet
}, 1);
}(global.Map, global.Set, global.WeakMap, global.WeakSet);
+3 -3
View File
@@ -65,11 +65,11 @@ var extendCollections = {
fn = target;
target = create(null);
} else target = Object(target);
this.forEach(fn, target);
this.forEach(part.call(fn, target));
return target;
}
};
extendBuiltInObject(Map[prototype], assign({
$define(PROTO, 'Map', assign({
map: function(fn, that){
assertFunction(fn);
var result = new Map;
@@ -107,7 +107,7 @@ extendBuiltInObject(Map[prototype], assign({
return result;
}
}, extendCollections));
extendBuiltInObject(Set[prototype], assign({
$define(PROTO, 'Set', assign({
map: function(fn, that){
assertFunction(fn);
var result = new Set;
+1 -1
View File
@@ -3,7 +3,7 @@
* http://wiki.ecmascript.org/doku.php?id=harmony:extended_object_api
* http://wiki.ecmascript.org/doku.php?id=strawman:extended_object_api
*/
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
getPropertyDescriptor: function(object, key){
if(key in object)do {
if(has(object, key))return getOwnPropertyDescriptor(object, key);
+3 -2
View File
@@ -1,5 +1,6 @@
function extendBuiltInObject(target, source, forced /* = false */){
if(target)for(var key in source){
function $define(type, name, source, forced /* = false */){
var target = type == GLOBAL ? global : type == STATIC ? global[name] : global[name][prototype];
for(var key in source){
try {
has(source, key)
&& (forced || !has(target, key) || !isNative(target[key]))
+4 -5
View File
@@ -1,10 +1,9 @@
function inherits(parent){
assertFunction(this);
assertFunction(parent);
assertFunction(this), assertFunction(parent);
this[prototype] = create(parent[prototype], getOwnPropertyDescriptors(this[prototype]));
return this;
}
extendBuiltInObject(Function, {
$define(STATIC, 'Function', {
/**
* Alternatives:
* http://underscorejs.org/#isFunction
@@ -18,7 +17,7 @@ extendBuiltInObject(Function, {
inherits: unbind(inherits),
_: _
});
extendBuiltInObject($Function, {
$define(PROTO, 'Function', {
invoke: function(args){
assertFunction(this);
var instance = create(this[prototype])
@@ -66,7 +65,7 @@ function createDeferred(set, clear, args){
},
run: function(){
id && clear(id);
id = apply.call(set, global, args);
id = apply.call(set, undefined, args);
return deferred;
}
}, id;
+1 -1
View File
@@ -1 +1 @@
global.global = global;
$define(GLOBAL, undefined, {global: global});
+4 -4
View File
@@ -6,7 +6,7 @@
* https://github.com/NobleJS/setImmediate
* https://github.com/calvinmetcalf/immediate
*/
if(!isSetImmediate){
global.setImmediate = setImmediate;
global.clearImmediate = clearImmediate;
}
$define(GLOBAL, undefined, {
setImmediate: setImmediate,
clearImmediate: clearImmediate
});
+1 -1
View File
@@ -52,7 +52,7 @@ isSetImmediate || !function(process, postMessage, MessageChannel, onreadystatech
} else if(isFunction(MessageChannel)){
channel = new MessageChannel();
channel.port1.onmessage = listner;
defer = tie.call(channel.port2, 'postMessage');
defer = ctx(channel.port2.postMessage, channel.port2);
// IE8-
// always run before timers, like nextTick => some problems with recursive call
} else if(document && onreadystatechange in document.createElement('script')){
+35 -30
View File
@@ -6,7 +6,6 @@ var prototype = 'prototype'
, String = global.String
, Number = global.Number
, RegExp = global.RegExp
, Date = global.Date
, Math = global.Math
, setTimeout = global.setTimeout
, clearTimeout = global.clearTimeout
@@ -16,9 +15,7 @@ var prototype = 'prototype'
, document = global.document
, Infinity = 1 / 0
, $Array = Array[prototype]
, $Number = Number[prototype]
, $Object = Object[prototype]
, $String = String[prototype]
, $Function = Function[prototype]
, console = global.console || {log: $Function};
@@ -49,40 +46,40 @@ function classof(it){
// Function:
var apply = $Function.apply
, call = $Function.call;
// unbind method from context
// foo.fn(arg1, arg2, ...) => fn(foo, arg1, arg2, ...)
function unbind(that){
return tie.call(that, 'call');
}
// simple bind context
function tie(key){
var that = this
, fn = that[key];
assertFunction(fn);
return function(){
return fn.apply(that, arguments);
}
}
// placeholder for partial apply
var _ = {};
// partial apply
function part(/*args...*/){
var fn = this
, lengthPart = arguments.length
, argsPart = Array(lengthPart)
, i = 0
var length = arguments.length
, args = Array(length)
, i = 0
, placeholder = false;
while(length > i)if((args[i] = arguments[i++]) === _)placeholder = true;
return createPartialApplication(this, args, length, placeholder, false);
}
function ctx(fn, that){
return function(){
return fn.apply(that, arguments);
}
}
function createPartialApplication(fn, argsPart, lengthPart, placeholder, bind, context){
assertFunction(fn);
while(lengthPart > i)if((argsPart[i] = arguments[i++]) === _)placeholder = true;
return function(/*args...*/){
var length = arguments.length
, i, j, args;
if(!placeholder && length === 0)return fn.apply(this, argsPart);
var that = bind ? context : this
, length = arguments.length
, i = 0, j = 0, args;
if(!placeholder && length == 0)return fn.apply(that, argsPart);
args = argsPart.slice();
i = j = 0;
if(placeholder)for(;lengthPart > i; i++)if(args[i] === _)args[i] = arguments[j++]
while(length > j)args.push(arguments[j++]);
return fn.apply(this, args);
return fn.apply(that, args);
}
}
// unbind method from context
// foo.fn(arg1, arg2, ...) => fn(foo, arg1, arg2, ...)
function unbind(that){
return function(){
return call.apply(that, arguments);
}
}
// add `this` as first argument
@@ -106,7 +103,7 @@ function has(object, key){
}
var isEnumerable = $Object.propertyIsEnumerable
, defineProperty = Object.defineProperty
, PROTO = '__proto__' in $Object
, __PROTO__ = '__proto__' in $Object
, DESCRIPTORS = 1;
// http://wiki.ecmascript.org/doku.php?id=strawman:extended_object_api
function getOwnPropertyDescriptors(object){
@@ -158,7 +155,11 @@ function reduceTo(target, callbackfn){
callbackfn = target;
target = {};
} else target = Object(target);
forEach.call(this, callbackfn, target);
assertFunction(callbackfn);
var self = ES5Object(this)
, length = toLength(self.length)
, i = 0;
for(;length > i; i++)i in self && callbackfn(target, self[i], i, this);
return target;
}
@@ -207,4 +208,8 @@ function descriptor(bitmap, value){
}
function hidden(object, key, value){
return defineProperty(object, key, descriptor(6, value));
}
}
var GLOBAL = 0
, STATIC = 1
, PROTO = 2;
+8 -8
View File
@@ -1,10 +1,10 @@
function extendBuiltInObject(target, source, forced /* = false */){
if(target)for(var key in source){
try {
has(source, key)
&& (forced || !has(target, key) || !isNative(target[key]))
&& delete target[key]
&& hidden(target, key, source[key]);
} catch(e){}
var $exports = typeof exports != 'undefined' ? exports : (global.C = {});
function $define(type, name, source, forced /* = false */){
var old = type == GLOBAL ? global : type == STATIC ? global[name] || {} : (global[name] || {})[prototype] || {}
, target = type == GLOBAL ? $exports : $exports[name] || ($exports[name] = {})
, key, prop;
for(key in source)if(has(source, key)){
prop = !forced && old && isNative(old[key]) ? old[key] : source[key];
target[key] = type == PROTO && isFunction(prop) ? unbind(prop) : prop;
}
}
+5 -5
View File
@@ -1,11 +1,11 @@
extendBuiltInObject(Number, {
$define(STATIC, 'Number', {
/**
* Alternatives:
* http://mootools.net/docs/core/Types/Number#Number:toInt
*/
toInteger: toInteger
});
extendBuiltInObject($Number, {
$define(PROTO, 'Number', {
/**
* Invoke function @ times and return array of results
* Alternatives:
@@ -46,7 +46,7 @@ extendBuiltInObject($Number, {
* http://sugarjs.com/api/Number/math
* http://mootools.net/docs/core/Types/Number#Number-Math
*/
extendBuiltInObject($Number, reduceTo.call(
$define(PROTO, 'Number', reduceTo.call(
// IE... getOwnPropertyNames(Math)
array(
// ES3
@@ -54,7 +54,7 @@ extendBuiltInObject($Number, reduceTo.call(
// ES6
'acosh,asinh,atanh,cbrt,cosh,expm1,hypot,imul,log1p,log10,log2,sign,sinh,tanh,trunc'
),
function(key){
if(key in Math)this[key] = methodize.call(Math[key]);
function(memo, key){
if(key in Math)memo[key] = methodize.call(Math[key]);
}
));
+31 -25
View File
@@ -1,6 +1,9 @@
!function(){
function make(proto, props){
return create(proto, props ? getOwnPropertyDescriptors(props) : undefined);
function mixin(target, source){
return defineProperties(target, getOwnPropertyDescriptors(source));
}
function make(proto, props, desc){
return props ? (desc ? mixin : assign)(create(proto), props) : create(proto);
}
function merge(target, source, deep /* = false */, reverse /* = false */, desc /* = false */, stackA, stackB){
if(isObject(target) && isObject(source)){
@@ -92,13 +95,13 @@
StackA = StackA.concat([a]);
StackB = StackB.concat([b]);
switch(type){
case'Boolean' :
case'String' :
case'Number' : return a.valueOf() == b.valueOf();
case'RegExp' : return '' + a == '' + b;
case'Error' : return a.message == b.message;/*
case'Array' :
case'Arguments' :
case 'Boolean' :
case 'String' :
case 'Number' : return a.valueOf() == b.valueOf();
case 'RegExp' : return '' + a == '' + b;
case 'Error' : return a.message == b.message;/*
case 'Array' :
case 'Arguments' :
length = toLength(a.length);
if(length != b.length)return false;
while(length--){
@@ -120,16 +123,6 @@
}
return true;
}
function forOwnKeys(object, fn, that /* = undefined */){
assertFunction(fn);
var O = ES5Object(object)
, props = keys(O)
, length = props.length
, i = 0
, key;
while(length > i)fn.call(that, O[key = props[i++]], key, object);
return object;
}
function findIndex(object, fn, that /* = undefined */){
assertFunction(fn);
var O = ES5Object(object)
@@ -141,7 +134,7 @@
if(fn.call(that, O[key = props[i++]], key, object))return key;
}
}
extendBuiltInObject(Object, {
$define(STATIC, 'Object', {
/**
* Alternatives:
* http://underscorejs.org/#has
@@ -172,9 +165,7 @@
* 19.1.3.15 Object.mixin ( target, source ) <= Removed in Draft Rev 22, January 20, 2014, http://esdiscuss.org/topic/november-19-2013-meeting-notes#content-1
* TODO: rename
*/
mixin: function(target, source){
return defineProperties(target, getOwnPropertyDescriptors(source));
},
mixin: mixin,
/**
* Alternatives:
* http://underscorejs.org/#clone
@@ -281,7 +272,16 @@
* http://api.jquery.com/jQuery.each/
* http://docs.angularjs.org/api/angular.forEach
*/
forEach: forOwnKeys,
forEach: function(object, fn, that /* = undefined */){
assertFunction(fn);
var O = ES5Object(object)
, props = keys(O)
, length = props.length
, i = 0
, key;
while(length > i)fn.call(that, O[key = props[i++]], key, object);
return object;
},
/**
* Alternatives:
* http://mootools.net/docs/core/Types/Object#Object:Object-keyOf
@@ -372,7 +372,13 @@
target = create(null);
}
else target = Object(target);
forOwnKeys(object, callbackfn, target);
assertFunction(callbackfn);
var O = ES5Object(object)
, props = keys(O)
, length = props.length
, i = 0
, key;
while(length > i)callbackfn(target, O[key = props[i++]], key, object);
return target;
},
/**
+2 -2
View File
@@ -35,7 +35,7 @@
, DETAIL = symbol('detail')
, ITERABLE_ERROR = 'You must pass an array to race or all';
// https://github.com/domenic/promises-unwrapping#the-promise-constructor
function Promise(resolver){
Promise = function(resolver){
var promise = this
, rejectPromise = part.call(handle, promise, REJECTED);
assertInstance(promise, Promise, 'Promise');
@@ -47,7 +47,6 @@
rejectPromise(e);
}
}
global.Promise = Promise;
function invokeCallback(settled, promise, callback, detail){
var hasCallback = isFunction(callback)
, value, error, succeeded, failed;
@@ -193,4 +192,5 @@
}
}
}();
$define(GLOBAL, undefined, {Promise: Promise});
}(global.Promise);
+1 -1
View File
@@ -11,7 +11,7 @@
, RegExpEscapeHTML = RegExp('[' + keys(dictionaryEscapeHTML).join('') + ']', 'g')
, RegExpUnescapeHTML = RegExp('(' + keys(dictionaryUnescapeHTML).join('|') + ')', 'g')
, RegExpEscapeRegExp = /([\\\/'*+?|()\[\]{}.^$])/g;
extendBuiltInObject($String, {
$define(PROTO, 'String', {
/**
* Alternatives:
* http://sugarjs.com/api/String/at
+4 -4
View File
@@ -6,8 +6,8 @@
* https://github.com/component/symbol
* https://github.com/anthonyshort/symbol
*/
if(!isNative(global.Symbol)){
global.Symbol = function(description){
$define(GLOBAL, undefined, {
Symbol: function(description){
var tag = symbol(description);
defineProperty($Object, tag, {set: function(value){
hidden(this, tag, value);
@@ -15,5 +15,5 @@ if(!isNative(global.Symbol)){
return {toString: function(){
return tag;
}};
};
};
}
});
+79 -75
View File
@@ -19,18 +19,18 @@
test('Array::reduceTo', function(){
var arr, obj;
ok(isFunction(Array.prototype.reduceTo), 'Is function');
(arr = [1]).reduceTo(function(val, key, that){
deepEqual({}, this, 'Default context is empty object');
(arr = [1]).reduceTo(function(memo, val, key, that){
deepEqual({}, memo, 'Default memo is empty object');
ok(val === 1, 'First argumert is value');
ok(key === 0, 'Second argumert is index');
return ok(that === arr, 'Third argumert is array');
});
[1].reduceTo(obj = {}, function(){
return ok(this === obj, 'Can reduce to exist object');
[1].reduceTo(obj = {}, function(memo){
return ok(memo === obj, 'Can reduce to exist object');
});
deepEqual([3, 2, 1], [1, 2, 3].reduceTo([], (function(it){
return this.unshift(it);
})), 'Reduce to object and return it');
deepEqual([3, 2, 1], [1, 2, 3].reduceTo([], function(memo, it){
return memo.unshift(it);
}), 'Reduce to object and return it');
});
test('Array::merge', function(){
var arr;
@@ -504,31 +504,31 @@
reduceTo = Array.reduceTo;
reduceTo(al = function(){
return arguments;
}(1), function(val, key, that){
deepEqual({}, this);
}(1), function(memo, val, key, that){
deepEqual({}, memo);
ok(val === 1);
ok(key === 0);
return ok(that === al);
});
reduceTo(al = '1', function(val, key, that){
deepEqual({}, this);
reduceTo(al = '1', function(memo, val, key, that){
deepEqual({}, memo);
ok(val === '1');
ok(key === 0);
return ok(that == al);
});
reduceTo(function(){
return arguments;
}(1), obj = {}, function(){
return ok(this === obj);
}(1), obj = {}, function(it){
return ok(it === obj);
});
deepEqual([3, 2, 1], reduceTo(function(){
return arguments;
}(1, 2, 3), [], (function(it){
return this.unshift(it);
})));
deepEqual(['3', '2', '1'], reduceTo('123', [], (function(it){
return this.unshift(it);
})));
}(1, 2, 3), [], function(memo, it){
return memo.unshift(it);
}));
deepEqual(['3', '2', '1'], reduceTo('123', [], function(memo, it){
return memo.unshift(it);
}));
});
test('Array.merge', function(){
var merge, args;
@@ -2033,6 +2033,59 @@
});
}).call(this);
// Generated by LiveScript 1.2.0
(function(){
var bzzzzz;
asyncTest('setImmediate / clearImmediate', 6, function(){
var tmp1, id, tmp2, tmp3, tmp4;
ok(Function.isFunction(global.setImmediate), 'setImmediate is function');
ok(Function.isFunction(global.clearImmediate), 'clearImmediate is function');
id = setImmediate(function(){
tmp1 = 42;
});
ok(tmp1 === void 8, 'setImmediate is async');
setImmediate(function(){
tmp2 = true;
});
setTimeout(function(){
ok(tmp2, 'setImmediate works');
}, 20);
setImmediate(function(b, c){
tmp3 = b + c;
}, 'b', 'c');
setTimeout(function(){
ok(tmp3 === 'bc', 'setImmediate works with additional params');
}, 20);
clearImmediate(setImmediate(function(){
tmp4 = 42;
}));
setTimeout(function(){
ok(tmp4 === void 8, 'clearImmediate works');
}, 20);
setTimeout(start, 50);
});
bzzzzz = function(){
var x, now, inc;
x = 0;
now = Date.now();
return (inc = function(){
return setImmediate(function(){
x = x + 1;
if (Date.now() - now < 1000) {
return inc();
} else {
return console("setImmediate: " + x + " per second");
}
});
})();
};
if (typeof window != 'undefined' && window !== null) {
window.onload = bzzzzz;
} else {
bzzzzz();
}
}).call(this);
// Generated by LiveScript 1.2.0
(function(){
var isFunction;
@@ -2813,23 +2866,23 @@
ok(isFunction(reduceTo), 'Is function');
reduceTo(obj = {
q: 1
}, function(val, key, that){
deepEqual(this, plane());
}, function(memo, val, key, that){
deepEqual(memo, plane());
ok(val === 1);
ok(key === 'q');
return ok(that === obj);
});
reduceTo({
q: 1
}, obj = {}, function(){
return ok(this === obj);
}, obj = {}, function(it){
return ok(it === obj);
});
deepEqual(reduceTo({
q: 1,
w: 2,
e: 3
}, function(it){
return this[it] = it;
}, function(memo, it){
return memo[it] = it;
}), plane({
1: 1,
2: 2,
@@ -3029,8 +3082,7 @@
// Generated by LiveScript 1.2.0
(function(){
var isFunction, bzzzzz, slice$ = [].slice;
isFunction = Function.isFunction;
var slice$ = [].slice;
asyncTest('setTimeout / clearTimeout', 2, function(){
global.setTimeout(function(b, c){
ok(b + c === 'bc');
@@ -3060,54 +3112,6 @@
i = i + 1;
}, 1, 42);
});
asyncTest('setImmediate / clearImmediate', 6, function(){
var tmp1, id, tmp2, tmp3, tmp4;
ok(isFunction(global.setImmediate), 'setImmediate is function');
ok(isFunction(global.clearImmediate), 'clearImmediate is function');
id = setImmediate(function(){
tmp1 = 42;
});
ok(tmp1 === void 8, 'setImmediate is async');
setImmediate(function(){
tmp2 = true;
});
setTimeout(function(){
ok(tmp2, 'setImmediate works');
}, 20);
setImmediate(function(b, c){
tmp3 = b + c;
}, 'b', 'c');
setTimeout(function(){
ok(tmp3 === 'bc', 'setImmediate works with additional params');
}, 20);
clearImmediate(setImmediate(function(){
tmp4 = 42;
}));
setTimeout(function(){
ok(tmp4 === void 8, 'clearImmediate works');
}, 20);
setTimeout(start, 50);
});
bzzzzz = function(){
var x, now, inc;
x = 0;
now = Date.now();
return (inc = function(){
return setImmediate(function(){
x = x + 1;
if (Date.now() - now < 1000) {
return inc();
} else {
return console("setImmediate: " + x + " per second");
}
});
})();
};
if (typeof window != 'undefined' && window !== null) {
window.onload = bzzzzz;
} else {
bzzzzz();
}
function partialize$(f, args, where){
var context = this;
return function(){
+5 -5
View File
@@ -13,14 +13,14 @@ test 'Array::pluck' !->
deepEqual [\1 2 void]pluck(\length), [1 void void], 'works with undefined'
test 'Array::reduceTo' !->
ok isFunction(Array::reduceTo), 'Is function'
(arr = [1])reduceTo (val, key, that)->
deepEqual {} @, 'Default context is empty object'
(arr = [1])reduceTo (memo, val, key, that)->
deepEqual {} memo, 'Default memo is empty object'
ok val is 1, 'First argumert is value'
ok key is 0, 'Second argumert is index'
ok that is arr, 'Third argumert is array'
[1]reduceTo obj = {} ->
ok @ is obj, 'Can reduce to exist object'
deepEqual [3 2 1] [1 2 3]reduceTo([] (@unshift <|)), 'Reduce to object and return it'
[1]reduceTo obj = {} (memo)->
ok memo is obj, 'Can reduce to exist object'
deepEqual [3 2 1] [1 2 3]reduceTo([] (memo, it)-> memo.unshift it), 'Reduce to object and return it'
test 'Array::merge' !->
ok isFunction(Array::merge), 'Is function'
arr = [1 2 3]
+7 -7
View File
@@ -219,20 +219,20 @@ test 'Array.pluck' !->
deepEqual pluck(\123 \length), [1 1 1]
test 'Array.reduceTo' !->
{reduceTo} = Array
reduceTo (al = (->&)(1)), (val, key, that)->
deepEqual {} @
reduceTo (al = (->&)(1)), (memo, val, key, that)->
deepEqual {} memo
ok val is 1
ok key is 0
ok that is al
reduceTo (al = \1), (val, key, that)->
deepEqual {} @
reduceTo (al = \1), (memo, val, key, that)->
deepEqual {} memo
ok val is \1
ok key is 0
ok that ~= al
reduceTo (->&)(1), obj = {} ->
ok @ is obj
deepEqual [3 2 1], reduceTo (->&)(1 2 3), [] (@unshift <|)
deepEqual [\3 \2 \1], reduceTo \123 [] (@unshift <|)
ok it is obj
deepEqual [3 2 1], reduceTo (->&)(1 2 3), [] (memo, it)-> memo.unshift it
deepEqual [\3 \2 \1], reduceTo \123 [] (memo, it)-> memo.unshift it
test 'Array.merge' !->
{merge} = Array
args = (->&)(1 2 3)
+31
View File
@@ -0,0 +1,31 @@
asyncTest 'setImmediate / clearImmediate' 6 !->
ok Function.isFunction(global.setImmediate), 'setImmediate is function'
ok Function.isFunction(global.clearImmediate), 'clearImmediate is function'
var tmp1
id = setImmediate !-> tmp1 := 42
ok tmp1 is void, 'setImmediate is async'
#ok Number.isInteger(id) && id > 0, 'setImmediate return id' # fail in node 0.9+
var tmp2
setImmediate !->
tmp2 := on
setTimeout _, 20 <| !->
ok tmp2, 'setImmediate works'
var tmp3
setImmediate _, \b \c <| (b, c)!->
tmp3 := b + c
setTimeout _, 20 <| !->
ok tmp3 is \bc, 'setImmediate works with additional params'
var tmp4
clearImmediate setImmediate !-> tmp4 := 42
setTimeout _, 20 <| !->
ok tmp4 is void, 'clearImmediate works'
setTimeout start, 50
bzzzzz = ->
x = 0
now = Date.now!
do inc = -> setImmediate ->
x := x + 1
if Date.now! - now < 1000 => inc!
else console "setImmediate: #x per second"
if window? => window.onload = bzzzzz else bzzzzz!
+4 -4
View File
@@ -355,14 +355,14 @@ test 'Object.pluck' !->
test 'Object.reduceTo' !->
{reduceTo, plane} = Object
ok isFunction(reduceTo), 'Is function'
reduceTo (obj = q: 1), (val, key, that)->
deepEqual @, plane!
reduceTo (obj = q: 1), (memo, val, key, that)->
deepEqual memo, plane!
ok val is 1
ok key is \q
ok that is obj
reduceTo {q:1} obj = {} ->
ok @ is obj
deepEqual reduceTo({q:1 w:2 e:3} -> @[it] = it), plane {1:1 2:2 3:3}
ok it is obj
deepEqual reduceTo({q:1 w:2 e:3} (memo, it)-> memo[it] = it), plane {1:1 2:2 3:3}
test 'Object.isObject' !->
{isObject} = Object
ok isFunction(isObject), 'Is function'
+1 -33
View File
@@ -1,4 +1,3 @@
{isFunction} = Function
asyncTest 'setTimeout / clearTimeout' 2 !->
global.setTimeout _, 1 \b \c <| (b, c)!->
ok b + c is \bc
@@ -15,35 +14,4 @@ asyncTest 'setInterval / clearInterval' 6 !->
if i is 3
clearInterval interval
start!
i := i + 1), 1, 42
asyncTest 'setImmediate / clearImmediate' 6 !->
ok isFunction(global.setImmediate), 'setImmediate is function'
ok isFunction(global.clearImmediate), 'clearImmediate is function'
var tmp1
id = setImmediate !-> tmp1 := 42
ok tmp1 is void, 'setImmediate is async'
#ok Number.isInteger(id) && id > 0, 'setImmediate return id' # fail in node 0.9+
var tmp2
setImmediate !->
tmp2 := on
setTimeout _, 20 <| !->
ok tmp2, 'setImmediate works'
var tmp3
setImmediate _, \b \c <| (b, c)!->
tmp3 := b + c
setTimeout _, 20 <| !->
ok tmp3 is \bc, 'setImmediate works with additional params'
var tmp4
clearImmediate setImmediate !-> tmp4 := 42
setTimeout _, 20 <| !->
ok tmp4 is void, 'clearImmediate works'
setTimeout start, 50
bzzzzz = ->
x = 0
now = Date.now!
do inc = -> setImmediate ->
x := x + 1
if Date.now! - now < 1000 => inc!
else console "setImmediate: #x per second"
if window? => window.onload = bzzzzz else bzzzzz!
i := i + 1), 1, 42