@nxtedition/lib 14.0.8 → 14.0.9

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/util/template/index.js +148 -106
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "14.0.8",
3
+ "version": "14.0.9",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "files": [
@@ -42,102 +42,177 @@ module.exports = (options) => {
42
42
  }
43
43
  }
44
44
 
45
- function compileArrayTemplate(arr) {
45
+ const hashTemplate = (template, prefix) => {
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
+ if (!hash) {
80
+ return null
81
+ }
82
+
83
+ let resolvers
84
+ let indices
85
+
86
+ for (let i = 0; i < arr.length; i++) {
87
+ const resolver = compileTemplate(arr[i])
88
+ if (resolver) {
89
+ resolvers ??= []
90
+ resolvers.push(resolver)
91
+ indices ??= []
92
+ indices.push(i)
93
+ }
94
+ }
95
+
96
+ return resolvers
97
+ ? (args$) =>
98
+ rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
99
+ rx.map((values) => {
100
+ const ret = [...arr]
101
+ for (let n = 0; n < values.length; n++) {
102
+ ret[indices[n]] = values[n]
103
+ }
104
+ return ret
105
+ })
106
+ )
107
+ : null
108
+ },
109
+ (arr, hash) => hash ?? hashTemplate(arr)
110
+ )
111
+
112
+ const compileArrayTemplate = (arr) => {
46
113
  if (!fp.isArray(arr)) {
47
114
  throw new Error('invalid argument')
48
115
  }
116
+ const hash = hashTemplate(arr)
117
+ return hash ? _compileArrayTemplate(arr, hash) : null
118
+ }
49
119
 
50
- let resolvers
51
- let indices
120
+ const _compileObjectTemplate = weakCache(
121
+ (obj, hash) => {
122
+ if (!fp.isPlainObject(obj)) {
123
+ throw new Error('invalid argument')
124
+ }
52
125
 
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)
126
+ if (!hash) {
127
+ return null
60
128
  }
61
- }
62
129
 
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
- }
130
+ let resolvers
131
+ let indices
132
+
133
+ const keys = Object.keys(obj)
134
+
135
+ for (let i = 0; i < keys.length; i++) {
136
+ const resolver = compileTemplate(obj[keys[i]])
137
+ if (resolver) {
138
+ resolvers ??= []
139
+ resolvers.push(resolver)
140
+ indices ??= []
141
+ indices.push(keys[i])
142
+ }
143
+ }
76
144
 
77
- function compileObjectTemplate(obj) {
145
+ return resolvers
146
+ ? (args$) =>
147
+ rxjs.combineLatest(resolvers.map((resolver) => resolver(args$))).pipe(
148
+ rx.map((values) => {
149
+ const ret = { ...obj }
150
+ for (let n = 0; n < values.length; n++) {
151
+ ret[indices[n]] = values[n]
152
+ }
153
+ return ret
154
+ })
155
+ )
156
+ : null
157
+ },
158
+ (obj, hash) => hash ?? hashTemplate(obj)
159
+ )
160
+
161
+ const compileObjectTemplate = (obj) => {
78
162
  if (!fp.isPlainObject(obj)) {
79
163
  throw new Error('invalid argument')
80
164
  }
81
165
 
82
- let resolvers
83
- let indices
166
+ const hash = hashTemplate(obj)
167
+ return hash ? _compileObjectTemplate(obj, hash) : null
168
+ }
84
169
 
85
- const keys = Object.keys(obj)
170
+ const _compileStringTemplate = weakCache(
171
+ (str, hash) => {
172
+ if (!fp.isString(str)) {
173
+ throw new Error('invalid argument')
174
+ }
86
175
 
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])
176
+ if (!hash) {
177
+ return null
94
178
  }
95
- }
96
179
 
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
- }
180
+ const match = inner(str)
181
+ if (!match) {
182
+ return null
183
+ }
110
184
 
111
- function compileStringTemplate(str) {
112
- if (!fp.isString(str)) {
113
- throw new Error('invalid argument')
114
- }
185
+ const { pre, type, body, post } = match
115
186
 
116
- const match = inner(str)
117
- if (!match) {
118
- return null
119
- }
187
+ const compileExpression = compilers[type]
188
+ if (!compileExpression) {
189
+ throw new Error('unknown expression type: ' + type)
190
+ }
120
191
 
121
- const { pre, type, body, post } = match
192
+ const expr = compileExpression(body)
122
193
 
123
- const compileExpression = compilers[type]
124
- if (!compileExpression) {
125
- throw new Error('unknown expression type: ' + type)
126
- }
194
+ if (!pre && !post) {
195
+ return (args$) => expr(args$)
196
+ }
127
197
 
128
- const expr = compileExpression(body)
198
+ return (args$) =>
199
+ expr(args$).pipe(
200
+ rx.switchMap((body) => {
201
+ const str = `${pre}${stringify(body, type !== 'js')}${post}`
202
+ return compileStringTemplate(str)?.(args$) ?? rxjs.of(str)
203
+ })
204
+ )
205
+ },
206
+ (str, hash) => hash ?? hashTemplate(str)
207
+ )
129
208
 
130
- if (!pre && !post) {
131
- return (args$) => expr(args$)
209
+ const compileStringTemplate = (obj) => {
210
+ if (!fp.isString(obj)) {
211
+ throw new Error('invalid argument')
132
212
  }
133
213
 
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
- )
214
+ const hash = hashTemplate(obj)
215
+ return hash ? _compileStringTemplate(obj, hash) : null
141
216
  }
142
217
 
143
218
  function stringify(value, escape) {
@@ -155,35 +230,7 @@ module.exports = (options) => {
155
230
  return typeof val === 'string' && val.indexOf('{{') !== -1
156
231
  }
157
232
 
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) {
233
+ function compileTemplate(template) {
187
234
  if (fp.isPlainObject(template)) {
188
235
  return compileObjectTemplate(template)
189
236
  } else if (fp.isArray(template)) {
@@ -195,11 +242,6 @@ module.exports = (options) => {
195
242
  }
196
243
  }
197
244
 
198
- const compileTemplate = weakCache(
199
- (template) => _compileTemplate(template),
200
- (template) => hashTemplate(template)
201
- )
202
-
203
245
  async function resolveTemplate(template, args$) {
204
246
  return rxjs.firstValueFrom(onResolveTemplate(template, args$))
205
247
  }