Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

to-function

Package Overview
Dependencies
Maintainers
12
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

to-function

Convert property access strings into functions

  • 2.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
12
Created
Source

to-function

Convert property access strings into functions

Installation

$ component install component/to-function

Examples

var toFunction = require('to-function');
var fn = toFunction('name.first');
var user = { name: { first: 'Tobi' }};
fn(user);
// => "Tobi"

Dot access


var _ = require('..');

var users = [
  { name: { first: 'Tobi' }},
  { name: { first: 'Loki' }},
  { name: { first: 'Jane' }},
  { name: { first: 'Manny' }}
];

var short = users.map(_('name.first'));
console.log(short);
// => [ 'Tobi', 'Loki', 'Jane', 'Manny' ]

Equality

var _ = require('..');

var tobi = { name: { first: 'Tobi' }, age: 2 };
var loki = { name: { first: 'Loki' }, age: 2 };
var jane = { name: { first: 'Jane' }, age: 6 };

var users = [tobi, loki, jane];

var user = users.filter(_(loki)).pop();
console.log(user);
// => { name: { first: 'Loki' }, age: 2 }

Expressions

var _ = require('..');

var users = [
  { name: { first: 'Tobi' }, age: 2 },
  { name: { first: 'Loki' }, age: 2 },
  { name: { first: 'Jane' }, age: 6 }
];

var oldPets = users.filter(_('age > 2 && age < 10'));
console.log(oldPets);
// => [ { name: { first: 'Jane' }, age: 6 } ]

Regular expressions

var _ = require('..');

var users = [
  'Tobi',
  'Loki',
  'Jane'
];

var t = users.filter(_(/^T/));

console.log(t);
// => [ 'Tobi' ]

Nesting

var _ = require('..');

var users = [
  { name: { first: 'Tobi', last: 'Ferret' }, age: 2 },
  { name: { first: 'Loki', last: 'Ferret' }, age: 2 },
  { name: { first: 'Luna', last: 'Cat' }, age: 2 },
  { name: { first: 'Manny', last: 'Cat' }, age: 3 }
];

// single-key

var query = _({
  name: {
    last: 'Cat'
  }
});

console.log(users.filter(query));
// => [ { name: { first: 'Luna', last: 'Cat' }, age: 2 },
//      { name: { first: 'Manny', last: 'Cat' }, age: 3 } ]

// multi-key

var query = _({
  name: {
    first: /^L/,
    last: 'Cat'
  }
});

console.log(users.filter(query));
// => [ { name: { first: 'Luna', last: 'Cat' }, age: 2 } ]

// multi-level

var query = _({
  name: { last: 'Cat' },
  age: 3
});

console.log(users.filter(query));
// => [ { name: { first: 'Manny', last: 'Cat' }, age: 3 } ]

License

MIT

Keywords

FAQs

Package last updated on 30 Jan 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