Comparing version 0.1.0 to 0.2.0
@@ -15,3 +15,16 @@ (function(definition) { | ||
var global_isFinite = globall.isFinite; | ||
var unique = function(array) { | ||
var result = []; | ||
var item; | ||
for (var i = 0, length = array.length; i < length; i++) { | ||
item = array[i]; | ||
if (result.indexOf(item) === -1) { | ||
result.push(item); | ||
} | ||
} | ||
return result; | ||
}; | ||
var defineProperty = function(object, name, method) { | ||
@@ -34,12 +47,2 @@ if (!object[name]) { | ||
var sign = function(n) { | ||
return (n < 0) ? -1 : 1; | ||
}; | ||
var unique = function(iterable) { | ||
return Array.from(Set.from(iterable)); | ||
}; | ||
// http://wiki.ecmascript.org/doku.php?id=harmony:string.prototype.repeat | ||
// http://wiki.ecmascript.org/doku.php?id=harmony:string_extras | ||
defineProperties(String.prototype, { | ||
@@ -49,5 +52,7 @@ repeat: function(times) { | ||
}, | ||
startsWith: function(substring) { | ||
return this.indexOf(substring) === 0; | ||
}, | ||
endsWith: function(s) { | ||
@@ -57,5 +62,7 @@ var t = String(s); | ||
}, | ||
contains: function(s) { | ||
return this.indexOf(s) !== -1; | ||
}, | ||
toArray: function() { | ||
@@ -66,3 +73,2 @@ return this.split(''); | ||
// https://gist.github.com/1074126 | ||
defineProperties(Array, { | ||
@@ -102,3 +108,3 @@ from: function(iterable) { | ||
if (n === 0 || !global_isFinite(n)) return n; | ||
return sign(n) * Math.floor(Math.abs(n)); | ||
return Math.sign(n) * Math.floor(Math.abs(n)); | ||
} | ||
@@ -127,9 +133,9 @@ }); | ||
getPropertyNames: function(subject, name) { | ||
var set = Set.from(Object.getOwnPropertyNames(subject)); | ||
var result = Object.getOwnPropertyNames(subject); | ||
var proto = Object.getPrototypeOf(subject); | ||
while (proto !== null) { | ||
Object.getOwnPropertyNames(proto).forEach(set.add); | ||
result = result.concat(Object.getOwnPropertyNames(proto)); | ||
proto = Object.getPrototypeOf(proto); | ||
} | ||
return Array.from(set); | ||
return unique(result); | ||
}, | ||
@@ -151,2 +157,10 @@ | ||
}); | ||
defineProperties(Math, { | ||
sign: function(value) { | ||
var number = +value; | ||
if (global_isNaN(number) || number === 0) return number; | ||
return (number < 0) ? -1 : 1; | ||
} | ||
}); | ||
@@ -198,3 +212,2 @@ defineProperties(globall, { | ||
return Map; | ||
// TODO: iteration. | ||
})(), | ||
@@ -223,8 +236,7 @@ | ||
return Set; | ||
// TODO: iteration. | ||
})() | ||
}); | ||
defineProperties(globall.Set, { | ||
from: function(iterable) { | ||
/*defineProperties(globall.Set, { | ||
of: function(iterable) { | ||
var object = Object(iterable); | ||
@@ -240,8 +252,4 @@ var set = Set(); | ||
return set; | ||
}, | ||
of: function() { | ||
return Set.from(arguments); | ||
} | ||
}); | ||
});*/ | ||
}); |
@@ -5,3 +5,3 @@ { | ||
"description": "ECMAScript 6 (Harmony) compatibility shims for legacy JavaScript engines", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/paulmillr/es6-shim/", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -5,2 +5,6 @@ # ES6 Shim | ||
Project targets engines that support ES5 (Firefox, Chrome, Safari, Opera). With | ||
[ES5-shim](https://github.com/kriskowal/es5-shim) it could also work in older | ||
browsers. | ||
## Installation | ||
@@ -11,19 +15,15 @@ | ||
## Safe shims | ||
* Maps & Sets | ||
* String.prototype.repeat, String.prototype.startsWith, | ||
String.prototype.endsWith, String.prototype.contains, String.prototype.toArray | ||
* Array.from, Array.of | ||
* Number.isNaN, Number.toInteger | ||
* Object.getOwnPropertyDescriptors, Object.is, Object.isInteger | ||
* Maps & Sets | ||
* Number.isNaN, Number.toInteger, Number.isInteger | ||
* Object.getOwnPropertyDescriptors, Object.getPropertyDescriptor, | ||
Object.getPropertyNames, Object.is | ||
* Math.sign | ||
## Dubious shims | ||
## Shims that fail silently | ||
## License | ||
The MIT License (MIT) | ||
Copyright (c) 2011 Paul Miller | ||
Copyright (c) 2011 Paul Miller (http://paulmillr.com) | ||
@@ -30,0 +30,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of |
@@ -5,21 +5,40 @@ var expect = require('expect.js'); | ||
describe('Map', function() { | ||
var map; | ||
beforeEach(function() { | ||
map = Map(); | ||
}); | ||
it('should exist in global namespace', function() { | ||
expect(Map).to.be.ok(); | ||
}); | ||
it('should return true on values that set has', function() { | ||
map.set(1, 2); | ||
expect(map.has(1)).to.be.ok(); | ||
expect(map.get(1)).to.equal(2); | ||
describe('#get()', function() { | ||
var arr = [1, 2, 3]; | ||
map.set(arr, [3, 2, 1]); | ||
expect(map.has(arr)).to.be.ok(); | ||
expect(map.get(arr)).to.eql([3, 2, 1]); | ||
var obj = {a: 1}; | ||
map.set(obj, {b: 1}); | ||
expect(map.has(obj)).to.be.ok(); | ||
expect(map.get(obj)).to.eql({b: 1}); | ||
}); | ||
describe('#has()', function() { | ||
it('should return true on values that set has not', function() { | ||
map.set(1); | ||
expect(map.has(2)).to.not.be.ok(); | ||
expect(map.get(2)).to.equal(void 0); | ||
}); | ||
describe('#set()', function() { | ||
it('should delete props', function() { | ||
map.set(1, 2); | ||
map.delete(1); | ||
expect(map.has(1)).to.not.be.ok(); | ||
expect(map.get(1)).to.equal(void 0); | ||
}); | ||
describe('#delete()', function() { | ||
}); | ||
}); |
@@ -5,7 +5,7 @@ var expect = require('expect.js'); | ||
describe('Number', function() { | ||
describe('Number.isInteger', function() { | ||
describe('Number.isInteger()', function() { | ||
it('should be truthy on integers', function() { | ||
[5295, -5295, -9007199254740991, 9007199254740991, 0, | ||
-0].forEach(function(value) { | ||
expect(Number.isInteger(value)).to.be.ok; | ||
expect(Number.isInteger(value)).to.be.ok(); | ||
}); | ||
@@ -17,3 +17,3 @@ }); | ||
5.9, NaN].forEach(function(value) { | ||
expect(Number.isInteger(value)).to.not.be.ok; | ||
expect(Number.isInteger(value)).to.not.be.ok(); | ||
}); | ||
@@ -23,6 +23,5 @@ }); | ||
describe('Number.isNaN', function() { | ||
describe('Number.isNaN()', function() { | ||
it('should be truthy on NaN', function() { | ||
console.log(Number.isNaN); | ||
expect(Number.isNaN(NaN)).to.be.ok; | ||
expect(Number.isNaN(NaN)).to.be.ok(); | ||
}); | ||
@@ -32,3 +31,3 @@ | ||
[void 0, true, null, {}, 37, [], 'str'].forEach(function(item) { | ||
expect(Number.isNaN(item)).to.not.be.ok; | ||
expect(Number.isNaN(item)).to.not.be.ok(); | ||
}); | ||
@@ -38,11 +37,15 @@ }); | ||
describe('Number.toInteger', function() { | ||
it('', function() { | ||
describe('Number.toInteger()', function() { | ||
it('should convert things to integer value', function() { | ||
expect(Number.toInteger(4294967297)).to.equal(4294967297); | ||
expect(Number.toInteger(1.5)).to.equal(1); | ||
expect(Number.toInteger(-1.5)).to.equal(-1); | ||
expect(Number.toInteger(1/3)).to.equal(0); | ||
expect(Number.toInteger(Infinity)).to.equal(Infinity); | ||
expect(Number.toInteger(Infinity)).to.equal(Infinity); | ||
expect(Number.toInteger(Infinity)).to.equal(Infinity); | ||
expect(Number.toInteger(NaN)).to.equal(0); | ||
expect(Number.toInteger('thing')).to.equal(0); | ||
}); | ||
it('', function() { | ||
}); | ||
}); | ||
}); |
@@ -5,39 +5,61 @@ var expect = require('expect.js'); | ||
describe('Object', function() { | ||
describe('Object.is', function() { | ||
describe('Object.is()', function() { | ||
it('should compare regular objects correctly', function() { | ||
expect(Object.is(null, null)).to.be.ok; | ||
expect(Object.is(void 0, void 0)).to.be.ok; | ||
expect(Object.is([0], [0])).to.not.be.ok; | ||
expect(Object.is(5, 5)).to.be.ok; | ||
expect(Object.is('str', 'str')).to.be.ok; | ||
expect(Object.is(null, null)).to.be.ok(); | ||
expect(Object.is(void 0, void 0)).to.be.ok(); | ||
expect(Object.is([0], [0])).to.not.be.ok(); | ||
expect(Object.is(5, 5)).to.be.ok(); | ||
expect(Object.is('str', 'str')).to.be.ok(); | ||
var obj = {a: null}; | ||
expect(Object.is(obj, obj)).to.be.ok; | ||
expect(Object.is(obj, obj)).to.be.ok(); | ||
}); | ||
it('should compare 0 and -0 correctly', function() { | ||
expect(Object.is(0, -0)).to.not.be.ok; | ||
expect(Object.is(0, -0)).to.not.be.ok(); | ||
}); | ||
it('should compare NaNs correctly', function() { | ||
expect(Object.is(NaN, NaN)).to.be.ok; | ||
expect(Object.is(NaN, NaN)).to.be.ok(); | ||
}); | ||
}); | ||
describe('#getOwnPropertyDescriptors', function() { | ||
it('', function() { | ||
describe('Object.getOwnPropertyDescriptors()', function() { | ||
it('should produce an array of properties', function() { | ||
expect(Object.getOwnPropertyDescriptors({a: 1, b: 2, c: 3})).to.eql({ | ||
a: {configurable: true, enumerable: true, value: 1, writable: true}, | ||
b: {configurable: true, enumerable: true, value: 2, writable: true}, | ||
c: {configurable: true, enumerable: true, value: 3, writable: true} | ||
}); | ||
}); | ||
}); | ||
describe('#getPropertyDescriptor', function() { | ||
it('', function() { | ||
describe('Object.getPropertyDescriptor()', function() { | ||
it('should produce an array of properties including inherited ones', | ||
function() { | ||
expect(Object.getPropertyDescriptor([1], 'length')).to.eql({ | ||
configurable: false, enumerable: false, value: 1, writable: true | ||
}); | ||
expect(Object.getPropertyDescriptor([1, 5], 'length')).to.eql({ | ||
configurable: false, enumerable: false, value: 2, writable: true | ||
}); | ||
expect(Object.getPropertyDescriptor(function(a) {}, 'length')).to.eql({ | ||
configurable: false, enumerable: false, value: 1, writable: false | ||
}); | ||
}); | ||
}); | ||
describe('#getPropertyNames', function() { | ||
it('', function() { | ||
describe('Object.getPropertyNames()', function() { | ||
it('should produce an array of property names including inherited ones', | ||
function() { | ||
expect(Object.getPropertyNames(Object.create(null))).to.eql([]); | ||
var obj = {}; | ||
expect(Object.getPropertyNames(Object.create(obj))).to.eql( | ||
Object.getOwnPropertyNames(obj).concat( | ||
Object.getOwnPropertyNames(Object.getPrototypeOf(obj)) | ||
) | ||
); | ||
}); | ||
}); | ||
}); |
@@ -5,25 +5,43 @@ var expect = require('expect.js'); | ||
describe('Set', function() { | ||
var set; | ||
beforeEach(function() { | ||
set = Set(); | ||
}); | ||
it('should exist in global namespace', function() { | ||
expect(Set).to.be.ok(); | ||
}); | ||
describe('Set.from()', function() { | ||
}); | ||
describe('Set.of()', function() { | ||
}); | ||
it('should return true on values that set has', function() { | ||
set.add(1); | ||
expect(set.has(1)).to.be.ok(); | ||
describe('#has()', function() { | ||
var arr = [1, 2, 3]; | ||
set.add(arr); | ||
expect(set.has(arr)).to.be.ok(); | ||
var obj = {a: 1}; | ||
set.add(obj); | ||
expect(set.has(obj)).to.be.ok(); | ||
}); | ||
describe('#add()', function() { | ||
it('should return true on values that set has not', function() { | ||
set.add(1); | ||
expect(set.has(2)).to.not.be.ok(); | ||
}); | ||
describe('#delete()', function() { | ||
it('should delete props', function() { | ||
set.add(1); | ||
set.delete(1); | ||
expect(set.has(1)).to.not.be.ok(); | ||
}); | ||
//describe('Set.of()', function() { | ||
// it('should create new set from iterable', function() { | ||
// expect(Set.of([1, 2, 3, 4, 5, 4, 3, 2, 1])).to.eql([ | ||
// 1, 2, 3, 4, 5 | ||
// ]); | ||
// }); | ||
//}); | ||
}); |
@@ -5,37 +5,41 @@ var expect = require('expect.js'); | ||
describe('String', function() { | ||
describe('#repeat', function() { | ||
it('', function() { | ||
describe('#repeat()', function() { | ||
it('should work', function() { | ||
expect('test'.repeat(3)).to.eql('testtesttest'); | ||
}); | ||
}); | ||
describe('#startsWith', function() { | ||
it('', function() { | ||
expect('test'.startsWith('te')).to.be.ok; | ||
describe('#startsWith()', function() { | ||
it('should be truthy on correct results', function() { | ||
expect('test'.startsWith('te')).to.be.ok(); | ||
}); | ||
it('', function() { | ||
expect('test'.startsWith('st')).to.not.be.ok; | ||
it('should be falsy on incorrect results', function() { | ||
expect('test'.startsWith('st')).to.not.be.ok(); | ||
}); | ||
}); | ||
describe('#endsWith', function() { | ||
it('', function() { | ||
expect('test'.endsWith('st')).to.be.ok; | ||
describe('#endsWith()', function() { | ||
it('should be truthy on correct results', function() { | ||
expect('test'.endsWith('st')).to.be.ok(); | ||
}) | ||
it('', function() { | ||
expect('test'.endsWith('te')).to.not.be.ok; | ||
it('should be falsy on incorrect results', function() { | ||
expect('test'.endsWith('te')).to.not.be.ok(); | ||
}); | ||
}); | ||
describe('#contains', function() { | ||
it('', function() { | ||
expect('test'.contains('es')).to.be.ok; | ||
describe('#contains()', function() { | ||
it('should be truthy on correct results', function() { | ||
expect('test'.contains('es')).to.be.ok(); | ||
}); | ||
it('', function() { | ||
expect('test'.contains('1290')).to.not.be.ok; | ||
it('should be falsy on incorrect results', function() { | ||
expect('test'.contains('1290')).to.not.be.ok(); | ||
}); | ||
}); | ||
describe('#toArray', function() { | ||
it('', function() { | ||
describe('#toArray()', function() { | ||
it('should convert string to array of strings', function() { | ||
expect('string'.toArray()).to.eql(['s', 't', 'r', 'i', 'n', 'g']); | ||
}); | ||
it('', function() { | ||
expect(''.toArray()).to.eql([]); | ||
@@ -42,0 +46,0 @@ }); |
19483
14
479