@ldsg/utils 0.3.29 → 0.3.31

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/dist/browser.cjs CHANGED
@@ -1,5 +1,14 @@
1
1
  "use strict";
2
2
  var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
3
12
  (()=>{
4
13
  __webpack_require__.d = (exports1, definition)=>{
5
14
  for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
@@ -55,10 +64,19 @@ const getTreeResourceRecords = (params)=>{
55
64
  };
56
65
  return res;
57
66
  };
67
+ const external_lodash_namespaceObject = require("lodash");
68
+ var external_lodash_default = /*#__PURE__*/ __webpack_require__.n(external_lodash_namespaceObject);
58
69
  const replaceResourceIdsInString = (params)=>{
59
70
  const { jsonString, oldResourceIdToNewResourceIdMap } = params;
71
+ const invalidOldResourceIds = external_lodash_default().filter(Object.keys(oldResourceIdToNewResourceIdMap), (id)=>!id || !/^[a-zA-Z0-9_-]+$/.test(id)).map((id)=>id || '(empty)');
72
+ if (invalidOldResourceIds.length > 0) throw new Error(`Invalid old resource IDs: ${invalidOldResourceIds.join(', ')}. Only [a-zA-Z0-9_-] are allowed and must not be empty.`);
60
73
  let result = jsonString;
61
- for (const [oldResourceId, newResourceId] of Object.entries(oldResourceIdToNewResourceIdMap))result = result.split(oldResourceId).join(newResourceId);
74
+ const entries = Object.entries(oldResourceIdToNewResourceIdMap).sort((a, b)=>b[0].length - a[0].length);
75
+ for (const [oldResourceId, newResourceId] of entries){
76
+ const escaped = oldResourceId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
77
+ const regex = new RegExp(`(?<=^|[^a-zA-Z0-9_-])${escaped}(?=$|[^a-zA-Z0-9_-])`, 'g');
78
+ result = result.replace(regex, newResourceId);
79
+ }
62
80
  const res = {
63
81
  jsonString: result
64
82
  };
package/dist/browser.js CHANGED
@@ -1,3 +1,4 @@
1
+ import lodash from "lodash";
1
2
  const getTreeResourceRecords = (params)=>{
2
3
  const { resourceRecords, rootId } = params;
3
4
  const childrenMap = new Map();
@@ -28,8 +29,15 @@ const getTreeResourceRecords = (params)=>{
28
29
  };
29
30
  const replaceResourceIdsInString = (params)=>{
30
31
  const { jsonString, oldResourceIdToNewResourceIdMap } = params;
32
+ const invalidOldResourceIds = lodash.filter(Object.keys(oldResourceIdToNewResourceIdMap), (id)=>!id || !/^[a-zA-Z0-9_-]+$/.test(id)).map((id)=>id || '(empty)');
33
+ if (invalidOldResourceIds.length > 0) throw new Error(`Invalid old resource IDs: ${invalidOldResourceIds.join(', ')}. Only [a-zA-Z0-9_-] are allowed and must not be empty.`);
31
34
  let result = jsonString;
32
- for (const [oldResourceId, newResourceId] of Object.entries(oldResourceIdToNewResourceIdMap))result = result.split(oldResourceId).join(newResourceId);
35
+ const entries = Object.entries(oldResourceIdToNewResourceIdMap).sort((a, b)=>b[0].length - a[0].length);
36
+ for (const [oldResourceId, newResourceId] of entries){
37
+ const escaped = oldResourceId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
38
+ const regex = new RegExp(`(?<=^|[^a-zA-Z0-9_-])${escaped}(?=$|[^a-zA-Z0-9_-])`, 'g');
39
+ result = result.replace(regex, newResourceId);
40
+ }
33
41
  const res = {
34
42
  jsonString: result
35
43
  };
package/dist/index.cjs CHANGED
@@ -265,18 +265,14 @@ const readSourceData = async (uri)=>{
265
265
  if (external_path_default().isAbsolute(uri)) return external_fs_extra_default().readJson(uri);
266
266
  return external_fs_extra_default().readJson(external_path_default().resolve(process.cwd(), uri));
267
267
  };
268
- const normalizeSourceData = async (rawData, expression)=>{
269
- if (expression) {
270
- const exp = external_jsonata_default()(expression);
271
- return await exp.evaluate(rawData);
272
- }
273
- return rawData;
274
- };
275
268
  const loadAppResourceRecords = async (params)=>{
276
269
  const { appDataUri, apaasAppDataUri, apaasAppDataExpression } = params;
277
270
  if (apaasAppDataUri) {
278
271
  const rawData = await readSourceData(apaasAppDataUri);
279
- const { resourceRecords } = await normalizeSourceData(rawData, apaasAppDataExpression);
272
+ const expression = apaasAppDataExpression || '$';
273
+ const exp = external_jsonata_default()(expression);
274
+ const appData = await exp.evaluate(rawData);
275
+ const { resourceRecords } = appData;
280
276
  const apaasRecords = resourceRecords.map((record)=>({
281
277
  id: record.id,
282
278
  kind: record.kind,
@@ -371,8 +367,15 @@ const prepare = async (params)=>{
371
367
  };
372
368
  const replaceResourceIdsInString = (params)=>{
373
369
  const { jsonString, oldResourceIdToNewResourceIdMap } = params;
370
+ const invalidOldResourceIds = external_lodash_default().filter(Object.keys(oldResourceIdToNewResourceIdMap), (id)=>!id || !/^[a-zA-Z0-9_-]+$/.test(id)).map((id)=>id || '(empty)');
371
+ if (invalidOldResourceIds.length > 0) throw new Error(`Invalid old resource IDs: ${invalidOldResourceIds.join(', ')}. Only [a-zA-Z0-9_-] are allowed and must not be empty.`);
374
372
  let result = jsonString;
375
- for (const [oldResourceId, newResourceId] of Object.entries(oldResourceIdToNewResourceIdMap))result = result.split(oldResourceId).join(newResourceId);
373
+ const entries = Object.entries(oldResourceIdToNewResourceIdMap).sort((a, b)=>b[0].length - a[0].length);
374
+ for (const [oldResourceId, newResourceId] of entries){
375
+ const escaped = oldResourceId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
376
+ const regex = new RegExp(`(?<=^|[^a-zA-Z0-9_-])${escaped}(?=$|[^a-zA-Z0-9_-])`, 'g');
377
+ result = result.replace(regex, newResourceId);
378
+ }
376
379
  const res = {
377
380
  jsonString: result
378
381
  };
package/dist/index.js CHANGED
@@ -203,18 +203,14 @@ const readSourceData = async (uri)=>{
203
203
  if (path_0.isAbsolute(uri)) return fs_extra.readJson(uri);
204
204
  return fs_extra.readJson(path_0.resolve(process.cwd(), uri));
205
205
  };
206
- const normalizeSourceData = async (rawData, expression)=>{
207
- if (expression) {
208
- const exp = jsonata(expression);
209
- return await exp.evaluate(rawData);
210
- }
211
- return rawData;
212
- };
213
206
  const loadAppResourceRecords = async (params)=>{
214
207
  const { appDataUri, apaasAppDataUri, apaasAppDataExpression } = params;
215
208
  if (apaasAppDataUri) {
216
209
  const rawData = await readSourceData(apaasAppDataUri);
217
- const { resourceRecords } = await normalizeSourceData(rawData, apaasAppDataExpression);
210
+ const expression = apaasAppDataExpression || '$';
211
+ const exp = jsonata(expression);
212
+ const appData = await exp.evaluate(rawData);
213
+ const { resourceRecords } = appData;
218
214
  const apaasRecords = resourceRecords.map((record)=>({
219
215
  id: record.id,
220
216
  kind: record.kind,
@@ -307,8 +303,15 @@ const prepare = async (params)=>{
307
303
  };
308
304
  const replaceResourceIdsInString = (params)=>{
309
305
  const { jsonString, oldResourceIdToNewResourceIdMap } = params;
306
+ const invalidOldResourceIds = lodash.filter(Object.keys(oldResourceIdToNewResourceIdMap), (id)=>!id || !/^[a-zA-Z0-9_-]+$/.test(id)).map((id)=>id || '(empty)');
307
+ if (invalidOldResourceIds.length > 0) throw new Error(`Invalid old resource IDs: ${invalidOldResourceIds.join(', ')}. Only [a-zA-Z0-9_-] are allowed and must not be empty.`);
310
308
  let result = jsonString;
311
- for (const [oldResourceId, newResourceId] of Object.entries(oldResourceIdToNewResourceIdMap))result = result.split(oldResourceId).join(newResourceId);
309
+ const entries = Object.entries(oldResourceIdToNewResourceIdMap).sort((a, b)=>b[0].length - a[0].length);
310
+ for (const [oldResourceId, newResourceId] of entries){
311
+ const escaped = oldResourceId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
312
+ const regex = new RegExp(`(?<=^|[^a-zA-Z0-9_-])${escaped}(?=$|[^a-zA-Z0-9_-])`, 'g');
313
+ result = result.replace(regex, newResourceId);
314
+ }
312
315
  const res = {
313
316
  jsonString: result
314
317
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ldsg/utils",
3
- "version": "0.3.29",
3
+ "version": "0.3.31",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "wanxger",
@@ -33,11 +33,11 @@
33
33
  "jsonata": "^2.0.6",
34
34
  "jszip": "^3.10.1",
35
35
  "lodash": "^4.17.21",
36
- "@ldsg/apaas-constants": "0.3.29",
37
- "@ldsg/handler": "0.3.29",
38
- "@ldsg/core": "0.3.29",
39
- "@ldsg/constants": "0.3.29",
40
- "@ldsg/types": "0.3.29"
36
+ "@ldsg/apaas-constants": "0.3.31",
37
+ "@ldsg/constants": "0.3.31",
38
+ "@ldsg/core": "0.3.31",
39
+ "@ldsg/handler": "0.3.31",
40
+ "@ldsg/types": "0.3.31"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@rslib/core": "^0.17.1",