moren
v3.0.0
Published
Fill default values in object when empty.
Downloads
331
Maintainers
Readme
Mòrèn (默认)
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.