Code Monkey home page Code Monkey logo

unset-value's Introduction

unset-value NPM version NPM monthly downloads NPM total downloads Linux Build Status

Delete nested properties from an object using dot notation.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save unset-value

Usage

var unset = require('unset-value');
unset(obj, prop);

Params

  • obj {object}: The object to unset prop on
  • prop {string | string[]}: The property to unset. Dot-notation may be used or an array of nested properties.

Examples

Updates the object when a property is deleted

var obj = {a: 'b'};
unset(obj, 'a');
console.log(obj);
//=> {}

Returns true when a property is deleted

unset({a: 'b'}, 'a') // true

Returns true when a property does not exist

This is consistent with delete behavior in that it does not throw when a property does not exist.

unset({a: {b: {c: 'd'}}}, 'd') // true

delete nested values

var one = {a: {b: {c: 'd'}}};
unset(one, 'a.b');
console.log(one);
//=> {a: {}}

var two = {a: {b: {c: 'd'}}};
unset(two, ['a', 'b', 'c']);
console.log(two);
//=> {a: {b: {}}}

var three = {a: {b: {c: 'd', e: 'f'}}};
unset(three, 'a.b.c');
console.log(three);
//=> {a: {b: {e: 'f'}}}

throws on invalid args

unset();
// 'expected an object.'

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

  • get-value: Use property paths like 'a.b.c' to get a nested value from an object. Even works… more | homepage
  • get-values: Return an array of all values from the given object. | homepage
  • omit-value: Omit properties from an object or deeply nested property of an object using object path… more | homepage
  • put-value: Update only existing values from an object, works with dot notation paths like a.b.c and… more | homepage
  • set-value: Create nested values and any intermediaries using dot notation ('a.b.c') paths. | homepage
  • union-value: Set an array of unique values as the property of an object. Supports setting deeply… more | homepage
  • upsert-value: Update or set nested values and any intermediaries with dot notation ('a.b.c') paths. | homepage

Contributors

Commits Contributor
11 jonschlinkert
4 danez
2 wtgtybhertgeghgtwtg
1 TrySound
1 bluelovers

Author

Jon Schlinkert

License

Copyright © 2021, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on March 18, 2021.

unset-value's People

Contributors

bluelovers avatar danez avatar gfauchart avatar jonschlinkert avatar trysound avatar wtgtybhertgeghgtwtg avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

unset-value's Issues

isObject is not a function

I am suddenly encountering this isssue where the isObject function is somehow not defined.

/*!
 * unset-value <https://github.com/jonschlinkert/unset-value>
 *
 * Copyright (c) 2015, 2017, Jon Schlinkert.
 * Released under the MIT License.
 */

'use strict';

var isObject = require('isobject');
var has = require('has-value');

const isUnsafeKey = key => {
  return key === '__proto__' || key === 'constructor' || key === 'prototype';
};

const validateKey = key => {
  if (isUnsafeKey(key)) {
    throw new Error(`Cannot set unsafe key: "${key}"`);
  }
};

module.exports = function unset(obj, prop) {
  if (!isObject(obj)) {                 // => throws error here because isObject is not a function
    throw new TypeError('expected an object.');
  }

  //...

Any ideas why this might be happening?

bug when `a.b\\.c.arr.2`

import deep from './deep.json'
import _has from "has-value";
import _unset from "unset-value";
import _get, { Options } from 'get-value';

let paths = 'a.b\\.c.arr.2';

expect(_has(deep, paths as any)).to.be.ok;
expect(_get(deep, paths as any)).to.be.deep.equal({
		d: {
			e: { arr: [ { f: false }, { f: false } ] }
		}
	}
);

_unset(deep, paths as any)

deep.json

{
	"a": {
    "b.c": {
      "arr": [
        {
          "d": {
            "e": {
              "arr": [
                {
                  "f": true
                },
                {
                  "f": true
                }
              ]
            }
          },
          "x": false
        },
        {
          "d": {
            "e": {
              "arr": [
                {
                  "f": false
                },
                {
                  "f": false
                }
              ]
            }
          }
        },
        {
          "d": {
            "e": {
              "arr": [
                {
                  "f": false
                },
                {
                  "f": false
                }
              ]
            }
          }
        }
      ]
    },
		"b": {
			"c": {
        "arr": [
          {
            "d": {
              "e": {
                "arr": [
                  {
                    "f": true
                  },
                  {
                    "f": true
                  }
                ]
              }
            },
            "x": true
          },
          {
            "d": {
              "e": {
                "arr": [
                  {
                    "f": false
                  },
                  {
                    "f": false
                  }
                ]
              }
            }
          },
          {
            "d": {
              "e": {
                "arr": [
                  {
                    "f": false
                  },
                  {
                    "f": false
                  }
                ]
              }
            }
          }
        ]
      }
		}
	}
}

Cant unset if property has 'no value'

Hi,
Im trying to unset a property with an empty array, but unset-value doesnt unset that property

const unsetValue = require('unset-value')

let data = {
    "a": "a",
    "b": {
        "b": [],
        "bb": 1
    },
    "c": []
}

unsetValue(data, 'b.b') // b.b isnt removed
console.log(data)

unsetValue(data, 'b.bb') // b.b is removed
console.log(data)

unsetValue(data, 'c') // b.b is removed ?
console.log(data)

Seems that the "problem" is with 'has-value', but it works as expected. Maybe this should be checked against 'has-property' ?

Support passing object path as an array

I've been using both set-value and get-value of yours, and was surprised to see that unset-value does not support passing the object path as an array as well.

New release

Hi, could you please cut a new release with the new has-value dependency which is already in master? thanks

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.