All Projects → johnste → getify

johnste / getify

Licence: MIT license
A utility to grab nested values from objects.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to getify

JetBrainsRunner
A Krunner Plugin which allows you to open your recent projects
Stars: ✭ 31 (+158.33%)
Mutual labels:  utility
drain-js
Makes smooth transitions between two numbers.
Stars: ✭ 45 (+275%)
Mutual labels:  utility
RaycastVisualization
This asset allows users to view raycasts as the user fires them.
Stars: ✭ 61 (+408.33%)
Mutual labels:  utility
Octotab.crx
⚒ (I'm dead) A super tiny chrome extension making your Github news feed more organized.
Stars: ✭ 17 (+41.67%)
Mutual labels:  utility
DropPoint
Make drag-and-drop easier using DropPoint. Drag content without having to open side-by-side windows
Stars: ✭ 303 (+2425%)
Mutual labels:  utility
timestampy
🕒 Bunch of utilities useful when working with UNIX timestamps
Stars: ✭ 21 (+75%)
Mutual labels:  utility
avrocount
Count records in Avro files efficiently
Stars: ✭ 16 (+33.33%)
Mutual labels:  utility
character
tool for character manipulations
Stars: ✭ 26 (+116.67%)
Mutual labels:  utility
aimscroll
🍹 Painless utility libary to handle scroll positions and methods in react
Stars: ✭ 12 (+0%)
Mutual labels:  utility
rhq
Manages your local repositories
Stars: ✭ 48 (+300%)
Mutual labels:  utility
fsimilar
find/file similar
Stars: ✭ 13 (+8.33%)
Mutual labels:  utility
javascript
in this project, I will add Javascript functions from Basics to Advanced with design patterns
Stars: ✭ 41 (+241.67%)
Mutual labels:  javascript-tools
wifiqr
Create a QR code with your Wi-Fi login details
Stars: ✭ 207 (+1625%)
Mutual labels:  utility
cdetect
🔬 Detect which compiler and compiler version a Linux executable (in the ELF format) was compiled with
Stars: ✭ 23 (+91.67%)
Mutual labels:  utility
youtube-unofficial
Access parts of your account unavailable through normal YouTube API access.
Stars: ✭ 33 (+175%)
Mutual labels:  utility
Tasks
Tasks is an application that optimizes computer performance. Tasks improves overall system performance, boot times, and a safer experience while using your computer.
Stars: ✭ 65 (+441.67%)
Mutual labels:  utility
dotfiles
dotfiles symbolic links management CLI
Stars: ✭ 156 (+1200%)
Mutual labels:  utility
monoreaper
🌱 Create a monorepo by merging multiple github repositories
Stars: ✭ 21 (+75%)
Mutual labels:  utility
PathCleaner
Cleanup tool for polluted PATH environment variable
Stars: ✭ 18 (+50%)
Mutual labels:  utility
useful
🇨🇭 A collection of useful functions for working in Elixir
Stars: ✭ 21 (+75%)
Mutual labels:  utility

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].