New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

selectn

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

selectn

Resolves deeply-nested object properties via dot or bracket-notation for Node.js and the browser.

  • 0.10.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
79K
increased by4.78%
Maintainers
1
Weekly downloads
 
Created
Source

selectn Build Status Build Status NPM version

Resolves deeply-nested object properties via dot or bracket-notation for Node.js and the browser.

So you can do:
selectn('info.name.full', person);
instead of:
person && person.info && person.info.name && person.info.name.full

Features

  • Resolves deeply-nested object properties including dashed (-) keys and arrays.
  • Assists in avoiding if (obj && obj.a && obj.a.b && obj.a.b.c) { return obj.a.b.c; }.
  • Assists in avoiding Cannot read property '...' of undefined TypeError.
  • Supports multiple levels of array nesting (i.e. group[0].section.a.seat[3]).
  • Supports dashed key access (i.e. stats.temperature-today).
  • selectn is a curried function; thus, partial application is also supported.
  • Functions generated by selectn can be passed to higher-order functions like map or filter.
  • ES3, ES5, CommonJS, AMD, and legacy-global compatible.

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.

Installation

component

$ component install wilmoore/selectn

bower

$ bower install selectn

npm

NPM

volo

$ volo add wilmoore/selectn

manual

  1. download

     % curl -#O https://raw.github.com/wilmoore/selectn/master/selectn.js
    
  2. use

     <script src="selectn.js"></script>
    

Examples

Nested property access

Given the following object:

var talk = {
  info: { name: 'Go Ahead, Make a Mess' }
};

Apply the selectn function to the path and object parameters for error-free access to deeply nested properties.

selectn('info.name', talk);
// => 'Go Ahead, Make a Mess'

Avoid TypeError

Avoid the dreaded Cannot read property '...' of undefined TypeError. Instead, selectn will return undefined.

function getName(talk) {
  // NOTE: as called below, `talk` is `undefined`
  return selectn('info.name', talk);
}

getName();
//=> undefined

Dashed keys

Given the following object:

var talk = {
  info: { 'attendee-count': 200 }
};

Apply the selectn function to the path and object parameters for error-free access to deeply nested properties.

selectn('info.attendee-count', talk);
// => 200

Iterator

Given the following list:

var talks  = [
  { info: { name: 'Go Ahead, Make a Mess' }},
  { info: { name: 'Silex Anatomy' }},
  { info: { name: 'Unit Testing in Python' }},
  { info: { name: 'Setting the Stage' }}
];

The generated function can be used as a predicate for map:

var query = selectn('info.name');
//=> [Function]

talks.map(query);
// => [ 'Go Ahead, Make a Mess', 'Silex Anatomy', 'Unit Testing in Python', 'Setting the Stage' ]

Predicate

Given the following object of language strings:

var language = [
  { strings: { en: { name: 'english' } }},
  { strings: { es: { name: 'spanish' } }},
  { strings: { km: { name: 'khmer'   } }},
  { strings: { es: { name: 'spanish' } }},
];

The generated function can be used as a predicate for filter:

var spanish = selectn('strings.es');
//=> [Function]

language.filter(spanish).length;
//=> 2

Callback

You expect the following JSON data from an XMLHttpRequest:

var data = { Client: { Message: { id: d50afb80-a6be-11e2-9e96-0800200c9a66 } } };

Access the Client.Message.id property and log the result to the console:

$.ajax({...})
  .then(selectn('Client.Message.id'))
  .then(console.log.bind(console));

//=> d50afb80-a6be-11e2-9e96-0800200c9a66

While this example assumes a promises API, this is applicable with any API which takes a function and returns the result of applying that function.

Inspiration

Alternatives

License

MIT

Keywords

FAQs

Package last updated on 08 Sep 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc