is-something-equal
v0.1.3
Published
This module is designed to conceptually compare values.
Downloads
4
Maintainers
Readme
Intro
This module is designed to conceptually compare values. For example, if we try to compare objects with each other we always get false. But with this utility you can compare objects or other values and get true if they are conceptually same.
Module was developed because the author is faced with the problem of comparing objects in his project and the functionality is for the most part implemented precisely for these purposes.
Usage
Presently, module consists of one function: isEqual. First two arguments its values for comparison. Then you can pass object with settings for this comparison.
Settings:
fieldsToSkipInObjects [default: none] - an option where you can list the fields that should be skipped when comparing objects (array of strings). For example, you have different fields createdAt or updatedAt, but otherwise the objects are the same.
deepSkipping [default: true] - an option that is relevant only if you specify fieldsToSkipInObjects. By using it, you can specify whether to skip options only at the first level of nesting, or at all. By default it will skip all fields from first option on all levels.
isArrayOrderImportant [default: true] - an option, that shows is important the order of the elements in the array or not. If false, [1, 2, 3] will be equal to [3, 2, 1]. By default - not.
limitArrayCompare [default: unlimited] - an option that you can use if you need to slice each array from 0 element to to the specified value.
nullEqualsToUndefined [default: false] - all comparisons that uses in module are strict, so by default null is not equal to undefined.
stringsToFixedCase [default: false] - if you need to make all strings to the same register.
regExpToString [default: false] - an option, that you can use if you wanna compare some RegExp as string.
compareNot [default: none] - list of types that you don't want to compare in objects (array of strings). Available types are:
- number,
- string,
- boolean,
- object,
- undefined,
- function,
- array,
- regexp,
- null,
- symbol,
- date.
If you will not use option strongCompareNot, module will compare values just by their types. So, if compareNot = ['array'] and strongCompareNot is not defined, arrays [1, 2, 3] and [ {first: 'field'} ] will be equal. But if the array and object will be compared, it will be false.
strongCompareNot [default: false] - if you set it true, when module find in the same field array and object or null and number, etc, (in different objects that you are comparing), it will return true. It will skip all unavailable types as true.
objectToValue [default: false] - an option< that can be used if you need to get from String object, Number object or Boolean object their values and only after compare it.
Ok, if it wasn't too strange and you still wanna use it, lets see example.
Example
'use strict';
const isEqual = require('is-something-equal');
const test1 = {
test: null,
toSkip: 'str1',
first: {width: 320, params: ['-thumbnail', [1, 3, 2], `320x320^`]},
second: [3, [null, {first: [{second: 'sss'}], date: Date.now()}]],
reg: 'some text',
};
const test2 = {
test: undefined,
toSkip: 'str2',
first: {width: 320, params: [`320x320^`, [1, 2, 3], '-thumbnail']},
second: [3, [null, {first: [{second: 'sss'}], date: Date.now()}]],
reg: new RegExp('some text'),
};
console.log(isEqual(5, 5)); // true
console.log(isEqual({first: 1, second: 2}, {second: 2, first: 1})); // true
console.log(isEqual(test1, test2)); // false
console.log(isEqual(test1, test2, {
fieldsToSkipInObjects: ['toSkip'],
isArrayOrderImportant: false,
regExpToString: true,
nullEqualsToUndefined: true
})); // true
const test3 = {
first: [1, 2, 3],
second: new Number(5)
};
const test4 = {
first: { field: 'some field' },
second: new Number(5)
};
console.log(isEqual(test3, test4)); // false
console.log(isEqual(test3, test4, {
objectToValue: true,
compareNot: ['array']
})); // false
console.log(isEqual(test3, test4, {
objectToValue: true,
strongCompareNot: true,
compareNot: ['array']
})); // true
Finally
If you have some ideas how to improve my code, some advice's or features to add, please, contact me [email protected].
P.S. While module is in development, I do not guarantee full version compatibility.