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

@blakek/deep

v4.0.0

Published

๐Ÿก Get, set, remove, and test for deeply nested properties

Downloads

47,322

Readme

deep

๐Ÿก Get, set, remove, and test for deeply nested properties

Helps you safely work with nested properties.

Install

Using Yarn:

$ yarn add @blakek/deep

โ€ฆor using npm:

$ npm i --save @blakek/deep

Usage

import {
  clone,
  createGetter,
  get,
  has,
  omit,
  pluck,
  remove,
  set
  // also available:
  // - createHas
  // - createOmit
  // - createPluck
  // - createRemove
  // - createSetter
  // - isObject
  // - traverseObject
} from '@blakek/deep';

const user = {
  id: 'abf87de',
  roles: ['alert:create', 'alert:read'],
  sites: {
    github: {
      username: 'blakek'
    }
  }
};

// Deeply clone most values
const userCopy = clone(user);
user === userCopy; //ยป false
user.id === userCopy.id; //ยป true
user.roles === userCopy.roles; //ยป false
user.roles[0] === userCopy.roles[0]; //ยป true

// Get a property value
get('sites.github.username', user); //ยป 'blakek'

// Get a property value with a fallback other than `undefined`
get('sites.facebook.username', user, 'no-account'); //ยป 'no-account'

// Create a function to get a property value later
const getUsername = createGetter('sites.github.username');
getUsername(user); //ยป 'blakek'

// Test for a property value
has('sites.github', user); //ยป true

// Clone an object and omit properties
omit(['roles', 'sites'], user); //ยป { id: 'abf87de' }

// Pluck a subset of properties
pluck(['id', 'roles'], user);
//ยป { id: 'abf87de', roles: [ 'alert:create', 'alert:read' ] }

// Remove a property value, modifying the current object
remove('a', { a: 42, b: 123 }); //ยป { b: 123 }

// Set a property value, modifying the current object
set(123, 'a.b.c', { a: 42 }); //ยป { a: { b: { c: 123 } } }

API

For all these:

  • path can be either a dot-notation string or array of path parts

clone

Returns a deep clone / deep copy of most values: primitive values, objects, arrays, Map, Set, Date, etc.

function clone<T extends unknown>(value: T): T;
const object = { value: 'yep' };
const cloned = clone(object);

cloned === object; //ยป false
cloned.value === object.value; //ยป true

get / createGetter

Gets the value for a given path with an optional fallback value.

function get(path: Path, object: object, fallbackValue?: any): unknown;

function createGetter(
  path: Path,
  fallbackValue?: any
): (object: object) => unknown;
const user = {
  id: 'abf87de',
  roles: ['alert:create', 'alert:read'],
  sites: {
    github: {
      username: 'blakek'
    }
  }
};

get('id', user); //ยป 'abf87de'
get('roles.0', user); //ยป 'alert:create'
get('roles[0]', user); //ยป 'alert:create'
get(['roles', 1], user); //ยป 'alert:read'
get('sites.github.username', user); //ยป 'blakek'
get('sites.github.avatar.src', user, 'default.png'); //ยป 'default.png'

const getID = get('id');
getID(user); //ยป 'abf87de'

const getRoles = createGetter('roles');
getRoles(user); //ยป ['alert:create', 'alert:read']

has / createHas

Returns true if a value was found at the given path or false if nothing was found.

function has(path: Path, object: any): boolean;

function createHas(path: Path): (object: any) => boolean;
const product = {
  id: 'abf87de',
  name: 'Logo T-Shirt',
  attributes: {
    isCool: undefined,
    materials: ['cotton']
  }
};

has('attributes.materials', product); //ยป true
has(['avability', 'sizes'], product); //ยป false
has('attributes.isCool', product); //ยป true; property exists but is undefined

const hasMaterials = createHas('attributes.materials');
hasMaterials(product); //ยป true

// NOTE: `get()` should be used if you want to ensure a value is not `undefined`
get('attributes.isCool', product, false); //ยป false

omit / createOmit

Returns a clone of an object with a list of properties removed.

Note: omit() returns a clone with properties removed. If you'd rather modify the existing object for performance, consider using remove().

function omit(properties: Path[], object: any): any;

function createOmit(properties: Path[]): (object: any) => any;
const user = {
  username: 'blakek',
  roles: ['alert:create', 'alert:read'],
  sites: {
    github: {
      username: 'blakek'
    }
  }
};

omit(['roles', 'sites'], user); //ยป { username: 'blakek' }
omit(['username', 'roles', 'sites.doesnt.exist'], user);
//ยป { sites: { github: { username: 'blakek' } } }

const omitExtra = createOmit(['roles, sites']);
omitExtra(user); //ยป { username: 'blakek' }

pluck / createPluck

Gets a subset of properties from an object.

function pluck(properties: Path[], object: any): any;
const user = {
  username: 'blakek',
  roles: ['alert:create', 'alert:read'],
  sites: {
    github: {
      username: 'blakek'
    }
  }
};

pluck(['username'], user); //ยป { username: 'blakek' }
pluck(['username', 'roles'], user);
//ยป { username: 'blakek', roles: [ 'alert:create', 'alert:read' ] }

const permissionInfo = pluck(['roles', 'username']);
permissionInfo(user); //ยป { roles: [ 'alert:create', 'alert:read' ], username: 'blakek' }

remove / createRemove

Removes a value at a path and returns the object.

Note: remove() modifies the passed-in object rather than creating a copy. If you'd rather return a new object:

  • use omit(); omit() returns a clone with a list of properties removed
  • use clone() before remove()
  • consider another solution (unchanged is really good)
function remove(path: Path, object: any): any;

function createRemove(path: Path): (object: any) => any;
const user = {
  username: 'blakek',
  password: 'wouldntyouliketoknow'
};

remove('password', user); //ยป { username: 'blakek' }
remove('property.does.not.exist', user);
//ยป { username: 'blakek' } (same object from previous line)

const removePassword = createRemove('password');
removePassword({ username: 'bob', password: 'laskjfl' }); //ยป { username: 'bob' }

set / createSetter

Sets a value at a path and returns the object.

Note: set() modifies the passed-in object rather than creating a copy. If you'd rather return a new object:

  • use clone() before set()
  • consider another solution (unchanged is really good)
function set(value: any, path: Path, object: any): any;

function createSetter(path: Path, object: any): (value: any) => any;
const user = {
  profile: {
    bgColor: '#639'
  }
};

set('tomato', 'profile.bgColor', user); //ยป { profile: { bgColor: 'tomato' } }

set('/images/user.png', 'profile.bgImage', user);
//ยป { profile: { bgColor: 'tomato', bgImage: '/images/user.png' } }

const logout = set(null, 'profile');
logout(user); //ยป { profile: null }

const setUsername = createSetter('username', user);
setUsername('blakek'); //ยป  { profile: { bgColor: 'tomato', bgImage: '/images/user.png' }, username: 'blakek' }

Contributing

Node.js and Yarn are required to work with this project.

To install all dependencies, run:

yarn

Useful Commands

| | | | ------------------- | ----------------------------------------------- | | yarn build | Builds the project to ./dist | | yarn format | Format the source following the Prettier styles | | yarn test | Run project tests | | yarn test --watch | Run project tests, watching for file changes |

License

MIT