forked from cujojs/wire
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use spread operator instead of apply when supported
Closes cujojs#181
- Loading branch information
Krzysztof Chrapka
committed
Apr 23, 2016
1 parent
4185eb1
commit b123f54
Showing
8 changed files
with
277 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(function(define) { | ||
'use strict'; | ||
define(function() { | ||
/** | ||
* Carefully sets the instance's constructor property to the supplied | ||
* constructor, using Object.defineProperty if available. If it can't | ||
* set the constructor in a safe way, it will do nothing. | ||
* | ||
* @param instance {Object} component instance | ||
* @param ctor {Function} constructor | ||
*/ | ||
function defineConstructorIfPossible(instance, ctor) { | ||
try { | ||
Object.defineProperty(instance, 'constructor', { | ||
value: ctor, | ||
enumerable: false | ||
}); | ||
} catch(e) { | ||
// If we can't define a constructor, oh well. | ||
// This can happen if in envs where Object.defineProperty is not | ||
// available, or when using cujojs/poly or other ES5 shims | ||
} | ||
} | ||
|
||
return function(func, thisObj, args) { | ||
var result = null; | ||
|
||
if(thisObj && typeof thisObj[func] === 'function') { | ||
func = thisObj[func]; | ||
} | ||
|
||
// detect case when apply is called on constructor and fix prototype chain | ||
if (thisObj === func) { | ||
thisObj = Object.create(func.prototype); | ||
defineConstructorIfPossible(thisObj, func); | ||
func.apply(thisObj, args); | ||
result = thisObj; | ||
} else { | ||
result = func.apply(thisObj, args); | ||
} | ||
|
||
return result; | ||
}; | ||
}); | ||
})(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* jshint esversion: 6 */ | ||
(function(define) { | ||
'use strict'; | ||
define(function() { | ||
return function(func, thisObj, args) { | ||
var result = null; | ||
|
||
if(thisObj === func || (thisObj && thisObj.constructor === func)) { | ||
/* jshint newcap: false */ | ||
result = new func(...(args||[])); | ||
|
||
// detect broken old prototypes with missing constructor | ||
if (result.constructor !== func) { | ||
Object.defineProperty(result, 'constructor', { | ||
enumerable: false, | ||
value: func | ||
}); | ||
} | ||
} else if(thisObj && typeof thisObj[func] === 'function') { | ||
result = thisObj[func](...args); | ||
} else { | ||
result = func.apply(thisObj, args); | ||
} | ||
|
||
return result; | ||
}; | ||
}); | ||
})(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
(function(define) { | ||
define(function() { | ||
define(function(require) { | ||
|
||
var universalApply = require('./universalApply'); | ||
|
||
return function(methodName, args) { | ||
return function(target) { | ||
return target[methodName].apply(target, args); | ||
return universalApply(target[methodName], target, args); | ||
}; | ||
}; | ||
|
||
}); | ||
})(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(); }); | ||
})(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
(function(){ | ||
'use strict'; | ||
|
||
(function(define){ | ||
|
||
function evaluates (statement) { | ||
try { | ||
/* jshint evil: true */ | ||
eval(statement); | ||
/* jshint evil: false */ | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} | ||
|
||
// we have to know it synchronously, we are unable to load this module in asynchronous way | ||
// we cannot defer `define` and we cannot load module, that would not compile in browser | ||
// so we can't delegate this check to another module | ||
function isSpreadAvailable() { | ||
return evaluates('Math.max(...[ 5, 10 ])'); | ||
} | ||
|
||
var requires = []; | ||
if (typeof(process) !== 'undefined' && 'ES_VERSION' in process.env) { | ||
requires.push('./es'+process.env.ES_VERSION+'Apply'); | ||
} else { | ||
if(isSpreadAvailable()) { | ||
requires.push('./es6Apply'); | ||
} else { | ||
requires.push('./es5Apply'); | ||
} | ||
} | ||
|
||
define(requires, function(apply){ | ||
return apply; | ||
}); | ||
})( | ||
typeof define === 'function' | ||
? define | ||
: function(requires, factory) { | ||
module.exports = factory.apply(null, requires.map(require)); | ||
} | ||
); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,42 @@ | ||
'use strict'; | ||
|
||
require('gent/test-adapter/buster'); | ||
|
||
function evaluates (statement) { | ||
try { | ||
/* jshint evil: true */ | ||
eval(statement); | ||
/* jshint evil: false */ | ||
return true; | ||
} catch (err) { | ||
return false; | ||
} | ||
} | ||
|
||
function isClassAvailable() { | ||
return evaluates('class es6TestClass_ibyechBaloodren7 {}'); | ||
} | ||
|
||
function isSpreadAvailable() { | ||
return evaluates('parseInt(...["20", 10])'); | ||
} | ||
|
||
var tests = ['node/**/*-test.js']; | ||
|
||
console.log('class operator %savailable', isClassAvailable() ? '' : 'not '); | ||
console.log('spread operator %savailable', isSpreadAvailable() ? '' : 'not '); | ||
|
||
if( | ||
isClassAvailable() | ||
&& isSpreadAvailable() | ||
&& !('ES_VERSION' in process.env && parseFloat(process.env.ES_VERSION) < 6) | ||
) { | ||
tests.push('node-es6/**/*-test.js'); | ||
} | ||
|
||
module.exports['node'] = { | ||
environment: 'node', | ||
tests: ['node/**/*-test.js'] | ||
tests: tests | ||
// TODO: Why doesn't this work? | ||
//, testHelpers:['gent/test-adapter/buster'] | ||
}; | ||
}; |
Oops, something went wrong.