All files helpers.js

100% Statements 38/38
100% Branches 24/24
100% Functions 10/10
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110      20x 13x   20x 3x 6x 6x     3x     20x 99x               99x                                                   99x 99x 82x 90x                         90x   4x 86x 3x   665x     99x 79x   20x     20x 35x 9x   26x 1x   25x 1x   24x 17x   7x 4x   3x 2x   1x                  
import _ from 'lodash';
import diffObjects from './adapters/diff';
 
const capitalizeFirstLetter = string =>
	string.charAt(0).toUpperCase() + string.slice(1);
 
const toUnderscored = obj => {
	_.forEach(obj, (k, v) => {
		obj[k] = v
			.replace(/(?:^|\.?)([A-Z])/g, (x, y) => `_${y.toLowerCase()}`)
			.replace(/^_/, '');
	});
	return obj;
};
 
const calcDelta = (current, next, exclude, strict) => {
	const DEBUG = false;
	/* istanbul ignore next */
	if (DEBUG) {
		console.log('current', current);
		console.log('next', next);
		console.log('exclude', exclude);
	}
 
	const di = diffObjects(current, next);
 
	/* istanbul ignore next */
	if (DEBUG) {
		console.log('di', di);
 
		let diffs2 = [];
		if (di) {
			diffs2 = di.map(i =>
				JSON.parse(JSON.stringify(i).replace('"__data",', '')),
			);
		}
 
		console.log('diffs2', diffs2);
 
		console.log(
			'filter1',
			diffs2.filter(i => i.path.join(',').indexOf('_') === -1),
		);
 
		console.log(
			'filter2',
			diffs2.filter(i => exclude.every(x => i.path.indexOf(x) === -1)),
		);
	}
 
	let diffs = [];
	if (di) {
		diffs = di
			.map(i => JSON.parse(JSON.stringify(i).replace('"__data",', '')))
			.filter(i => {
				// return i.path.join(",").indexOf("_") === -1;
				// console.log('i', i)
				// if (!strict && i.kind === 'E') {
				// 	// console.log('str & num', i.lhs == i.rhs)
				// 	// console.log('str & num (strict)', i.lhs === i.rhs)
				// 	if (i.lhs != i.rhs) {
				// 		return i;
				// 	}
				// } else {
				// 	return i;
				// }
				if (!strict && i.kind === 'E') {
					// eslint-disable-next-line eqeqeq
					if (i.lhs != i.rhs) return i;
				} else return i;
				return false;
			})
			.filter(i => exclude.every(x => i.path.indexOf(x) === -1));
	}
 
	if (diffs.length > 0) {
		return diffs;
	}
	return null;
};
 
const diffToString = val => {
	if (typeof val === 'undefined' || val === null) {
		return '';
	}
	if (val === true) {
		return '1';
	}
	if (val === false) {
		return '0';
	}
	if (typeof val === 'string') {
		return val;
	}
	if (!Number.isNaN(Number(val))) {
		return `${String(val)}`;
	}
	if (typeof val === 'object') {
		return `${JSON.stringify(val)}`;
	}
	return '';
};
 
export default {
	capitalizeFirstLetter,
	toUnderscored,
	calcDelta,
	diffToString,
};