selectn / 0.9.6 last updated a year ago created on Mar 10th 2013
Install
npm install --save selectn
Clone
git clone git@github.com:wilmoore/selectn.git
dependencies
No dependencies listed.
maintainers
versions
12 total
| 0.9.6 | a year ago | wilmoore |
| 0.9.2 | a year ago | wilmoore |
| 0.9.0 | a year ago | wilmoore |
| 0.8.6 | a year ago | wilmoore |
| 0.8.4 | a year ago | wilmoore |
| 0.8.2 | a year ago | wilmoore |
| 0.8.0 | a year ago | wilmoore |
| 0.6.0 | a year ago | wilmoore |
| 0.4.0 | 2 years ago | wilmoore |
| 0.2.3 | 2 years ago | wilmoore |
| 0.2.1 | 2 years ago | wilmoore |
| 0.2.0 | 2 years ago | wilmoore |
readme
selectn
Resolves deeply-nested object properties via dot or bracket-notation for Node.js and the browser.
So you can do:
instead of:
Features
- Avoids
if (obj && obj.a && obj.a.b && obj.a.b.c) { return obj.a.b.c; }. - Supports multiple levels of array nesting (i.e.
group[0].section.a.seat[3]). - Supports dashed key access (i.e.
stats.temperature-today). - Partial application supported.
- Functions generated by
selectncan be passed to applicative functors like Array.prototype.map and Array.prototype.filter. - Works where typeof fails (i.e. deeply nested properties).
- ES5 and non-ES5 compatible.
- CommonJS, AMD, and legacy-global compatible.
- Provides access to global object if no object reference is given.
Non-Features
- No
evaland friends.
Installation
Examples
Nested property access
Given the following object:
Apply the selectn function to the path and object parameters for error-free access to deeply nested properties.
Dashed keys
Given the following object:
Apply the selectn function to the path and object parameters for error-free access to deeply nested properties.
Iterator
Given the following list:
The generated function can be used as a predicate for map:
Predicate
Given the following object of language strings:
The generated function can be used as a predicate for filter:
Callback
You expect the following JSON data from an XMLHttpRequest:
Access the Client.Message.id property and log the result to the console (using promises):
NOTE: Even if you don't use this methodology in production code, it can be a handy timesaver in terms of quick debugging.
Rationale
In larger, data-driven applications, there tends to be a need to do a lot of deep object access which can quickly lead to code like this:
The following is much more concise:
Neckbeard Info
In case you care about this sort of thing, we are able to do normal function application as well as partially apply when that is convenient due to currying.
selectn('info.name.full', contact)(normal function application)selectn('info.name.full')(contact)(partial application without apartialhelper likeFunction.prototype.bind)
Since selectn is a 2-ary function, we don't need to use an external library for currying as the algorithm is simple.
Alternatives
- You can use typeof; however, typeof only "appears" to work due to the way the global scope is implied.
- Other solutions involve eval and/or Function (
evalin disguise).
Inspiration
License
MIT


