Comparing version 0.2.3 to 0.4.0
{ | ||
"name": "selectn", | ||
"repo": "wilmoore/selectn", | ||
"description": "Select n-levels deep into an object by providing a dot/bracket-notation query string.", | ||
"version": "0.2.3", | ||
"description": "N-levels deep object access via dot/bracket-notation property access string.", | ||
"version": "0.4.0", | ||
"keywords": [ | ||
@@ -13,3 +13,2 @@ "object", | ||
"predicate", | ||
"functional", | ||
"functor" | ||
@@ -16,0 +15,0 @@ ], |
{ | ||
"name": "selectn", | ||
"author": "Wil Moore III <wil.moore@wilmoore.com>", | ||
"description": "Select n-levels deep into an object by providing a dot/bracket-notation query string.", | ||
"version": "0.2.3", | ||
"description": "N-levels deep object access via dot/bracket-notation property access string.", | ||
"version": "0.4.0", | ||
"keywords": [ | ||
@@ -13,3 +13,2 @@ "object", | ||
"predicate", | ||
"functional", | ||
"functor" | ||
@@ -16,0 +15,0 @@ ], |
# selectn | ||
Select n-levels deep into an object by providing a dot/bracket-notation query string. | ||
N-levels deep object access via dot/bracket-notation property access string allowing you to type `selectn('info.name.full')` instead of `obj && obj.info && obj.info.name &&` ∞. | ||
## Build Status | ||
[](https://travis-ci.org/wilmoore/selectn) | ||
## Features | ||
- `selectn` is simple, tiny and exposes a fluent API | ||
- ES5 and non-ES5 compatible | ||
- Avoid `if (obj && obj.a && obj.a.b && obj.a.b.c) { return obj.a.b.c; }`. | ||
- `selectn` works where [typeof][] fails (i.e. deeply nested properties). | ||
- Functions generated by `selectn` can be passed to applicative functors like [Array.prototype.map][map]. | ||
- ES5 and non-ES5 compatible. | ||
- CommonJS, AMD, and legacy-global compatible. | ||
## Non-Features | ||
- No `eval` and [friends][Function] | ||
- No `eval` and [friends][Function]. | ||
@@ -65,2 +72,25 @@ ## Installation | ||
## Example (Higher-Order Functions) | ||
You expect the following JSON data from an XMLHttpRequest: | ||
```js | ||
var data = { | ||
Client: { | ||
Message: { id: d50afb80-a6be-11e2-9e96-0800200c9a66 } | ||
} | ||
}; | ||
``` | ||
Use the [promise] API to extract the data: | ||
```js | ||
var id = selectn('Client.Message.id'); | ||
$.ajax({...}) | ||
.then(id) | ||
.then(console.log); | ||
//=> d50afb80-a6be-11e2-9e96-0800200c9a66 | ||
``` | ||
## Rationale | ||
@@ -83,8 +113,12 @@ | ||
Alternatively, you can use [`typeof`][typeof]; however, it's limited in it's fluency and tends to make otherwise easy to read code look more obtuse than it really is. There are also solutions involving [`eval`][eval] and/or [`Function`][Function] ([`eval`][note] in indisguise). Thanks, but no thanks. | ||
## Alternatives | ||
- You can use [`typeof`][typeof]; however, [`typeof`][typeof] only "appears" to work due to the way the global scope is _implied_. | ||
- Other similar solutions invole [`eval`][eval] and/or [`Function`][Function] ([`eval`][note] in indisguise). | ||
## Inspiration | ||
- [to-function][to-function] | ||
- [reach][reach] | ||
- [to-function][] | ||
- [reach][] | ||
- [dref][] | ||
@@ -97,8 +131,12 @@ ## License | ||
[to-function]: https://github.com/component/to-function | ||
[reach]: https://github.com/spumko/hoek#reachobj-chain | ||
[Function]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function | ||
[eval]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval | ||
[note]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Member_Operators#Note_on_eval | ||
[typeof]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof | ||
[to-function]: https://github.com/component/to-function | ||
[reach]: https://github.com/spumko/hoek#reachobj-chain | ||
[dref]: https://github.com/crcn/dref.js | ||
[Function]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function | ||
[eval]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/eval | ||
[note]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Member_Operators#Note_on_eval | ||
[typeof]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof | ||
[promise]: http://promises-aplus.github.io/promises-spec/ | ||
[map]: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map | ||
[primitive]: http://javascriptweblog.wordpress.com/2010/09/27/the-secret-life-of-javascript-primitives/ | ||
;(function(){ | ||
/** | ||
* hasOwnProperty. | ||
*/ | ||
var has = Object.prototype.hasOwnProperty; | ||
/** | ||
* Require the given path. | ||
@@ -86,6 +79,6 @@ * | ||
var path = paths[i]; | ||
if (has.call(require.modules, path)) return path; | ||
if (require.modules.hasOwnProperty(path)) return path; | ||
} | ||
if (has.call(require.aliases, index)) { | ||
if (require.aliases.hasOwnProperty(index)) { | ||
return require.aliases[index]; | ||
@@ -144,3 +137,3 @@ } | ||
require.alias = function(from, to) { | ||
if (!has.call(require.modules, from)) { | ||
if (!require.modules.hasOwnProperty(from)) { | ||
throw new Error('Failed to alias "' + from + '", it does not exist'); | ||
@@ -207,3 +200,3 @@ } | ||
localRequire.exists = function(path) { | ||
return has.call(require.modules, localRequire.resolve(path)); | ||
return require.modules.hasOwnProperty(localRequire.resolve(path)); | ||
}; | ||
@@ -286,3 +279,3 @@ | ||
} else { | ||
window["selectn"] = require("selectn"); | ||
this["selectn"] = require("selectn"); | ||
}})(); |
var select = require('..'); | ||
var expect = require('chai').expect; | ||
var assert = require('chai').assert; | ||
@@ -49,5 +50,7 @@ describe('selectn()', function(){ | ||
var accessor = select('info["feedback"]'); | ||
expect(accessor(talks[0])).to.be.an('undefined'); | ||
var result = accessor(talks[0]); | ||
assert(result === undefined); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
13842
10
140
343