1.0.5 • Published 2 years ago
remove-prop v1.0.5
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 deletionpath
: takes a string specifying the prop to be deletedparserOptions
: takes of object of options
throws: PropMissing
and ObjectInvalid
errors
returns true
if succeeds otherwise false
Parser Options
separator
(default:.
): specify the prop separator which is to be used during parsing of the pathoptionalChar
(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
- Path =
"galaxies"
// the prop must exist on the object otherwisePropMissing
error will be thrown - Path =
"?galaxies"
// the prop is optional and only gets deleted if it exists
Path examples
"galaxies"
: delete universe"galaxies""galaxies.milkyway"
: delete universe.galaxies"milkyway""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