@nxtedition/lib 14.0.11 → 14.0.12
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 +90 -141
package/package.json
CHANGED
package/util/template/index.js
CHANGED
|
@@ -70,158 +70,99 @@ module.exports = (options) => {
|
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
function compileArrayTemplate(template) {
|
|
74
|
+
let resolvers
|
|
75
|
+
let indices
|
|
76
|
+
|
|
77
|
+
for (let i = 0; i < template.length; i++) {
|
|
78
|
+
const resolver = compileTemplate(template[i])
|
|
79
|
+
if (resolver) {
|
|
80
|
+
resolvers ??= []
|
|
81
|
+
resolvers.push(resolver)
|
|
82
|
+
indices ??= []
|
|
83
|
+
indices.push(i)
|
|
77
84
|
}
|
|
85
|
+
}
|
|
78
86
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
for (let i = 0; i < template.length; i++) {
|
|
87
|
-
const resolver = compileTemplate(template[i])
|
|
88
|
-
if (resolver) {
|
|
89
|
-
resolvers ??= []
|
|
90
|
-
resolvers.push(resolver)
|
|
91
|
-
indices ??= []
|
|
92
|
-
indices.push(i)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return resolvers
|
|
97
|
-
? (arr, args$) => {
|
|
98
|
-
const len = resolvers.length
|
|
99
|
-
const values = new Array(len)
|
|
100
|
-
for (let n = 0; n < len; n++) {
|
|
101
|
-
values[n] = resolvers[n](arr[indices[n]], args$)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return rxjs.combineLatest(values).pipe(
|
|
105
|
-
rx.map((values) => {
|
|
106
|
-
const ret = [...arr]
|
|
107
|
-
for (let n = 0; n < values.length; n++) {
|
|
108
|
-
ret[indices[n]] = values[n]
|
|
109
|
-
}
|
|
110
|
-
return ret
|
|
111
|
-
})
|
|
112
|
-
)
|
|
87
|
+
return resolvers
|
|
88
|
+
? (arr, args$) => {
|
|
89
|
+
const len = resolvers.length
|
|
90
|
+
const values = new Array(len)
|
|
91
|
+
for (let n = 0; n < len; n++) {
|
|
92
|
+
values[n] = resolvers[n](arr[indices[n]], args$)
|
|
113
93
|
}
|
|
114
|
-
: null
|
|
115
|
-
},
|
|
116
|
-
(arr, hash) => hash ?? hashTemplate(arr)
|
|
117
|
-
)
|
|
118
94
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
95
|
+
return rxjs.combineLatest(values).pipe(
|
|
96
|
+
rx.map((values) => {
|
|
97
|
+
const ret = [...arr]
|
|
98
|
+
for (let n = 0; n < values.length; n++) {
|
|
99
|
+
ret[indices[n]] = values[n]
|
|
100
|
+
}
|
|
101
|
+
return ret
|
|
102
|
+
})
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
: null
|
|
125
106
|
}
|
|
126
107
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
throw new Error('invalid argument')
|
|
131
|
-
}
|
|
108
|
+
function compileObjectTemplate(template) {
|
|
109
|
+
let resolvers
|
|
110
|
+
let indices
|
|
132
111
|
|
|
133
|
-
|
|
134
|
-
return null
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
let resolvers
|
|
138
|
-
let indices
|
|
139
|
-
|
|
140
|
-
const keys = Object.keys(template)
|
|
112
|
+
const keys = Object.keys(template)
|
|
141
113
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
114
|
+
for (let i = 0; i < keys.length; i++) {
|
|
115
|
+
const resolver = compileTemplate(template[keys[i]])
|
|
116
|
+
if (resolver) {
|
|
117
|
+
resolvers ??= []
|
|
118
|
+
resolvers.push(resolver)
|
|
119
|
+
indices ??= []
|
|
120
|
+
indices.push(keys[i])
|
|
150
121
|
}
|
|
122
|
+
}
|
|
151
123
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return rxjs.combineLatest(values).pipe(
|
|
161
|
-
rx.map((values) => {
|
|
162
|
-
const ret = { ...obj }
|
|
163
|
-
for (let n = 0; n < values.length; n++) {
|
|
164
|
-
ret[indices[n]] = values[n]
|
|
165
|
-
}
|
|
166
|
-
return ret
|
|
167
|
-
})
|
|
168
|
-
)
|
|
124
|
+
return resolvers
|
|
125
|
+
? (obj, args$) => {
|
|
126
|
+
const len = resolvers.length
|
|
127
|
+
const values = new Array(len)
|
|
128
|
+
for (let n = 0; n < len; n++) {
|
|
129
|
+
values[n] = resolvers[n](obj[indices[n]], args$)
|
|
169
130
|
}
|
|
170
|
-
: null
|
|
171
|
-
},
|
|
172
|
-
(obj, hash) => hash ?? hashTemplate(obj)
|
|
173
|
-
)
|
|
174
|
-
|
|
175
|
-
const compileObjectTemplate = (template) => {
|
|
176
|
-
if (!fp.isPlainObject(template)) {
|
|
177
|
-
throw new Error('invalid argument')
|
|
178
|
-
}
|
|
179
131
|
|
|
180
|
-
|
|
181
|
-
|
|
132
|
+
return rxjs.combineLatest(values).pipe(
|
|
133
|
+
rx.map((values) => {
|
|
134
|
+
const ret = { ...obj }
|
|
135
|
+
for (let n = 0; n < values.length; n++) {
|
|
136
|
+
ret[indices[n]] = values[n]
|
|
137
|
+
}
|
|
138
|
+
return ret
|
|
139
|
+
})
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
: null
|
|
182
143
|
}
|
|
183
144
|
|
|
184
|
-
|
|
185
|
-
(template
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
if (!hash) {
|
|
191
|
-
return null
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const match = inner(template)
|
|
195
|
-
if (!match) {
|
|
196
|
-
return null
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const { pre, type, body, post } = match
|
|
200
|
-
|
|
201
|
-
const compileExpression = compilers[type]
|
|
202
|
-
if (!compileExpression) {
|
|
203
|
-
throw new Error('unknown expression type: ' + type)
|
|
204
|
-
}
|
|
145
|
+
function compileStringTemplate(template) {
|
|
146
|
+
const match = inner(template)
|
|
147
|
+
if (!match) {
|
|
148
|
+
return null
|
|
149
|
+
}
|
|
205
150
|
|
|
206
|
-
|
|
151
|
+
const { pre, type, body, post } = match
|
|
207
152
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
153
|
+
const compileExpression = compilers[type]
|
|
154
|
+
if (!compileExpression) {
|
|
155
|
+
throw new Error('unknown expression type: ' + type)
|
|
156
|
+
}
|
|
211
157
|
|
|
212
|
-
|
|
213
|
-
expr(args$).pipe(rx.map((body) => `${pre}${stringify(body, type !== 'js')}${post}`))
|
|
214
|
-
},
|
|
215
|
-
(str, hash) => hash ?? hashTemplate(str)
|
|
216
|
-
)
|
|
158
|
+
const expr = compileExpression(body)
|
|
217
159
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
throw new Error('invalid argument')
|
|
160
|
+
if (!pre && !post) {
|
|
161
|
+
return (str, args$) => expr(args$)
|
|
221
162
|
}
|
|
222
163
|
|
|
223
|
-
|
|
224
|
-
|
|
164
|
+
return (str, args$) =>
|
|
165
|
+
expr(args$).pipe(rx.map((body) => `${pre}${stringify(body, type !== 'js')}${post}`))
|
|
225
166
|
}
|
|
226
167
|
|
|
227
168
|
function stringify(value, escape) {
|
|
@@ -239,16 +180,24 @@ module.exports = (options) => {
|
|
|
239
180
|
return typeof val === 'string' && val.indexOf('{{') !== -1
|
|
240
181
|
}
|
|
241
182
|
|
|
183
|
+
const compileTemplateCache = weakCache(
|
|
184
|
+
(template) => {
|
|
185
|
+
if (fp.isPlainObject(template)) {
|
|
186
|
+
return compileObjectTemplate(template)
|
|
187
|
+
} else if (fp.isArray(template)) {
|
|
188
|
+
return compileArrayTemplate(template)
|
|
189
|
+
} else if (isTemplate(template)) {
|
|
190
|
+
return compileStringTemplate(template)
|
|
191
|
+
} else {
|
|
192
|
+
return null
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
(template, hash) => hash ?? hashTemplate(template)
|
|
196
|
+
)
|
|
197
|
+
|
|
242
198
|
function compileTemplate(template) {
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
} else if (fp.isArray(template)) {
|
|
246
|
-
return compileArrayTemplate(template)
|
|
247
|
-
} else if (isTemplate(template)) {
|
|
248
|
-
return compileStringTemplate(template)
|
|
249
|
-
} else {
|
|
250
|
-
return null
|
|
251
|
-
}
|
|
199
|
+
const hash = hashTemplate(template)
|
|
200
|
+
return hash ? compileTemplateCache(template, hash) : null
|
|
252
201
|
}
|
|
253
202
|
|
|
254
203
|
async function resolveTemplate(template, args$) {
|