merge-left-utils

0

Описание

Merge objects without structure changes

Языки

  • TypeScript94,6%
  • JavaScript5,4%
README.md

merge-left-utils

Version License: MIT codecov

Merge objects without structural changes

Library provides bunch of functions to simplify objects updates, preserving their initial structure.

I've created this library to simplify work with react state and apollo data in forms, but it may be used in various scenarios when you want update object fields values, but not object structure itself.

mergeLeftKeys (fields: string[], source: T, target: T, replaceDecision: Function) -> result: T

Copy fields, existing in source, from target. replaceDecision is user level function to make decision about replacing key.

Arguments:

  • fields: array of strings - which fields are being remain in result
  • source: js object with source data
  • target: js object with target data
  • replaceDecision: boolean function of 3 - (key: string, source, target) => boolean - if returns true - field from {source} will be replaced with target[key], else field replacing skips

mergeLeft (source: T, target: T, replaceDecision: Function) -> result: T

Simple binary function - copies all fields from target which exists in source. Fields that not present in source are ignored

It's shortcut for mergeLeftKeys -

(a, b) => mergeLeftKeys(Object.keys(a), a, b)

mergeLeftDropping (dropKeys: string[], source: T, target: T) -> result: T

Acts as mergeLeft but dropping keys from dropKeys argument.

mergeLeftExcept (skippingKeys: string[], source: T, target: T) -> result: T

Acts as mergeLeft but skip keys from skipKeys from being replaced

mergeLeftOnly (replaceableKeys: string[], source: T, target: T) -> result: T

Acts as mergeLeft, skipping all keys from being replaced which are not listed in replaceableKeys

mergeLeftTruthy (source: T, target: T, replaceDecision: Function) -> result: T

Acts as mergeLeft, skipping all falsy values

Install

Examples

Visit examples readme to see use case examples

Run tests

Author

👤 ikenfin

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator