
Product
Introducing Scala and Kotlin Support in Socket
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Curried property accessor function that resolves deeply-nested object properties via dot/bracket-notation string path while mitigating TypeErrors via friendly and composable API.
Curried property accessor function that resolves deeply-nested object properties via dot/bracket-notation string path while mitigating
TypeErrors
via friendly and composable API.
yarn add selectn
or
npm install selectn --save
or
<script src="https://unpkg.com/selectn/selectn.min.js"></script>
person && person.info && person.info.name && person.info.name.full
selectn('info.name.full', person)
contacts.map(function (contact) {
return contact && contact.addresses && contact.addresses[0]
})
contacts.map(selectn('addresses[0]')))
if (obj && obj.a && obj.a.b && obj.a.b.c)
.Cannot read property '...' of undefined
.'group[0].section.a.seat[3]'
.'stats.temperature-today'
.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.selectn
are higher-order property accessors which can be passed to other higher-order functions like map or filter.eval
in disguise).Avoid annoying Cannot read property '...' of undefined
TypeError
without writing boilerplate anonymous functions or guards.
var selectn = require('selectn')
var language = [
{ strings: { en: { name: 'english' } }},
{ strings: { es: { name: 'spanish' } }},
{ strings: { km: { name: 'khmer' } }},
{ strings: { es: { name: 'spanish' } }},
{ nodatas: {}}
]
var spanish = selectn('strings.es')
//=> [Function]
language.filter(spanish).length
//=> 2
Access deeply nested properties (including dashed properties) using point-free style.
var selectn = require('selectn')
var data = {
client: {
message: { 'message-id': 'd50afb80-a6be-11e2-9e96-0800200c9a66' }
}
}
var getId = selectn('client.message.message-id')
//=> [Function]
Promise.resolve(data).then(getId)
//=> 'd50afb80-a6be-11e2-9e96-0800200c9a66'
Avoid wrapping property accessors in anonymous functions.
var selectn = require('selectn')
var contacts = [
{ addresses: [ '123 Main St, Broomfield, CO 80020', '123 Main St, Denver, CO 80202' ] },
{ addresses: [ '123 Main St, Kirkland, IL 60146' ] },
{ phones: [] },
]
var primaryAddress = selectn('addresses[0]')
//=> [Function]
contacts.map(primaryAddress)
//=> [ '123 Main St, Broomfield, CO 80020', '123 Main St, Kirkland, IL 60146', undefined ]
.
Pass an array as path instead of a string.
var selectn = require('selectn')
var data = {
client: {
'message.id': 'd50afb80-a6be-11e2-9e96-0800200c9a66'
}
}
selectn(['client', 'message.id'], data)
//=> 'd50afb80-a6be-11e2-9e96-0800200c9a66'
Avoid
var fn = data.may.be.a.fn; if (typeof fn === 'function') fn()
.
var selectn = require('selectn')
function hi () { return 'hi' }
var data = { may: { be: { a: { fn: hi } } } }
selectn('may.be.a.fn', data)
//=> 'hi'
selectn(String|Array)
path (String|Array)
Dot/bracket-notation string path or array.(Function)
Unary function accepting the object to access.selectn(String|Array, Object)
path (String|Array)
Dot/bracket-notation string path or array.object (String|Array)
Object to access.(*|undefined)
Value at path if path exists or undefined
if path does not exist.selectn
has inspired ports to other languages:
Language | Project |
---|---|
Python | selectn |
Other JS packages whose friendly API is driven by selectn
:
JS packages that have inspired selectn
:
Alternative packages you might like instead of selectn
:
FAQs
Curried property accessor function that resolves deeply-nested object properties via dot/bracket-notation string path while mitigating TypeErrors via friendly and composable API.
The npm package selectn receives a total of 91,652 weekly downloads. As such, selectn popularity was classified as popular.
We found that selectn demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.
Application Security
/Security News
Socket CEO Feross Aboukhadijeh and a16z partner Joel de la Garza discuss vibe coding, AI-driven software development, and how the rise of LLMs, despite their risks, still points toward a more secure and innovative future.
Research
/Security News
Threat actors hijacked Toptal’s GitHub org, publishing npm packages with malicious payloads that steal tokens and attempt to wipe victim systems.