@rsdk/common 2.4.2 → 2.6.0-next.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/CHANGELOG.md +22 -0
- package/dist/functions/misc.d.ts +0 -16
- package/dist/functions/misc.js +1 -30
- package/dist/functions/misc.js.map +1 -1
- package/dist/index.d.ts +1 -4
- package/dist/index.js +1 -17
- package/dist/index.js.map +1 -1
- package/dist/size/size.class.js +2 -2
- package/dist/size/size.class.js.map +1 -1
- package/dist/strings/capitalize.fn.d.ts +7 -0
- package/dist/{functions/capitalize.js → strings/capitalize.fn.js} +7 -1
- package/dist/strings/capitalize.fn.js.map +1 -0
- package/dist/strings/case.d.ts +15 -0
- package/dist/strings/case.js +24 -0
- package/dist/strings/case.js.map +1 -0
- package/dist/strings/index.d.ts +3 -0
- package/dist/{serialization/errors → strings}/index.js +3 -3
- package/dist/strings/index.js.map +1 -0
- package/dist/strings/text.fn.d.ts +10 -0
- package/dist/strings/text.fn.js +21 -0
- package/dist/strings/text.fn.js.map +1 -0
- package/dist/time/timespan.class.d.ts +12 -5
- package/dist/time/timespan.class.js +32 -19
- package/dist/time/timespan.class.js.map +1 -1
- package/package.json +4 -6
- package/src/functions/misc.ts +0 -40
- package/src/index.ts +1 -5
- package/src/size/size.class.ts +1 -1
- package/src/strings/capitalize.fn.ts +8 -0
- package/src/strings/case.ts +20 -0
- package/src/strings/index.ts +3 -0
- package/src/strings/text.fn.ts +21 -0
- package/src/time/timespan.class.ts +35 -29
- package/src/time/timespan.spec.ts +28 -1
- package/dist/functions/capitalize.d.ts +0 -1
- package/dist/functions/capitalize.js.map +0 -1
- package/dist/serialization/errors/index.d.ts +0 -3
- package/dist/serialization/errors/index.js.map +0 -1
- package/dist/serialization/errors/not-absolute-path.error.d.ts +0 -3
- package/dist/serialization/errors/not-absolute-path.error.js +0 -10
- package/dist/serialization/errors/not-absolute-path.error.js.map +0 -1
- package/dist/serialization/errors/unknown-file-ext.error.d.ts +0 -3
- package/dist/serialization/errors/unknown-file-ext.error.js +0 -10
- package/dist/serialization/errors/unknown-file-ext.error.js.map +0 -1
- package/dist/serialization/errors/unknown-format.error.d.ts +0 -5
- package/dist/serialization/errors/unknown-format.error.js +0 -11
- package/dist/serialization/errors/unknown-format.error.js.map +0 -1
- package/dist/serialization/index.d.ts +0 -5
- package/dist/serialization/index.js +0 -22
- package/dist/serialization/index.js.map +0 -1
- package/dist/serialization/read-obj-sync.d.ts +0 -9
- package/dist/serialization/read-obj-sync.js +0 -25
- package/dist/serialization/read-obj-sync.js.map +0 -1
- package/dist/serialization/read-obj.d.ts +0 -8
- package/dist/serialization/read-obj.js +0 -24
- package/dist/serialization/read-obj.js.map +0 -1
- package/dist/serialization/reviving-serializer.d.ts +0 -8
- package/dist/serialization/reviving-serializer.js +0 -30
- package/dist/serialization/reviving-serializer.js.map +0 -1
- package/dist/serialization/save-obj.d.ts +0 -10
- package/dist/serialization/save-obj.js +0 -25
- package/dist/serialization/save-obj.js.map +0 -1
- package/dist/serialization/serializer.class.d.ts +0 -14
- package/dist/serialization/serializer.class.js +0 -65
- package/dist/serialization/serializer.class.js.map +0 -1
- package/dist/types/nest-module-definition.d.ts +0 -3
- package/dist/types/nest-module-definition.js +0 -3
- package/dist/types/nest-module-definition.js.map +0 -1
- package/src/functions/capitalize.ts +0 -2
- package/src/serialization/errors/index.ts +0 -3
- package/src/serialization/errors/not-absolute-path.error.ts +0 -5
- package/src/serialization/errors/unknown-file-ext.error.ts +0 -5
- package/src/serialization/errors/unknown-format.error.ts +0 -9
- package/src/serialization/index.ts +0 -5
- package/src/serialization/read-obj-sync.ts +0 -24
- package/src/serialization/read-obj.ts +0 -23
- package/src/serialization/reviving-serializer.ts +0 -32
- package/src/serialization/save-obj.ts +0 -31
- package/src/serialization/serializer.class.ts +0 -85
- package/src/types/nest-module-definition.ts +0 -5
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { promises as fs } from 'node:fs';
|
|
2
|
-
import { isAbsolute } from 'node:path';
|
|
3
|
-
|
|
4
|
-
import { NotAbsolutePathError } from './errors';
|
|
5
|
-
import { Serializer } from './serializer.class';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Reads file into an object.
|
|
9
|
-
* Supported formats: json, yaml, yml, .env
|
|
10
|
-
*
|
|
11
|
-
* @param {string} pathToFile Absolute path to file.
|
|
12
|
-
* @returns {any}
|
|
13
|
-
*/
|
|
14
|
-
export const readObj = async (pathToFile: string): Promise<unknown> => {
|
|
15
|
-
if (!isAbsolute(pathToFile)) {
|
|
16
|
-
throw new NotAbsolutePathError(pathToFile);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const format = Serializer.getFormat(pathToFile);
|
|
20
|
-
const content = await fs.readFile(pathToFile);
|
|
21
|
-
|
|
22
|
-
return Serializer.parse(content.toString(), format);
|
|
23
|
-
};
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* For serializing and deserializing objects with methods.
|
|
3
|
-
* See more in https://clck.ru/Qc3e4
|
|
4
|
-
* */
|
|
5
|
-
export class RevivingSerializer {
|
|
6
|
-
static serialize<T>(obj: T): string {
|
|
7
|
-
return JSON.stringify(obj, (_key: string, value: unknown): unknown => {
|
|
8
|
-
if (typeof value === 'function') {
|
|
9
|
-
return value.toString();
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
return value;
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
static deserialize<T>(serializedObject: string): T {
|
|
17
|
-
return JSON.parse(
|
|
18
|
-
serializedObject,
|
|
19
|
-
(_key: string, value: unknown): unknown => {
|
|
20
|
-
if (typeof value === 'string' && value.startsWith('function')) {
|
|
21
|
-
const functionTemplate = `(${value})`;
|
|
22
|
-
|
|
23
|
-
// eval is the best the best way to revive function from serialized object.
|
|
24
|
-
// eslint-disable-next-line no-eval
|
|
25
|
-
return eval(functionTemplate) as unknown;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return value;
|
|
29
|
-
},
|
|
30
|
-
) as T;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { promises as fs } from 'node:fs';
|
|
2
|
-
import { isAbsolute } from 'node:path';
|
|
3
|
-
|
|
4
|
-
import { NotAbsolutePathError } from './errors';
|
|
5
|
-
import { Serializer } from './serializer.class';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Saves object into file.
|
|
9
|
-
* Supported formats: json, yaml, yml, .env
|
|
10
|
-
*
|
|
11
|
-
* @param {*} obj
|
|
12
|
-
* @param {string} filename Absolute path to file.
|
|
13
|
-
* @param {string} [format] If not specified serializer will try
|
|
14
|
-
* to recognize it by file extension.
|
|
15
|
-
*/
|
|
16
|
-
export const saveObj = async (
|
|
17
|
-
obj: Record<string, unknown>,
|
|
18
|
-
filename: string,
|
|
19
|
-
format?: string,
|
|
20
|
-
): Promise<void> => {
|
|
21
|
-
if (!isAbsolute(filename)) {
|
|
22
|
-
throw new NotAbsolutePathError(filename);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const text = Serializer.stringify(
|
|
26
|
-
obj,
|
|
27
|
-
format || Serializer.getFormat(filename),
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
await fs.writeFile(filename, `${text}\n`);
|
|
31
|
-
};
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import dotenv from 'dotenv';
|
|
2
|
-
import { extname } from 'node:path';
|
|
3
|
-
import TOML from 'toml';
|
|
4
|
-
import YAML from 'yaml';
|
|
5
|
-
|
|
6
|
-
import { UnknownFileExtensionError, UnknownFormatError } from './errors';
|
|
7
|
-
|
|
8
|
-
export type Parser = (text: string) => unknown;
|
|
9
|
-
export type Stringifier = (obj: Record<string, unknown>) => string;
|
|
10
|
-
export type Format = string;
|
|
11
|
-
export type Extension = string;
|
|
12
|
-
|
|
13
|
-
const INDENT = 2;
|
|
14
|
-
|
|
15
|
-
const stringifyEnv = (obj): string =>
|
|
16
|
-
Object.entries(obj)
|
|
17
|
-
.map(
|
|
18
|
-
([key, value]) =>
|
|
19
|
-
`${key}=${typeof value === 'object' ? JSON.stringify(obj) : value}`,
|
|
20
|
-
)
|
|
21
|
-
.join('\n');
|
|
22
|
-
|
|
23
|
-
export class Serializer {
|
|
24
|
-
private static readonly parsers = new Map<Format, Parser>()
|
|
25
|
-
.set('json', (text) => JSON.parse(text))
|
|
26
|
-
.set('yaml', (text) => YAML.parse(text))
|
|
27
|
-
.set('toml', (text) => TOML.parse(text))
|
|
28
|
-
.set('env', dotenv.parse);
|
|
29
|
-
|
|
30
|
-
private static readonly stringifiers = new Map<Format, Stringifier>()
|
|
31
|
-
.set('json', (obj) => JSON.stringify(obj, null, INDENT))
|
|
32
|
-
.set('yaml', (obj) => YAML.stringify(obj))
|
|
33
|
-
.set('env', stringifyEnv);
|
|
34
|
-
|
|
35
|
-
private static readonly extensions = new Map<Extension, Format>()
|
|
36
|
-
.set('.json', 'json')
|
|
37
|
-
.set('.yaml', 'yaml')
|
|
38
|
-
.set('.yml', 'yaml')
|
|
39
|
-
.set('.toml', 'toml')
|
|
40
|
-
.set('.env', 'env');
|
|
41
|
-
|
|
42
|
-
static getFormat(filename: string): Format {
|
|
43
|
-
let ext = extname(filename);
|
|
44
|
-
|
|
45
|
-
if (!ext && filename.includes('.env')) {
|
|
46
|
-
ext = '.env';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const format = Serializer.extensions.get(ext);
|
|
50
|
-
|
|
51
|
-
if (!format) {
|
|
52
|
-
throw new UnknownFileExtensionError(ext);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return format;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
static stringify(obj: Record<string, unknown>, format: Format): string {
|
|
59
|
-
const stringifier = Serializer.stringifiers.get(format);
|
|
60
|
-
|
|
61
|
-
if (!stringifier) {
|
|
62
|
-
throw new UnknownFormatError(format);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return stringifier(obj);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
static parse(text: string, format: Format): unknown {
|
|
69
|
-
const parser = Serializer.parsers.get(format);
|
|
70
|
-
|
|
71
|
-
if (!parser) {
|
|
72
|
-
throw new UnknownFormatError(format);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return parser(text);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
static useParser(format: Format, parser: Parser): void {
|
|
79
|
-
Serializer.parsers.set(format, parser);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
static useStringifier(format: Format, stringifier: Stringifier): void {
|
|
83
|
-
Serializer.stringifiers.set(format, stringifier);
|
|
84
|
-
}
|
|
85
|
-
}
|