render-script 1.1.476 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -3
- package/src/evaluate.cjs +11 -0
- package/src/evaluate.mjs +135 -0
- package/src/index.cjs +4 -4
- package/src/index.mjs +11 -138
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "render-script",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"main": "./src/index.cjs",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -37,7 +37,13 @@
|
|
|
37
37
|
"mocha": "^10.2.0"
|
|
38
38
|
},
|
|
39
39
|
"exports": {
|
|
40
|
-
"
|
|
41
|
-
|
|
40
|
+
".": {
|
|
41
|
+
"import": "./src/index.mjs",
|
|
42
|
+
"require": "./src/index.cjs"
|
|
43
|
+
},
|
|
44
|
+
"./evaluate": {
|
|
45
|
+
"import": "./src/evaluate.mjs",
|
|
46
|
+
"require": "./src/evaluate.cjs"
|
|
47
|
+
}
|
|
42
48
|
}
|
|
43
49
|
}
|
package/src/evaluate.cjs
ADDED
package/src/evaluate.mjs
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* renderapp.js
|
|
3
|
+
* http://renderapp.co
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2021 Renderapp Limited
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import debug from 'debug'
|
|
9
|
+
|
|
10
|
+
const log = debug('render-script')
|
|
11
|
+
|
|
12
|
+
log('`render-script` is awake')
|
|
13
|
+
|
|
14
|
+
const RULE_MAP = new Map()
|
|
15
|
+
|
|
16
|
+
export const isTruthy = (v) => Boolean((v || '').trim())
|
|
17
|
+
|
|
18
|
+
function getEvaluationFor (rule, configuration) {
|
|
19
|
+
/**
|
|
20
|
+
* log('evaluate')
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
function isSubRuleValid (subRule) {
|
|
24
|
+
/**
|
|
25
|
+
* log('isSubRuleValid')
|
|
26
|
+
*/
|
|
27
|
+
return (
|
|
28
|
+
subRule.startsWith('!')
|
|
29
|
+
? configuration.includes('+' + subRule.substring(1)) !== true // does not include
|
|
30
|
+
: configuration.includes('+' + subRule)
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function isTopRuleValid (topRule) {
|
|
35
|
+
/**
|
|
36
|
+
* log('isTopRuleValid')
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Split the `topRule` argument on the `/` character
|
|
41
|
+
* into an array of `subRule` strings
|
|
42
|
+
*/
|
|
43
|
+
return topRule.split('/') // split `topRule` into an array of `subRule` strings
|
|
44
|
+
/**
|
|
45
|
+
* Remove any zero-length strings from the array
|
|
46
|
+
*/
|
|
47
|
+
.filter(isTruthy)
|
|
48
|
+
/**
|
|
49
|
+
* Some `subRule` strings must be valid for the
|
|
50
|
+
* configuration
|
|
51
|
+
*/
|
|
52
|
+
.some(isSubRuleValid) // At least one `subRule` must be valid
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isRuleSetValid (ruleSet) {
|
|
56
|
+
/**
|
|
57
|
+
* log('isRuleSetValid')
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Split the `ruleSet` argument on the `+` character
|
|
62
|
+
* into an array of `topRule` strings
|
|
63
|
+
*/
|
|
64
|
+
return ruleSet.split('+')
|
|
65
|
+
/**
|
|
66
|
+
* Remove any zero-length strings from the array
|
|
67
|
+
*/
|
|
68
|
+
.filter(isTruthy)
|
|
69
|
+
/**
|
|
70
|
+
* Every `topRule` string must be valid for the
|
|
71
|
+
* configuration
|
|
72
|
+
*/
|
|
73
|
+
.every(isTopRuleValid)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function evaluateRule (rule) {
|
|
77
|
+
/**
|
|
78
|
+
* log('evaluateRule')
|
|
79
|
+
*/
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Split the `rule` argument on the `;` character
|
|
83
|
+
* into an array of `ruleSet` strings
|
|
84
|
+
*/
|
|
85
|
+
return rule.split(';')
|
|
86
|
+
/**
|
|
87
|
+
* Remove any zero-length strings from the array
|
|
88
|
+
*/
|
|
89
|
+
.filter(isTruthy)
|
|
90
|
+
/**
|
|
91
|
+
* Some `ruleSet` strings must be valid for the
|
|
92
|
+
* configuration
|
|
93
|
+
*/
|
|
94
|
+
.some(isRuleSetValid)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return evaluateRule(rule)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function getRuleMap (ruleMap = RULE_MAP) {
|
|
101
|
+
return ruleMap
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function getConfigurationMap (ruleMap = new Map(), rule = '') {
|
|
105
|
+
/**
|
|
106
|
+
* log('getConfigurationMap')
|
|
107
|
+
*/
|
|
108
|
+
|
|
109
|
+
let configurationMap
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Does the `ruleMap` contain the `rule` argument as a key?
|
|
113
|
+
*/
|
|
114
|
+
if (ruleMap.has(rule)) {
|
|
115
|
+
/**
|
|
116
|
+
* It does! Get the `configurationMap` from the `ruleMap`
|
|
117
|
+
*/
|
|
118
|
+
configurationMap = ruleMap.get(rule)
|
|
119
|
+
} else {
|
|
120
|
+
/**
|
|
121
|
+
* It doesn't. Get a `Map` instance and set it into the `ruleMap`
|
|
122
|
+
*/
|
|
123
|
+
ruleMap.set(rule, (configurationMap = new Map()))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return configurationMap
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function evaluate (rule = '', configuration = '') {
|
|
130
|
+
/**
|
|
131
|
+
* log('evaluate')
|
|
132
|
+
*/
|
|
133
|
+
|
|
134
|
+
return isTruthy(rule) ? isTruthy(configuration) ? getEvaluationFor(rule, configuration) : false : false
|
|
135
|
+
}
|
package/src/index.cjs
CHANGED
package/src/index.mjs
CHANGED
|
@@ -1,140 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const log = debug('render-script')
|
|
11
|
-
|
|
12
|
-
log('`render-script` is awake')
|
|
13
|
-
|
|
14
|
-
const RULE_MAP = new Map()
|
|
15
|
-
|
|
16
|
-
const isTruthy = (v) => Boolean((v || '').trim())
|
|
17
|
-
|
|
18
|
-
function evaluate (rule, configuration) {
|
|
19
|
-
/**
|
|
20
|
-
* log('evaluate')
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
function isSubRuleValid (subRule) {
|
|
24
|
-
/**
|
|
25
|
-
* log('isSubRuleValid')
|
|
26
|
-
*/
|
|
27
|
-
return (
|
|
28
|
-
subRule.startsWith('!')
|
|
29
|
-
? configuration.includes('+' + subRule.substring(1)) !== true // does not include
|
|
30
|
-
: configuration.includes('+' + subRule)
|
|
31
|
-
)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function isTopRuleValid (topRule) {
|
|
35
|
-
/**
|
|
36
|
-
* log('isTopRuleValid')
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Split the `topRule` argument on the `/` character
|
|
41
|
-
* into an array of `subRule` strings
|
|
42
|
-
*/
|
|
43
|
-
return topRule.split('/') // split `topRule` into an array of `subRule` strings
|
|
44
|
-
/**
|
|
45
|
-
* Remove any zero-length strings from the array
|
|
46
|
-
*/
|
|
47
|
-
.filter(isTruthy)
|
|
48
|
-
/**
|
|
49
|
-
* Some `subRule` strings must be valid for the
|
|
50
|
-
* configuration
|
|
51
|
-
*/
|
|
52
|
-
.some(isSubRuleValid) // At least one `subRule` must be valid
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function isRuleSetValid (ruleSet) {
|
|
56
|
-
/**
|
|
57
|
-
* log('isRuleSetValid')
|
|
58
|
-
*/
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Split the `ruleSet` argument on the `+` character
|
|
62
|
-
* into an array of `topRule` strings
|
|
63
|
-
*/
|
|
64
|
-
return ruleSet.split('+')
|
|
65
|
-
/**
|
|
66
|
-
* Remove any zero-length strings from the array
|
|
67
|
-
*/
|
|
68
|
-
.filter(isTruthy)
|
|
69
|
-
/**
|
|
70
|
-
* Every `topRule` string must be valid for the
|
|
71
|
-
* configuration
|
|
72
|
-
*/
|
|
73
|
-
.every(isTopRuleValid)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function evaluateRule (rule) {
|
|
77
|
-
/**
|
|
78
|
-
* log('evaluateRule')
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Split the `rule` argument on the `;` character
|
|
83
|
-
* into an array of `ruleSet` strings
|
|
84
|
-
*/
|
|
85
|
-
return rule.split(';')
|
|
86
|
-
/**
|
|
87
|
-
* Remove any zero-length strings from the array
|
|
88
|
-
*/
|
|
89
|
-
.filter(isTruthy)
|
|
90
|
-
/**
|
|
91
|
-
* Some `ruleSet` strings must be valid for the
|
|
92
|
-
* configuration
|
|
93
|
-
*/
|
|
94
|
-
.some(isRuleSetValid)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
return evaluateRule(rule)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function evaluateRuleForConfiguration (rule = '', configuration = '') {
|
|
101
|
-
/**
|
|
102
|
-
* log('evaluateRuleForConfiguration')
|
|
103
|
-
*/
|
|
104
|
-
|
|
105
|
-
return isTruthy(rule) ? isTruthy(configuration) ? evaluate(rule, configuration) : false : false
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export function getRuleMap (ruleMap = RULE_MAP) {
|
|
109
|
-
return ruleMap
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export function getConfigurationMap (ruleMap = new Map(), rule = '') {
|
|
113
|
-
/**
|
|
114
|
-
* log('getConfigurationMap')
|
|
115
|
-
*/
|
|
116
|
-
|
|
117
|
-
let configurationMap
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Does the `ruleMap` contain the `rule` argument as a key?
|
|
121
|
-
*/
|
|
122
|
-
if (ruleMap.has(rule)) {
|
|
123
|
-
/**
|
|
124
|
-
* It does! Get the `configurationMap` from the `ruleMap`
|
|
125
|
-
*/
|
|
126
|
-
configurationMap = ruleMap.get(rule)
|
|
127
|
-
} else {
|
|
128
|
-
/**
|
|
129
|
-
* It doesn't. Get a `Map` instance and set it into the `ruleMap`
|
|
130
|
-
*/
|
|
131
|
-
ruleMap.set(rule, (configurationMap = new Map()))
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return configurationMap
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export function getEvaluation (configurationMap = new Map(), rule = '', configuration = '') {
|
|
1
|
+
import {
|
|
2
|
+
isTruthy,
|
|
3
|
+
getRuleMap,
|
|
4
|
+
getConfigurationMap,
|
|
5
|
+
evaluate as evaluateRuleForConfiguration
|
|
6
|
+
} from './evaluate.mjs'
|
|
7
|
+
|
|
8
|
+
function getEvaluation (configurationMap = new Map(), rule = '', configuration = '') {
|
|
138
9
|
/**
|
|
139
10
|
* log('getEvaluation')
|
|
140
11
|
*/
|
|
@@ -159,4 +30,6 @@ export function getEvaluation (configurationMap = new Map(), rule = '', configur
|
|
|
159
30
|
return evaluation
|
|
160
31
|
}
|
|
161
32
|
|
|
162
|
-
export default (rule = '', configuration = '')
|
|
33
|
+
export default function evaluate (rule = '', configuration = '') {
|
|
34
|
+
return isTruthy(rule) ? isTruthy(configuration) ? getEvaluation(getConfigurationMap(getRuleMap(), rule), rule, configuration) : false : false
|
|
35
|
+
}
|