npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

Iโ€™ve always been into building performant and accessible sites, but lately Iโ€™ve been taking it extremely seriously. So much so that Iโ€™ve been building a tool to help me optimize and monitor the sites that I build to make sure that Iโ€™m making an attempt to offer the best experience to those who visit them. If youโ€™re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, ๐Ÿ‘‹, Iโ€™m Ryan Hefnerย  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If youโ€™re interested in other things Iโ€™m working on, follow me on Twitter or check out the open source projects Iโ€™ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soonโ€“ish.

Open Software & Tools

This site wouldnโ€™t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you ๐Ÿ™

ยฉ 2024 โ€“ย Pkg Stats / Ryan Hefner

nyxdefaults

v0.0.3

Published

๐Ÿ”„ Recursively assign default properties. ๐Ÿ’จ Lightweight and Fast!

Downloads

32

Readme

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 ๐Ÿ’ž