npm

nyxdefaults
TypeScript icon, indicating that this package has built-in type declarations

0.0.3Β β€’Β PublicΒ β€’Β Published

cover npm version npm downloads bundle JSDocs License

Assign default properties, recursively πŸ”„. Lightweight and Fast πŸ’¨.

Install

Install package:

# nyxi
nyxi nyxdefaults

# pnpm
pnpm add nyxdefaults

# npm
npm i nyxdefaults

# yarn
yarn add nyxdefaults

Usage

import { nyxdefaults } from 'nyxdefaults'

const options = nyxdefaults(object, ...defaults)

Leftmost arguments have more priority when assigning defaults.

Arguments

  • object (Object): The destination object.
  • source (Object): The source object.
import { nyxdefaults } from 'nyxdefaults'

console.log(nyxdefaults({ 'a': { 'b': 2 } }, { 'a': { 'b': 1, 'c': 3 } }))
// => { a: { b: 2, c: 3 } }

Using with CommonJS

const { nyxdefaults } = require('nyxdefaults')

Custom Merger

Sometimes default merging strategy is not desirable. Using createNyxdefaults we can create a custom instance with different merging strategy.

This function accepts obj (source object), key and value (current value) and should return true if applied custom merging.

Example: Sum numbers instead of overriding

import { createNyxdefaults } from 'nyxdefaults'

const ext = createDefu((obj, key, value) => {
  if (typeof obj[key] === 'number' && typeof value === 'number') {
    obj[key] += val
    return true
  }
})

ext({ cost: 15 }, { cost: 10 }) // { cost: 25 }

Function Merger

Using nyxdefaultsFn, if user provided a function, it will be called with default value instead of merging.

I can be useful for default values manipulation.

Example: Filter some items from defaults (array) and add 20 to the count default value.

import { nyxdefaultsArrayFn } from 'nyxdefaults';

nyxdefaultsArrayFn({
  ignore: val => val.filter(i => i !== 'dist'),
  count: () => 20
}, {
  ignore: [
    'node_modules',
    'dist'
  ],
  count: 10
});
 /*
 {
    ignore: ['node_modules'],
    count: 30
  }
  */

Note: if the default value is not defined, the function defined won't be called and kept as value.

Array Function Merger

nyxdefaultsArrayFn is similar to nyxdefaultsFn but only applies to array values defined in defaults.

Example: Filter some items from defaults (array) and add 20 to the count default value.

import { nyxdefaultsArrayFn } from 'nyxdefaults'

nyxdefaultsArrayFn({
  ignore(val) => val.filter(i => i !== 'dist'),
  count: () => 20
 }, {
   ignore: [
     'node_modules',
     'dist'
   ],
   count: 10
 })
 /*
  {
    ignore: ['node_modules'],
    count: () => 20
  }
  */

Note: the function is called only if the value defined in defaults is an aray.

Remarks

  • object and defaults are not modified
  • Nullish values (null and undefined) are skipped. Please use defaults-deep or omit-deep or lodash.defaultsdeep if you need to preserve or different behavior.
  • Assignment of __proto__ and constructor keys will be skipped to prevent security issues with object pollution.
  • Will concat array values (if default property is defined)
console.log(nyxdefaults({ array: ['b', 'c'] }, { array: ['a'] }))
// => { array: ['a', 'b', 'c']}

Type

We expose Nyxdefaults as a type utility to return a merged type that follows the rules that nyxdefaults follows.

import type { Nyxdefaults } from 'nyxdefaults'

type Options = Nyxdefaults<{ foo: 'bar' }, [{}, { bar: 'baz' }, { something: 42 }]>
// returns { foo: 'bar', bar: 'baz', 'something': 42 }

License

MIT. Made with πŸ’ž

Readme

Keywords

none

Package Sidebar

Install

npm i nyxdefaults

Weekly Downloads

4

Version

0.0.3

License

MIT

Unpacked Size

15.6 kB

Total Files

7

Last publish

Collaborators

  • dennisollhoff