Skip to content

boraseoksoon/cljs-merge

Repository files navigation

cljs-merge

'cljs-merge' recursively merges two jsons into a unified one, excluding null or undefined in the same order.

NPM Version

const { merge } = require('cljs-merge')

const js1 = { k1: "v1", k2: "v2", k3: "v3" }
const js2 = { k1: "v1", k2: undefined, k4: "v4", k5: "v5", k6: null }

const res = merge({ src:js1, target: js2 })
console.log("res ==> ", res);
// res ==> { k1: 'v1', k2: 'v2', k3: 'v3', k4: 'v4', k5: 'v5' }
const { merge } = require("cljs-merge")

const src = {	
	key1: "1",
	key2: "2",
	key3: "3",
	key4: "4",
	key5: {
		key22: null,
		key11: "10",
		key33: "30"
	},
	special: "BORA"
}

const target = {	
	key1: "10",
	key3: null,
	key4: "40",
	key5: {
		key11: null,
		key22: "200",
		key33: null
	}, 
	key6: "6", 
	quit: -1
}

const res = merge({ src, target })

console.log("res ==> ", res);
// res ==> 
// {	
// 	key1: "10",
// 	key2: "2",
// 	key3: "3",
// 	key4: "40"
//  key5: {
// 		key11: "10", 
// 		key22: "200", 
// 		key33: "30", 
// 	}
// 	key6: "6", 
// 	special: "BORA"
//  quit: -1
// }

Installation for Node.js

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Node.js 0.10 or higher is required.

If this is a brand new project, make sure to create a package.json first with the npm init command.

Installation is done using the npm install command:

$ npm install cljs-merge

Installation for Web Browser

CDN

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<script src="https://cdn.jsdelivr.net/npm/cljs-merge@1.1.1/cljs_dist/core/web/cljs.js"></script>
	<script>
		const { merge } = cljs.merge.core
		
		const src = {
			k1: "v1",
			k2: "v2",
			k3: "v3"
		}
	
		const target = {
			k1: "v1",
			k2: undefined,
			k4: "v4",
			k5: "v5",
			k6: null
		}
	
		const res = merge({ src, target })
		
		console.log("res ==> ", res);
		// res ==> { k1: 'v1', k2: 'v2', k3: 'v3', k4: 'v4', k5: 'v5' }
	</script>
</head>
<body>
	
</body>
</html>
	<script src="https://cdn.jsdelivr.net/npm/cljs-merge@1.1.1/cljs_dist/core/web/cljs.js"></script>
	<script>
		const { merge } = cljs.merge.core
		
		const src = {
			k1: "v1",
			k2: "v2",
			k3: "v3"
		}
	
		const target = {
			k1: "v1",
			k2: undefined,
			k4: "v4",
			k5: "v5",
			k6: null
		}
	
		const res = merge({ src, target })
		
		console.log("res ==> ", res);
		// res ==> { k1: 'v1', k2: 'v2', k3: 'v3', k4: 'v4', k5: 'v5' }
	</script>

Contributing

Contributing Guide

License

MIT