render-script 1.2.0 → 1.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "render-script",
3
- "version": "1.2.0",
4
- "main": "./src/index.cjs",
3
+ "version": "1.2.1",
4
+ "main": "./src/index.mjs",
5
5
  "type": "module",
6
6
  "author": {
7
7
  "name": "Renderapp",
package/src/evaluate.cjs CHANGED
@@ -1,11 +1,142 @@
1
- require('@babel/register')({
2
- ignore: [
3
- /node_modules\/(?!render-script)/
4
- ]
5
- })
1
+ /*!
2
+ * renderapp.js
3
+ * http://renderapp.co
4
+ *
5
+ * Copyright 2021 Renderapp Limited
6
+ */
6
7
 
7
- const evaluate = require('./evaluate.mjs')
8
+ const debug = require('debug')
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 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
+ function getRuleMap (ruleMap = RULE_MAP) {
101
+ return ruleMap
102
+ }
103
+
104
+ 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
+ function evaluate (rule = '', configuration = '') {
130
+ /**
131
+ * log('evaluate')
132
+ */
133
+
134
+ return isTruthy(rule) ? isTruthy(configuration) ? getEvaluationFor(rule, configuration) : false : false
135
+ }
8
136
 
9
137
  module.exports = {
10
- ...evaluate
138
+ isTruthy,
139
+ getRuleMap,
140
+ getConfigurationMap,
141
+ evaluate
11
142
  }
package/src/index.cjs CHANGED
@@ -1,11 +1,35 @@
1
- require('@babel/register')({
2
- ignore: [
3
- /node_modules\/(?!render-script)/
4
- ]
5
- })
6
-
7
1
  const {
8
- default: evaluate
9
- } = require('./index.mjs')
2
+ isTruthy,
3
+ getRuleMap,
4
+ getConfigurationMap,
5
+ evaluate: evaluateRuleForConfiguration
6
+ } = require('./evaluate.cjs')
7
+
8
+ function getEvaluation (configurationMap = new Map(), rule = '', configuration = '') {
9
+ /**
10
+ * log('getEvaluation')
11
+ */
12
+
13
+ let evaluation
14
+
15
+ /**
16
+ * Does the `configurationMap` contain the `configuration` argument as a key?
17
+ */
18
+ if (configurationMap.has(configuration)) {
19
+ /**
20
+ * It does! Get the evaluation from the `configurationMap`
21
+ */
22
+ evaluation = configurationMap.get(configuration)
23
+ } else {
24
+ /**
25
+ * It doesn't. Get the evaluation from `evaluateRuleForConfiguration` and set it into the `configurationMap`
26
+ */
27
+ configurationMap.set(configuration, (evaluation = evaluateRuleForConfiguration(rule, configuration)))
28
+ }
29
+
30
+ return evaluation
31
+ }
10
32
 
11
- module.exports = evaluate
33
+ module.exports = function evaluate (rule = '', configuration = '') {
34
+ return isTruthy(rule) ? isTruthy(configuration) ? getEvaluation(getConfigurationMap(getRuleMap(), rule), rule, configuration) : false : false
35
+ }