2

GitHub - aralroca/default-composer: A tiny (300B) JavaScript library that allows...

 11 months ago
source link: https://github.com/aralroca/default-composer
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Default composer

A tiny (300B) JavaScript library that allows you to set default values for nested objects

"default-composer" is a JavaScript library that allows you to set default values for nested objects. The library replaces empty strings/arrays/objects, null, or undefined values in an existing object with the defined default values, which helps simplify programming logic and reduce the amount of code needed to set default values.

Installation

You can install "default-composer" using npm:

npm install default-composer

or with yarn:

yarn add default-composer

Usage

To use "default-composer", simply require the library and call the defaultComposer() function with the default values object and the original object that you want to set default values for. For example:

import defaultComposer from 'default-composer';

const defaults = {
  name: 'Aral 😊',
  surname: '',
  isDeveloper: true,
  isDesigner: false,
  age: 33,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
  },
  emails: ['[email protected]'],
  hobbies: ['programming'],
};

const originalObject = {
  name: 'Aral',
  emails: [],
  phone: '555555555',
  age: null,
  address: {
    zip: '54321'
  },
  hobbies: ['parkour', 'computer science', 'books', 'nature'],
};

const result = defaultComposer(defaults, originalObject);

console.log(result);

This will output:

{
  name: 'Aral',
  surname: '',
  isDeveloper: true,
  isDesigner: false,
  emails: ['[email protected]'],
  phone: '555555555',
  age: 33,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '54321'
  },
  hobbies: ['parkour', 'computer science', 'books', 'nature'],
}
defaultComposer(defaults, object1[, object2, ...])

This function takes one or more objects as arguments and returns a new object with default values applied. The first argument should be an object containing the default values to apply. Subsequent arguments should be the objects to apply the default values to.

defaultComposer(priority3, priority2, priority1)

If a property in a given object is either empty, null, or undefined, and the corresponding property in the defaults object is not empty, null, or undefined, the default value will be used.

Example

import defaultComposer from 'default-composer';

const defaultsPriority1 = {
  name: 'Aral 😊',
  hobbies: ['reading']
};

const defaultsPriority2 = {
  name: 'Aral 🤔',
  age: 33,
  address: {
    street: '123 Main St',
    city: 'Anytown',
    state: 'CA',
    zip: '12345'
  },
  hobbies: ['reading', 'hiking']
}

const object = {
  address: {
    street: '',
    city: 'Anothercity',
    state: 'NY',
    zip: ''
  },
  hobbies: ['running']
};

const result = defaultComposer(defaultsPriority2, defaultsPriority1, object);

console.log(result);

This will output:

{
  name: 'Aral 😊',
  age: 33,
  address: {
    street: '123 Main St',
    city: 'Anothercity',
    state: 'NY',
    zip: '12345'
  },
  hobbies: ['running']
}

TypeScript

In order to use in TypeScript you can pass a generic with the expected output, and all the expected input by default should be partials of this generic.

Example:

type Addres = {
  street: string,
  city: string,
  state: string,
  zip: string
}

type User = {
  name: string,
  age: number,
  address: Address,
  hobbies: string[]
}

const defaults = {
  name: 'Aral 😊',
  hobbies: ['reading']
};

const object = {
  age: 33,
  address: {
    street: '',
    city: 'Anothercity',
    state: 'NY',
    zip: ''
  },
  hobbies: []
};

defaultComposer<User>(defaults, object)

Contributing

Contributions to "default-composer" are welcome! If you find a bug or want to suggest a new feature, please open an issue on the GitHub repository. If you want to contribute code, please fork the repository and submit a pull request with your changes.

License

"default-composer" is licensed under the MIT license. See LICENSE for more information.

Credits

"default-composer" was created by Aral Roca.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK