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

merge-left-utils

v1.0.7

Published

Merge objects without structure changes

Downloads

38

Readme

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
const a = { a: 'a', b: 'b' }
const b = { a: 'newA', b: 'newB' }

// simple case: operate only with {b} key
mergeLeftKeys(['b'], a, b) // -> { b: 'newB' }

// custom replace logic: do not replace {b} key
// signature: (key: string, source: T, target: Partial<T>) => boolean
mergeLeftKeys(['a', 'b'], a, b, (key) => key !== 'b')

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)

const a = { a: 'a', b: 'b' }
const b = { b: 'newB', c: 'newC' }

// preserve [a.a], replace [a.b] with [b.b], skip [b.c]
mergeLeft(a, b) // -> { a: 'a', b: 'newB' }

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

Acts as mergeLeft but dropping keys from dropKeys argument.

const a = { a: 'a', b: 'b' }
const b = { a: 'newA', b: 'newB' }

// replace [a.a] with [b.a], drop [b] field from result
mergeLeftDropping(['b'], a, b) // -> { a: 'newA' }

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

Acts as mergeLeft but skip keys from skipKeys from being replaced

const a = { a: 'a', b: 'b' }
const b = { a: 'newA', b: 'newB' }

// replace [a.b] with [b.b], skipping replace [a.a]
mergeLeftExcept(['a'], a, b) // -> { a: 'a', b: 'newB' }

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

const a = { a: 'a', b: 'b', c: 'c' }
const b = { a: undefined, b: '', c: 'newC' }

// replace [a.c] with [b.c], skipping others
mergeLeftTruthy(a, b) // -> { a: 'a', b: 'b' }

Install

# with npm
npm install merge-left-utils
# with yarn
yarn add merge-left-utils

Examples

Visit examples readme to see use case examples

Run tests

yarn run test

Author

👤 ikenfin

  • Website: https://ikfi.ru
  • Github: @ikenfin

Show your support

Give a ⭐️ if this project helped you!


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