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' ;
313import { 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
546function 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
180233export { compareToPattern } ;
0 commit comments