namirasoft-core 1.2.0 → 1.2.2

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/src/Country.ts CHANGED
@@ -1,22 +1,22 @@
1
- export class Country
2
- {
3
- continent: string = "";
4
- name: string = "";
5
- iso2: string = "";
6
- iso3: string = "";
7
- code: string = "";
8
- european_union: boolean = false;
9
- coeff: number;
10
- other: string[] = [];
11
- constructor(continent: string, name: string, iso2: string, iso3: string, code: string, european_union: boolean, coeff: number, other: string[] = [])
12
- {
13
- this.continent = continent;
14
- this.name = name;
15
- this.iso2 = iso2;
16
- this.iso3 = iso3;
17
- this.code = code;
18
- this.european_union = european_union;
19
- this.coeff = coeff;
20
- this.other = other;
21
- }
1
+ export class Country
2
+ {
3
+ continent: string = "";
4
+ name: string = "";
5
+ iso2: string = "";
6
+ iso3: string = "";
7
+ code: string = "";
8
+ european_union: boolean = false;
9
+ coeff: number;
10
+ other: string[] = [];
11
+ constructor(continent: string, name: string, iso2: string, iso3: string, code: string, european_union: boolean, coeff: number, other: string[] = [])
12
+ {
13
+ this.continent = continent;
14
+ this.name = name;
15
+ this.iso2 = iso2;
16
+ this.iso3 = iso3;
17
+ this.code = code;
18
+ this.european_union = european_union;
19
+ this.coeff = coeff;
20
+ this.other = other;
21
+ }
22
22
  }
@@ -1,99 +1,99 @@
1
- import { Country } from "./Country";
2
- import { Countries } from "./Countries";
3
-
4
- export class CountryOperation
5
- {
6
- private static simplify(input: string): string
7
- {
8
- if (!input)
9
- input = "";
10
- let arr = [" ", ".", "/", "'", "\"", "?", "!", "-", "+", "_", '\$', "@", "%", " of ", " the ", " a ", " an "];
11
- arr.map(pattern => input = input.split(pattern).join(""));
12
- input = input.split("é").join("e");
13
- return input.trim().toLowerCase();
14
- }
15
- static find(input: string): Country | null
16
- {
17
- if (!input)
18
- input = "";
19
- input = this.simplify(input);
20
- if (input)
21
- for (let country of Countries.getAll())
22
- {
23
- let values = [country.iso2, country.iso3, country.name];
24
- if (input.startsWith(country.code))
25
- return country;
26
- if (country.other)
27
- for (let i = 0; i < country.other.length; i++)
28
- values.push(country.other[i]);
29
- for (let i = 0; i < values.length; i++)
30
- if (this.simplify(values[i]) === input)
31
- return country;
32
- }
33
- return null;
34
- }
35
- static findPhone(input: string): Country[]
36
- {
37
- if (!input)
38
- input = "";
39
- input = this.simplify(input);
40
- let ans = [];
41
- let max = 0;
42
- if (input)
43
- for (let country of Countries.getAll())
44
- if (input.startsWith(country.code))
45
- {
46
- ans.push(country);
47
- max = Math.max(country.code.length, max);
48
- }
49
- let res = [];
50
- for (let i = 0; i < ans.length; i++)
51
- {
52
- if (ans[i].code.length == max)
53
- res.push(ans[i]);
54
- }
55
- return res;
56
- }
57
- static isPhoneFromCountry(input: string, country: Country | string | null)
58
- {
59
- if (country instanceof String)
60
- country = this.find(country as string);
61
- let countries = this.findPhone(input);
62
- if (country)
63
- for (let i = 0; i < countries.length; i++)
64
- if (this.areEqual([countries[i], country]))
65
- return true;
66
- return false;
67
- }
68
- static areEqual(countries: (Country | string)[]): boolean
69
- {
70
- countries = countries.map(country =>
71
- {
72
- let c: Country | null = null;
73
- if (country instanceof Country)
74
- c = country;
75
- else
76
- c = this.find(country as string);
77
- if (c)
78
- return c.name;
79
- throw new Error("Couldn't find country: " + country);
80
- });
81
- countries = countries.filter(v => v);
82
- countries = countries.filter((v, i, a) => a.indexOf(v) === i);
83
- return countries.length === 1;
84
- }
85
- static getCoeff(code: string, amount: number, times: number = 1): number
86
- {
87
- if (!times)
88
- times = 1;
89
- let country: Country | null = this.find(code);
90
- let ans: number = country?.coeff ?? 0;
91
- if (isNaN(ans))
92
- ans = 0.1;
93
- ans += Math.min(Math.max(1 - ans, 0), 1) * (times - 1) / times;
94
- ans = Math.min(Math.max(ans, 0), 1);
95
- if (amount)
96
- ans = amount * ans;
97
- return ans;
98
- }
1
+ import { Country } from "./Country";
2
+ import { Countries } from "./Countries";
3
+
4
+ export class CountryOperation
5
+ {
6
+ private static simplify(input: string): string
7
+ {
8
+ if (!input)
9
+ input = "";
10
+ let arr = [" ", ".", "/", "'", "\"", "?", "!", "-", "+", "_", '\$', "@", "%", " of ", " the ", " a ", " an "];
11
+ arr.map(pattern => input = input.split(pattern).join(""));
12
+ input = input.split("é").join("e");
13
+ return input.trim().toLowerCase();
14
+ }
15
+ static find(input: string): Country | null
16
+ {
17
+ if (!input)
18
+ input = "";
19
+ input = this.simplify(input);
20
+ if (input)
21
+ for (let country of Countries.getAll())
22
+ {
23
+ let values = [country.iso2, country.iso3, country.name];
24
+ if (input.startsWith(country.code))
25
+ return country;
26
+ if (country.other)
27
+ for (let i = 0; i < country.other.length; i++)
28
+ values.push(country.other[i]);
29
+ for (let i = 0; i < values.length; i++)
30
+ if (this.simplify(values[i]) === input)
31
+ return country;
32
+ }
33
+ return null;
34
+ }
35
+ static findPhone(input: string): Country[]
36
+ {
37
+ if (!input)
38
+ input = "";
39
+ input = this.simplify(input);
40
+ let ans = [];
41
+ let max = 0;
42
+ if (input)
43
+ for (let country of Countries.getAll())
44
+ if (input.startsWith(country.code))
45
+ {
46
+ ans.push(country);
47
+ max = Math.max(country.code.length, max);
48
+ }
49
+ let res = [];
50
+ for (let i = 0; i < ans.length; i++)
51
+ {
52
+ if (ans[i].code.length == max)
53
+ res.push(ans[i]);
54
+ }
55
+ return res;
56
+ }
57
+ static isPhoneFromCountry(input: string, country: Country | string | null)
58
+ {
59
+ if (country instanceof String)
60
+ country = this.find(country as string);
61
+ let countries = this.findPhone(input);
62
+ if (country)
63
+ for (let i = 0; i < countries.length; i++)
64
+ if (this.areEqual([countries[i], country]))
65
+ return true;
66
+ return false;
67
+ }
68
+ static areEqual(countries: (Country | string)[]): boolean
69
+ {
70
+ countries = countries.map(country =>
71
+ {
72
+ let c: Country | null = null;
73
+ if (country instanceof Country)
74
+ c = country;
75
+ else
76
+ c = this.find(country as string);
77
+ if (c)
78
+ return c.name;
79
+ throw new Error("Couldn't find country: " + country);
80
+ });
81
+ countries = countries.filter(v => v);
82
+ countries = countries.filter((v, i, a) => a.indexOf(v) === i);
83
+ return countries.length === 1;
84
+ }
85
+ static getCoeff(code: string, amount: number, times: number = 1): number
86
+ {
87
+ if (!times)
88
+ times = 1;
89
+ let country: Country | null = this.find(code);
90
+ let ans: number = country?.coeff ?? 0;
91
+ if (isNaN(ans))
92
+ ans = 0.1;
93
+ ans += Math.min(Math.max(1 - ans, 0), 1) * (times - 1) / times;
94
+ ans = Math.min(Math.max(ans, 0), 1);
95
+ if (amount)
96
+ ans = amount * ans;
97
+ return ans;
98
+ }
99
99
  }
@@ -1,14 +1,14 @@
1
- import { HTTPError } from "./HTTPError";
2
- import { StringOperation } from "./StringOperation";
3
-
4
- export class ErrorOperation
5
- {
6
- static throwHTTP(code: number, message: string, ...args: string[])
7
- {
8
- throw this.getHTTP(code, message, ...args);
9
- }
10
- static getHTTP(code: number, message: string, ...args: string[])
11
- {
12
- return new HTTPError(code, StringOperation.format(message, ...args));
13
- }
1
+ import { HTTPError } from "./HTTPError";
2
+ import { StringOperation } from "./StringOperation";
3
+
4
+ export class ErrorOperation
5
+ {
6
+ static throwHTTP(code: number, message: string, ...args: string[])
7
+ {
8
+ throw this.getHTTP(code, message, ...args);
9
+ }
10
+ static getHTTP(code: number, message: string, ...args: string[])
11
+ {
12
+ return new HTTPError(code, StringOperation.format(message, ...args));
13
+ }
14
14
  }
@@ -1,46 +1,46 @@
1
- import * as fs from 'fs';
2
- import * as path from 'path';
3
- export class FileOperation
4
- {
5
- static findUp(fileName: string): string[]
6
- {
7
- let currentDir: string = __dirname;
8
- const rootDir: string = process.cwd();
9
- const foundFilePaths: string[] = [];
10
- function searchRecursively(dir: string)
11
- {
12
- const files: string[] = fs.readdirSync(dir);
13
- if (files.includes(fileName))
14
- foundFilePaths.push(path.join(dir, fileName));
15
- if (rootDir === dir)
16
- return;
17
- searchRecursively(path.dirname(dir));
18
- }
19
- searchRecursively(currentDir);
20
- return foundFilePaths;
21
- }
22
- static async foreachFolder(base: string, handler: (base: string, sub: string, full: string) => Promise<void>, folders: boolean = true, files: boolean = true)
23
- {
24
- async function recursive(base: string, sub: string)
25
- {
26
- let files_folders = fs.readdirSync(path.join(base, sub));
27
- for (let folder of files_folders)
28
- {
29
- let s = path.join(sub, folder)
30
- let p = path.join(base, s);
31
- if (fs.statSync(p).isDirectory())
32
- {
33
- if (folders)
34
- await handler(base, sub, folder);
35
- await recursive(base, s);
36
- }
37
- else
38
- {
39
- if (files)
40
- await handler(base, sub, folder);
41
- }
42
- }
43
- }
44
- await recursive(base, "");
45
- }
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ export class FileOperation
4
+ {
5
+ static findUp(fileName: string): string[]
6
+ {
7
+ let currentDir: string = __dirname;
8
+ const rootDir: string = process.cwd();
9
+ const foundFilePaths: string[] = [];
10
+ function searchRecursively(dir: string)
11
+ {
12
+ const files: string[] = fs.readdirSync(dir);
13
+ if (files.includes(fileName))
14
+ foundFilePaths.push(path.join(dir, fileName));
15
+ if (rootDir === dir)
16
+ return;
17
+ searchRecursively(path.dirname(dir));
18
+ }
19
+ searchRecursively(currentDir);
20
+ return foundFilePaths;
21
+ }
22
+ static async foreachFolder(base: string, handler: (base: string, sub: string, full: string) => Promise<void>, folders: boolean = true, files: boolean = true)
23
+ {
24
+ async function recursive(base: string, sub: string)
25
+ {
26
+ let files_folders = fs.readdirSync(path.join(base, sub));
27
+ for (let folder of files_folders)
28
+ {
29
+ let s = path.join(sub, folder)
30
+ let p = path.join(base, s);
31
+ if (fs.statSync(p).isDirectory())
32
+ {
33
+ if (folders)
34
+ await handler(base, sub, folder);
35
+ await recursive(base, s);
36
+ }
37
+ else
38
+ {
39
+ if (files)
40
+ await handler(base, sub, folder);
41
+ }
42
+ }
43
+ }
44
+ await recursive(base, "");
45
+ }
46
46
  }
@@ -1,19 +1,19 @@
1
- export class GeoOperation
2
- {
3
- static distance(lat1: number, lng1: number, lat2: number, lng2: number)
4
- {
5
- function degreeToRadian(degree: number)
6
- {
7
- return degree * Math.PI / 180;
8
- }
9
- let R = 6371; // km
10
- let dLat = degreeToRadian(lat2 - lat1);
11
- let dLng = degreeToRadian(lng2 - lng1);
12
- lat1 = degreeToRadian(lat1);
13
- lat2 = degreeToRadian(lat2);
14
- let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
15
- Math.sin(dLng / 2) * Math.sin(dLng / 2) * Math.cos(lat1) * Math.cos(lat2);
16
- let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
17
- return R * c;
18
- }
1
+ export class GeoOperation
2
+ {
3
+ static distance(lat1: number, lng1: number, lat2: number, lng2: number)
4
+ {
5
+ function degreeToRadian(degree: number)
6
+ {
7
+ return degree * Math.PI / 180;
8
+ }
9
+ let R = 6371; // km
10
+ let dLat = degreeToRadian(lat2 - lat1);
11
+ let dLng = degreeToRadian(lng2 - lng1);
12
+ lat1 = degreeToRadian(lat1);
13
+ lat2 = degreeToRadian(lat2);
14
+ let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
15
+ Math.sin(dLng / 2) * Math.sin(dLng / 2) * Math.cos(lat1) * Math.cos(lat2);
16
+ let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
17
+ return R * c;
18
+ }
19
19
  }
package/src/HTTPError.ts CHANGED
@@ -1,9 +1,9 @@
1
- export class HTTPError extends Error
2
- {
3
- code: number;
4
- constructor(code: number, message: string)
5
- {
6
- super(message);
7
- this.code = code;
8
- }
1
+ export class HTTPError extends Error
2
+ {
3
+ code: number;
4
+ constructor(code: number, message: string)
5
+ {
6
+ super(message);
7
+ this.code = code;
8
+ }
9
9
  }
package/src/HTTPMethod.ts CHANGED
@@ -1,7 +1,7 @@
1
- export enum HTTPMethod
2
- {
3
- GET,
4
- POST,
5
- PUT,
6
- DELETE
1
+ export enum HTTPMethod
2
+ {
3
+ GET,
4
+ POST,
5
+ PUT,
6
+ DELETE
7
7
  }
package/src/IStorage.ts CHANGED
@@ -1,6 +1,6 @@
1
- export abstract class IStorage
2
- {
3
- abstract get(name: string, defaultValue: string): string;
4
- abstract set(name: string, value: string): void;
5
- abstract del(name: string): void;
1
+ export abstract class IStorage
2
+ {
3
+ abstract get(name: string, defaultValue: string): string;
4
+ abstract set(name: string, value: string): void;
5
+ abstract del(name: string): void;
6
6
  }
@@ -1,17 +1,17 @@
1
- import { IStorage } from "./IStorage.js";
2
-
3
- export class IStorageLocal extends IStorage
4
- {
5
- override get(name: string, defaultValue: string)
6
- {
7
- return localStorage.getItem(name) ?? defaultValue;
8
- }
9
- override set(name: string, value: string)
10
- {
11
- localStorage.setItem(name, value);
12
- }
13
- override del(name: string)
14
- {
15
- localStorage.removeItem(name);
16
- }
1
+ import { IStorage } from "./IStorage.js";
2
+
3
+ export class IStorageLocal extends IStorage
4
+ {
5
+ override get(name: string, defaultValue: string)
6
+ {
7
+ return localStorage.getItem(name) ?? defaultValue;
8
+ }
9
+ override set(name: string, value: string)
10
+ {
11
+ localStorage.setItem(name, value);
12
+ }
13
+ override del(name: string)
14
+ {
15
+ localStorage.removeItem(name);
16
+ }
17
17
  }
@@ -1,18 +1,18 @@
1
- import { IStorage } from "./IStorage";
2
-
3
- export class IStorageMemory extends IStorage
4
- {
5
- static data: { [name: string]: string } = {};
6
- override get(name: string, defaultValue: string)
7
- {
8
- return IStorageMemory.data[name] ?? defaultValue;
9
- }
10
- override set(name: string, value: string)
11
- {
12
- IStorageMemory.data[name] = value;
13
- }
14
- override del(name: string)
15
- {
16
- delete IStorageMemory.data[name];
17
- }
1
+ import { IStorage } from "./IStorage";
2
+
3
+ export class IStorageMemory extends IStorage
4
+ {
5
+ static data: { [name: string]: string } = {};
6
+ override get(name: string, defaultValue: string)
7
+ {
8
+ return IStorageMemory.data[name] ?? defaultValue;
9
+ }
10
+ override set(name: string, value: string)
11
+ {
12
+ IStorageMemory.data[name] = value;
13
+ }
14
+ override del(name: string)
15
+ {
16
+ delete IStorageMemory.data[name];
17
+ }
18
18
  }
@@ -1,24 +1,24 @@
1
- import { ConvertService } from "./ConvertService";
2
- import { ParsedNameValue } from "./ParsedNameValue";
3
-
4
- export class ObjectService extends ConvertService
5
- {
6
- private object: ParsedNameValue;
7
- constructor(object: ParsedNameValue)
8
- {
9
- super();
10
- this.object = object;
11
- }
12
- override getNullString()
13
- {
14
- let ans: string | null = null;
15
- if (Array.isArray(this.object))
16
- {
17
- if (this.object.length > 0)
18
- ans = this.object[0];
19
- }
20
- else if (this.object != null && this.object != undefined)
21
- ans = this.object + "";
22
- return ans;
23
- }
1
+ import { ConvertService } from "./ConvertService";
2
+ import { ParsedNameValue } from "./ParsedNameValue";
3
+
4
+ export class ObjectService extends ConvertService
5
+ {
6
+ private object: ParsedNameValue;
7
+ constructor(object: ParsedNameValue)
8
+ {
9
+ super();
10
+ this.object = object;
11
+ }
12
+ override getNullString()
13
+ {
14
+ let ans: string | null = null;
15
+ if (Array.isArray(this.object))
16
+ {
17
+ if (this.object.length > 0)
18
+ ans = this.object[0];
19
+ }
20
+ else if (this.object != null && this.object != undefined)
21
+ ans = this.object + "";
22
+ return ans;
23
+ }
24
24
  }