Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More โ†’
Socket
Sign inDemoInstall
Socket

@blakek/deep

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@blakek/deep

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

  • 3.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5.3K
decreased by-57.32%
Maintainers
1
Weekly downloads
ย 
Created
Source

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, get, getOr, has, omit, pluck, remove, set } 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'

// Arguments can be partially applied
const githubUsername = get('sites.github.username');
githubUsername(user); //ยป 'blakek'

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

// 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
  • arguments can be partially applied

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

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

function get(path: Path, object: any): any;
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'

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

getOr

Like get, gets a value from an object. Will return a fallback other than undefined if the value was not found equal to undefined.

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

getOr('/images/placeholder.png', 'sites.github.image', user); //ยป '/images/placeholder.png'

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

has

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

function has(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

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

omit

Returns a clone of an object with some 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;
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' } } }

pluck

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' ] }

remove

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;
const user = {
  username: 'blakek',
  password: 'wouldntyouliketoknow'
};

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

set

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;
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 }

Contributing

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

To install all dependencies, run:

yarn

Useful Commands

yarn buildBuilds the project to ./dist
yarn formatFormat the source following the Prettier styles
yarn testRun project tests
yarn test --watchRun project tests, watching for file changes

License

MIT

Keywords

FAQs

Package last updated on 06 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc