mblabs-roccato-backend-commons 1.0.42 → 1.0.44
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/services/file.d.ts +1 -1
- package/dist/services/file.js +8 -5
- package/dist/utils/parser.d.ts +1 -0
- package/dist/utils/parser.js +4 -0
- package/package.json +1 -1
- package/src/services/file.ts +11 -6
- package/src/utils/parser.ts +4 -0
package/dist/services/file.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
declare class FileService {
|
|
2
2
|
readTemplate(path: string, encoding: string): Promise<unknown>;
|
|
3
|
-
fillTemplate(template: string, replacements: Record<string, string | number | boolean>,
|
|
3
|
+
fillTemplate(template: string, replacements: Record<string, string | number | boolean>, delimiters: [string, string][]): string;
|
|
4
4
|
getBase64Metadata(file: string): {
|
|
5
5
|
buffer: Buffer<ArrayBuffer>;
|
|
6
6
|
extension: string;
|
package/dist/services/file.js
CHANGED
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const fs_1 = require("fs");
|
|
7
|
+
const utils_1 = require("../utils");
|
|
7
8
|
const date_1 = __importDefault(require("./date"));
|
|
8
9
|
class FileService {
|
|
9
10
|
readTemplate(path, encoding) {
|
|
@@ -17,11 +18,13 @@ class FileService {
|
|
|
17
18
|
});
|
|
18
19
|
return promise;
|
|
19
20
|
}
|
|
20
|
-
fillTemplate(template, replacements,
|
|
21
|
-
Object.
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
fillTemplate(template, replacements, delimiters) {
|
|
22
|
+
for (const [key, value] of Object.entries(replacements)) {
|
|
23
|
+
for (const [open, close] of delimiters) {
|
|
24
|
+
const regex = new RegExp(`${(0, utils_1.parseRegex)(open)}${key}${(0, utils_1.parseRegex)(close)}`, 'g');
|
|
25
|
+
template = template.replace(regex, String(value ?? ''));
|
|
26
|
+
}
|
|
27
|
+
}
|
|
25
28
|
return template;
|
|
26
29
|
}
|
|
27
30
|
getBase64Metadata(file) {
|
package/dist/utils/parser.d.ts
CHANGED
package/dist/utils/parser.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseRecord = parseRecord;
|
|
4
|
+
exports.parseRegex = parseRegex;
|
|
4
5
|
function parseRecord(record) {
|
|
5
6
|
let parsed;
|
|
6
7
|
try {
|
|
@@ -11,3 +12,6 @@ function parseRecord(record) {
|
|
|
11
12
|
}
|
|
12
13
|
return parsed;
|
|
13
14
|
}
|
|
15
|
+
function parseRegex(text) {
|
|
16
|
+
return text.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
17
|
+
}
|
package/package.json
CHANGED
package/src/services/file.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readFile } from 'fs';
|
|
2
2
|
|
|
3
|
+
import { parseRegex } from '../utils';
|
|
3
4
|
import DateService from './date';
|
|
4
5
|
|
|
5
6
|
class FileService {
|
|
@@ -20,13 +21,17 @@ class FileService {
|
|
|
20
21
|
public fillTemplate (
|
|
21
22
|
template: string,
|
|
22
23
|
replacements: Record<string, string | number | boolean>,
|
|
23
|
-
|
|
24
|
+
delimiters: [string, string][]
|
|
24
25
|
): string {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
for (const [ key, value ] of Object.entries(replacements)) {
|
|
27
|
+
for (const [ open, close ] of delimiters) {
|
|
28
|
+
const regex = new RegExp(
|
|
29
|
+
`${parseRegex(open)}${key}${parseRegex(close)}`,
|
|
30
|
+
'g'
|
|
31
|
+
);
|
|
32
|
+
template = template.replace(regex, String(value ?? ''));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
30
35
|
|
|
31
36
|
return template;
|
|
32
37
|
}
|