Skip to content

Commit

Permalink
[experimental] add fix for Array#sort, #156
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Jan 11, 2016
1 parent ab92628 commit cf15d10
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
list: [
'es5',
'es5.array.sort',
'es6.symbol',
'es6.object.assign',
'es6.object.is',
Expand Down Expand Up @@ -157,6 +158,7 @@ module.exports = {
'core.string.unescape-html'
],
experimental: [
'es5.array.sort',
'es7.reflect.define-metadata',
'es7.reflect.delete-metadata',
'es7.reflect.get-metadata',
Expand Down
19 changes: 19 additions & 0 deletions library/modules/es5.array.sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
var $export = require('./_export')
, aFunction = require('./_a-function')
, fails = require('./_fails')
, $sort = [].sort
, test = [1, 2, 3];

$export($export.P + $export.F * (fails(function(){
// IE8-
test.sort(undefined);
}) || !fails(function(){
// V8 bug
test.sort(null);
})), 'Array', {
// 22.1.3.25 Array.prototype.sort(comparefn)
sort: function sort(comparefn){
return comparefn === undefined ? $sort.call(this) : $sort.call(this, aFunction(comparefn));
}
});
19 changes: 19 additions & 0 deletions modules/es5.array.sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';
var $export = require('./_export')
, aFunction = require('./_a-function')
, fails = require('./_fails')
, $sort = [].sort
, test = [1, 2, 3];

$export($export.P + $export.F * (fails(function(){
// IE8-
test.sort(undefined);
}) || !fails(function(){
// V8 bug
test.sort(null);
})), 'Array', {
// 22.1.3.25 Array.prototype.sort(comparefn)
sort: function sort(comparefn){
return comparefn === undefined ? $sort.call(this) : $sort.call(this, aFunction(comparefn));
}
});
47 changes: 47 additions & 0 deletions tests/experimental.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/experimental/es5.array.sort.ls
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{module, test} = QUnit
module \ES5

test 'Array#sort' (assert)!->
assert.isFunction Array::sort
assert.arity Array::sort, 1
assert.name Array::sort, \sort
assert.looksNative Array::sort
assert.looksNative Array::sort
assert.ok !!(try [1 2 3]sort void), 'works with undefined'
assert.throws (!-> [1 2 3]sort null), 'throws on null'
assert.throws (!-> [1 2 3]sort {}), 'throws on {}'
if STRICT
assert.throws (-> Array::sort.call null), TypeError, 'ToObject(this)'
assert.throws (-> Array::sort.call void), TypeError, 'ToObject(this)'
if NATIVE => assert.ok !!(try Array::sort.call {0: 1, 1: 3, 2: 2, length: -1}, -> throw 42), 'ToLength(? Get(obj, "length"))'

0 comments on commit cf15d10

Please sign in to comment.