npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

getify

v1.1.4

Published

Getify is a utility to grab nested values from objects.

Downloads

28

Readme

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.