@websolutespa/bom-core 0.0.1
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/CHANGELOG.md +7 -0
- package/README.md +11 -0
- package/dist/index.d.ts +474 -0
- package/dist/index.js +675 -0
- package/dist/index.mjs +604 -0
- package/package.json +57 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
// src/classNames/classNames.ts
|
|
2
|
+
function isClassNameCollection(value) {
|
|
3
|
+
return typeof value === "object" && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
function classNameCollectionToString(className) {
|
|
6
|
+
const keys = Object.keys(className);
|
|
7
|
+
const total = keys.length;
|
|
8
|
+
let classNames = "";
|
|
9
|
+
for (let i = 0; i < total; i++) {
|
|
10
|
+
const key = keys[i];
|
|
11
|
+
const value = className[keys[i]];
|
|
12
|
+
if (!value) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
classNames = classNames ? `${classNames} ${String(key)}` : String(key);
|
|
16
|
+
}
|
|
17
|
+
return classNames;
|
|
18
|
+
}
|
|
19
|
+
function getClassNames(...props) {
|
|
20
|
+
const total = props.length;
|
|
21
|
+
let classNames = "";
|
|
22
|
+
if (total === 0) {
|
|
23
|
+
return classNames;
|
|
24
|
+
}
|
|
25
|
+
for (let i = 0; i < total; i++) {
|
|
26
|
+
const value = props[i];
|
|
27
|
+
if (!value) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (isClassNameCollection(value)) {
|
|
31
|
+
classNames += ` ${classNameCollectionToString(value)}`;
|
|
32
|
+
} else {
|
|
33
|
+
classNames += ` ${String(value).trim()}`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return classNames.trim();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/collection/collection.ts
|
|
40
|
+
import pluralize from "pluralize";
|
|
41
|
+
function getCollectionDescription(text) {
|
|
42
|
+
text = toValidCollectionName(text);
|
|
43
|
+
const displayName = toDisplayName(text);
|
|
44
|
+
const pluralName = toPluralName(text);
|
|
45
|
+
const pluralDisplayName = toPluralName(displayName);
|
|
46
|
+
const singularName = text;
|
|
47
|
+
const title = toTitleCase(text);
|
|
48
|
+
return {
|
|
49
|
+
displayName,
|
|
50
|
+
pluralName,
|
|
51
|
+
pluralDisplayName,
|
|
52
|
+
singularName,
|
|
53
|
+
title
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function isValidCollectionDescription(description) {
|
|
57
|
+
return description.singularName !== description.pluralName;
|
|
58
|
+
}
|
|
59
|
+
function toValidCollectionName(text) {
|
|
60
|
+
text = text.trim().replace(/\W/g, "_").replace(/^[_]*(.)/, (_, c) => c);
|
|
61
|
+
text = camelToSnakeCase(text);
|
|
62
|
+
text = text.replace(/__/g, "_");
|
|
63
|
+
return text.toLowerCase();
|
|
64
|
+
}
|
|
65
|
+
function camelToSnakeCase(text) {
|
|
66
|
+
return text.replace(/[A-Z]/g, (letter, index) => {
|
|
67
|
+
return index > 0 ? `_${letter.toLowerCase()}` : letter.toLowerCase();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function toDisplayName(text) {
|
|
71
|
+
return toTitleCase(text).replace(/\s/g, "");
|
|
72
|
+
}
|
|
73
|
+
function toPluralName(text) {
|
|
74
|
+
return pluralize.plural(text);
|
|
75
|
+
}
|
|
76
|
+
function toTitleCase(text) {
|
|
77
|
+
return text.replace(/^[-_]*(.)/, (_, c) => c.toUpperCase()).replace(/[-_]+(.)/g, (_, c) => " " + c.toUpperCase());
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/consoleWarn/consoleWarn.ts
|
|
81
|
+
var warningStack = {};
|
|
82
|
+
function consoleWarn(message, component) {
|
|
83
|
+
if (typeof console === "undefined") {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const tag = component ? ` [${component}]` : " ";
|
|
87
|
+
const log = `[DesignSystem]${tag}: ${message}`;
|
|
88
|
+
if (warningStack[log]) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
warningStack[log] = true;
|
|
92
|
+
if (process.env.NODE_ENV !== "production") {
|
|
93
|
+
return console.error(log);
|
|
94
|
+
}
|
|
95
|
+
console.warn(log);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/utils/index.ts
|
|
99
|
+
function getIsDevelopment() {
|
|
100
|
+
return process && (!process.env.NODE_ENV || process.env.NODE_ENV === "development");
|
|
101
|
+
}
|
|
102
|
+
var isDevelopment = getIsDevelopment();
|
|
103
|
+
var isBrowser = typeof window !== "undefined";
|
|
104
|
+
function withSchema(component, schema) {
|
|
105
|
+
return Object.assign(component, schema);
|
|
106
|
+
}
|
|
107
|
+
function asServerProps(props) {
|
|
108
|
+
return JSON.parse(JSON.stringify(props));
|
|
109
|
+
}
|
|
110
|
+
async function awaitAll(array, callback) {
|
|
111
|
+
const promises = array.map(callback);
|
|
112
|
+
return await Promise.all(promises);
|
|
113
|
+
}
|
|
114
|
+
function merge(target, source) {
|
|
115
|
+
if (source === null) {
|
|
116
|
+
return source;
|
|
117
|
+
}
|
|
118
|
+
if (!target) {
|
|
119
|
+
if (source && typeof source === "object") {
|
|
120
|
+
return Object.assign({}, source);
|
|
121
|
+
} else {
|
|
122
|
+
return source;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (source && typeof source === "object") {
|
|
126
|
+
Object.keys(source).forEach((key) => {
|
|
127
|
+
const value = source[key];
|
|
128
|
+
if (typeof value === "object" && !Array.isArray(value)) {
|
|
129
|
+
target[key] = merge(target[key], value);
|
|
130
|
+
} else {
|
|
131
|
+
target[key] = value;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return target;
|
|
136
|
+
}
|
|
137
|
+
function deepCopy(source) {
|
|
138
|
+
if (Array.isArray(source)) {
|
|
139
|
+
return source.map((x) => deepCopy(x));
|
|
140
|
+
} else if (source && typeof source === "object") {
|
|
141
|
+
const copy = {};
|
|
142
|
+
Object.keys(source).forEach((key) => {
|
|
143
|
+
copy[key] = deepCopy(source[key]);
|
|
144
|
+
});
|
|
145
|
+
return copy;
|
|
146
|
+
} else {
|
|
147
|
+
return source;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/http/http.service.ts
|
|
152
|
+
var defaultOptions = {
|
|
153
|
+
// method: 'POST', // *GET, POST, PUT, DELETE, etc.
|
|
154
|
+
// mode: 'cors', // no-cors, *cors, same-origin
|
|
155
|
+
// cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
|
|
156
|
+
// credentials: 'same-origin', // include, *same-origin, omit
|
|
157
|
+
headers: {
|
|
158
|
+
"Content-Type": "application/json"
|
|
159
|
+
// 'Content-Type': 'application/x-www-form-urlencoded',
|
|
160
|
+
}
|
|
161
|
+
// redirect: 'follow', // manual, *follow, error
|
|
162
|
+
// referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
|
|
163
|
+
// body: JSON.stringify(data) // body data type must match 'Content-Type' header
|
|
164
|
+
};
|
|
165
|
+
async function httpFetch(url, options = {}, serviceOptions) {
|
|
166
|
+
const httpOptions = merge({ ...defaultOptions }, options);
|
|
167
|
+
const httpResponse = await fetch(url, httpOptions);
|
|
168
|
+
if (!httpResponse.ok) {
|
|
169
|
+
throw httpResponse;
|
|
170
|
+
}
|
|
171
|
+
const response = await httpResponse.json();
|
|
172
|
+
return response;
|
|
173
|
+
}
|
|
174
|
+
async function httpGet(url, options = {}) {
|
|
175
|
+
return await httpFetch(url, { ...options, method: "GET" });
|
|
176
|
+
}
|
|
177
|
+
async function httpPost(url, payload, options = {}) {
|
|
178
|
+
return await httpFetch(url, { ...options, method: "POST", body: JSON.stringify(payload) });
|
|
179
|
+
}
|
|
180
|
+
async function httpPut(url, payload, options = {}) {
|
|
181
|
+
return await httpFetch(url, { ...options, method: "PUT", body: JSON.stringify(payload) });
|
|
182
|
+
}
|
|
183
|
+
async function httpPatch(url, payload, options = {}) {
|
|
184
|
+
return await httpFetch(url, { ...options, method: "PATCH", body: JSON.stringify(payload) });
|
|
185
|
+
}
|
|
186
|
+
async function httpDelete(url, options = {}) {
|
|
187
|
+
return await httpFetch(url, { ...options, method: "DELETE" });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/json/json.service.ts
|
|
191
|
+
var JsonService = class {
|
|
192
|
+
constructor(items) {
|
|
193
|
+
this.items = items;
|
|
194
|
+
}
|
|
195
|
+
async getItems() {
|
|
196
|
+
return this.items;
|
|
197
|
+
}
|
|
198
|
+
async findOne(id, params = {}) {
|
|
199
|
+
let items = await this.getItems();
|
|
200
|
+
params.where = params.where || {};
|
|
201
|
+
params.where.id = params.where.id || { equals: id };
|
|
202
|
+
items = this.whereItems_(items, params.where);
|
|
203
|
+
if (items.length > 0) {
|
|
204
|
+
return this.decorator_(items[0], params);
|
|
205
|
+
} else {
|
|
206
|
+
console.log("JsonService.findOne.notfound", id, params);
|
|
207
|
+
return void 0;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
async findMany(params = {}) {
|
|
211
|
+
params.pagination = false;
|
|
212
|
+
let items = await this.getItems();
|
|
213
|
+
if (params.where) {
|
|
214
|
+
items = this.whereItems_(items, params.where);
|
|
215
|
+
}
|
|
216
|
+
if (params.sort) {
|
|
217
|
+
items = this.sortCollection_(items, params.sort);
|
|
218
|
+
}
|
|
219
|
+
return items.map((x) => this.decorator_(x, params));
|
|
220
|
+
}
|
|
221
|
+
async findPaged(params = {}) {
|
|
222
|
+
params.pagination = true;
|
|
223
|
+
let items = await this.getItems();
|
|
224
|
+
if (params.where) {
|
|
225
|
+
items = this.whereItems_(items, params.where);
|
|
226
|
+
}
|
|
227
|
+
if (params.sort) {
|
|
228
|
+
items = this.sortCollection_(items, params.sort);
|
|
229
|
+
}
|
|
230
|
+
const pagination = this.paginatedItems_(items, params.page, params.limit);
|
|
231
|
+
pagination.docs = pagination.docs.map((x) => this.decorator_(x, params));
|
|
232
|
+
return pagination;
|
|
233
|
+
}
|
|
234
|
+
async create(payload) {
|
|
235
|
+
const items = await this.getItems();
|
|
236
|
+
const item = { ...payload, id: this.newUUID_() };
|
|
237
|
+
this.items = [...items, item];
|
|
238
|
+
return item;
|
|
239
|
+
}
|
|
240
|
+
async update(payload) {
|
|
241
|
+
let items = await this.getItems();
|
|
242
|
+
const index = items.reduce((p, c, i) => {
|
|
243
|
+
return c.id === payload.id ? i : p;
|
|
244
|
+
}, -1);
|
|
245
|
+
if (index !== -1) {
|
|
246
|
+
const item = { ...items[index], payload };
|
|
247
|
+
items = [...items];
|
|
248
|
+
items[index] = item;
|
|
249
|
+
this.items = items;
|
|
250
|
+
return item;
|
|
251
|
+
}
|
|
252
|
+
throw "not found";
|
|
253
|
+
}
|
|
254
|
+
async delete(id) {
|
|
255
|
+
let items = await this.getItems();
|
|
256
|
+
const index = items.reduce((p, c, i) => {
|
|
257
|
+
return c.id === id ? i : p;
|
|
258
|
+
}, -1);
|
|
259
|
+
if (index !== -1) {
|
|
260
|
+
const item = items[index];
|
|
261
|
+
items = [...items];
|
|
262
|
+
items.splice(index, 1);
|
|
263
|
+
this.items = items;
|
|
264
|
+
return item;
|
|
265
|
+
}
|
|
266
|
+
return void 0;
|
|
267
|
+
}
|
|
268
|
+
decorator_(item, params = {}) {
|
|
269
|
+
return item;
|
|
270
|
+
}
|
|
271
|
+
newUUID_() {
|
|
272
|
+
return (/* @__PURE__ */ new Date()).getTime();
|
|
273
|
+
}
|
|
274
|
+
// where
|
|
275
|
+
evaluateValue_(value) {
|
|
276
|
+
if (typeof value === "object" && value) {
|
|
277
|
+
return value.id;
|
|
278
|
+
} else {
|
|
279
|
+
return value;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
existsClause_(value, exists) {
|
|
283
|
+
if (typeof exists === "boolean") {
|
|
284
|
+
return value != null === exists;
|
|
285
|
+
}
|
|
286
|
+
return true;
|
|
287
|
+
}
|
|
288
|
+
equalsClause_(value, equals) {
|
|
289
|
+
if (typeof equals !== "undefined") {
|
|
290
|
+
return this.evaluateValue_(value) === equals;
|
|
291
|
+
}
|
|
292
|
+
return true;
|
|
293
|
+
}
|
|
294
|
+
inClause_(value, values) {
|
|
295
|
+
if (Array.isArray(values)) {
|
|
296
|
+
return values.indexOf(this.evaluateValue_(value)) !== -1;
|
|
297
|
+
}
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
notInClause_(value, values) {
|
|
301
|
+
if (Array.isArray(values)) {
|
|
302
|
+
return values.indexOf(this.evaluateValue_(value)) === -1;
|
|
303
|
+
}
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
likeClause_(value, query) {
|
|
307
|
+
if (typeof query === "string") {
|
|
308
|
+
return String(value).toLowerCase().indexOf(query.toLowerCase()) !== -1;
|
|
309
|
+
}
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
// old where
|
|
313
|
+
/*
|
|
314
|
+
protected where_(items: any[], params: FindParams): any[] {
|
|
315
|
+
const where = params.where;
|
|
316
|
+
if (where) {
|
|
317
|
+
const keys = Object.keys(where);
|
|
318
|
+
items = items.filter(x => {
|
|
319
|
+
return keys.reduce<boolean>((p, c) => {
|
|
320
|
+
const whereValue = where[c];
|
|
321
|
+
const shouldMatch = Array.isArray(whereValue) ? whereValue : [whereValue];
|
|
322
|
+
// console.log('key', c, 'value', x[c], 'shouldMatch', shouldMatch);
|
|
323
|
+
return p && (shouldMatch.indexOf(x[c]) !== -1);
|
|
324
|
+
}, true);
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
return items;
|
|
328
|
+
}
|
|
329
|
+
*/
|
|
330
|
+
whereItem_(item, where) {
|
|
331
|
+
let has = true;
|
|
332
|
+
if (typeof where === "object") {
|
|
333
|
+
Object.entries(where).forEach(([k, v]) => {
|
|
334
|
+
switch (k) {
|
|
335
|
+
case "and":
|
|
336
|
+
if (Array.isArray(v)) {
|
|
337
|
+
has = v.reduce((p, c) => {
|
|
338
|
+
return p && this.whereItem_(item, c);
|
|
339
|
+
}, has);
|
|
340
|
+
}
|
|
341
|
+
break;
|
|
342
|
+
case "or":
|
|
343
|
+
if (Array.isArray(v)) {
|
|
344
|
+
has = has && v.reduce((p, c) => {
|
|
345
|
+
return p || this.whereItem_(item, c);
|
|
346
|
+
}, false);
|
|
347
|
+
}
|
|
348
|
+
break;
|
|
349
|
+
default:
|
|
350
|
+
if (Array.isArray(v)) {
|
|
351
|
+
} else if (typeof v === "object") {
|
|
352
|
+
const value = item[k];
|
|
353
|
+
has = has && this.existsClause_(value, v.exists);
|
|
354
|
+
has = has && this.equalsClause_(value, v.equals);
|
|
355
|
+
has = has && this.inClause_(value, v.in);
|
|
356
|
+
has = has && this.notInClause_(value, v.not_in);
|
|
357
|
+
has = has && this.likeClause_(value, v.like);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
return has;
|
|
363
|
+
}
|
|
364
|
+
whereItems_(items, where) {
|
|
365
|
+
if (typeof where === "object") {
|
|
366
|
+
items = items.filter((item) => this.whereItem_(item, where));
|
|
367
|
+
}
|
|
368
|
+
return items;
|
|
369
|
+
}
|
|
370
|
+
// ^ where
|
|
371
|
+
// sort
|
|
372
|
+
sortCollection_(items, sort) {
|
|
373
|
+
if (sort) {
|
|
374
|
+
const reverse = sort.indexOf("-") === 0 ? -1 : 1;
|
|
375
|
+
const key = sort.replace(/^-/, "");
|
|
376
|
+
items = [...items];
|
|
377
|
+
items.sort((a, b) => {
|
|
378
|
+
const aValue = String(a[key]);
|
|
379
|
+
const bValue = String(b[key]);
|
|
380
|
+
if (aValue.localeCompare(bValue) < 0) {
|
|
381
|
+
return -1 * reverse;
|
|
382
|
+
} else if (aValue.localeCompare(bValue) > 0) {
|
|
383
|
+
return 1 * reverse;
|
|
384
|
+
}
|
|
385
|
+
return 0;
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
return items;
|
|
389
|
+
}
|
|
390
|
+
// ^ sort
|
|
391
|
+
// pagination
|
|
392
|
+
paginatedItems_(items, page = 1, limit = 10) {
|
|
393
|
+
const totalDocs = items.length;
|
|
394
|
+
const totalPages = Math.ceil(items.length / limit);
|
|
395
|
+
const hasNextPage = page < totalPages;
|
|
396
|
+
const hasPrevPage = page > 1;
|
|
397
|
+
const index = page - 1;
|
|
398
|
+
const from = index * limit;
|
|
399
|
+
const to = Math.min(page * limit, totalDocs);
|
|
400
|
+
const docs = items.slice(from, to);
|
|
401
|
+
const pagination = {
|
|
402
|
+
totalDocs,
|
|
403
|
+
totalPages,
|
|
404
|
+
page,
|
|
405
|
+
limit,
|
|
406
|
+
hasNextPage,
|
|
407
|
+
hasPrevPage,
|
|
408
|
+
nextPage: hasNextPage ? page + 1 : null,
|
|
409
|
+
prevPage: hasPrevPage ? page - 1 : null,
|
|
410
|
+
pagingCounter: from + 1
|
|
411
|
+
};
|
|
412
|
+
return {
|
|
413
|
+
docs,
|
|
414
|
+
...pagination
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
// ^ sort
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
// src/localized/localized.ts
|
|
421
|
+
function isLocalizedString(value) {
|
|
422
|
+
let isLocalizedString2 = false;
|
|
423
|
+
if (value && !Array.isArray(value) && typeof value === "object") {
|
|
424
|
+
const matchKeys = Object.keys(value).reduce((p, c) => p && /^(\w{2})(-\w{2})?$/.test(c), true);
|
|
425
|
+
const matchValues = Object.values(value).reduce((p, c) => p && (typeof c === "string" || !c), true);
|
|
426
|
+
isLocalizedString2 = Boolean(matchKeys && matchValues);
|
|
427
|
+
}
|
|
428
|
+
return isLocalizedString2;
|
|
429
|
+
}
|
|
430
|
+
function localizedToString(json, locale = "en", defaultLocale = "en") {
|
|
431
|
+
const localizedString = json[locale] || json[defaultLocale] || Object.values(json)[0];
|
|
432
|
+
return localizedString;
|
|
433
|
+
}
|
|
434
|
+
function localizeValue(value, locale = "en", defaultLocale = "en") {
|
|
435
|
+
if (value) {
|
|
436
|
+
if (isLocalizedString(value)) {
|
|
437
|
+
return localizedToString(value, locale, defaultLocale);
|
|
438
|
+
} else {
|
|
439
|
+
return localizeItem(value, locale, defaultLocale);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
function localizeItem(item, locale = "en", defaultLocale = "en") {
|
|
444
|
+
if (Array.isArray(item)) {
|
|
445
|
+
item = item.map((x) => localizeValue(x, locale, defaultLocale));
|
|
446
|
+
} else if (typeof item === "object") {
|
|
447
|
+
item = { ...item };
|
|
448
|
+
Object.keys(item).forEach((key) => {
|
|
449
|
+
item[key] = localizeValue(item[key], locale, defaultLocale);
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
return item;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// src/qs/qs.ts
|
|
456
|
+
function serializeKey(key, parentKey = "") {
|
|
457
|
+
return parentKey ? `${parentKey}[${key}]` : String(key);
|
|
458
|
+
}
|
|
459
|
+
function serializeParam(key, value, serializedParams = [], parentKey = "") {
|
|
460
|
+
if (value != null) {
|
|
461
|
+
if (typeof value === "object") {
|
|
462
|
+
serializedParams = serializeParams(value, serializedParams, serializeKey(key, parentKey));
|
|
463
|
+
} else {
|
|
464
|
+
serializedParams.push({
|
|
465
|
+
key: serializeKey(key, parentKey),
|
|
466
|
+
value
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return serializedParams;
|
|
471
|
+
}
|
|
472
|
+
function serializeParams(params, serializedParams = [], parentKey = "") {
|
|
473
|
+
if (Array.isArray(params)) {
|
|
474
|
+
params.forEach((value, key) => {
|
|
475
|
+
serializedParams = serializeParam(key, value, serializedParams, parentKey);
|
|
476
|
+
});
|
|
477
|
+
} else {
|
|
478
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
479
|
+
serializedParams = serializeParam(key, value, serializedParams, parentKey);
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
return serializedParams;
|
|
483
|
+
}
|
|
484
|
+
function qsSerialize(queryParams = {}) {
|
|
485
|
+
const params = serializeParams(queryParams);
|
|
486
|
+
if (params.length) {
|
|
487
|
+
return params.map((x) => `${x.key}=${x.value}`).join("&");
|
|
488
|
+
}
|
|
489
|
+
return "";
|
|
490
|
+
}
|
|
491
|
+
function asEquatable(value) {
|
|
492
|
+
if (String(parseInt(value)) === value) {
|
|
493
|
+
return Number(value);
|
|
494
|
+
}
|
|
495
|
+
return value;
|
|
496
|
+
}
|
|
497
|
+
function deserializeValue(value) {
|
|
498
|
+
if (String(parseInt(value)) === value) {
|
|
499
|
+
return Number(value);
|
|
500
|
+
} else if (String(value) === "true") {
|
|
501
|
+
return true;
|
|
502
|
+
} else if (String(value) === "false") {
|
|
503
|
+
return false;
|
|
504
|
+
}
|
|
505
|
+
return value;
|
|
506
|
+
}
|
|
507
|
+
function deserializeKey(value) {
|
|
508
|
+
if (String(parseInt(value)) === value) {
|
|
509
|
+
return Number(value);
|
|
510
|
+
}
|
|
511
|
+
return value;
|
|
512
|
+
}
|
|
513
|
+
function deserializeKeys(key) {
|
|
514
|
+
const keys = key.replace(/[\]]/g, "").split("[").map((x) => deserializeKey(x));
|
|
515
|
+
return keys;
|
|
516
|
+
}
|
|
517
|
+
function getNextKeyItem(key) {
|
|
518
|
+
if (typeof key === "number") {
|
|
519
|
+
return [];
|
|
520
|
+
} else {
|
|
521
|
+
return {};
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
function deserializeParam(key, value, deserializedParams = {}) {
|
|
525
|
+
const keys = deserializeKeys(key);
|
|
526
|
+
let target = deserializedParams;
|
|
527
|
+
while (keys.length) {
|
|
528
|
+
const key2 = keys.shift();
|
|
529
|
+
if (keys.length === 0) {
|
|
530
|
+
target[key2] = deserializeValue(value);
|
|
531
|
+
} else {
|
|
532
|
+
const next = target[key2] ? target[key2] : target[key2] = getNextKeyItem(keys[0]);
|
|
533
|
+
target = next;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return deserializedParams;
|
|
537
|
+
}
|
|
538
|
+
function deserializeParams(params, deserializedParams = {}) {
|
|
539
|
+
params.forEach((x) => deserializeParam(x[0], x[1], deserializedParams));
|
|
540
|
+
return deserializedParams;
|
|
541
|
+
}
|
|
542
|
+
function qsDeserialize(query = "", deserializedParams = {}) {
|
|
543
|
+
if (query.indexOf("?") === 0) {
|
|
544
|
+
query = query.substring(1);
|
|
545
|
+
}
|
|
546
|
+
if (query !== "") {
|
|
547
|
+
const params = query.split("&").map((x) => x.split("="));
|
|
548
|
+
if (params.length) {
|
|
549
|
+
return deserializeParams(params, deserializedParams);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
return deserializedParams;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
// src/slug/slug.ts
|
|
556
|
+
function titleToSlug(title) {
|
|
557
|
+
return title.toString().normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/\s+/g, "-").replace(/&/g, "-and-").replace(/[^\w-]+/g, "").replace(/--+/g, "-").replace(/^-+/, "").replace(/-+$/, "");
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// src/types/querable.ts
|
|
561
|
+
function toFindParams(idOrParams) {
|
|
562
|
+
if (typeof idOrParams === "number" || typeof idOrParams === "string") {
|
|
563
|
+
return { where: { id: idOrParams } };
|
|
564
|
+
} else {
|
|
565
|
+
return idOrParams;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
export {
|
|
569
|
+
JsonService,
|
|
570
|
+
asEquatable,
|
|
571
|
+
asServerProps,
|
|
572
|
+
awaitAll,
|
|
573
|
+
camelToSnakeCase,
|
|
574
|
+
consoleWarn,
|
|
575
|
+
deepCopy,
|
|
576
|
+
deserializeKey,
|
|
577
|
+
deserializeValue,
|
|
578
|
+
getClassNames,
|
|
579
|
+
getCollectionDescription,
|
|
580
|
+
getIsDevelopment,
|
|
581
|
+
httpDelete,
|
|
582
|
+
httpFetch,
|
|
583
|
+
httpGet,
|
|
584
|
+
httpPatch,
|
|
585
|
+
httpPost,
|
|
586
|
+
httpPut,
|
|
587
|
+
isBrowser,
|
|
588
|
+
isDevelopment,
|
|
589
|
+
isLocalizedString,
|
|
590
|
+
isValidCollectionDescription,
|
|
591
|
+
localizeItem,
|
|
592
|
+
localizeValue,
|
|
593
|
+
localizedToString,
|
|
594
|
+
merge,
|
|
595
|
+
qsDeserialize,
|
|
596
|
+
qsSerialize,
|
|
597
|
+
titleToSlug,
|
|
598
|
+
toDisplayName,
|
|
599
|
+
toFindParams,
|
|
600
|
+
toPluralName,
|
|
601
|
+
toTitleCase,
|
|
602
|
+
toValidCollectionName,
|
|
603
|
+
withSchema
|
|
604
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@websolutespa/bom-core",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Core module of the BOM Repository",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bom",
|
|
7
|
+
"mixer",
|
|
8
|
+
"core"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git://github.com/websolutespa/bom.git"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"lint": "eslint *.ts*",
|
|
17
|
+
"test": "jest",
|
|
18
|
+
"test-watch": "npm test -- --watch",
|
|
19
|
+
"coverage:integration": "npm run test -- --coverage",
|
|
20
|
+
"coverage": "run-s -c --silent coverage:*",
|
|
21
|
+
"compile": "tsup ./src/index.ts --format esm,cjs --dts --external react",
|
|
22
|
+
"prepack": "bom prepack",
|
|
23
|
+
"postpack": "bom postpack"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"next": ">= 12.3.2",
|
|
27
|
+
"pluralize": ">= 8.0.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@websolutespa/bom-cli": "*",
|
|
31
|
+
"@websolutespa/test": "*",
|
|
32
|
+
"@websolutespa/tsconfig": "*",
|
|
33
|
+
"eslint": "^8.36.0",
|
|
34
|
+
"eslint-config-websolute": "*",
|
|
35
|
+
"npm-run-all": "^4.1.5",
|
|
36
|
+
"raw-loader": "^4.0.2",
|
|
37
|
+
"ts-node": "^10.9.1",
|
|
38
|
+
"tsup": "^6.7.0",
|
|
39
|
+
"typescript": "^4.8.4"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"sideEffects": false,
|
|
45
|
+
"prepack": {
|
|
46
|
+
"types": "./dist/index.d.ts",
|
|
47
|
+
"main": "./dist/index.js",
|
|
48
|
+
"module": "./dist/index.mjs"
|
|
49
|
+
},
|
|
50
|
+
"postpack": {
|
|
51
|
+
"types": "./src/index.ts",
|
|
52
|
+
"main": "./src/index.ts"
|
|
53
|
+
},
|
|
54
|
+
"types": "./dist/index.d.ts",
|
|
55
|
+
"main": "./dist/index.js",
|
|
56
|
+
"module": "./dist/index.mjs"
|
|
57
|
+
}
|