Skip to content

me-npm/pick-defined

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pick-defined

Removes undefined values from objects while preserving falsy values (0, false, null).

Features

  • Removes undefined: Cleans your objects of undefined properties.
  • Preserves Falsy: Keeps 0, false, null, and "".
  • Type Safe: Returns a Partial<T> of your input type.
  • Tiny & Fast: Zero dependencies.
  • Dual Support: Works with both ES Modules (ESM) and CommonJS (CJS).

Installation

npm install pick-defined

Usage

Basic Usage

import { pickDefined } from 'pick-defined';

const user = {
  name: "Poly",
  age: undefined,
  active: false,
};

const cleaned = pickDefined(user);
// Result: { name: "Poly", active: false }

Preserving Falsy Values

Unlike some other libraries or JSON.stringify tricks, pick-defined keeps falsy values that are often valid data.

const filters = {
  search: "",      // Kept
  page: 0,         // Kept
  limit: 10,       // Kept
  sort: undefined, // Removed
  category: null   // Kept
};

const query = pickDefined(filters);
// Result: { search: "", page: 0, limit: 10, category: null }

CommonJS (require)

const { pickDefined } = require('pick-defined');

const cleaned = pickDefined({ a: 1, b: undefined });

API

pickDefined<T>(obj: T): Partial<T>

  • obj: The object to clean.
  • Returns: A shallow copy of the object with all undefined properties removed.

License

MIT


Author: Ahmer Arain

pick-defined

Releases

No releases published

Packages

 
 
 

Contributors