Comparing version 1.0.1 to 1.0.2
@@ -18,3 +18,3 @@ "use strict" | ||
if (obj == null) return obj | ||
if (isPrimitive(obj)) obj = new obj.constructor(obj) | ||
if (isPrimitive(obj)) obj = Object(obj) | ||
return Object.getPrototypeOf(obj) | ||
@@ -25,5 +25,4 @@ } | ||
return ( | ||
!(item instanceof Object) && | ||
Object.prototype.toString.call(item) !== '[object Object]' | ||
item === null || typeof item !== 'object' | ||
) | ||
} |
{ | ||
"name": "protochain", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Prototype chain of any value as an Array", | ||
@@ -5,0 +5,0 @@ "main": "protochain.js", |
@@ -19,3 +19,3 @@ "use strict"; | ||
return obj; | ||
}if (isPrimitive(obj)) obj = new obj.constructor(obj); | ||
}if (isPrimitive(obj)) obj = Object(obj); | ||
return Object.getPrototypeOf(obj); | ||
@@ -25,4 +25,4 @@ } | ||
function isPrimitive(item) { | ||
return !(item instanceof Object) && Object.prototype.toString.call(item) !== "[object Object]"; | ||
return item === null || typeof item !== "object"; | ||
} | ||
# protochain | ||
[](https://travis-ci.org/timoxley/protochain) | ||
Get the prototype chain of an object or primitive as an Array. | ||
@@ -4,0 +6,0 @@ |
74
test.js
@@ -9,10 +9,10 @@ "use strict" | ||
let obj = {} | ||
t.deepEqual(protochain(obj), [Object.prototype]) | ||
t.deepEqual(protochain(Object.create(obj)), [obj, Object.prototype]) | ||
t.deepEqual(protochain(new Error('message')), [Error.prototype, Object.prototype]) | ||
t.deepEqual(protochain(new TypeError('message')), [TypeError.prototype, Error.prototype, Object.prototype]) | ||
t.deepEqual(protochain(new String()), [String.prototype, Object.prototype]) | ||
t.deepEqual(protochain(new Number()), [Number.prototype, Object.prototype]) | ||
t.deepEqual(protochain(new RegExp('abc')), [RegExp.prototype, Object.prototype]) | ||
t.deepEqual(protochain(new Date()), [Date.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(obj), [Object.prototype]) | ||
strictEqualArray(t, protochain(Object.create(obj)), [obj, Object.prototype]) | ||
strictEqualArray(t, protochain(new Error('message')), [Error.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new TypeError('message')), [TypeError.prototype, Error.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new String()), [String.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new Number()), [Number.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new RegExp('abc')), [RegExp.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new Date()), [Date.prototype, Object.prototype]) | ||
t.end() | ||
@@ -23,4 +23,4 @@ }) | ||
let noProtoObject = Object.create(null) | ||
t.deepEqual(protochain(noProtoObject), []) | ||
t.deepEqual(protochain(Object.create(noProtoObject)), [noProtoObject]) | ||
strictEqualArray(t, protochain(noProtoObject), []) | ||
strictEqualArray(t, protochain(Object.create(noProtoObject)), [noProtoObject]) | ||
t.end() | ||
@@ -30,10 +30,9 @@ }) | ||
t.test('non-object values cooerce to object counterparts correctly', t => { | ||
t.deepEqual(protochain('abc'), [String.prototype, Object.prototype]) | ||
t.deepEqual(protochain(123), [Number.prototype, Object.prototype]) | ||
t.deepEqual(protochain(/abc/), [RegExp.prototype, Object.prototype]) | ||
t.deepEqual(protochain(true), [Boolean.prototype, Object.prototype]) | ||
t.deepEqual(protochain(false), [Boolean.prototype, Object.prototype]) | ||
// falsey values | ||
t.deepEqual(protochain(''), [String.prototype, Object.prototype]) | ||
t.deepEqual(protochain(0), [Number.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain('abc'), [String.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(123), [Number.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(/abc/), [RegExp.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(true), [Boolean.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(false), [Boolean.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(''), [String.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(0), [Number.prototype, Object.prototype]) | ||
t.end() | ||
@@ -43,5 +42,5 @@ }) | ||
t.test('null values produce empty list', t => { | ||
t.deepEqual(protochain(), []) | ||
t.deepEqual(protochain(undefined), []) | ||
t.deepEqual(protochain(null), []) | ||
strictEqualArray(t, protochain(), []) | ||
strictEqualArray(t, protochain(undefined), []) | ||
strictEqualArray(t, protochain(null), []) | ||
t.end() | ||
@@ -58,18 +57,41 @@ }) | ||
t.deepEquals(protochain(new Person()), [Person.prototype, Object.prototype]) | ||
t.deepEquals(protochain(new FancyPerson()), [FancyPerson.prototype, Person.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new Person()), [Person.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new FancyPerson()), [FancyPerson.prototype, Person.prototype, Object.prototype]) | ||
t.end() | ||
}) | ||
t.test('ES6', t => { | ||
// note this will in-fact be compiled to ES5 | ||
class Person {} | ||
t.deepEquals(protochain(new Person()), [Person.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new Person()), [Person.prototype, Object.prototype]) | ||
class FancyPerson extends Person {} | ||
t.deepEquals(protochain(new FancyPerson()), [FancyPerson.prototype, Person.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new FancyPerson()), [FancyPerson.prototype, Person.prototype, Object.prototype]) | ||
t.end() | ||
}) | ||
}) | ||
// new native types which may not be supported | ||
if (typeof Symbol !== 'undefined') { | ||
t.test('symbol support', t => { | ||
let foo = Symbol('foo') | ||
strictEqualArray(t, protochain(foo), [Symbol.prototype, Object.prototype]) | ||
t.end() | ||
}) | ||
} | ||
if (typeof Promise !== 'undefined') { | ||
t.test('promise support', t => { | ||
let foo = new Promise((Y, N) => Y()) | ||
strictEqualArray(t, protochain(foo), [Promise.prototype, Object.prototype]) | ||
t.end() | ||
}) | ||
} | ||
t.end() | ||
}) | ||
function strictEqualArray(t, a, b) { | ||
a.forEach((item, index) => t.strictEqual(a[index], b[index], `strictEqual at index ${index}`)) | ||
t.equal(a.length, b.length, 'same length') | ||
} |
9336
10
140
105