UNPKG

2.2 kBJavaScriptView Raw
1import * as is from '../is';
2
3export const valCmp = (fieldVal, operator, value) => {
4 let matches;
5 let isFieldStr = is.string( fieldVal );
6 let isFieldNum = is.number( fieldVal );
7 let isValStr = is.string(value);
8 let fieldStr, valStr;
9 let caseInsensitive = false;
10 let notExpr = false;
11 let isIneqCmp = false;
12
13 if( operator.indexOf( '!' ) >= 0 ){
14 operator = operator.replace( '!', '' );
15 notExpr = true;
16 }
17
18 if( operator.indexOf( '@' ) >= 0 ){
19 operator = operator.replace( '@', '' );
20 caseInsensitive = true;
21 }
22
23 if( isFieldStr || isValStr || caseInsensitive ){
24 fieldStr = !isFieldStr && !isFieldNum ? '' : '' + fieldVal;
25 valStr = '' + value;
26 }
27
28 // if we're doing a case insensitive comparison, then we're using a STRING comparison
29 // even if we're comparing numbers
30 if( caseInsensitive ){
31 fieldVal = fieldStr = fieldStr.toLowerCase();
32 value = valStr = valStr.toLowerCase();
33 }
34
35 switch( operator ){
36 case '*=':
37 matches = fieldStr.indexOf( valStr ) >= 0;
38 break;
39 case '$=':
40 matches = fieldStr.indexOf( valStr, fieldStr.length - valStr.length ) >= 0;
41 break;
42 case '^=':
43 matches = fieldStr.indexOf( valStr ) === 0;
44 break;
45 case '=':
46 matches = fieldVal === value;
47 break;
48 case '>':
49 isIneqCmp = true;
50 matches = fieldVal > value;
51 break;
52 case '>=':
53 isIneqCmp = true;
54 matches = fieldVal >= value;
55 break;
56 case '<':
57 isIneqCmp = true;
58 matches = fieldVal < value;
59 break;
60 case '<=':
61 isIneqCmp = true;
62 matches = fieldVal <= value;
63 break;
64 default:
65 matches = false;
66 break;
67 }
68
69 // apply the not op, but null vals for inequalities should always stay non-matching
70 if( notExpr && ( fieldVal != null || !isIneqCmp ) ){
71 matches = !matches;
72 }
73
74 return matches;
75};
76
77export const boolCmp = (fieldVal, operator) => {
78 switch( operator ){
79 case '?':
80 return fieldVal ? true : false;
81 case '!':
82 return fieldVal ? false : true;
83 case '^':
84 return fieldVal === undefined;
85 }
86};
87
88export const existCmp = (fieldVal) => fieldVal !== undefined;
89
90export const data = (ele, field) => ele.data(field);
91
92export const meta = (ele, field) => ele[field]();
\No newline at end of file