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

3.0.0 • Public • Published

Mòrèn (默认)

license npm npm Gitlab pipeline status Snyk Vulnerabilities for npm package

Fill default values in object when empty.

Features

  • Fill default values in object easily.
  • Can use function to create dynamic default value.
  • TypeScript declaration included.

Installation

Via npm:

npm i moren

Via Yarn:

yarn add moren

Usage

const moren = require('moren');
 
const defaults = {
    a: 'aaa',
    b: false,
    c: obj => obj.a.length + 1
};
 
const completeObj = {
    a: 'XXX',
    b: true,
    c: 0
};
const incompleteObj = {
    a: 'X',
    b: true
};
const emptyObj = {};
 
const result1 = moren(completeObj, defaults);
/**
 * {
 *   a: 'XXX',
 *   b: true,
 *   c: 0
 * }
 */
 
const result2 = moren(incompleteObj, defaults);
/**
 * {
 *   a: 'X',
 *   b: true,
 *   c: 2      // It's `a.length` (1) + 1
 * }
 */
 
const result3 = moren(emptyObj, defaults);
/**
 * {
 *   a: 'aaa',
 *   b: false,
 *   c: 4      // It's `a.length` (3) + 1
 * }
 */

API

moren(obj, defaults)

obj

An object to be filled.

defaults

An object that contains default value or the function that return value.

If you give a function as object's value, moren will pass current obj into function.
So, you can use given obj to create and return actual default value back.

Package Sidebar

Install

npm i moren

Weekly Downloads

28

Version

3.0.0

License

MIT

Unpacked Size

7.58 kB

Total Files

11

Last publish

Collaborators

  • illuminator