@nxtedition/lib 14.0.7 → 14.0.8
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 +107 -137
package/package.json
CHANGED
package/util/template/index.js
CHANGED
|
@@ -42,165 +42,102 @@ module.exports = (options) => {
|
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
if (fp.isPlainObject(template)) {
|
|
47
|
-
let hashes
|
|
48
|
-
for (const key of Object.keys(template)) {
|
|
49
|
-
const hash = hashTemplate(template[key], key)
|
|
50
|
-
if (hash) {
|
|
51
|
-
hashes ??= []
|
|
52
|
-
hashes.push(hash)
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return hashes ? objectHash([prefix, hashes]) : ''
|
|
56
|
-
} else if (fp.isArray(template)) {
|
|
57
|
-
let hashes
|
|
58
|
-
for (let idx = 0; idx < template.length; idx++) {
|
|
59
|
-
const hash = hashTemplate(template[idx], idx)
|
|
60
|
-
if (hash) {
|
|
61
|
-
hashes ??= []
|
|
62
|
-
hashes.push(hash)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return hashes ? objectHash([prefix, hashes]) : ''
|
|
66
|
-
} else if (isTemplate(template)) {
|
|
67
|
-
return objectHash([prefix, template])
|
|
68
|
-
} else {
|
|
69
|
-
return ''
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const _compileArrayTemplate = weakCache(
|
|
74
|
-
(arr, hash) => {
|
|
75
|
-
if (!fp.isArray(arr)) {
|
|
76
|
-
throw new Error('invalid argument')
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
let resolvers
|
|
80
|
-
let indices
|
|
81
|
-
|
|
82
|
-
for (let i = 0; i < arr.length; i++) {
|
|
83
|
-
const resolver = compileTemplate(arr[i])
|
|
84
|
-
if (resolver) {
|
|
85
|
-
resolvers ??= []
|
|
86
|
-
resolvers.push(resolver)
|
|
87
|
-
indices ??= []
|
|
88
|
-
indices.push(i)
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return resolvers
|
|
93
|
-
? (args$) =>
|
|
94
|
-
rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
|
|
95
|
-
rx.map((values) => {
|
|
96
|
-
const ret = [...arr]
|
|
97
|
-
for (let n = 0; n < values.length; n++) {
|
|
98
|
-
ret[indices[n]] = values[n]
|
|
99
|
-
}
|
|
100
|
-
return ret
|
|
101
|
-
})
|
|
102
|
-
)
|
|
103
|
-
: null
|
|
104
|
-
},
|
|
105
|
-
(arr, hash) => hash ?? hashTemplate(arr)
|
|
106
|
-
)
|
|
107
|
-
|
|
108
|
-
const compileArrayTemplate = (arr) => {
|
|
45
|
+
function compileArrayTemplate(arr) {
|
|
109
46
|
if (!fp.isArray(arr)) {
|
|
110
47
|
throw new Error('invalid argument')
|
|
111
48
|
}
|
|
112
|
-
const hash = hashTemplate(arr)
|
|
113
|
-
return hash ? _compileArrayTemplate(arr, hash) : null
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const _compileObjectTemplate = weakCache(
|
|
117
|
-
(obj) => {
|
|
118
|
-
if (!fp.isPlainObject(obj)) {
|
|
119
|
-
throw new Error('invalid argument')
|
|
120
|
-
}
|
|
121
49
|
|
|
122
|
-
|
|
123
|
-
|
|
50
|
+
let resolvers
|
|
51
|
+
let indices
|
|
124
52
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
indices ??= []
|
|
133
|
-
indices.push(keys[i])
|
|
134
|
-
}
|
|
53
|
+
for (let i = 0; i < arr.length; i++) {
|
|
54
|
+
const resolver = _compileTemplate(arr[i])
|
|
55
|
+
if (resolver) {
|
|
56
|
+
resolvers ??= []
|
|
57
|
+
resolvers.push(resolver)
|
|
58
|
+
indices ??= []
|
|
59
|
+
indices.push(i)
|
|
135
60
|
}
|
|
61
|
+
}
|
|
136
62
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
(obj, hash) => hash ?? hashTemplate(obj)
|
|
151
|
-
)
|
|
63
|
+
return resolvers
|
|
64
|
+
? (args$) =>
|
|
65
|
+
rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
|
|
66
|
+
rx.map((values) => {
|
|
67
|
+
const ret = [...arr]
|
|
68
|
+
for (let n = 0; n < values.length; n++) {
|
|
69
|
+
ret[indices[n]] = values[n]
|
|
70
|
+
}
|
|
71
|
+
return ret
|
|
72
|
+
})
|
|
73
|
+
)
|
|
74
|
+
: null
|
|
75
|
+
}
|
|
152
76
|
|
|
153
|
-
|
|
77
|
+
function compileObjectTemplate(obj) {
|
|
154
78
|
if (!fp.isPlainObject(obj)) {
|
|
155
79
|
throw new Error('invalid argument')
|
|
156
80
|
}
|
|
157
81
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
82
|
+
let resolvers
|
|
83
|
+
let indices
|
|
161
84
|
|
|
162
|
-
|
|
163
|
-
(str, hash) => {
|
|
164
|
-
if (!fp.isString(str)) {
|
|
165
|
-
throw new Error('invalid argument')
|
|
166
|
-
}
|
|
85
|
+
const keys = Object.keys(obj)
|
|
167
86
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
87
|
+
for (let i = 0; i < keys.length; i++) {
|
|
88
|
+
const resolver = _compileTemplate(obj[keys[i]])
|
|
89
|
+
if (resolver) {
|
|
90
|
+
resolvers ??= []
|
|
91
|
+
resolvers.push(resolver)
|
|
92
|
+
indices ??= []
|
|
93
|
+
indices.push(keys[i])
|
|
171
94
|
}
|
|
95
|
+
}
|
|
172
96
|
|
|
173
|
-
|
|
97
|
+
return resolvers
|
|
98
|
+
? (args$) =>
|
|
99
|
+
rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
|
|
100
|
+
rx.map((values) => {
|
|
101
|
+
const ret = { ...obj }
|
|
102
|
+
for (let n = 0; n < values.length; n++) {
|
|
103
|
+
ret[indices[n]] = values[n]
|
|
104
|
+
}
|
|
105
|
+
return ret
|
|
106
|
+
})
|
|
107
|
+
)
|
|
108
|
+
: null
|
|
109
|
+
}
|
|
174
110
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
111
|
+
function compileStringTemplate(str) {
|
|
112
|
+
if (!fp.isString(str)) {
|
|
113
|
+
throw new Error('invalid argument')
|
|
114
|
+
}
|
|
179
115
|
|
|
180
|
-
|
|
116
|
+
const match = inner(str)
|
|
117
|
+
if (!match) {
|
|
118
|
+
return null
|
|
119
|
+
}
|
|
181
120
|
|
|
182
|
-
|
|
183
|
-
return (args$) => expr(args$)
|
|
184
|
-
}
|
|
121
|
+
const { pre, type, body, post } = match
|
|
185
122
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
return compileStringTemplate(str)?.(args$) ?? rxjs.of(str)
|
|
191
|
-
})
|
|
192
|
-
)
|
|
193
|
-
},
|
|
194
|
-
(str, hash) => hash ?? hashTemplate(str)
|
|
195
|
-
)
|
|
123
|
+
const compileExpression = compilers[type]
|
|
124
|
+
if (!compileExpression) {
|
|
125
|
+
throw new Error('unknown expression type: ' + type)
|
|
126
|
+
}
|
|
196
127
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
128
|
+
const expr = compileExpression(body)
|
|
129
|
+
|
|
130
|
+
if (!pre && !post) {
|
|
131
|
+
return (args$) => expr(args$)
|
|
200
132
|
}
|
|
201
133
|
|
|
202
|
-
|
|
203
|
-
|
|
134
|
+
return (args$) =>
|
|
135
|
+
expr(args$).pipe(
|
|
136
|
+
rx.switchMap((body) => {
|
|
137
|
+
const str = `${pre}${stringify(body, type !== 'js')}${post}`
|
|
138
|
+
return compileStringTemplate(str)?.(args$) ?? rxjs.of(str)
|
|
139
|
+
})
|
|
140
|
+
)
|
|
204
141
|
}
|
|
205
142
|
|
|
206
143
|
function stringify(value, escape) {
|
|
@@ -218,7 +155,35 @@ module.exports = (options) => {
|
|
|
218
155
|
return typeof val === 'string' && val.indexOf('{{') !== -1
|
|
219
156
|
}
|
|
220
157
|
|
|
221
|
-
function
|
|
158
|
+
function hashTemplate(template, prefix) {
|
|
159
|
+
if (fp.isPlainObject(template)) {
|
|
160
|
+
let hashes
|
|
161
|
+
for (const key of Object.keys(template)) {
|
|
162
|
+
const hash = hashTemplate(template[key], key)
|
|
163
|
+
if (hash) {
|
|
164
|
+
hashes ??= []
|
|
165
|
+
hashes.push(hash)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return hashes ? objectHash([prefix, hashes]) : ''
|
|
169
|
+
} else if (fp.isArray(template)) {
|
|
170
|
+
let hashes
|
|
171
|
+
for (let idx = 0; idx < template.length; idx++) {
|
|
172
|
+
const hash = hashTemplate(template[idx], idx)
|
|
173
|
+
if (hash) {
|
|
174
|
+
hashes ??= []
|
|
175
|
+
hashes.push(hash)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return hashes ? objectHash([prefix, hashes]) : ''
|
|
179
|
+
} else if (isTemplate(template)) {
|
|
180
|
+
return objectHash([prefix, template])
|
|
181
|
+
} else {
|
|
182
|
+
return ''
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function _compileTemplate(template) {
|
|
222
187
|
if (fp.isPlainObject(template)) {
|
|
223
188
|
return compileObjectTemplate(template)
|
|
224
189
|
} else if (fp.isArray(template)) {
|
|
@@ -230,6 +195,11 @@ module.exports = (options) => {
|
|
|
230
195
|
}
|
|
231
196
|
}
|
|
232
197
|
|
|
198
|
+
const compileTemplate = weakCache(
|
|
199
|
+
(template) => _compileTemplate(template),
|
|
200
|
+
(template) => hashTemplate(template)
|
|
201
|
+
)
|
|
202
|
+
|
|
233
203
|
async function resolveTemplate(template, args$) {
|
|
234
204
|
return rxjs.firstValueFrom(onResolveTemplate(template, args$))
|
|
235
205
|
}
|