Giter Club home page Giter Club logo

remove-prop's Introduction

remove-prop

Remove own nested properties (props) from a object using prop-path notation

Install

npm install remove-prop

API

removeOwnProp(obj, path, parserOptions)

  • obj : takes a object to perform prop deletion
  • path: takes a string specifying the prop to be deleted
  • parserOptions: takes of object of options

throws: PropMissing and ObjectInvalid errors

returns true if succeeds otherwise false

Parser Options

  1. separator (default: .): specify the prop separator which is to be used during parsing of the path
  2. optionalChar (default: ?): specify the optional character

Features and Usage

Import

const { removeOwnProp } = require("remove-prop");

Example Target Object

const universe = {
  galaxies: {
    milkyway: {
      sun: "Burning",
      earth: "Life",
    },
    andromeda: {
      life: "not found"
    },
  }
};
Strict prop vs Optional prop
  1. Path = "galaxies" // the prop must exist on the object otherwise PropMissing error will be thrown
  2. Path = "?galaxies" // the prop is optional and only gets deleted if it exists
Path examples
  1. "galaxies" : delete universe["galaxies"]
  2. "galaxies.milkyway" : delete universe.galaxies["milkyway"]
  3. "galaxies.milkyway.?sun": delete universe.galaxies.milkyway["sun"]
To remove strict props on level 1
removeOwnProp(universe, "galaxies"); // => true
console.log(universe); // {}
To remove deep nested strict props
removeOwnProp(universe, "galaxies.milkyway.sun"); // => true
To remove optional prop on level 1
removeOwnProp(universe, "galaxies.?beautifulGalaxy"); // => false
To remove deep nested optional prop
removeOwnProp(universe, "galaxies.milkyway.?blackHole"); // => false
removeOwnProp(universe, "galaxies.milkyway.?earth"); // => true

โŒ Not Supported

  • Cyclic Objects
  • Symbolic keys

remove-prop's People

Contributors

bhishekprajapati avatar

Watchers

 avatar

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.