Skip to content

Commit

Permalink
Ensure Array functions have the correct name.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 24, 2015
1 parent 530f456 commit 87e880c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
14 changes: 7 additions & 7 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@
}

var ArrayShims = {
from: function (iterable) {
from: function from(iterable) {
var mapFn = arguments.length > 1 ? arguments[1] : void 0;

var list = ES.ToObject(iterable, 'bad iterable');
Expand Down Expand Up @@ -739,7 +739,7 @@
return result;
},

of: function () {
of: function of() {
return Array.from(arguments);
}
};
Expand Down Expand Up @@ -857,7 +857,7 @@
addIterator(ObjectIterator.prototype);

var ArrayPrototypeShims = {
copyWithin: function (target, start) {
copyWithin: function copyWithin(target, start) {
var end = arguments[2]; // copyWithin.length must be 2
var o = ES.ToObject(this);
var len = ES.ToLength(o.length);
Expand Down Expand Up @@ -887,7 +887,7 @@
return o;
},

fill: function (value) {
fill: function fill(value) {
var start = arguments.length > 1 ? arguments[1] : void 0;
var end = arguments.length > 2 ? arguments[2] : void 0;
var O = ES.ToObject(this);
Expand Down Expand Up @@ -938,15 +938,15 @@
return -1;
},

keys: function () {
keys: function keys() {
return new ArrayIterator(this, 'key');
},

values: function () {
values: function values() {
return new ArrayIterator(this, 'value');
},

entries: function () {
entries: function entries() {
return new ArrayIterator(this, 'entry');
}
};
Expand Down
56 changes: 51 additions & 5 deletions test/array.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global describe, it, expect, require, beforeEach */
/*global describe, it, xit, expect, require, beforeEach */


var runArrayTests = function () {
Expand All @@ -10,6 +10,8 @@ var runArrayTests = function () {
return typeof Sym === 'function' && typeof sym === 'symbol';
/*jshint notypeof: false */
};
var functionsHaveNames = (function foo() {}).name === 'foo';
var ifFunctionsHaveNamesIt = functionsHaveNames ? it : xit;

describe('Array', function () {
var list = [5, 10, 15, 20];
Expand Down Expand Up @@ -47,6 +49,10 @@ var runArrayTests = function () {
expect(Array.propertyIsEnumerable('from')).to.equal(false);
});

ifFunctionsHaveNamesIt('has the correct name', function () {
expect(Array.from.name).to.equal('from');
});

it('works with primitives', function () {
expect(Array.from(false)).to.eql([]);
expect(Array.from(true)).to.eql([]);
Expand Down Expand Up @@ -238,6 +244,18 @@ var runArrayTests = function () {
});
}

it('has a length of 0', function () {
expect(Array.of.length).to.equal(0);
});

it('is not enumerable', function () {
expect(Array.propertyIsEnumerable('of')).to.equal(false);
});

ifFunctionsHaveNamesIt('has the correct name', function () {
expect(Array.of.name).to.equal('of');
});

it('should create correct array from arguments', function () {
expect(Array.of(1, null, undefined)).to.eql([1, null, undefined]);
});
Expand All @@ -254,6 +272,10 @@ var runArrayTests = function () {
expect(Array.prototype.copyWithin.length).to.equal(2);
});

ifFunctionsHaveNamesIt('has the correct name', function () {
expect(Array.prototype.copyWithin.name).to.equal('copyWithin');
});

it('modifies the object in-place', function () {
var arr = [1, 2, 3, 4, 5];
expect(arr.copyWithin(0, 3)).to.eql([4, 5, 3, 4, 5]);
Expand Down Expand Up @@ -299,6 +321,10 @@ var runArrayTests = function () {
});
}

ifFunctionsHaveNamesIt('has the correct name', function () {
expect(Array.prototype.find.name).to.equal('find');
});

it('should have a length of 1', function () {
expect(Array.prototype.find.length).to.equal(1);
});
Expand Down Expand Up @@ -378,6 +404,10 @@ var runArrayTests = function () {
});
}

ifFunctionsHaveNamesIt('has the correct name', function () {
expect(Array.prototype.findIndex.name).to.equal('findIndex');
});

it('should have a length of 1', function () {
expect(Array.prototype.findIndex.length).to.equal(1);
});
Expand Down Expand Up @@ -483,10 +513,14 @@ var runArrayTests = function () {
});
}

it('should have a length of zero', function () {
it('should have the right arity ', function () {
expect(Array.prototype.keys.length).to.equal(0);
});

ifFunctionsHaveNamesIt('has the correct name', function () {
expect(Array.prototype.keys.name).to.equal('keys');
});

var mylist = [5, 10, 15, 20];
var keys;
beforeEach(function () {
Expand Down Expand Up @@ -546,10 +580,14 @@ var runArrayTests = function () {
});
}

it('should have a length of zero', function () {
it('should have the right arity', function () {
expect(Array.prototype.values.length).to.equal(0);
});

ifFunctionsHaveNamesIt('has the correct name', function () {
expect(Array.prototype.values.name).to.equal('values');
});

var mylist = [5, 10, 15, 20];
var values;
beforeEach(function () {
Expand Down Expand Up @@ -609,10 +647,14 @@ var runArrayTests = function () {
});
}

it('should have a length of zero', function () {
it('should have the right arity', function () {
expect(Array.prototype.entries.length).to.equal(0);
});

ifFunctionsHaveNamesIt('has the correct name', function () {
expect(Array.prototype.entries.name).to.equal('entries');
});

var mylist = [5, 10, 15, 20];
var entries;
beforeEach(function () {
Expand Down Expand Up @@ -678,10 +720,14 @@ var runArrayTests = function () {
});
}

it('has the right length', function () {
it('has the right arity', function () {
expect(Array.prototype.fill.length).to.equal(1);
});

ifFunctionsHaveNamesIt('has the correct name', function () {
expect(Array.prototype.fill.name).to.equal('fill');
});

it('works with just a value', function () {
var original = [1, 2, 3, 4, 5, 6];
var filled = [-1, -1, -1, -1, -1, -1];
Expand Down

0 comments on commit 87e880c

Please sign in to comment.