GitHunt
D4

d4nyll/rundef

Remove undefined properties from object

rundef

Remove undefined properties from object.

Build Status codecov Codacy Badge CodeFactor Test Coverage Maintainability NSP Status Known Vulnerabilities Greenkeeper badge
FOSSA Status

N.B. Does not remove null or falsy values, just undefined.

Install

NPM

$ npm install rundef
$ yarn add rundef

Usage

const rundef = require('rundef');
import rundef from 'rundef';

For the most accurate examples, see the test.js file

Basic

const input = {
  a: undefined,
  b: 1
}

rundef(input); // { b: 1 }

Advanced

rundef supports two options:

  • mutate boolean - if truthy, the original object will be mutated; if falsy, a new object will be constructed and returned. Defaults to false
  • recursive boolean | int - whether rundef should recursively process nested objects. If it's an integer, it will specify the number of nested layers, or levels, to process. If it is set to true, it will recursively process all layers. Defaults to 0, which is equivalent to false.
const input = {
  a: undefined,     // Level 0
  b: {
    c: 1,
    d: undefined,   // Level 1
    e: {
      f: undefined  // Level 2
    }
  }
}

const output = rundef(
  input,
  false, // mutate - whether to mutate the original object or return a new one
  1,     // recursive - whether to apply recursively
);

output;

{                   // Level 0
  b: {
    c: 1,           // Level 1
    e: {
      f: undefined  // Level 2 - Not removed as level 1 was specified
    }
  }
}

License

FOSSA Status

Languages

JavaScript100.0%

Contributors

MIT License
Created December 26, 2017
Updated March 10, 2023