Giter Site home page Giter Site logo

getify's Introduction

Getify

A utility to grab nested values from objects. Like lodash's _.get, or countless other variants. Getify uses ES6 proxies to enable usage without string or array paths.

NPM Version Build Status Dependency Status devDependency Status

How to use

import getify from 'getify'

const obj = {
  a: {
  	b: ['c', 'd'],
  	e: ['f', 'g']
  }
}

// Get existing value from object
getify(obj).a.b[1]() // returns "d"

// Get undefined if path doesn't exist
getify(obj).nothing.here() // returns undefined

// Use a default value if path doesn't exist
getify(obj).nothing.here('oops!') // returns "oops!"

Advanced usage

const obj = {
  a: {
  	b: ['c', 'd', 'e'],
  	f: {0: 'g', 1: 'h'},
  }
}

// Save intermediate values paths (object destructuring works fine)
const { f, b } = getify(obj).a
b[1]() // returns 'd'
f[0]() // returns 'g'

// Use getify.all to get all properties on the current path
getify(obj).a[getify.all][1]() // returns ['d', 'h']
getify(obj).a[getify.all]() // returns [['c', 'd', 'e'], {0: 'g', 1: 'h'}]

// Use getify.first to get first property on the current path
getify(obj).a[getify.first][1]() // returns 'd'

// Use getify.first to get first property on the current path
getify(obj).a[getify.last][1]() // returns 'h'

// Or combine them
getify(obj).a[getify.all][getify.last]() // returns ['e', 'h']

Browser and server support

ES6 Proxy is currently supported by the latest stable version of Chrome, Firefox and Edge. It's also supported by Node 6.x. Safari doesn't support Proxies.

ES6 compatibility table: Proxy

Install

npm install getify

API

Get a path from a value

getify(value).any.path['here'][3](defaultValue)

value is any valid javascript value: objects, strings, arrays, numbers. The path is any valid javascript path. If the path is not available in the object, getify will return undefined or the default value. If the path exists and contains the value undefined, that will be returned regardless of the default value provided.

Symbols

getify.all - get all keys on the current object. If there are no keys, this will result in an empty array

getify.first - get the first key on the current object. The key is the first one returned from Object.keys

getify.last - get the last key on the current object. The key is the last one returned from Object.keys

A note on performance

Currently Getify is around 1-3 times slower than lodash _.get for simpler use cases in Chrome, and around 15-20 times slower in Firefox. Currently using getify is a two step process where the accessing paths on the Getified object creates a path in an array. When you execute the returned function Getify finds the nested value in the object in a way that's very close to how _.get works. I've been fiddling around trying to make this process go faster but my attempts thus far have had worse performance than the current one.

getify's People

Contributors

johnste avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.