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

N-levels deep object access via dot/bracket-notation property access string.

  • 0.8.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

N-levels deep object access via dot/bracket-notation property access string allowing you to type selectn('info.name.full') instead of obj && obj.info && obj.info.name &&, ∞.

Features

  • Avoids if (obj && obj.a && obj.a.b && obj.a.b.c) { return obj.a.b.c; }.
  • Partial application supported.
  • Functions generated by selectn can 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

Installation

component

$ component install wilmoore/selectn

bower

$ bower install selectn

npm

$ npm install selectn

Example (immediate access)

Given the following object:

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

The generated function can be immediately invoked for error-free and immediate access to deeply nested properties.

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

Iterator Example

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 Example

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 Example

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 (using promises):

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

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

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:

var name;

if (contact && contact.info && contact.info.name) {
  name = contact.info.name.full || 'unknown';
}

The following is much more concise:

var name = selectn('info.name.full')(contact) || 'unknown';

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 a partial helper like Function.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 (eval in disguise).

Inspiration

License

MIT

Keywords

FAQs

Package last updated on 12 Jul 2013

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