namirasoft-core 1.4.24 → 1.4.25
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/BaseMetaDatabase.d.ts +1 -0
- package/dist/BaseMetaDatabase.js +3 -0
- package/dist/BaseMetaDatabase.js.map +1 -1
- package/dist/BaseMetaTable.d.ts +1 -0
- package/dist/BaseMetaTable.js +3 -0
- package/dist/BaseMetaTable.js.map +1 -1
- package/dist/EncryptionOperation.js +7 -17
- package/dist/EncryptionOperation.js.map +1 -1
- package/dist/HashOperation.js +7 -17
- package/dist/HashOperation.js.map +1 -1
- package/dist/PhoneOperation.js +7 -17
- package/dist/PhoneOperation.js.map +1 -1
- package/package.json +26 -26
- package/src/BaseDatabaseRow.ts +6 -6
- package/src/BaseMetaColumn.ts +17 -17
- package/src/BaseMetaDatabase.ts +30 -26
- package/src/BaseMetaTable.ts +40 -36
- package/src/BaseServer.ts +113 -113
- package/src/BaseUUID.ts +55 -55
- package/src/CacheService.ts +57 -57
- package/src/ConsoleOperation.ts +68 -68
- package/src/ConvertService.ts +100 -100
- package/src/CookieService.ts +33 -33
- package/src/Countries.ts +486 -486
- package/src/Country.ts +21 -21
- package/src/CountryOperation.ts +98 -98
- package/src/EncodingOperation.ts +12 -12
- package/src/EncryptionOperation.ts +40 -40
- package/src/EnvService.ts +22 -22
- package/src/ErrorOperation.ts +13 -13
- package/src/FileOperation.ts +57 -57
- package/src/FilterItem.ts +128 -128
- package/src/FilterItemColumnType.ts +6 -6
- package/src/FilterItemOperator.ts +51 -51
- package/src/GeoOperation.ts +18 -18
- package/src/HTTPError.ts +8 -8
- package/src/HTTPMethod.ts +6 -6
- package/src/HashOperation.ts +24 -24
- package/src/IStorage.ts +5 -5
- package/src/IStorageCookie.ts +52 -52
- package/src/IStorageJsonFile.ts +45 -45
- package/src/IStorageLocal.ts +16 -16
- package/src/IStorageMemory.ts +17 -17
- package/src/NamingConvention.ts +145 -145
- package/src/ObjectService.ts +27 -27
- package/src/PackageService.ts +76 -76
- package/src/PhoneOperation.ts +8 -8
- package/src/PriceOperation.ts +18 -18
- package/src/RegexTemplate.ts +7 -7
- package/src/SearchOperation.ts +29 -29
- package/src/SortItem.ts +88 -88
- package/src/StringOperation.ts +18 -18
- package/src/TimeOperation.ts +262 -262
- package/src/URLOperation.ts +54 -54
- package/src/VersionOperation.ts +46 -46
- package/src/index.ts +42 -42
package/src/BaseUUID.ts
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { v4 } from "uuid";
|
|
2
|
-
|
|
3
|
-
export class BaseUUID
|
|
4
|
-
{
|
|
5
|
-
private static SIZE = 20;
|
|
6
|
-
static isValid(id: any)
|
|
7
|
-
{
|
|
8
|
-
let value = id + "";
|
|
9
|
-
if (value.length != BaseUUID.SIZE)
|
|
10
|
-
return false;
|
|
11
|
-
let parts = id.split("@")[0].split("-");
|
|
12
|
-
if (parts.length < 2)
|
|
13
|
-
return false;
|
|
14
|
-
return /^[a-z][a-z][a-z]$/.test(parts[0]);
|
|
15
|
-
}
|
|
16
|
-
static getShort(id: string)
|
|
17
|
-
{
|
|
18
|
-
let parts = id.split("@")[0].split("-");
|
|
19
|
-
let ans = [];
|
|
20
|
-
for (let index = 0; index < parts.length - 1; index++)
|
|
21
|
-
{
|
|
22
|
-
const part = parts[index];
|
|
23
|
-
if (/^[a-z][a-z][a-z]$/.test(part))
|
|
24
|
-
ans.push(part);
|
|
25
|
-
else
|
|
26
|
-
break;
|
|
27
|
-
}
|
|
28
|
-
return ans.join("-");
|
|
29
|
-
}
|
|
30
|
-
short: string;
|
|
31
|
-
constructor(short: string)
|
|
32
|
-
{
|
|
33
|
-
this.short = short;
|
|
34
|
-
}
|
|
35
|
-
static uuid(lenght: number = 16): string
|
|
36
|
-
{
|
|
37
|
-
return v4().replace(/-/g, '').substring(0, lenght);
|
|
38
|
-
}
|
|
39
|
-
static isChild(id: string): boolean
|
|
40
|
-
{
|
|
41
|
-
let index = id.indexOf("@")
|
|
42
|
-
return index != -1;
|
|
43
|
-
}
|
|
44
|
-
static changeToChild(owner_id: string, name: string): string
|
|
45
|
-
{
|
|
46
|
-
let index = owner_id.indexOf("@")
|
|
47
|
-
if (index != -1)
|
|
48
|
-
return owner_id.substring(0, index + 1) + name.substring(0, BaseUUID.SIZE - index - 1).padStart(BaseUUID.SIZE - index - 1, '-');
|
|
49
|
-
return "";
|
|
50
|
-
}
|
|
51
|
-
new()
|
|
52
|
-
{
|
|
53
|
-
let ans = this.short + "-" + BaseUUID.uuid();
|
|
54
|
-
return ans.substring(0, BaseUUID.SIZE);
|
|
55
|
-
}
|
|
1
|
+
import { v4 } from "uuid";
|
|
2
|
+
|
|
3
|
+
export class BaseUUID
|
|
4
|
+
{
|
|
5
|
+
private static SIZE = 20;
|
|
6
|
+
static isValid(id: any)
|
|
7
|
+
{
|
|
8
|
+
let value = id + "";
|
|
9
|
+
if (value.length != BaseUUID.SIZE)
|
|
10
|
+
return false;
|
|
11
|
+
let parts = id.split("@")[0].split("-");
|
|
12
|
+
if (parts.length < 2)
|
|
13
|
+
return false;
|
|
14
|
+
return /^[a-z][a-z][a-z]$/.test(parts[0]);
|
|
15
|
+
}
|
|
16
|
+
static getShort(id: string)
|
|
17
|
+
{
|
|
18
|
+
let parts = id.split("@")[0].split("-");
|
|
19
|
+
let ans = [];
|
|
20
|
+
for (let index = 0; index < parts.length - 1; index++)
|
|
21
|
+
{
|
|
22
|
+
const part = parts[index];
|
|
23
|
+
if (/^[a-z][a-z][a-z]$/.test(part))
|
|
24
|
+
ans.push(part);
|
|
25
|
+
else
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
return ans.join("-");
|
|
29
|
+
}
|
|
30
|
+
short: string;
|
|
31
|
+
constructor(short: string)
|
|
32
|
+
{
|
|
33
|
+
this.short = short;
|
|
34
|
+
}
|
|
35
|
+
static uuid(lenght: number = 16): string
|
|
36
|
+
{
|
|
37
|
+
return v4().replace(/-/g, '').substring(0, lenght);
|
|
38
|
+
}
|
|
39
|
+
static isChild(id: string): boolean
|
|
40
|
+
{
|
|
41
|
+
let index = id.indexOf("@")
|
|
42
|
+
return index != -1;
|
|
43
|
+
}
|
|
44
|
+
static changeToChild(owner_id: string, name: string): string
|
|
45
|
+
{
|
|
46
|
+
let index = owner_id.indexOf("@")
|
|
47
|
+
if (index != -1)
|
|
48
|
+
return owner_id.substring(0, index + 1) + name.substring(0, BaseUUID.SIZE - index - 1).padStart(BaseUUID.SIZE - index - 1, '-');
|
|
49
|
+
return "";
|
|
50
|
+
}
|
|
51
|
+
new()
|
|
52
|
+
{
|
|
53
|
+
let ans = this.short + "-" + BaseUUID.uuid();
|
|
54
|
+
return ans.substring(0, BaseUUID.SIZE);
|
|
55
|
+
}
|
|
56
56
|
};
|
package/src/CacheService.ts
CHANGED
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { IStorage } from "./IStorage";
|
|
2
|
-
|
|
3
|
-
interface NSCachStorage<DataType>
|
|
4
|
-
{
|
|
5
|
-
expires_at: Date;
|
|
6
|
-
data: DataType;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export class CacheService<DataType>
|
|
10
|
-
{
|
|
11
|
-
private name: string;
|
|
12
|
-
private duration_minutes: number;
|
|
13
|
-
private storage: IStorage;
|
|
14
|
-
private getFromSource: () => Promise<DataType>;
|
|
15
|
-
private onExpired: () => void;
|
|
16
|
-
constructor(name: string, duration_minutes: number, storage: IStorage, getFromSource: () => Promise<DataType>, onExpired: () => void)
|
|
17
|
-
{
|
|
18
|
-
this.name = name;
|
|
19
|
-
this.duration_minutes = duration_minutes;
|
|
20
|
-
this.storage = storage;
|
|
21
|
-
this.getFromSource = getFromSource;
|
|
22
|
-
this.onExpired = onExpired;
|
|
23
|
-
}
|
|
24
|
-
async get(): Promise<DataType>
|
|
25
|
-
{
|
|
26
|
-
let ans: DataType;
|
|
27
|
-
let value = this.storage.get(this.name, "");
|
|
28
|
-
if (value)
|
|
29
|
-
{
|
|
30
|
-
let cache = JSON.parse(value) as NSCachStorage<DataType>;
|
|
31
|
-
if (new Date(cache.expires_at) > new Date())
|
|
32
|
-
return cache.data;
|
|
33
|
-
}
|
|
34
|
-
ans = await this.getFromSource();
|
|
35
|
-
this.set(ans);
|
|
36
|
-
return ans;
|
|
37
|
-
}
|
|
38
|
-
set(data: DataType)
|
|
39
|
-
{
|
|
40
|
-
let expires_at = new Date();
|
|
41
|
-
expires_at.setMinutes(expires_at.getMinutes() + this.duration_minutes);
|
|
42
|
-
let value = JSON.stringify({ data, expires_at });
|
|
43
|
-
this.storage.set(this.name, value);
|
|
44
|
-
//
|
|
45
|
-
let sleep = expires_at.getTime() - new Date().getTime();
|
|
46
|
-
if (sleep <= 0)
|
|
47
|
-
sleep = 5 * 1000;
|
|
48
|
-
setTimeout(() =>
|
|
49
|
-
{
|
|
50
|
-
if (this.onExpired)
|
|
51
|
-
this.onExpired();
|
|
52
|
-
}, sleep);
|
|
53
|
-
}
|
|
54
|
-
del()
|
|
55
|
-
{
|
|
56
|
-
this.storage.del(this.name);
|
|
57
|
-
}
|
|
1
|
+
import { IStorage } from "./IStorage";
|
|
2
|
+
|
|
3
|
+
interface NSCachStorage<DataType>
|
|
4
|
+
{
|
|
5
|
+
expires_at: Date;
|
|
6
|
+
data: DataType;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class CacheService<DataType>
|
|
10
|
+
{
|
|
11
|
+
private name: string;
|
|
12
|
+
private duration_minutes: number;
|
|
13
|
+
private storage: IStorage;
|
|
14
|
+
private getFromSource: () => Promise<DataType>;
|
|
15
|
+
private onExpired: () => void;
|
|
16
|
+
constructor(name: string, duration_minutes: number, storage: IStorage, getFromSource: () => Promise<DataType>, onExpired: () => void)
|
|
17
|
+
{
|
|
18
|
+
this.name = name;
|
|
19
|
+
this.duration_minutes = duration_minutes;
|
|
20
|
+
this.storage = storage;
|
|
21
|
+
this.getFromSource = getFromSource;
|
|
22
|
+
this.onExpired = onExpired;
|
|
23
|
+
}
|
|
24
|
+
async get(): Promise<DataType>
|
|
25
|
+
{
|
|
26
|
+
let ans: DataType;
|
|
27
|
+
let value = this.storage.get(this.name, "");
|
|
28
|
+
if (value)
|
|
29
|
+
{
|
|
30
|
+
let cache = JSON.parse(value) as NSCachStorage<DataType>;
|
|
31
|
+
if (new Date(cache.expires_at) > new Date())
|
|
32
|
+
return cache.data;
|
|
33
|
+
}
|
|
34
|
+
ans = await this.getFromSource();
|
|
35
|
+
this.set(ans);
|
|
36
|
+
return ans;
|
|
37
|
+
}
|
|
38
|
+
set(data: DataType)
|
|
39
|
+
{
|
|
40
|
+
let expires_at = new Date();
|
|
41
|
+
expires_at.setMinutes(expires_at.getMinutes() + this.duration_minutes);
|
|
42
|
+
let value = JSON.stringify({ data, expires_at });
|
|
43
|
+
this.storage.set(this.name, value);
|
|
44
|
+
//
|
|
45
|
+
let sleep = expires_at.getTime() - new Date().getTime();
|
|
46
|
+
if (sleep <= 0)
|
|
47
|
+
sleep = 5 * 1000;
|
|
48
|
+
setTimeout(() =>
|
|
49
|
+
{
|
|
50
|
+
if (this.onExpired)
|
|
51
|
+
this.onExpired();
|
|
52
|
+
}, sleep);
|
|
53
|
+
}
|
|
54
|
+
del()
|
|
55
|
+
{
|
|
56
|
+
this.storage.del(this.name);
|
|
57
|
+
}
|
|
58
58
|
}
|
package/src/ConsoleOperation.ts
CHANGED
|
@@ -1,69 +1,69 @@
|
|
|
1
|
-
export class ConsoleOperation
|
|
2
|
-
{
|
|
3
|
-
public static colors = {
|
|
4
|
-
reset: '\x1b[0m',
|
|
5
|
-
red: '\x1b[31m',
|
|
6
|
-
green: '\x1b[32m',
|
|
7
|
-
yellow: '\x1b[33m',
|
|
8
|
-
blue: '\x1b[34m',
|
|
9
|
-
magenta: '\x1b[35m',
|
|
10
|
-
cyan: '\x1b[36m',
|
|
11
|
-
white: '\x1b[37m',
|
|
12
|
-
};
|
|
13
|
-
static formatLogColor(message: string)
|
|
14
|
-
{
|
|
15
|
-
return (ConsoleOperation.colors.white + message + ConsoleOperation.colors.reset);
|
|
16
|
-
}
|
|
17
|
-
static formatInfoColor(message: string)
|
|
18
|
-
{
|
|
19
|
-
return (ConsoleOperation.colors.cyan + message + ConsoleOperation.colors.reset);
|
|
20
|
-
}
|
|
21
|
-
static formatTraceColor(message: string)
|
|
22
|
-
{
|
|
23
|
-
return (ConsoleOperation.colors.blue + message + ConsoleOperation.colors.reset);
|
|
24
|
-
}
|
|
25
|
-
static formatDebugColor(message: string)
|
|
26
|
-
{
|
|
27
|
-
return (ConsoleOperation.colors.magenta + message + ConsoleOperation.colors.reset);
|
|
28
|
-
}
|
|
29
|
-
static formatSuccessColor(message: string)
|
|
30
|
-
{
|
|
31
|
-
return (ConsoleOperation.colors.green + message + ConsoleOperation.colors.reset);
|
|
32
|
-
}
|
|
33
|
-
static formatWarningColor(message: string)
|
|
34
|
-
{
|
|
35
|
-
return (ConsoleOperation.colors.yellow + message + ConsoleOperation.colors.reset);
|
|
36
|
-
}
|
|
37
|
-
static formatErrorColor(message: string)
|
|
38
|
-
{
|
|
39
|
-
return (ConsoleOperation.colors.red + message + ConsoleOperation.colors.reset);
|
|
40
|
-
}
|
|
41
|
-
static log(message: string)
|
|
42
|
-
{
|
|
43
|
-
console.log(ConsoleOperation.formatLogColor(message));
|
|
44
|
-
}
|
|
45
|
-
static info(message: string)
|
|
46
|
-
{
|
|
47
|
-
console.info(ConsoleOperation.formatInfoColor(message));
|
|
48
|
-
}
|
|
49
|
-
static trace(message: string)
|
|
50
|
-
{
|
|
51
|
-
console.trace(ConsoleOperation.formatTraceColor(message));
|
|
52
|
-
}
|
|
53
|
-
static debug(message: string)
|
|
54
|
-
{
|
|
55
|
-
console.debug(ConsoleOperation.formatDebugColor(message));
|
|
56
|
-
}
|
|
57
|
-
static success(message: string)
|
|
58
|
-
{
|
|
59
|
-
console.info(ConsoleOperation.formatSuccessColor(message));
|
|
60
|
-
}
|
|
61
|
-
static warning(message: string)
|
|
62
|
-
{
|
|
63
|
-
console.warn(ConsoleOperation.formatWarningColor(message));
|
|
64
|
-
}
|
|
65
|
-
static error(message: string)
|
|
66
|
-
{
|
|
67
|
-
console.error(ConsoleOperation.formatErrorColor(message));
|
|
68
|
-
}
|
|
1
|
+
export class ConsoleOperation
|
|
2
|
+
{
|
|
3
|
+
public static colors = {
|
|
4
|
+
reset: '\x1b[0m',
|
|
5
|
+
red: '\x1b[31m',
|
|
6
|
+
green: '\x1b[32m',
|
|
7
|
+
yellow: '\x1b[33m',
|
|
8
|
+
blue: '\x1b[34m',
|
|
9
|
+
magenta: '\x1b[35m',
|
|
10
|
+
cyan: '\x1b[36m',
|
|
11
|
+
white: '\x1b[37m',
|
|
12
|
+
};
|
|
13
|
+
static formatLogColor(message: string)
|
|
14
|
+
{
|
|
15
|
+
return (ConsoleOperation.colors.white + message + ConsoleOperation.colors.reset);
|
|
16
|
+
}
|
|
17
|
+
static formatInfoColor(message: string)
|
|
18
|
+
{
|
|
19
|
+
return (ConsoleOperation.colors.cyan + message + ConsoleOperation.colors.reset);
|
|
20
|
+
}
|
|
21
|
+
static formatTraceColor(message: string)
|
|
22
|
+
{
|
|
23
|
+
return (ConsoleOperation.colors.blue + message + ConsoleOperation.colors.reset);
|
|
24
|
+
}
|
|
25
|
+
static formatDebugColor(message: string)
|
|
26
|
+
{
|
|
27
|
+
return (ConsoleOperation.colors.magenta + message + ConsoleOperation.colors.reset);
|
|
28
|
+
}
|
|
29
|
+
static formatSuccessColor(message: string)
|
|
30
|
+
{
|
|
31
|
+
return (ConsoleOperation.colors.green + message + ConsoleOperation.colors.reset);
|
|
32
|
+
}
|
|
33
|
+
static formatWarningColor(message: string)
|
|
34
|
+
{
|
|
35
|
+
return (ConsoleOperation.colors.yellow + message + ConsoleOperation.colors.reset);
|
|
36
|
+
}
|
|
37
|
+
static formatErrorColor(message: string)
|
|
38
|
+
{
|
|
39
|
+
return (ConsoleOperation.colors.red + message + ConsoleOperation.colors.reset);
|
|
40
|
+
}
|
|
41
|
+
static log(message: string)
|
|
42
|
+
{
|
|
43
|
+
console.log(ConsoleOperation.formatLogColor(message));
|
|
44
|
+
}
|
|
45
|
+
static info(message: string)
|
|
46
|
+
{
|
|
47
|
+
console.info(ConsoleOperation.formatInfoColor(message));
|
|
48
|
+
}
|
|
49
|
+
static trace(message: string)
|
|
50
|
+
{
|
|
51
|
+
console.trace(ConsoleOperation.formatTraceColor(message));
|
|
52
|
+
}
|
|
53
|
+
static debug(message: string)
|
|
54
|
+
{
|
|
55
|
+
console.debug(ConsoleOperation.formatDebugColor(message));
|
|
56
|
+
}
|
|
57
|
+
static success(message: string)
|
|
58
|
+
{
|
|
59
|
+
console.info(ConsoleOperation.formatSuccessColor(message));
|
|
60
|
+
}
|
|
61
|
+
static warning(message: string)
|
|
62
|
+
{
|
|
63
|
+
console.warn(ConsoleOperation.formatWarningColor(message));
|
|
64
|
+
}
|
|
65
|
+
static error(message: string)
|
|
66
|
+
{
|
|
67
|
+
console.error(ConsoleOperation.formatErrorColor(message));
|
|
68
|
+
}
|
|
69
69
|
}
|
package/src/ConvertService.ts
CHANGED
|
@@ -1,101 +1,101 @@
|
|
|
1
|
-
import { ErrorOperation } from "./ErrorOperation";
|
|
2
|
-
import { StringOperation } from "./StringOperation";
|
|
3
|
-
|
|
4
|
-
export abstract class ConvertService
|
|
5
|
-
{
|
|
6
|
-
repair: boolean = true;
|
|
7
|
-
private mandatory: boolean;
|
|
8
|
-
constructor(mandatory: boolean = false)
|
|
9
|
-
{
|
|
10
|
-
this.mandatory = mandatory;
|
|
11
|
-
}
|
|
12
|
-
abstract getNullString(): string | null;
|
|
13
|
-
private checkMandatory<D>(value: D): D
|
|
14
|
-
{
|
|
15
|
-
if (!process.env.NAMIRASOFT_MUTE)
|
|
16
|
-
if (this.mandatory)
|
|
17
|
-
if (value == null || value == undefined)
|
|
18
|
-
this.onMandatoryError();
|
|
19
|
-
return value;
|
|
20
|
-
}
|
|
21
|
-
protected onMandatoryError(): void
|
|
22
|
-
{
|
|
23
|
-
if (!process.env.NAMIRASOFT_MUTE)
|
|
24
|
-
ErrorOperation.throwHTTP(500, "Object value can not be converted.");
|
|
25
|
-
}
|
|
26
|
-
getString(_default: string = ""): string
|
|
27
|
-
{
|
|
28
|
-
let ans = this.checkMandatory(this.getNullString()) ?? _default;
|
|
29
|
-
if (this.repair)
|
|
30
|
-
ans = StringOperation.repair(ans);
|
|
31
|
-
return ans;
|
|
32
|
-
}
|
|
33
|
-
getNullInt(): number | null
|
|
34
|
-
{
|
|
35
|
-
let str = this.getString();
|
|
36
|
-
if (str == null)
|
|
37
|
-
return null;
|
|
38
|
-
let ans = parseInt(str);
|
|
39
|
-
if (isNaN(ans))
|
|
40
|
-
return null;
|
|
41
|
-
return ans;
|
|
42
|
-
}
|
|
43
|
-
getInt(_default: number = 0): number
|
|
44
|
-
{
|
|
45
|
-
return this.checkMandatory(this.getNullInt()) ?? _default;
|
|
46
|
-
}
|
|
47
|
-
getNullFloat(): number | null
|
|
48
|
-
{
|
|
49
|
-
let str = this.getString();
|
|
50
|
-
if (str == null)
|
|
51
|
-
return null;
|
|
52
|
-
let ans = parseFloat(str);
|
|
53
|
-
if (isNaN(ans))
|
|
54
|
-
return null;
|
|
55
|
-
return ans;
|
|
56
|
-
}
|
|
57
|
-
getFloat(_default: number = 0): number
|
|
58
|
-
{
|
|
59
|
-
return this.checkMandatory(this.getNullFloat()) ?? _default;
|
|
60
|
-
}
|
|
61
|
-
getNullBoolean(): boolean | null
|
|
62
|
-
{
|
|
63
|
-
let str = this.getString();
|
|
64
|
-
if (str == null)
|
|
65
|
-
return null;
|
|
66
|
-
let ans = str.toLowerCase();
|
|
67
|
-
if (ans == "true")
|
|
68
|
-
return true;
|
|
69
|
-
if (ans == "false")
|
|
70
|
-
return false;
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
getBoolean(_default: boolean = false): boolean
|
|
74
|
-
{
|
|
75
|
-
return this.checkMandatory(this.getNullBoolean()) ?? _default;
|
|
76
|
-
}
|
|
77
|
-
getNullEnum<T>(enumObj: { [s: string]: T }): T | null
|
|
78
|
-
{
|
|
79
|
-
let str = this.getString();
|
|
80
|
-
return Object.values(enumObj).find((enumValue) => enumValue === str) as T | null;
|
|
81
|
-
}
|
|
82
|
-
getEnum<T>(enumObj: { [s: string]: T }, _default: T): T
|
|
83
|
-
{
|
|
84
|
-
return this.checkMandatory(this.getNullEnum<T>(enumObj)) ?? _default;
|
|
85
|
-
}
|
|
86
|
-
getStringArray(delimiter: string = ","): string[]
|
|
87
|
-
{
|
|
88
|
-
let ans: string[] = this.getString("").split(delimiter);
|
|
89
|
-
ans = ans.map(v => `${v}`);
|
|
90
|
-
ans = ans.filter(x => x);
|
|
91
|
-
return ans;
|
|
92
|
-
}
|
|
93
|
-
getIntArray(delimiter: string = ","): number[]
|
|
94
|
-
{
|
|
95
|
-
return this.getStringArray(delimiter).map(x => parseInt(x)).filter(x => !isNaN(x));
|
|
96
|
-
}
|
|
97
|
-
getFloatArray(delimiter: string = ","): number[]
|
|
98
|
-
{
|
|
99
|
-
return this.getStringArray(delimiter).map(x => parseFloat(x)).filter(x => !isNaN(x));
|
|
100
|
-
}
|
|
1
|
+
import { ErrorOperation } from "./ErrorOperation";
|
|
2
|
+
import { StringOperation } from "./StringOperation";
|
|
3
|
+
|
|
4
|
+
export abstract class ConvertService
|
|
5
|
+
{
|
|
6
|
+
repair: boolean = true;
|
|
7
|
+
private mandatory: boolean;
|
|
8
|
+
constructor(mandatory: boolean = false)
|
|
9
|
+
{
|
|
10
|
+
this.mandatory = mandatory;
|
|
11
|
+
}
|
|
12
|
+
abstract getNullString(): string | null;
|
|
13
|
+
private checkMandatory<D>(value: D): D
|
|
14
|
+
{
|
|
15
|
+
if (!process.env.NAMIRASOFT_MUTE)
|
|
16
|
+
if (this.mandatory)
|
|
17
|
+
if (value == null || value == undefined)
|
|
18
|
+
this.onMandatoryError();
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
protected onMandatoryError(): void
|
|
22
|
+
{
|
|
23
|
+
if (!process.env.NAMIRASOFT_MUTE)
|
|
24
|
+
ErrorOperation.throwHTTP(500, "Object value can not be converted.");
|
|
25
|
+
}
|
|
26
|
+
getString(_default: string = ""): string
|
|
27
|
+
{
|
|
28
|
+
let ans = this.checkMandatory(this.getNullString()) ?? _default;
|
|
29
|
+
if (this.repair)
|
|
30
|
+
ans = StringOperation.repair(ans);
|
|
31
|
+
return ans;
|
|
32
|
+
}
|
|
33
|
+
getNullInt(): number | null
|
|
34
|
+
{
|
|
35
|
+
let str = this.getString();
|
|
36
|
+
if (str == null)
|
|
37
|
+
return null;
|
|
38
|
+
let ans = parseInt(str);
|
|
39
|
+
if (isNaN(ans))
|
|
40
|
+
return null;
|
|
41
|
+
return ans;
|
|
42
|
+
}
|
|
43
|
+
getInt(_default: number = 0): number
|
|
44
|
+
{
|
|
45
|
+
return this.checkMandatory(this.getNullInt()) ?? _default;
|
|
46
|
+
}
|
|
47
|
+
getNullFloat(): number | null
|
|
48
|
+
{
|
|
49
|
+
let str = this.getString();
|
|
50
|
+
if (str == null)
|
|
51
|
+
return null;
|
|
52
|
+
let ans = parseFloat(str);
|
|
53
|
+
if (isNaN(ans))
|
|
54
|
+
return null;
|
|
55
|
+
return ans;
|
|
56
|
+
}
|
|
57
|
+
getFloat(_default: number = 0): number
|
|
58
|
+
{
|
|
59
|
+
return this.checkMandatory(this.getNullFloat()) ?? _default;
|
|
60
|
+
}
|
|
61
|
+
getNullBoolean(): boolean | null
|
|
62
|
+
{
|
|
63
|
+
let str = this.getString();
|
|
64
|
+
if (str == null)
|
|
65
|
+
return null;
|
|
66
|
+
let ans = str.toLowerCase();
|
|
67
|
+
if (ans == "true")
|
|
68
|
+
return true;
|
|
69
|
+
if (ans == "false")
|
|
70
|
+
return false;
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
getBoolean(_default: boolean = false): boolean
|
|
74
|
+
{
|
|
75
|
+
return this.checkMandatory(this.getNullBoolean()) ?? _default;
|
|
76
|
+
}
|
|
77
|
+
getNullEnum<T>(enumObj: { [s: string]: T }): T | null
|
|
78
|
+
{
|
|
79
|
+
let str = this.getString();
|
|
80
|
+
return Object.values(enumObj).find((enumValue) => enumValue === str) as T | null;
|
|
81
|
+
}
|
|
82
|
+
getEnum<T>(enumObj: { [s: string]: T }, _default: T): T
|
|
83
|
+
{
|
|
84
|
+
return this.checkMandatory(this.getNullEnum<T>(enumObj)) ?? _default;
|
|
85
|
+
}
|
|
86
|
+
getStringArray(delimiter: string = ","): string[]
|
|
87
|
+
{
|
|
88
|
+
let ans: string[] = this.getString("").split(delimiter);
|
|
89
|
+
ans = ans.map(v => `${v}`);
|
|
90
|
+
ans = ans.filter(x => x);
|
|
91
|
+
return ans;
|
|
92
|
+
}
|
|
93
|
+
getIntArray(delimiter: string = ","): number[]
|
|
94
|
+
{
|
|
95
|
+
return this.getStringArray(delimiter).map(x => parseInt(x)).filter(x => !isNaN(x));
|
|
96
|
+
}
|
|
97
|
+
getFloatArray(delimiter: string = ","): number[]
|
|
98
|
+
{
|
|
99
|
+
return this.getStringArray(delimiter).map(x => parseFloat(x)).filter(x => !isNaN(x));
|
|
100
|
+
}
|
|
101
101
|
}
|
package/src/CookieService.ts
CHANGED
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import { ConvertService } from "./ConvertService";
|
|
2
|
-
|
|
3
|
-
export class CookieService extends ConvertService
|
|
4
|
-
{
|
|
5
|
-
private cookies: string;
|
|
6
|
-
private cookies_object: {
|
|
7
|
-
[key: string]: string;
|
|
8
|
-
};
|
|
9
|
-
private name: string;
|
|
10
|
-
constructor(cookies: string, name: string, mandatory: boolean = false)
|
|
11
|
-
{
|
|
12
|
-
super(mandatory);
|
|
13
|
-
this.cookies = cookies;
|
|
14
|
-
this.name = name;
|
|
15
|
-
this.cookies_object = this.cookies.split(';').reduce((acc: { [key: string]: string }, cookie) =>
|
|
16
|
-
{
|
|
17
|
-
const [key, value] = cookie.trim().split('=');
|
|
18
|
-
acc[key] = decodeURIComponent(value);
|
|
19
|
-
return acc;
|
|
20
|
-
}, {});
|
|
21
|
-
}
|
|
22
|
-
override getNullString()
|
|
23
|
-
{
|
|
24
|
-
let ans = this.cookies_object[this.name];
|
|
25
|
-
if (ans)
|
|
26
|
-
return ans;
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
protected override onMandatoryError(): void
|
|
30
|
-
{
|
|
31
|
-
if (!process.env.NAMIRASOFT_MUTE)
|
|
32
|
-
throw new Error(`Cookie value was not provided: ${this.name}`);
|
|
33
|
-
}
|
|
1
|
+
import { ConvertService } from "./ConvertService";
|
|
2
|
+
|
|
3
|
+
export class CookieService extends ConvertService
|
|
4
|
+
{
|
|
5
|
+
private cookies: string;
|
|
6
|
+
private cookies_object: {
|
|
7
|
+
[key: string]: string;
|
|
8
|
+
};
|
|
9
|
+
private name: string;
|
|
10
|
+
constructor(cookies: string, name: string, mandatory: boolean = false)
|
|
11
|
+
{
|
|
12
|
+
super(mandatory);
|
|
13
|
+
this.cookies = cookies;
|
|
14
|
+
this.name = name;
|
|
15
|
+
this.cookies_object = this.cookies.split(';').reduce((acc: { [key: string]: string }, cookie) =>
|
|
16
|
+
{
|
|
17
|
+
const [key, value] = cookie.trim().split('=');
|
|
18
|
+
acc[key] = decodeURIComponent(value);
|
|
19
|
+
return acc;
|
|
20
|
+
}, {});
|
|
21
|
+
}
|
|
22
|
+
override getNullString()
|
|
23
|
+
{
|
|
24
|
+
let ans = this.cookies_object[this.name];
|
|
25
|
+
if (ans)
|
|
26
|
+
return ans;
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
protected override onMandatoryError(): void
|
|
30
|
+
{
|
|
31
|
+
if (!process.env.NAMIRASOFT_MUTE)
|
|
32
|
+
throw new Error(`Cookie value was not provided: ${this.name}`);
|
|
33
|
+
}
|
|
34
34
|
}
|