GithubHelp home page GithubHelp logo

calvinfo / to-function Goto Github PK

View Code? Open in Web Editor NEW

This project forked from component/to-function

0.0 1.0 0.0 123 KB

Convert property access strings to a function ("user.name.first" etc)

JavaScript 100.00%

to-function's Introduction

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

to-function's People

Contributors

forbeslindesay avatar jonathanong avatar matthewmueller avatar rauchg avatar retrofox avatar timoxley avatar tj avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.