tpmkms 9.7.1-beta.13 → 9.7.1-beta.14
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/common/helpers/gdefaults.js +143 -0
- package/package.json +3 -2
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
const helpers = require('../helpers')
|
|
2
|
+
|
|
3
|
+
function interpolate(args) {
|
|
4
|
+
return async (interpolate, context) => {
|
|
5
|
+
async function evaluator(key) {
|
|
6
|
+
if (Array.isArray(context[key])) {
|
|
7
|
+
return args.gsp(context[key])
|
|
8
|
+
} else {
|
|
9
|
+
return args.gp(context[key])
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function getValue(keyOrValue) {
|
|
13
|
+
if (typeof keyOrValue == 'string' && context[keyOrValue]) {
|
|
14
|
+
return context[keyOrValue]
|
|
15
|
+
}
|
|
16
|
+
return keyOrValue // it's a value
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (Array.isArray(interpolate)) {
|
|
20
|
+
const strings = []
|
|
21
|
+
let separator = ''
|
|
22
|
+
const byPosition = []
|
|
23
|
+
let number;
|
|
24
|
+
for (const [elementIndex, element] of interpolate.entries()) {
|
|
25
|
+
const isLastElement = elementIndex == interpolate.length - 1
|
|
26
|
+
// { "word": { "marker": "canPassive" } ie { word: <selectionCriteria> }
|
|
27
|
+
async function handleElement(context, element) {
|
|
28
|
+
if (element.context?.invisible) {
|
|
29
|
+
return
|
|
30
|
+
}
|
|
31
|
+
if (element.word) {
|
|
32
|
+
const word = args.getWordFromDictionary(element.word) || element.word
|
|
33
|
+
if (word) {
|
|
34
|
+
strings.push(separator)
|
|
35
|
+
strings.push(await args.gp(word))
|
|
36
|
+
separator = ' '
|
|
37
|
+
}
|
|
38
|
+
} else if (typeof element == 'string') {
|
|
39
|
+
separator = element
|
|
40
|
+
} else if (element.separator && element.values) {
|
|
41
|
+
let ctr = 0
|
|
42
|
+
const values = getValue(element.values)
|
|
43
|
+
const vstrings = []
|
|
44
|
+
for (const value of values) {
|
|
45
|
+
if (ctr == values.length-1) {
|
|
46
|
+
vstrings.push(getValue(element.separator))
|
|
47
|
+
}
|
|
48
|
+
ctr += 1
|
|
49
|
+
vstrings.push(getValue(value))
|
|
50
|
+
}
|
|
51
|
+
strings.push(await args.gsp(vstrings))
|
|
52
|
+
} else if (element.semantic) {
|
|
53
|
+
const wordContext = {}
|
|
54
|
+
for (const term of element.semantic) {
|
|
55
|
+
if (term.property) {
|
|
56
|
+
Object.assign(wordContext, context[term.property])
|
|
57
|
+
} else if (term.overrides) {
|
|
58
|
+
Object.assign(wordContext, term.overrides)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const value = await args.gp(wordContext) //, { options: { debug: { apply: true } } })
|
|
62
|
+
if (value) {
|
|
63
|
+
strings.push(separator)
|
|
64
|
+
strings.push(await args.gp(value))
|
|
65
|
+
separator = ' '
|
|
66
|
+
}
|
|
67
|
+
} else if (element.property) {
|
|
68
|
+
value = context[element.property]
|
|
69
|
+
if (value) {
|
|
70
|
+
if (element.context) {
|
|
71
|
+
value = { ...value, ...element.context }
|
|
72
|
+
}
|
|
73
|
+
async function handleProperty(value) {
|
|
74
|
+
strings.push(separator)
|
|
75
|
+
if (element.isQuantified && number && isLastElement) {
|
|
76
|
+
value.number = number
|
|
77
|
+
}
|
|
78
|
+
if (Array.isArray(value)) {
|
|
79
|
+
strings.push(await args.gsp(value))
|
|
80
|
+
} else {
|
|
81
|
+
strings.push(await args.gp(value))
|
|
82
|
+
}
|
|
83
|
+
separator = ' '
|
|
84
|
+
}
|
|
85
|
+
if (element.isQuantifier) {
|
|
86
|
+
number = helpers.isMany({ quantity: value }) ? "many": "one"
|
|
87
|
+
}
|
|
88
|
+
if (element.byPosition) {
|
|
89
|
+
const element = { start: value.range.start, insert: ((value) => () => handleProperty(value))(value) }
|
|
90
|
+
if (byPosition.length == 0) {
|
|
91
|
+
byPosition.push(element)
|
|
92
|
+
} else {
|
|
93
|
+
const index = byPosition.findIndex((element) => element.start < value.range.start)
|
|
94
|
+
if (index == -1) {
|
|
95
|
+
byPosition.unshift(element)
|
|
96
|
+
} else {
|
|
97
|
+
byPosition.splice(index+1, 0, element)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
await handleProperty(value)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
} else if (element.context) {
|
|
105
|
+
let value = element.context
|
|
106
|
+
if (element.property) {
|
|
107
|
+
value = context[element.property]
|
|
108
|
+
if (element.context) {
|
|
109
|
+
Object.assign(value, element.context)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (value?.form !== 'infinitive' && element.number) {
|
|
113
|
+
value.number = helpers.isMany(context[element.number]) ? "many": "one"
|
|
114
|
+
}
|
|
115
|
+
if (value) {
|
|
116
|
+
strings.push(separator)
|
|
117
|
+
strings.push(await args.gp(value))
|
|
118
|
+
separator = ' '
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let currentContext = context
|
|
124
|
+
let currentElement = element
|
|
125
|
+
while (currentElement.inside) {
|
|
126
|
+
currentContext = currentContext[currentElement.inside]
|
|
127
|
+
currentElement = currentElement.value
|
|
128
|
+
}
|
|
129
|
+
await handleElement(currentContext, currentElement)
|
|
130
|
+
}
|
|
131
|
+
for (const { insert } of byPosition) {
|
|
132
|
+
await insert()
|
|
133
|
+
}
|
|
134
|
+
return strings.join('')
|
|
135
|
+
} else {
|
|
136
|
+
return await helpers.processTemplateString(interpolate, evaluator)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
module.exports = {
|
|
142
|
+
interpolate
|
|
143
|
+
}
|
package/package.json
CHANGED
|
@@ -254,6 +254,7 @@
|
|
|
254
254
|
"common/helpers/drone.js",
|
|
255
255
|
"common/helpers/formulas.js",
|
|
256
256
|
"common/helpers/frankenhash.js",
|
|
257
|
+
"common/helpers/gdefaults.js",
|
|
257
258
|
"common/helpers/menus.js",
|
|
258
259
|
"common/helpers/meta.js",
|
|
259
260
|
"common/helpers/ordering.js",
|
|
@@ -392,8 +393,8 @@
|
|
|
392
393
|
"scriptjs": "^2.5.9",
|
|
393
394
|
"table": "^6.7.1",
|
|
394
395
|
"uuid": "^9.0.0",
|
|
395
|
-
"theprogrammablemind": "9.7.1-beta.
|
|
396
|
+
"theprogrammablemind": "9.7.1-beta.14"
|
|
396
397
|
},
|
|
397
|
-
"version": "9.7.1-beta.
|
|
398
|
+
"version": "9.7.1-beta.14",
|
|
398
399
|
"license": "UNLICENSED"
|
|
399
400
|
}
|