only-prop
v1.0.1
Published
Return whitelisted properties of an object
Maintainers
Readme
only-prop
Return whitelisted properties of an object. Zero dependencies, dual ESM/CJS, TypeScript-ready.
Install
npm install only-propUsage
import only from 'only-prop';
const user = {
name: 'tobi',
email: '[email protected]',
_id: '12345',
password: 'secret',
};
// Space-delimited string
only(user, 'name email');
// → { name: 'tobi', email: '[email protected]' }
// Array of keys
only(user, ['name', 'email']);
// → { name: 'tobi', email: '[email protected]' }Keys whose values are null or undefined are automatically omitted.
API
only(obj, keys)
| Parameter | Type | Description |
|-----------|------|-------------|
| obj | object | Source object |
| keys | string \| string[] | Space-delimited string or array of property names to keep |
Returns a new object containing only the specified keys that exist (and are non-null) on obj.
License
MIT — see LICENSE
