@nxtedition/lib 13.2.3 → 14.0.0-alpha.2
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 +1 -1
- package/util/template/index.js +102 -49
- package/weakCache.js +4 -2
package/package.json
CHANGED
package/util/template/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const rx = require('rxjs/operators')
|
|
2
|
-
const
|
|
2
|
+
const rxjs = require('rxjs')
|
|
3
3
|
const fp = require('lodash/fp')
|
|
4
4
|
const getNxtpressionsCompiler = require('./nextpressions')
|
|
5
5
|
const getJavascriptCompiler = require('./javascript')
|
|
@@ -22,52 +22,86 @@ module.exports = (options) => {
|
|
|
22
22
|
try {
|
|
23
23
|
return compileObjectTemplate(obj)(...args)
|
|
24
24
|
} catch (err) {
|
|
25
|
-
return
|
|
25
|
+
return rxjs.throwError(err)
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
const compileArrayTemplate = weakCache(function compileArrayTemplate(arr) {
|
|
29
|
+
const compileArrayTemplateLazy = (arr) => {
|
|
31
30
|
if (!fp.isArray(arr)) {
|
|
32
31
|
throw new Error('invalid argument')
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
let resolvers
|
|
35
|
+
let indices
|
|
36
|
+
|
|
37
|
+
for (let i = 0; i < arr.length; i++) {
|
|
38
|
+
const resolver = compileTemplateLazy(arr[i])
|
|
39
|
+
if (resolver) {
|
|
40
|
+
resolvers ??= []
|
|
41
|
+
resolvers.push(resolver)
|
|
42
|
+
indices ??= []
|
|
43
|
+
indices.push(i)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!resolvers) {
|
|
48
|
+
return null
|
|
37
49
|
}
|
|
38
50
|
|
|
39
|
-
|
|
51
|
+
return (...args) =>
|
|
52
|
+
rxjs.combineLatest(resolvers.map((resolver) => resolver(...args))).pipe(
|
|
53
|
+
rx.map((values) => {
|
|
54
|
+
const ret = [...arr]
|
|
55
|
+
for (let n = 0; n < values.length; n++) {
|
|
56
|
+
ret[indices[n]] = values[n]
|
|
57
|
+
}
|
|
58
|
+
return ret
|
|
59
|
+
})
|
|
60
|
+
)
|
|
61
|
+
}
|
|
40
62
|
|
|
41
|
-
|
|
63
|
+
const compileArrayTemplate = weakCache(function compileArrayTemplate(arr) {
|
|
64
|
+
return compileArrayTemplateLazy(arr) ?? (() => rxjs.of(arr))
|
|
42
65
|
})
|
|
43
66
|
|
|
44
|
-
|
|
45
|
-
const compileObjectTemplate = weakCache(function compileObjectTemplate(obj) {
|
|
67
|
+
const compileObjectTemplateLazy = (obj) => {
|
|
46
68
|
if (!fp.isPlainObject(obj)) {
|
|
47
69
|
throw new Error('invalid argument')
|
|
48
70
|
}
|
|
49
71
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
72
|
+
let resolvers
|
|
73
|
+
let indices
|
|
74
|
+
|
|
75
|
+
const keys = Object.keys(obj)
|
|
54
76
|
|
|
55
|
-
|
|
56
|
-
|
|
77
|
+
for (let i = 0; i < keys.length; i++) {
|
|
78
|
+
const resolver = compileTemplateLazy(obj[keys[i]])
|
|
79
|
+
if (resolver) {
|
|
80
|
+
resolvers ??= []
|
|
81
|
+
resolvers.push(resolver)
|
|
82
|
+
indices ??= []
|
|
83
|
+
indices.push(keys[i])
|
|
84
|
+
}
|
|
57
85
|
}
|
|
58
86
|
|
|
59
|
-
|
|
87
|
+
if (!resolvers) {
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
60
90
|
|
|
61
91
|
return (...args) =>
|
|
62
|
-
|
|
63
|
-
rx.map((
|
|
64
|
-
const ret = {}
|
|
65
|
-
for (let n = 0; n <
|
|
66
|
-
ret[
|
|
92
|
+
rxjs.combineLatest(resolvers.map((resolver) => resolver(...args))).pipe(
|
|
93
|
+
rx.map((values) => {
|
|
94
|
+
const ret = { ...obj }
|
|
95
|
+
for (let n = 0; n < values.length; n++) {
|
|
96
|
+
ret[indices[n]] = values[n]
|
|
67
97
|
}
|
|
68
98
|
return ret
|
|
69
99
|
})
|
|
70
100
|
)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const compileObjectTemplate = weakCache(function compileObjectTemplate(obj) {
|
|
104
|
+
return compileObjectTemplateLazy(obj) ?? (() => rxjs.of(obj))
|
|
71
105
|
})
|
|
72
106
|
|
|
73
107
|
function inner(str) {
|
|
@@ -91,6 +125,7 @@ module.exports = (options) => {
|
|
|
91
125
|
}
|
|
92
126
|
|
|
93
127
|
return {
|
|
128
|
+
input: str,
|
|
94
129
|
pre: str.slice(0, templateStart),
|
|
95
130
|
type,
|
|
96
131
|
body: str.slice(bodyStart, bodyEnd),
|
|
@@ -98,37 +133,43 @@ module.exports = (options) => {
|
|
|
98
133
|
}
|
|
99
134
|
}
|
|
100
135
|
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const match = inner(str)
|
|
136
|
+
const _compileStringTemplate = weakCache(
|
|
137
|
+
function _compileStringTemplate(match) {
|
|
138
|
+
const { pre, type, body, post } = match
|
|
107
139
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
140
|
+
const compileExpression = compilers[type]
|
|
141
|
+
if (!compileExpression) {
|
|
142
|
+
throw new Error('unknown expression type')
|
|
143
|
+
}
|
|
111
144
|
|
|
112
|
-
|
|
145
|
+
const expr = compileExpression(body)
|
|
113
146
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
147
|
+
if (!pre && !post) {
|
|
148
|
+
return expr
|
|
149
|
+
}
|
|
118
150
|
|
|
119
|
-
|
|
151
|
+
return (...args) =>
|
|
152
|
+
expr(...args).pipe(
|
|
153
|
+
rx.switchMap((body) =>
|
|
154
|
+
compileStringTemplate(`${pre}${stringify(body, type !== 'js')}${post}`)(...args)
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
},
|
|
158
|
+
(match) => match.input
|
|
159
|
+
)
|
|
120
160
|
|
|
121
|
-
|
|
122
|
-
|
|
161
|
+
function compileStringTemplateLazy(str) {
|
|
162
|
+
if (!fp.isString(str)) {
|
|
163
|
+
throw new Error('invalid argument')
|
|
123
164
|
}
|
|
124
165
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
166
|
+
const match = inner(str)
|
|
167
|
+
return match ? _compileStringTemplate(str, match) : null
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function compileStringTemplate(str) {
|
|
171
|
+
return compileStringTemplateLazy(str) ?? (() => rxjs.of(str))
|
|
172
|
+
}
|
|
132
173
|
|
|
133
174
|
function stringify(value, escape) {
|
|
134
175
|
if (value == null) {
|
|
@@ -145,6 +186,18 @@ module.exports = (options) => {
|
|
|
145
186
|
return typeof val === 'string' && val.indexOf('{{') !== -1
|
|
146
187
|
}
|
|
147
188
|
|
|
189
|
+
function compileTemplateLazy(template) {
|
|
190
|
+
if (fp.isPlainObject(template)) {
|
|
191
|
+
return compileObjectTemplateLazy(template)
|
|
192
|
+
} else if (fp.isArray(template)) {
|
|
193
|
+
return compileArrayTemplateLazy(template)
|
|
194
|
+
} else if (fp.isString(template)) {
|
|
195
|
+
return compileStringTemplateLazy(template)
|
|
196
|
+
} else {
|
|
197
|
+
return null
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
148
201
|
function compileTemplate(template) {
|
|
149
202
|
if (fp.isPlainObject(template)) {
|
|
150
203
|
return compileObjectTemplate(template)
|
|
@@ -153,7 +206,7 @@ module.exports = (options) => {
|
|
|
153
206
|
} else if (fp.isString(template)) {
|
|
154
207
|
return compileStringTemplate(template)
|
|
155
208
|
} else {
|
|
156
|
-
return () =>
|
|
209
|
+
return () => rxjs.of(template)
|
|
157
210
|
}
|
|
158
211
|
}
|
|
159
212
|
|
|
@@ -165,13 +218,13 @@ module.exports = (options) => {
|
|
|
165
218
|
|
|
166
219
|
function onResolveTemplate(str, ...args) {
|
|
167
220
|
if (fp.isString(str) && str.lastIndexOf('{{') === -1) {
|
|
168
|
-
return
|
|
221
|
+
return rxjs.of(str)
|
|
169
222
|
}
|
|
170
223
|
|
|
171
224
|
try {
|
|
172
225
|
return compileTemplate(str)(...args)
|
|
173
226
|
} catch (err) {
|
|
174
|
-
return
|
|
227
|
+
return rxjs.throwError(err)
|
|
175
228
|
}
|
|
176
229
|
}
|
|
177
230
|
|
package/weakCache.js
CHANGED
|
@@ -18,8 +18,10 @@ module.exports = function weakCache(valueSelector, keySelector) {
|
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
const value = valueSelector(...args)
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
if (value != null) {
|
|
22
|
+
cache.set(key, new WeakRef(value))
|
|
23
|
+
finalizationRegistry.register(value, key)
|
|
24
|
+
}
|
|
23
25
|
return value
|
|
24
26
|
}
|
|
25
27
|
}
|