All files / json-map-transform/lib/utils objectHandler.js

100% Statements 25/25
100% Branches 11/11
100% Functions 7/7
100% Lines 24/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44237x   1x 30x 30x 30x 30x 30x 30x   30x     1x 107x 107x 107x 123x   107x       1x 105x 105x   105x 135x 120x   135x     105x     1x            
const isUndefined = (value) => value == undefined;
 
const has = (object, path) => {
	const subPaths = path.split('.');
	let hasPath = true;
	let obj = Object.assign({}, object);
	subPaths.forEach(subPath => {
		hasPath = obj && hasPath && Object.keys(obj).indexOf(subPath) > -1;
		obj = obj[subPath];
	});
	return hasPath;
};
 
const get = (object, path) => {
	const subPaths = path.split('.');
	let value = Object.assign({}, object);
	subPaths.forEach(subPath => {
		value = (value && !isUndefined(value[subPath])) ? value[subPath] : undefined;
	});
	return value;
};
 
 
const set =(object, path, value) => {
	let objectCopy = object;
	const subPaths = path.split('.');
 
	subPaths.forEach((subPath, index) => {
		if( !objectCopy[subPath] ) {
			objectCopy[subPath] = (index == subPaths.length -1) ? value : {};
		}
		objectCopy = objectCopy[subPath];
	});
 
	return object;
};
 
module.exports = {
	has,
	get,
	set,
	isUndefined
};