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

@kamuisdo/deepmerge-fn

v1.1.1

Published

deepmerge js objects including function and array

Downloads

118

Readme

deepmerge-fn

Install

npm i @kamuisdo/deepmerge-fn --save
pnpm add @kamuisdo/deepmerge-fn

Introduce

  • 合并source对象和target对象,source对象会被改变,target对象不变
  • 递归合并两个对象
  • 对于数组、方法可以通过配置进行合并

How to use

import deepMerge from '@kamuisdo/deepmerge'
const result = deepMerge(obj1, obj2, options?)

Options

  1. Options是String类型时,所有的字段都按照统一的规则合并,例如 { options: 'cover' },object1所有字段都会被Object2的值覆盖
  2. Options是对象类型时,Options中的字段会按照各自指定的方式合并
  3. mode的值,对于值类型的字段都是obj2覆盖obj1的值,对于functionarrayObject类型,根据mode不同有不同的合并方式
    • cover:对所有字段都做覆盖合并
    • after
      • function:obj1的方法先执行,obj2的方法后执行,obj1的方法的执行结果作为obj2的方法的参数,最终输出obj2的执行结果
      • object: 合并
      • array:obj2的数组拼接在obj1的数组后面
    • before
      • function:obj2的方法先执行,obj1的方法后执行,obj2的方法的执行结果作为obj1的方法的参数,最终输出obj1的执行结果
      • object: 合并
      • array:obj2的数组拼接在obj1的数组后面
    • 自定义方法: TODO

Example

const obj1 = {
	string: 'string1',
	obj:{
		childString:'child string1',
		childAfterArr:[1],
		childCoverArr:[1,2],
		childObj:{
			subChildNumber:1
		},
		childFun: (a)=>{ return a+1 }
	}
}
const obj2 = {
	string: 'string2',
	array: [1,2,3],
	obj:{
		childString:'child string2',
		childNumber:199,
		childAfterArr:[2],
		childCoverArr: [1,2,3],
		childObj:{
			subChildNumber:2,
			subChildString:'subChildString2'
		},
		childFun: (a)=>{ return a+2 }
	}
}

const mergeResult = deepMerge(obj1,obj2,{
	mode:{
		obj:{
			childAfterArr:'after',	// array will be concat
			childFun:'after'		// function will be concat
		}
	}
})

// mergeResult expect to be: 
{
	string: 'string2',
	array: [1,2,3],
	obj:{
		childString:'child string2',
		childNumber:199,
		childAfterArr:[1,2],
		childCoverArr: [1,2,3],
		childObj:{
			subChildNumber:2,
			subChildString:'subChildString2'
		},
		childFun: (val)=>{
			const fn1 = (a)=>{ return a+1 }
			const fn2 = (a)=>{ return a+2 }
			return fn2(fn1(val))
		}
	}
}

// 方法合并的效果是先后调用两个方法,并返回后一个方法的返回值
mergeResult.obj.childFun(1) === 4