Skip to content

Commit 7a35d84

Browse files
committed
add 1.0.0 release, regex as pattern, wrap comparison functions
1 parent 64da0db commit 7a35d84

File tree

4 files changed

+87
-21
lines changed

4 files changed

+87
-21
lines changed

lib/compare.ts

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,55 @@
11
/* eslint-disable sonarjs/cognitive-complexity */
2-
import { isArray, isObject, isPrimitive, isString, isUndefined, isNumber, getType, isEmptyObject } from './types';
2+
import {
3+
isArray,
4+
isObject,
5+
isPrimitive,
6+
isString,
7+
isUndefined,
8+
isNumber,
9+
getType,
10+
isEmptyObject,
11+
isRegExp,
12+
} from './types';
313
import { execNumberExpression, toArray, safeHasOwnPropery } from './utils';
14+
import { getRandomString } from './randomizer';
15+
16+
const { toDataIncludes, checkThatDataIncludes, removeDataIncludesId } = (function () {
17+
const checkIncludesId = `${getRandomString(7)}_check_number=`;
18+
19+
return {
20+
toDataIncludes: (item: string) => `${checkIncludesId}${item}`,
21+
checkThatDataIncludes: (item: string) => item.indexOf(checkIncludesId) === 0,
22+
removeDataIncludesId: (item: string) => item.replace(checkIncludesId, ''),
23+
};
24+
})();
25+
26+
const { toPatternIncludes, checkThatPatternIncludes, removePatternIncludesId } = (function () {
27+
const patternIncludesId = `${getRandomString(7)}_pattern_includes=`;
28+
29+
return {
30+
toPatternIncludes: (item: string) => `${patternIncludesId}${item}`,
31+
checkThatPatternIncludes: (item: string) => item.indexOf(patternIncludesId) === 0,
32+
removePatternIncludesId: (item: string) => item.replace(patternIncludesId, ''),
33+
};
34+
})();
35+
36+
const { toCheckNumber, checkThatCheckNumber, removeCheckNumberId } = (function () {
37+
const patternIncludesId = `${getRandomString(7)}_check_number=`;
38+
39+
return {
40+
toCheckNumber: (item: string) => `${patternIncludesId}${item}`,
41+
checkThatCheckNumber: (item: string) => item.indexOf(patternIncludesId) === 0,
42+
removeCheckNumberId: (item: string) => item.replace(patternIncludesId, ''),
43+
};
44+
})();
445

546
function getErrorMessage(data, piece) {
6-
if (isNumber(data) && isString(piece) && piece.indexOf('_check_number=') === 0) {
7-
return `expected: ${piece.replace('_check_number=', '').trim()}, actual: ${data}`;
8-
} else if (isString(data) && isString(piece) && piece.indexOf('_pattern_includes=') === 0) {
9-
return `pattern does not include data expected: ${piece.replace('_pattern_includes=', '')} to include ${data}`;
10-
} else if (isString(data) && isString(piece) && piece.indexOf('_data_includes=') === 0) {
11-
return `data does not include pattern expected: ${piece.replace('_data_includes=', '')} to be part of ${data}`;
47+
if (isNumber(data) && isString(piece) && checkThatCheckNumber(piece)) {
48+
return `expected: ${removeCheckNumberId(piece).trim()}, actual: ${data}`;
49+
} else if (isString(data) && isString(piece) && checkThatPatternIncludes(piece)) {
50+
return `pattern does not include data expected: ${removePatternIncludesId(piece)} to include ${data}`;
51+
} else if (isString(data) && isString(piece) && checkThatDataIncludes(piece)) {
52+
return `data does not include pattern expected: ${removeDataIncludesId(piece)} to be part of ${data}`;
1253
} else if (getType(data) !== getType(piece)) {
1354
return `data should match to pattern expected: ${getType(piece)} ${piece}, actual: ${getType(data)} ${data}`;
1455
}
@@ -34,7 +75,13 @@ type TCompareOpts = {
3475
ignoreProperties?: string | string[];
3576
};
3677

37-
function compareToPattern(dataToCheck, pattern, options?: TCompareOpts) {
78+
type TCompareToPattern = ((data: any, patter: any, options?: TCompareOpts) => { result: boolean; message: string }) & {
79+
toDataIncludes: (arg: string) => string;
80+
toPatternIncludes: (arg: string) => string;
81+
toCheckNumber: (arg: string) => string;
82+
};
83+
84+
const compareToPattern: TCompareToPattern = function (dataToCheck, pattern, options?: TCompareOpts) {
3885
const {
3986
separator = '->',
4087
ignoreProperties,
@@ -52,14 +99,16 @@ function compareToPattern(dataToCheck, pattern, options?: TCompareOpts) {
5299

53100
if (allowNumberTypecast && ((isNumber(data) && isString(piece)) || (isNumber(piece) && isString(data)))) {
54101
compareResult = data == piece;
55-
} else if (isNumber(data) && isString(piece) && piece.indexOf('_check_number=') === 0) {
56-
compareResult = execNumberExpression(piece.replace('_check_number=', '').trim(), data);
57-
} else if (isString(data) && isString(piece) && piece.indexOf('_pattern_includes=') === 0) {
58-
compareResult = piece.replace('_pattern_includes=', '').includes(data);
59-
} else if (isString(data) && isString(piece) && piece.indexOf('_data_includes=') === 0) {
60-
compareResult = data.includes(piece.replace('_data_includes=', ''));
102+
} else if (isNumber(data) && isString(piece) && checkThatCheckNumber(piece)) {
103+
compareResult = execNumberExpression(removeCheckNumberId(piece).trim(), data);
104+
} else if (isString(data) && isString(piece) && checkThatPatternIncludes(piece)) {
105+
compareResult = removePatternIncludesId(piece).includes(data);
106+
} else if (isString(data) && isString(piece) && checkThatDataIncludes(piece)) {
107+
compareResult = data.includes(removeDataIncludesId(piece));
61108
} else if (isString(data) && isString(piece) && stringIncludes) {
62109
compareResult = data.includes(piece);
110+
} else if (isString(data) && isRegExp(piece)) {
111+
compareResult = (piece as RegExp).test(data);
63112
} else {
64113
compareResult = data === piece;
65114
}
@@ -175,6 +224,10 @@ function compareToPattern(dataToCheck, pattern, options?: TCompareOpts) {
175224
}
176225

177226
return { result, message };
178-
}
227+
} as any;
228+
229+
compareToPattern.toCheckNumber = toCheckNumber;
230+
compareToPattern.toDataIncludes = toDataIncludes;
231+
compareToPattern.toPatternIncludes = toPatternIncludes;
179232

180233
export { compareToPattern };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sat-utils",
3-
"version": "0.3.2",
3+
"version": "1.0.0",
44
"description": "Utils library",
55
"main": "built/index.js",
66
"scripts": {

readme.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,19 @@ const { compareToPattern } = require('sat-utils');
363363
b: { c: { d: { text: 'second' } } },
364364
};
365365

366+
const pattern = {
367+
b: { c: { d: { text: /.*/ } } },
368+
};
369+
370+
const { result, message } = compareToPattern(data, pattern);
371+
// result is true, message is ''
372+
}
373+
{
374+
const data = {
375+
a: { text: 'first' },
376+
b: { c: { d: { text: 'second' } } },
377+
};
378+
366379
const pattern = {
367380
b: { c: { d: { text: 'second' } } },
368381
};

specs/compare.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('SPEC', function () {
8484
it('[P] compareToPattern _check_number', function () {
8585
{
8686
const pattern = {
87-
field: '_check_number= > 10',
87+
field: compareToPattern.toCheckNumber('> 10'),
8888
};
8989
const data = {
9090
field: 11,
@@ -96,7 +96,7 @@ describe('SPEC', function () {
9696
}
9797
{
9898
const pattern = {
99-
field: '_check_number= > 10 and < 12',
99+
field: compareToPattern.toCheckNumber('> 10 and < 12'),
100100
};
101101
const data = {
102102
field: 11,
@@ -111,7 +111,7 @@ describe('SPEC', function () {
111111
it('[N] compareToPattern _check_number', function () {
112112
{
113113
const pattern = {
114-
field: '_check_number= > 11',
114+
field: compareToPattern.toCheckNumber(' > 11'),
115115
};
116116
const data = {
117117
field: 11,
@@ -209,7 +209,7 @@ describe('SPEC', function () {
209209
const pattern = {
210210
c: {
211211
length: 1,
212-
a: '_data_includes=different part',
212+
a: compareToPattern.toDataIncludes('different part'),
213213
},
214214
};
215215

@@ -226,7 +226,7 @@ describe('SPEC', function () {
226226
const pattern = {
227227
c: {
228228
length: 1,
229-
a: '_pattern_includes=long string with different parts',
229+
a: compareToPattern.toPatternIncludes('long string with different parts'),
230230
},
231231
};
232232

0 commit comments

Comments
 (0)