Comparing version 0.0.2 to 1.0.0
@@ -1,2 +0,2 @@ | ||
module.exports = function(path) { | ||
module.exports = function createPeek(path) { | ||
if (typeof path !== 'string') { | ||
@@ -6,5 +6,5 @@ throw new TypeError('path must be a string') | ||
var parts = path.split('.') | ||
return function(obj) { | ||
return parts.reduce(function(obj, segment) { | ||
if(obj === null || typeof obj !== 'object') { | ||
return function peek(obj) { | ||
return parts.reduce(function peekSegment(obj, segment) { | ||
if (obj === null || obj === undefined) { | ||
return undefined | ||
@@ -11,0 +11,0 @@ } |
{ | ||
"name": "peek", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "Retrieve nested object properties easily.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
39
test.js
var peek = require('./index') | ||
, test = require('tape') | ||
test('Test basic fetches', function(assert) { | ||
test('basic fetches', function(assert) { | ||
assert.strictEqual(peek('a')({a: 5}), 5) | ||
assert.strictEqual(peek('1')([3, 7]), 7) | ||
assert.strictEqual(peek('')({'': 6}), 6) | ||
@@ -11,3 +12,3 @@ | ||
test('Test advanced fetches', function(assert) { | ||
test('advanced fetches', function(assert) { | ||
var find = peek('part.key.attribute') | ||
@@ -29,3 +30,3 @@ , found = find({part: {key: {attribute: 'woop woop'}}}) | ||
test('Test example from README', function(assert) { | ||
test('example from README', function(assert) { | ||
var booty = peek("lower deck.captain's quarters.secret panel.treasure") | ||
@@ -46,3 +47,3 @@ var pirate_ship = { | ||
test('Test exception cases', function(assert) { | ||
test('bad input cases', function(assert) { | ||
[{}, true, undefined, null, 5, Infinity].forEach(function(bad_type) { | ||
@@ -54,1 +55,31 @@ assert.throws(function() { peek(bad_type) }, TypeError) | ||
}) | ||
test('accessing properties of primitives', function(assert) { | ||
var primitives = [ | ||
"grilled cheese sandwiches" | ||
, true | ||
, false | ||
, 15 | ||
] | ||
var toString = peek('toString') | ||
primitives.forEach(function testPrimitive(value) { | ||
assert.doesNotThrow(function () { toString(value) }) | ||
assert.equal(toString(value).call(value), value.toString(), value + '.toString') | ||
}) | ||
assert.end() | ||
}) | ||
test('accessing properties of functions', function(assert) { | ||
function f() {} | ||
f.prototype.lettuce = 'optional' | ||
var prototype = peek('prototype') | ||
var lettuce = peek('prototype.lettuce') | ||
assert.doesNotThrow(function () { prototype(f) }) | ||
assert.doesNotThrow(function () { lettuce(f) }) | ||
assert.strictEqual(prototype(f), f.prototype, 'prototype') | ||
assert.strictEqual(lettuce(f), f.prototype.lettuce, 'prototype property') | ||
assert.end() | ||
}) |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4569
79
1