Skip to content

smithki/refine-deep

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧼 refine-deep

code style: airbnb code style: prettier

Recursively remove falsey values from JavaScript objects.

💁🏼‍♂️ Introduction

A simple utility that exposes a lodash-style interface for removing falsey values from JavaScript/TypeScript objects.

🔗 Installation

Install via yarn (recommended):

yarn add refine-deep

Install via npm:

npm install refine-deep

🛠️ Usage

Shallow functionality

import { refine } from 'refine-deep';

refine(['hello world', null, 0, undefined, '', [], {}]);
// => ['hello world']

refine({ helloWorld: '', foo: 'bar', baz: null });
// => { foo: 'bar' }

Recursive functionality

import { refineDeep } from 'refine-deep';

refineDeep([[null, [999]], { hello: 'world', foo: { bar: 1234, baz: null } }]);
// => [[[999]], { hello: 'world', foo: { bar: 1234 } }]

You can optionally specify a maximum recursion depth as the last argument to refineDeep:

refineDeep(myCollection); // Infinity is assumed.
refineDeep(myCollection, depth);
refineDeep(myCollection, options, depth);

Configuration

You can configure refine and refineDeep with the same options:

All options are false by default

export interface RefineOptions {
  ignoreNil?: boolean; // Retain `null` and `undefined` values.
  ignoreNull?: boolean; // Retain `null` values.
  ignoreUndefined?: boolean; // Retain `undefined` values.
  ignoreEmptyAny?: boolean; // Retain empty arrays, objects, and strings.
  ignoreEmptyArrays?: boolean; // Retain empty arrays.
  ignoreEmptyObjects?: boolean; // Retain empty objects.
  ignoreEmptyStrings?: boolean; // Retain empty strings.
  ignoreZeros?: boolean; // Retain zeros.
  ignoreNaN?: boolean; // Retain NaN.
}

refine(myCollection, { ignore*: true });
refineDeep(myCollection, { ignore*: true }, depth?: number);

UMD interface

If using refine-deep as a UMD module (for instance, via unpkg), usage is slightly different:

refine(...) // Exactly the same, non-recursive.

// For recursive behavior, use the following:
refine.deep(...) // Type signature is equivalent to `refineDeep`.

About

Recursively remove falsey values from JavaScript objects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published