1- import { isMatchingPattern , truncate } from '../src/string' ;
1+ import { isMatchingPattern , stringMatchesSomePattern , truncate } from '../src/string' ;
22
33describe ( 'truncate()' , ( ) => {
44 test ( 'it works as expected' , ( ) => {
@@ -18,13 +18,31 @@ describe('truncate()', () => {
1818} ) ;
1919
2020describe ( 'isMatchingPattern()' , ( ) => {
21- test ( 'match using string substring' , ( ) => {
21+ test ( 'match using string substring if `requireExactStringMatch` not given ' , ( ) => {
2222 expect ( isMatchingPattern ( 'foobar' , 'foobar' ) ) . toEqual ( true ) ;
2323 expect ( isMatchingPattern ( 'foobar' , 'foo' ) ) . toEqual ( true ) ;
2424 expect ( isMatchingPattern ( 'foobar' , 'bar' ) ) . toEqual ( true ) ;
2525 expect ( isMatchingPattern ( 'foobar' , 'nope' ) ) . toEqual ( false ) ;
2626 } ) ;
2727
28+ test ( 'match using string substring if `requireExactStringMatch` is `false`' , ( ) => {
29+ expect ( isMatchingPattern ( 'foobar' , 'foobar' , false ) ) . toEqual ( true ) ;
30+ expect ( isMatchingPattern ( 'foobar' , 'foo' , false ) ) . toEqual ( true ) ;
31+ expect ( isMatchingPattern ( 'foobar' , 'bar' , false ) ) . toEqual ( true ) ;
32+ expect ( isMatchingPattern ( 'foobar' , 'nope' , false ) ) . toEqual ( false ) ;
33+ } ) ;
34+
35+ test ( 'match using exact string match if `requireExactStringMatch` is `true`' , ( ) => {
36+ expect ( isMatchingPattern ( 'foobar' , 'foobar' , true ) ) . toEqual ( true ) ;
37+ expect ( isMatchingPattern ( 'foobar' , 'foo' , true ) ) . toEqual ( false ) ;
38+ expect ( isMatchingPattern ( 'foobar' , 'nope' , true ) ) . toEqual ( false ) ;
39+ } ) ;
40+
41+ test ( 'matches when `value` constains `pattern` but not vice-versa' , ( ) => {
42+ expect ( isMatchingPattern ( 'foobar' , 'foo' ) ) . toEqual ( true ) ;
43+ expect ( isMatchingPattern ( 'foobar' , 'foobarbaz' ) ) . toEqual ( false ) ;
44+ } ) ;
45+
2846 test ( 'match using regexp test' , ( ) => {
2947 expect ( isMatchingPattern ( 'foobar' , / ^ f o o / ) ) . toEqual ( true ) ;
3048 expect ( isMatchingPattern ( 'foobar' , / f o o / ) ) . toEqual ( true ) ;
@@ -45,3 +63,48 @@ describe('isMatchingPattern()', () => {
4563 expect ( isMatchingPattern ( [ ] as any , 'foo' ) ) . toEqual ( false ) ;
4664 } ) ;
4765} ) ;
66+
67+ describe ( 'stringMatchesSomePattern()' , ( ) => {
68+ test ( 'match using string substring if `requireExactStringMatch` not given' , ( ) => {
69+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'foobar' , 'nope' ] ) ) . toEqual ( true ) ;
70+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'foo' , 'nope' ] ) ) . toEqual ( true ) ;
71+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'baz' , 'nope' ] ) ) . toEqual ( false ) ;
72+ } ) ;
73+
74+ test ( 'match using string substring if `requireExactStringMatch` is `false`' , ( ) => {
75+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'foobar' , 'nope' ] , false ) ) . toEqual ( true ) ;
76+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'foo' , 'nope' ] , false ) ) . toEqual ( true ) ;
77+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'baz' , 'nope' ] , false ) ) . toEqual ( false ) ;
78+ } ) ;
79+
80+ test ( 'match using exact string match if `requireExactStringMatch` is `true`' , ( ) => {
81+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'foobar' , 'nope' ] , true ) ) . toEqual ( true ) ;
82+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'foo' , 'nope' ] , true ) ) . toEqual ( false ) ;
83+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'baz' , 'nope' ] , true ) ) . toEqual ( false ) ;
84+ } ) ;
85+
86+ test ( 'matches when `testString` constains a pattern but not vice-versa' , ( ) => {
87+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'foo' , 'nope' ] ) ) . toEqual ( true ) ;
88+ expect ( stringMatchesSomePattern ( 'foobar' , [ 'foobarbaz' , 'nope' ] ) ) . toEqual ( false ) ;
89+ } ) ;
90+
91+ test ( 'match using regexp test' , ( ) => {
92+ expect ( stringMatchesSomePattern ( 'foobar' , [ / ^ f o o / , 'nope' ] ) ) . toEqual ( true ) ;
93+ expect ( stringMatchesSomePattern ( 'foobar' , [ / f o o / , 'nope' ] ) ) . toEqual ( true ) ;
94+ expect ( stringMatchesSomePattern ( 'foobar' , [ / b .{ 1 } r / , 'nope' ] ) ) . toEqual ( true ) ;
95+ expect ( stringMatchesSomePattern ( 'foobar' , [ / ^ f o o $ / , 'nope' ] ) ) . toEqual ( false ) ;
96+ } ) ;
97+
98+ test ( 'should match empty pattern as true' , ( ) => {
99+ expect ( stringMatchesSomePattern ( 'foo' , [ '' , 'nope' ] ) ) . toEqual ( true ) ;
100+ expect ( stringMatchesSomePattern ( 'bar' , [ '' , 'nope' ] ) ) . toEqual ( true ) ;
101+ expect ( stringMatchesSomePattern ( '' , [ '' , 'nope' ] ) ) . toEqual ( true ) ;
102+ } ) ;
103+
104+ test ( 'should bail out with false when given non-string value' , ( ) => {
105+ expect ( stringMatchesSomePattern ( null as any , [ 'foo' , 'nope' ] ) ) . toEqual ( false ) ;
106+ expect ( stringMatchesSomePattern ( undefined as any , [ 'foo' , 'nope' ] ) ) . toEqual ( false ) ;
107+ expect ( stringMatchesSomePattern ( { } as any , [ 'foo' , 'nope' ] ) ) . toEqual ( false ) ;
108+ expect ( stringMatchesSomePattern ( [ ] as any , [ 'foo' , 'nope' ] ) ) . toEqual ( false ) ;
109+ } ) ;
110+ } ) ;
0 commit comments