1.0.1 • Published 9 years ago

fuzzy-path v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

fuzzy-path

String -> { k: v } -> v | undefined

Drill down an object in a fuzzy fashion.

Build Status

Example

import fpath from 'fuzzy-path';

const info = {
    firstName: 'Trevor',
    lastName: 'Senior',
    address: {
        city: 'Seattle',
        state: 'VA',
        zip: 12345,
        address1: '55 W St.',
        address2: '#4',
        phoneNumbers: {
            home: 9993334444,
            mobile: 1112223333
        }
    }
};

fpath('firstName', info); // 'Trevor'
fpath('first', info); // 'Trevor'
fpath('add.city', info); // 'Seattle'
fpath('add.phone.mob', info); // 111222333

fpath('foo', info); // undefined
fpath('address.foo', info); // undefined

// throws error! too many options to choose from
fpath('name', info);

// throws error! invalid path (firstName is a string)
fpath('first.foo', info);

Currying

var getMobileNumber = fpath('add.phone.mob');

getMobileNumber(info); // 111222333

Install

npm install --save fuzzy-path