Curried property accessor function that resolves deeply-nested object properties via dot/bracket-notation string path while mitigating TypeErrors via friendly and composable API.
Installations
npm install selectn
Releases
Unable to fetch releases
Developer
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
6.3.1
NPM Version
3.10.5
Statistics
196 Stars
226 Commits
16 Forks
5 Watching
189 Branches
6 Contributors
Updated on 13 Jun 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
30,107,299
Last day
-11.7%
17,642
Compared to previous day
Last week
9%
118,189
Compared to previous week
Last month
17.8%
534,786
Compared to previous month
Last year
10.1%
4,136,390
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
selectn
Curried property accessor function that resolves deeply-nested object properties via dot/bracket-notation string path while mitigating
TypeErrors
via friendly and composable API.
1yarn add selectn
or
1npm install selectn --save
or
1<script src="https://unpkg.com/selectn/selectn.min.js"></script>
npm stats
browser support
The browsers listed in .zuul.yml are continuously tested; however,
selectn
is also known to work on older unlisted legacy browsers.
Overview
allows you to refactor this:
person && person.info && person.info.name && person.info.name.full
into:
selectn('info.name.full', person)
or refactor this:
contacts.map(function (contact) {
return contact && contact.addresses && contact.addresses[0]
})
into:
contacts.map(selectn('addresses[0]')))
Demo
Features
- Mitigate boilerplate guards like
if (obj && obj.a && obj.a.b && obj.a.b.c)
. - Mitigate TypeError
Cannot read property '...' of undefined
. - Multiple levels of array nesting:
'group[0].section.a.seat[3]'
. - Dashed key access:
'stats.temperature-today'
. - When the value at the given path is a function, it is invoked and the functions returned value is returned.
selectn
is auto-curried so partial application is automatic when you omit the second argument.selectn
uses Haskell style parameter order (AKA: data-last) which enables pointfree style programming.- Functions returned by
selectn
are higher-order property accessors which can be passed to other higher-order functions like map or filter. - Compatible with modern and legacy browsers, Node/CommonJS, and AMD.
Non-Features
- No eval or Function (see:
eval
in disguise). - No typeof since, typeof is not a real solution to this problem but can appear to be due to the way the global scope is implied.
Usage example(s)
property accessor as predicate
Avoid annoying Cannot read property '...' of undefined
TypeError
without writing boilerplate anonymous functions or guards.
1var selectn = require('selectn') 2var language = [ 3 { strings: { en: { name: 'english' } }}, 4 { strings: { es: { name: 'spanish' } }}, 5 { strings: { km: { name: 'khmer' } }}, 6 { strings: { es: { name: 'spanish' } }}, 7 { nodatas: {}} 8] 9 10var spanish = selectn('strings.es') 11//=> [Function] 12 13language.filter(spanish).length 14//=> 2
point-free property accessor
Access deeply nested properties (including dashed properties) using point-free style.
1var selectn = require('selectn') 2var data = { 3 client: { 4 message: { 'message-id': 'd50afb80-a6be-11e2-9e96-0800200c9a66' } 5 } 6} 7 8var getId = selectn('client.message.message-id') 9//=> [Function] 10 11Promise.resolve(data).then(getId) 12//=> 'd50afb80-a6be-11e2-9e96-0800200c9a66'
property accessor for functor
Avoid wrapping property accessors in anonymous functions.
1var selectn = require('selectn') 2var contacts = [ 3 { addresses: [ '123 Main St, Broomfield, CO 80020', '123 Main St, Denver, CO 80202' ] }, 4 { addresses: [ '123 Main St, Kirkland, IL 60146' ] }, 5 { phones: [] }, 6] 7 8var primaryAddress = selectn('addresses[0]') 9//=> [Function] 10 11contacts.map(primaryAddress) 12//=> [ '123 Main St, Broomfield, CO 80020', '123 Main St, Kirkland, IL 60146', undefined ]
support for keys containing .
Pass an array as path instead of a string.
1var selectn = require('selectn') 2var data = { 3 client: { 4 'message.id': 'd50afb80-a6be-11e2-9e96-0800200c9a66' 5 } 6} 7 8selectn(['client', 'message.id'], data) 9//=> 'd50afb80-a6be-11e2-9e96-0800200c9a66'
value at path is a function
Avoid
var fn = data.may.be.a.fn; if (typeof fn === 'function') fn()
.
1var selectn = require('selectn') 2function hi () { return 'hi' } 3var data = { may: { be: { a: { fn: hi } } } } 4 5selectn('may.be.a.fn', data) 6//=> 'hi'
API (partial application)
selectn(String|Array)
arguments
path (String|Array)
Dot/bracket-notation string path or array.
returns
(Function)
Unary function accepting the object to access.
API (full application)
selectn(String|Array, Object)
arguments
path (String|Array)
Dot/bracket-notation string path or array.object (String|Array)
Object to access.
returns
(*|undefined)
Value at path if path exists orundefined
if path does not exist.
Other Languages
selectn
has inspired ports to other languages:
Language | Project |
---|---|
Python | selectn |
Related
Other JS packages whose friendly API is driven by selectn
:
Inspiration
JS packages that have inspired selectn
:
Alternatives
Alternative packages you might like instead of selectn
:
Licenses
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
Found 6/15 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 22 are checked with a SAST tool
Score
3.6
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More