Comparing version 1.0.3 to 1.0.4
@@ -0,2 +1,11 @@ | ||
1.0.4 / 2016-06-30 | ||
================== | ||
* Lint code with standard. | ||
* Build with babel 6. | ||
* Remove broken class extends Function test. Does not work with babel 6. | ||
* Add more tests, including tests for collections & typed arrays. | ||
* Mild internal refactoring. | ||
* Update tape. | ||
1.0.3 / 2015-06-29 | ||
@@ -3,0 +12,0 @@ ================== |
23
index.js
@@ -1,26 +0,19 @@ | ||
"use strict" | ||
'use strict' | ||
export default protochain | ||
function protochain(obj) { | ||
let result = [] | ||
function protochain (obj) { | ||
const chain = [] | ||
let target = getPrototypeOf(obj) | ||
while (target) { | ||
result.push(target) | ||
chain.push(target) | ||
target = getPrototypeOf(target) | ||
} | ||
return result | ||
return chain | ||
} | ||
function getPrototypeOf(obj) { | ||
if (obj == null) return obj | ||
if (isPrimitive(obj)) obj = Object(obj) | ||
return Object.getPrototypeOf(obj) | ||
function getPrototypeOf (obj) { | ||
if (obj == null) return null | ||
return Object.getPrototypeOf(Object(obj)) | ||
} | ||
function isPrimitive(item) { | ||
return ( | ||
item === null || (typeof item !== 'object' && typeof item !== 'function') | ||
) | ||
} |
{ | ||
"name": "protochain", | ||
"version": "1.0.3", | ||
"description": "Prototype chain of any value as an Array", | ||
"version": "1.0.4", | ||
"description": "Get the prototype chain of any value as an Array", | ||
"main": "protochain.js", | ||
"scripts": { | ||
"test": "(babel test.js > protochain-test.js) && tape protochain-test.js", | ||
"test": "(babel test.js > protochain-test.js) && tape protochain-test.js && standard index.js test.js", | ||
"prepublish": "babel index.js > protochain.js", | ||
@@ -12,6 +12,13 @@ "pretest": "npm run prepublish", | ||
}, | ||
"babel": { | ||
"presets": [ | ||
"es2015" | ||
] | ||
}, | ||
"keywords": [ | ||
"object", | ||
"inherit", | ||
"inheritance", | ||
"prototypical", | ||
"prototype", | ||
"utility", | ||
@@ -27,4 +34,6 @@ "proto", | ||
"devDependencies": { | ||
"babel": "^5.6.14", | ||
"tape": "^4.0.0" | ||
"babel-cli": "^6.10.1", | ||
"babel-preset-es2015": "^6.9.0", | ||
"standard": "^7.1.2", | ||
"tape": "^4.6.0" | ||
}, | ||
@@ -31,0 +40,0 @@ "repository": { |
@@ -1,26 +0,24 @@ | ||
"use strict"; | ||
'use strict'; | ||
module.exports = protochain; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = protochain; | ||
function protochain(obj) { | ||
var result = []; | ||
var chain = []; | ||
var target = getPrototypeOf(obj); | ||
while (target) { | ||
result.push(target); | ||
chain.push(target); | ||
target = getPrototypeOf(target); | ||
} | ||
return result; | ||
return chain; | ||
} | ||
function getPrototypeOf(obj) { | ||
if (obj == null) { | ||
return obj; | ||
}if (isPrimitive(obj)) obj = Object(obj); | ||
return Object.getPrototypeOf(obj); | ||
if (obj == null) return null; | ||
return Object.getPrototypeOf(Object(obj)); | ||
} | ||
function isPrimitive(item) { | ||
return item === null || typeof item !== "object" && typeof item !== "function"; | ||
} | ||
104
test.js
@@ -1,2 +0,2 @@ | ||
"use strict" | ||
'use strict' | ||
@@ -8,3 +8,3 @@ import test from 'tape' | ||
t.test('finds correct prototype chain', t => { | ||
let obj = {} | ||
const obj = {} | ||
strictEqualArray(t, protochain(obj), [Object.prototype]) | ||
@@ -14,4 +14,6 @@ strictEqualArray(t, protochain(Object.create(obj)), [obj, Object.prototype]) | ||
strictEqualArray(t, protochain(new TypeError('message')), [TypeError.prototype, Error.prototype, Object.prototype]) | ||
/* eslint-disable no-new-wrappers */ | ||
strictEqualArray(t, protochain(new String()), [String.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new Number()), [Number.prototype, Object.prototype]) | ||
/* eslint-enable no-new-wrappers */ | ||
strictEqualArray(t, protochain(new RegExp('abc')), [RegExp.prototype, Object.prototype]) | ||
@@ -23,3 +25,3 @@ strictEqualArray(t, protochain(new Date()), [Date.prototype, Object.prototype]) | ||
t.test('null prototype is handled correctly', t => { | ||
let noProtoObject = Object.create(null) | ||
const noProtoObject = Object.create(null) | ||
strictEqualArray(t, protochain(noProtoObject), []) | ||
@@ -30,2 +32,13 @@ strictEqualArray(t, protochain(Object.create(noProtoObject)), [noProtoObject]) | ||
t.test('falsey valueOf', t => { | ||
const obj = { | ||
valueOf () { | ||
return false | ||
} | ||
} | ||
strictEqualArray(t, protochain(obj), [Object.prototype]) | ||
t.end() | ||
}) | ||
t.test('non-object values cooerce to object counterparts correctly', t => { | ||
@@ -36,5 +49,9 @@ strictEqualArray(t, protochain('abc'), [String.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.test('falsey values with prototypes', t => { | ||
strictEqualArray(t, protochain(NaN), [Number.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() | ||
}) | ||
t.end() | ||
@@ -50,36 +67,28 @@ }) | ||
t.test('examples', t => { | ||
t.test('ES5', t => { | ||
function Person() {} | ||
function FancyPerson() { | ||
Person.call(this) | ||
} | ||
FancyPerson.prototype = Object.create(Person.prototype) | ||
t.test('ES5 inheritance', t => { | ||
function Person () {} | ||
function FancyPerson () { | ||
Person.call(this) | ||
} | ||
FancyPerson.prototype = Object.create(Person.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 {} | ||
strictEqualArray(t, protochain(new Person()), [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() | ||
}) | ||
class FancyPerson extends Person {} | ||
strictEqualArray(t, protochain(new FancyPerson()), [FancyPerson.prototype, Person.prototype, Object.prototype]) | ||
t.end() | ||
}) | ||
t.test('functions', t => { | ||
class Foo extends Function {} | ||
class Bar extends Foo {} | ||
strictEqualArray(t, protochain(new Bar()), [Bar.prototype, Foo.prototype, Function.prototype, Object.prototype]) | ||
t.end() | ||
}) | ||
t.test('ES6 inheritance', t => { | ||
// note this will be compiled to ES5 in the test | ||
class Person {} | ||
class FancyPerson extends Person {} | ||
strictEqualArray(t, protochain(new Person()), [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') | ||
const foo = Symbol('foo') | ||
strictEqualArray(t, protochain(foo), [Symbol.prototype, Object.prototype]) | ||
@@ -92,3 +101,3 @@ t.end() | ||
t.test('promise support', t => { | ||
let foo = new Promise((Y, N) => Y()) | ||
const foo = Promise.resolve() | ||
strictEqualArray(t, protochain(foo), [Promise.prototype, Object.prototype]) | ||
@@ -99,8 +108,33 @@ t.end() | ||
if (typeof Map !== 'undefined') { | ||
t.test('collections support', t => { | ||
strictEqualArray(t, protochain(new Map()), [Map.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new Set()), [Set.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new WeakMap()), [WeakMap.prototype, Object.prototype]) | ||
strictEqualArray(t, protochain(new WeakSet()), [WeakSet.prototype, Object.prototype]) | ||
t.end() | ||
}) | ||
} | ||
if (typeof Uint8Array !== 'undefined') { | ||
t.test('typed array support', t => { | ||
const TypedArray = Object.getPrototypeOf(Int8Array.prototype) | ||
strictEqualArray(t, protochain(new Int8Array()), [Int8Array.prototype, TypedArray, Object.prototype]) | ||
strictEqualArray(t, protochain(new Uint8Array()), [Uint8Array.prototype, TypedArray, Object.prototype]) | ||
strictEqualArray(t, protochain(new Uint8ClampedArray()), [Uint8ClampedArray.prototype, TypedArray, Object.prototype]) | ||
strictEqualArray(t, protochain(new Int16Array()), [Int16Array.prototype, TypedArray, Object.prototype]) | ||
strictEqualArray(t, protochain(new Int32Array()), [Int32Array.prototype, TypedArray, Object.prototype]) | ||
strictEqualArray(t, protochain(new Uint32Array()), [Uint32Array.prototype, TypedArray, Object.prototype]) | ||
strictEqualArray(t, protochain(new Float32Array()), [Float32Array.prototype, TypedArray, Object.prototype]) | ||
strictEqualArray(t, protochain(new Float64Array()), [Float64Array.prototype, TypedArray, Object.prototype]) | ||
t.end() | ||
}) | ||
} | ||
t.end() | ||
}) | ||
function strictEqualArray(t, a, b) { | ||
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') | ||
} |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
11141
148
4
9
1