npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

selectn

v1.1.2

Published

Curried property accessor function that resolves deeply-nested object properties via dot/bracket-notation string path while mitigating TypeErrors via friendly and composable API.

Downloads

262,302

Readme

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.

Build Status Sauce Test Status Code Climate js-standard-style

npm install selectn --save

or

<script src="https://unpkg.com/selectn/selectn.min.js"></script>

You may also install selectn via Bower, Duo, or jspm.

npm stats

npm NPM downloads David

browser support

The following browsers are continuously tested; however, selectn is also supported and known to work on even older browsers not listed below:

Sauce Test Status

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

npm

Features

  • Mitigates boilerplate guards like if (obj && obj.a && obj.a.b && obj.a.b.c) { return obj.a.b.c; }.
  • Mitigates TypeError Cannot read property '...' of undefined.
  • Supports multiple levels of array nesting (i.e. group[0].section.a.seat[3]).
  • Supports dashed key access (i.e. stats.temperature-today).
  • If value at path is a function, the value returned is the return value of invoking the function.
  • Partial application is automatic when you omit the second argument (i.e. selectn is curried).
  • Property accessor generated by selectn can be passed to higher-order functions like map or filter.
  • Compatible with modern and legacy browsers, Node/CommonJS, and AMD.
  • Haskell style parameter order allows for pointfree style programming.

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.

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

point-free property accessor

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'

property accessor for functor

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 ]

support for keys containing .

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'

value at path is a function

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'

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 or undefined 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 inpsired selectn:

Alternatives

Alternative packages you might like instead of selectn:

Licenses

LICENSE