MCPcopy Create free account
hub / github.com/SmartTokenLabs/TokenScript / compareStringToProps

Function compareStringToProps

bootstrap-js/modules/ts_helpers.js:8–122  ·  view source on GitHub ↗
(str, props, debug = false)

Source from the content-addressed store, hash-verified

6 * @returns {string}
7 */
8export function compareStringToProps(str, props, debug = false){
9 if (!str || !str.trim()) {
10 debug && console.log('empty filter');
11 return 1;
12 }
13 let match = str.matchAll(/(\([^)(]+\))/g);
14 let matchAll = Array.from(match);
15 debug && console.log('matchAll');
16 debug && console.log(matchAll);
17 matchAll.forEach(item=>{
18 let single = item[1];
19 // console.log('wrong pattern : ' + item + ', skipped');
20 let matchProp = single.match(/\(([^<>=]+)(=|<=|>=)([^<>=]+)\)/);
21 if (!matchProp) {
22 str = str.replace(single,0);
23 debug && console.log('wrong pattern : ' + single + ', skipped, str = "'+ str +'"');
24 return;
25 }
26 debug && console.log('matchProp');
27 debug && console.log(matchProp);
28 let propName = matchProp[1].trim();
29 let propCompare = matchProp[2];
30 let propTest = matchProp[3].trim();
31 let propValue = props[propName];
32
33 if (!propValue) {
34 debug && console.log('property check false(prop not exists or empty) for: '+ propName + ', while serve '+item[0]);
35 str = str.replace(item[0],'0');
36 } else {
37 // let compareRes = propValue.match(//);
38 let result;
39 switch (propCompare) {
40 case '=':
41 if (propTest.trim() == '*') {
42 result = '1';
43 break;
44 }
45 // quote all regex symbols except of *
46 var re = new RegExp(propTest.replace(/([.?+^$[\]\\(){}|-])/g, "\\$1"));
47 result = propValue.match(re);
48 break;
49 case "<=":
50 result = parseFloat(propValue) < parseFloat(propTest);
51 debug && console.log('propValue = '+ parseFloat(propValue) + " ; propTest = " + parseFloat(propTest) + "; compare = " + propCompare + " ; result = "+result);
52 break;
53 case ">=":
54 result = parseFloat(propValue) > parseFloat(propTest);
55 break;
56 default:
57 debug && console.log('compare string error. wrong part: '+matchProp[0]+' at the moment: '+str);
58 str = "-1";
59 }
60 str = str.replace(item[0], result ? '1' : '0');
61 }
62 debug && console.log('compare string result after : '+single+' = '+str);
63 })
64
65 let counter = 0;

Callers 2

renderActivityItemMethod · 0.90
negotiateFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected