1
1
2
2
export const byNameCaseInsensitive = ( name ?: string ) => {
3
- return < TObject extends { name ?: string } > ( object : TObject ) => {
3
+ return < TObject extends { name ?: string } > ( object : TObject ) => {
4
4
if ( object ?. name && name ) {
5
5
return object . name . toLocaleLowerCase ( ) . localeCompare ( name . toLocaleLowerCase ( ) ) === 0 ;
6
6
}
@@ -9,7 +9,7 @@ export const byNameCaseInsensitive = (name?: string) => {
9
9
}
10
10
11
11
export const byClassCaseInsensitive = ( className ?: string ) => {
12
- return < TObject extends { class ?: string } > ( object : TObject ) => {
12
+ return < TObject extends { class ?: string } > ( object : TObject ) => {
13
13
if ( object ?. class && className ) {
14
14
return object . class . toLocaleLowerCase ( ) . localeCompare ( className . toLocaleLowerCase ( ) ) === 0 ;
15
15
}
@@ -20,17 +20,13 @@ export const byClassCaseInsensitive = (className?: string) => {
20
20
const copyPropsLowerCase = ( properties : Map < string , string | number | boolean > ) => {
21
21
const lowercase = new Map < string , string | number | boolean > ( ) ;
22
22
for ( let [ key , value ] of properties ) {
23
- let normalizedValue = value ;
24
- if ( typeof value === 'string' ) {
25
- normalizedValue = value . toLocaleLowerCase ( ) ;
26
- }
27
- lowercase . set ( key . toLocaleLowerCase ( ) , normalizedValue ) ;
23
+ lowercase . set ( key . toLocaleLowerCase ( ) , value ) ;
28
24
}
29
25
return lowercase ;
30
26
}
31
27
32
28
export const byPropertyCaseInsensitive = ( propertyName : string , value ?: any ) => {
33
- return < TObject extends { properties : Map < string , string | number | boolean > } > ( object : TObject ) => {
29
+ return < TObject extends { properties : Map < string , string | number | boolean > } > ( object : TObject ) => {
34
30
const lowercase = copyPropsLowerCase ( object . properties ) ;
35
31
36
32
if ( value !== undefined ) {
@@ -39,9 +35,43 @@ export const byPropertyCaseInsensitive = (propertyName: string, value?: any) =>
39
35
normalizedValue = value . toLocaleLowerCase ( ) ;
40
36
}
41
37
38
+ const maybeValue = lowercase . get ( propertyName . toLocaleLowerCase ( ) ) ;
39
+ if ( typeof maybeValue === 'string' ) {
40
+ return maybeValue . toLocaleLowerCase ( ) === normalizedValue ;
41
+ }
42
+
43
+ return lowercase . get ( propertyName . toLocaleLowerCase ( ) ) === normalizedValue ;
44
+ } else {
45
+ return lowercase . has ( propertyName . toLocaleLowerCase ( ) ) ;
46
+ }
47
+ }
48
+ }
49
+
50
+ export const byProperty = ( propertyName : string , value ?: any , valueMatchInsensitve = true ) => {
51
+ return < TObject extends { properties : Map < string , string | number | boolean > } > ( object : TObject ) => {
52
+ const lowercase = copyPropsLowerCase ( object . properties ) ;
53
+
54
+ if ( value !== undefined ) {
55
+ let normalizedValue = value ;
56
+ if ( typeof value === 'string' ) {
57
+ normalizedValue = valueMatchInsensitve ? value . toLocaleLowerCase ( ) : value ;
58
+ }
59
+
60
+ const maybeValue = lowercase . get ( propertyName . toLocaleLowerCase ( ) ) ;
61
+ if ( typeof maybeValue === 'string' ) {
62
+ return ( valueMatchInsensitve ? maybeValue . toLocaleLowerCase ( ) : maybeValue ) === normalizedValue ;
63
+ }
64
+
42
65
return lowercase . get ( propertyName . toLocaleLowerCase ( ) ) === normalizedValue ;
43
66
} else {
44
67
return lowercase . has ( propertyName . toLocaleLowerCase ( ) ) ;
45
68
}
46
69
}
47
- }
70
+ }
71
+
72
+ export const byPropertyMatcher = ( propertyName : string , matchValue : ( val : any ) => boolean ) => {
73
+ return < TObject extends { properties : Map < string , string | number | boolean > } > ( object : TObject ) => {
74
+ const lowercase = copyPropsLowerCase ( object . properties ) ;
75
+ return matchValue ( lowercase . get ( propertyName . toLocaleLowerCase ( ) ) ) ;
76
+ }
77
+ }
0 commit comments