@digione/node-custom-api 0.0.1
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/constants/header.d.ts +10 -0
- package/constants/header.js +2 -0
- package/constants/http.d.ts +44 -0
- package/constants/http.js +2 -0
- package/database/index.d.ts +3 -0
- package/database/index.js +34 -0
- package/database/redis.d.ts +15 -0
- package/database/redis.js +68 -0
- package/errors/authError.d.ts +55 -0
- package/errors/authError.js +57 -0
- package/errors/index.d.ts +32 -0
- package/errors/index.js +113 -0
- package/index.d.ts +1 -0
- package/index.js +4 -0
- package/interface/param.d.ts +41 -0
- package/interface/param.js +2 -0
- package/middlewares/accessToken.d.ts +13 -0
- package/middlewares/accessToken.js +200 -0
- package/middlewares/coreSite.d.ts +4 -0
- package/middlewares/coreSite.js +52 -0
- package/middlewares/paramQuery.d.ts +16 -0
- package/middlewares/paramQuery.js +220 -0
- package/middlewares/refSite.d.ts +2 -0
- package/middlewares/refSite.js +58 -0
- package/middlewares/refreshToken.d.ts +2 -0
- package/middlewares/refreshToken.js +56 -0
- package/middlewares/systemSite.d.ts +6 -0
- package/middlewares/systemSite.js +106 -0
- package/middlewares/webSite.d.ts +7 -0
- package/middlewares/webSite.js +109 -0
- package/models/core/city.d.ts +66 -0
- package/models/core/city.js +79 -0
- package/models/core/country.d.ts +35 -0
- package/models/core/country.js +44 -0
- package/models/core/district.d.ts +17 -0
- package/models/core/district.js +23 -0
- package/models/core/domain.d.ts +57 -0
- package/models/core/domain.js +75 -0
- package/models/core/site.d.ts +60 -0
- package/models/core/site.js +70 -0
- package/models/core/state.d.ts +49 -0
- package/models/core/state.js +58 -0
- package/models/core/zipcode.d.ts +5 -0
- package/models/core/zipcode.js +28 -0
- package/models/stream/field.d.ts +60 -0
- package/models/stream/field.js +66 -0
- package/models/stream/field_section.d.ts +33 -0
- package/models/stream/field_section.js +39 -0
- package/models/stream/stream.d.ts +89 -0
- package/models/stream/stream.js +97 -0
- package/models/system/currency.d.ts +5 -0
- package/models/system/currency.js +32 -0
- package/models/system/email_log.d.ts +5 -0
- package/models/system/email_log.js +54 -0
- package/models/system/email_sender.d.ts +22 -0
- package/models/system/email_sender.js +53 -0
- package/models/system/email_template.d.ts +5 -0
- package/models/system/email_template.js +49 -0
- package/models/system/file.d.ts +98 -0
- package/models/system/file.js +111 -0
- package/models/system/folder.d.ts +59 -0
- package/models/system/folder.js +78 -0
- package/models/system/language.d.ts +44 -0
- package/models/system/language.js +50 -0
- package/models/system/metadata.d.ts +5 -0
- package/models/system/metadata.js +24 -0
- package/models/system/module.d.ts +5 -0
- package/models/system/module.js +35 -0
- package/models/system/notification_log.d.ts +25 -0
- package/models/system/notification_log.js +76 -0
- package/models/system/notification_module.d.ts +5 -0
- package/models/system/notification_module.js +35 -0
- package/models/system/notification_setting.d.ts +22 -0
- package/models/system/notification_setting.js +44 -0
- package/models/system/notification_template.d.ts +5 -0
- package/models/system/notification_template.js +49 -0
- package/models/system/notification_type.d.ts +5 -0
- package/models/system/notification_type.js +38 -0
- package/models/system/payment_transaction.d.ts +5 -0
- package/models/system/payment_transaction.js +46 -0
- package/models/system/pdf_page.d.ts +5 -0
- package/models/system/pdf_page.js +39 -0
- package/models/system/pdf_template.d.ts +22 -0
- package/models/system/pdf_template.js +67 -0
- package/models/system/review_template.d.ts +5 -0
- package/models/system/review_template.js +35 -0
- package/models/system/review_vote.d.ts +5 -0
- package/models/system/review_vote.js +36 -0
- package/models/system/session.d.ts +5 -0
- package/models/system/session.js +19 -0
- package/models/system/setting.d.ts +15 -0
- package/models/system/setting.js +22 -0
- package/models/system/site_token.d.ts +25 -0
- package/models/system/site_token.js +31 -0
- package/models/system/sms_log.d.ts +4 -0
- package/models/system/sms_log.js +52 -0
- package/models/system/sms_setting.d.ts +4 -0
- package/models/system/sms_setting.js +39 -0
- package/models/system/sms_template.d.ts +5 -0
- package/models/system/sms_template.js +48 -0
- package/models/system/tax.d.ts +5 -0
- package/models/system/tax.js +33 -0
- package/models/system/website.d.ts +56 -0
- package/models/system/website.js +66 -0
- package/models/system/zone.d.ts +57 -0
- package/models/system/zone.js +141 -0
- package/models/user/access_token.d.ts +37 -0
- package/models/user/access_token.js +46 -0
- package/models/user/address.d.ts +69 -0
- package/models/user/address.js +119 -0
- package/models/user/b2b_group.d.ts +57 -0
- package/models/user/b2b_group.js +63 -0
- package/models/user/group.d.ts +28 -0
- package/models/user/group.js +34 -0
- package/models/user/group_permission.d.ts +4 -0
- package/models/user/group_permission.js +27 -0
- package/models/user/notification_token.d.ts +32 -0
- package/models/user/notification_token.js +39 -0
- package/models/user/organ.d.ts +85 -0
- package/models/user/organ.js +100 -0
- package/models/user/profile.d.ts +31 -0
- package/models/user/profile.js +37 -0
- package/models/user/refresh_token.d.ts +41 -0
- package/models/user/refresh_token.js +51 -0
- package/models/user/team.d.ts +59 -0
- package/models/user/team.js +71 -0
- package/models/user/user.d.ts +79 -0
- package/models/user/user.js +100 -0
- package/models/user/user_organ.d.ts +52 -0
- package/models/user/user_organ.js +87 -0
- package/package.json +59 -0
- package/utils/auth.d.ts +25 -0
- package/utils/auth.js +137 -0
- package/utils/config.d.ts +8 -0
- package/utils/config.js +6 -0
- package/utils/core.d.ts +6 -0
- package/utils/core.js +39 -0
- package/utils/currency.d.ts +16 -0
- package/utils/currency.js +38 -0
- package/utils/db.d.ts +94 -0
- package/utils/db.js +399 -0
- package/utils/file.d.ts +232 -0
- package/utils/file.js +772 -0
- package/utils/helper.d.ts +113 -0
- package/utils/helper.js +508 -0
- package/utils/index.d.ts +10 -0
- package/utils/index.js +13 -0
- package/utils/language.d.ts +18 -0
- package/utils/language.js +48 -0
- package/utils/metadata.d.ts +18 -0
- package/utils/metadata.js +50 -0
- package/utils/session.d.ts +37 -0
- package/utils/session.js +60 -0
- package/utils/setting.d.ts +14 -0
- package/utils/setting.js +70 -0
- package/utils/stream.d.ts +303 -0
- package/utils/stream.js +1893 -0
- package/utils/user.d.ts +61 -0
- package/utils/user.js +282 -0
- package/utils/validator.d.ts +56 -0
- package/utils/validator.js +269 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import * as moment from 'moment';
|
|
2
|
+
export declare const getDateRangeFormat: (dates?: any, { format: { day, month, year } }?: {
|
|
3
|
+
format?: {
|
|
4
|
+
day?: string;
|
|
5
|
+
month?: string;
|
|
6
|
+
year?: string;
|
|
7
|
+
};
|
|
8
|
+
}) => string;
|
|
9
|
+
export declare const getDateFromString: (value: any) => Date;
|
|
10
|
+
export declare const getDaysBetweenDates: (startDate: any, endDate: any, { utc }?: {
|
|
11
|
+
utc?: boolean;
|
|
12
|
+
}) => any[];
|
|
13
|
+
export declare const Joi_date: any;
|
|
14
|
+
export declare const getENV: (str: string, def?: any) => any;
|
|
15
|
+
export declare const getSlug: (length?: number) => string;
|
|
16
|
+
export declare const getDateMysql: (date?: any) => any;
|
|
17
|
+
export declare const getTimestampMysql: (date?: any) => number;
|
|
18
|
+
export declare const getDateTime: (time?: any, { utc }?: {
|
|
19
|
+
utc?: boolean;
|
|
20
|
+
}) => moment.Moment;
|
|
21
|
+
export declare const getValueFunctionFile: (key: any) => (value: any, { result, fileUtil }?: {
|
|
22
|
+
result?: {};
|
|
23
|
+
fileUtil?: any;
|
|
24
|
+
}) => Promise<void>;
|
|
25
|
+
export declare const getValueFunctionLang: (key: any) => (value?: {}, { result, transform, spread, spread_all, raw, lang_code }?: {
|
|
26
|
+
result?: {};
|
|
27
|
+
transform?: boolean;
|
|
28
|
+
spread?: boolean;
|
|
29
|
+
spread_all?: boolean;
|
|
30
|
+
raw?: boolean;
|
|
31
|
+
lang_code?: string;
|
|
32
|
+
}) => void;
|
|
33
|
+
export declare const getValueByLang: (value: any, { result, field, lang_code, transform, raw }?: {
|
|
34
|
+
result?: {};
|
|
35
|
+
field?: any[];
|
|
36
|
+
lang_code?: string;
|
|
37
|
+
transform?: boolean;
|
|
38
|
+
raw?: boolean;
|
|
39
|
+
}) => any;
|
|
40
|
+
export declare const sleep: any;
|
|
41
|
+
export declare const getUUID: () => string;
|
|
42
|
+
export declare const getUUID32: () => string;
|
|
43
|
+
export declare const getTokenDataFromTokenBySecret: (token: string, secret: string) => Promise<any>;
|
|
44
|
+
export declare const signToken: (secret: string, opt?: any) => string;
|
|
45
|
+
export declare const removeHyphen: (str: string, replace?: string, { encode, replace_word }?: {
|
|
46
|
+
encode?: boolean;
|
|
47
|
+
replace_word?: string;
|
|
48
|
+
}) => string;
|
|
49
|
+
export declare const getMobileNumber: (str: string, { prefix }?: {
|
|
50
|
+
prefix?: string;
|
|
51
|
+
}) => string;
|
|
52
|
+
export declare const removeSpace: (str: string, replace?: string) => string;
|
|
53
|
+
export declare const removeAt: (str: string, replace?: string) => string;
|
|
54
|
+
export declare const isNumber: (val: any, { negative }?: {
|
|
55
|
+
negative?: boolean;
|
|
56
|
+
}) => boolean;
|
|
57
|
+
export declare const isBase64: (val: any) => boolean;
|
|
58
|
+
export declare const multiExecArray: (multi: any, { json_parse }?: {
|
|
59
|
+
json_parse?: boolean;
|
|
60
|
+
}) => Promise<any[]>;
|
|
61
|
+
export declare const randomRef: ({ length_digital, length_alphabet }?: {
|
|
62
|
+
length_digital?: number;
|
|
63
|
+
length_alphabet?: number;
|
|
64
|
+
}) => string;
|
|
65
|
+
export declare const treeToArray: (items: Array<any>, array?: Array<any>, level?: number) => Promise<any[]>;
|
|
66
|
+
export declare const buildTree: (nodes: Array<any>, { index, field_children, json_convert, key_index, key_parent, root, to_array }?: {
|
|
67
|
+
index?: any;
|
|
68
|
+
field_children?: string;
|
|
69
|
+
json_convert?: boolean;
|
|
70
|
+
key_index?: string;
|
|
71
|
+
key_parent?: string;
|
|
72
|
+
root?: any;
|
|
73
|
+
to_array?: boolean;
|
|
74
|
+
}) => Promise<any[]>;
|
|
75
|
+
export declare const expandTree: (items: Array<any>, { array, parent, level, prefix, field_index, field_children, key_value }?: {
|
|
76
|
+
array?: any[];
|
|
77
|
+
parent?: any;
|
|
78
|
+
level?: number;
|
|
79
|
+
prefix?: string;
|
|
80
|
+
field_index?: string;
|
|
81
|
+
field_children?: string;
|
|
82
|
+
key_value?: string;
|
|
83
|
+
}) => any[];
|
|
84
|
+
export declare const convertToBigEndian: (text: string) => string;
|
|
85
|
+
export declare const slugify: (string?: any, { lowercase }?: {
|
|
86
|
+
lowercase?: boolean;
|
|
87
|
+
}) => any;
|
|
88
|
+
export declare const slugifyField: (string?: any) => any;
|
|
89
|
+
export declare const stripHtml: (string: string) => string;
|
|
90
|
+
export declare const replaceColon: (string: string, { decode }?: {
|
|
91
|
+
decode?: boolean;
|
|
92
|
+
}) => string;
|
|
93
|
+
export declare const getParent: (nodes: Array<any>, { id, key_index }?: {
|
|
94
|
+
id?: string;
|
|
95
|
+
key_index?: string;
|
|
96
|
+
}) => any;
|
|
97
|
+
export declare const getDomain: (domain: string) => Promise<string>;
|
|
98
|
+
export declare const getInstanceNumber: () => string;
|
|
99
|
+
export declare const xorEncode: (str: string, key?: string) => string;
|
|
100
|
+
export declare const xorDecode: (str: string, key?: string) => string;
|
|
101
|
+
export declare const xorMerge: (str: string, key: string) => string;
|
|
102
|
+
export declare const convertCurrency: (data: any, currency?: any, { fields, fields_tax, fn_value, fn_field, fn_tax, setting, vat_value, tax_include, tax_dif, obj_format }?: {
|
|
103
|
+
fields?: any[];
|
|
104
|
+
fields_tax?: any[];
|
|
105
|
+
fn_value?: {};
|
|
106
|
+
fn_field?: {};
|
|
107
|
+
fn_tax?: {};
|
|
108
|
+
setting?: boolean;
|
|
109
|
+
vat_value?: number;
|
|
110
|
+
tax_include?: boolean;
|
|
111
|
+
tax_dif?: boolean;
|
|
112
|
+
obj_format?: {};
|
|
113
|
+
}) => {};
|
package/utils/helper.js
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertCurrency = exports.xorMerge = exports.xorDecode = exports.xorEncode = exports.getInstanceNumber = exports.getDomain = exports.getParent = exports.replaceColon = exports.stripHtml = exports.slugifyField = exports.slugify = exports.convertToBigEndian = exports.expandTree = exports.buildTree = exports.treeToArray = exports.randomRef = exports.multiExecArray = exports.isBase64 = exports.isNumber = exports.removeAt = exports.removeSpace = exports.getMobileNumber = exports.removeHyphen = exports.signToken = exports.getTokenDataFromTokenBySecret = exports.getUUID32 = exports.getUUID = exports.sleep = exports.getValueByLang = exports.getValueFunctionLang = exports.getValueFunctionFile = exports.getDateTime = exports.getTimestampMysql = exports.getDateMysql = exports.getSlug = exports.getENV = exports.Joi_date = exports.getDaysBetweenDates = exports.getDateFromString = exports.getDateRangeFormat = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const crypto = require("crypto");
|
|
6
|
+
const dotenv = require("dotenv");
|
|
7
|
+
const moment = require("moment");
|
|
8
|
+
const uuid_1 = require("uuid");
|
|
9
|
+
const short_unique_id_1 = require("short-unique-id");
|
|
10
|
+
const jwt = require("jsonwebtoken");
|
|
11
|
+
const Joi = require("joi");
|
|
12
|
+
const date_1 = require("@joi/date");
|
|
13
|
+
const numeral = require("numeral");
|
|
14
|
+
const _ = require("lodash");
|
|
15
|
+
const sha1 = require('js-sha1');
|
|
16
|
+
dotenv.config();
|
|
17
|
+
const getDateRangeFormat = (dates = [], { format: { day = "D", month = "MMM", year = "Y" } = {} } = {}) => {
|
|
18
|
+
dates = dates.map(i => (0, exports.getDateTime)(i));
|
|
19
|
+
const min = moment.min(dates);
|
|
20
|
+
const max = moment.max(dates);
|
|
21
|
+
let start = "", end = max.format(day) + " " + max.format(month) + " " + max.format(year);
|
|
22
|
+
if (!min.isSame(max)) {
|
|
23
|
+
start = min.format(day);
|
|
24
|
+
if (!min.isSame(max, 'month')) {
|
|
25
|
+
start = start + " " + min.format(month);
|
|
26
|
+
}
|
|
27
|
+
if (!min.isSame(max, 'year')) {
|
|
28
|
+
start = start + " " + min.format(year);
|
|
29
|
+
}
|
|
30
|
+
start = start + " - ";
|
|
31
|
+
}
|
|
32
|
+
let result = start + end;
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
exports.getDateRangeFormat = getDateRangeFormat;
|
|
36
|
+
const getDateFromString = (value) => {
|
|
37
|
+
if (value instanceof Date) {
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
let str = value.split(' ');
|
|
41
|
+
return new Date(`${str[0]}T${str[1] || '00:00:00'}.000Z`);
|
|
42
|
+
};
|
|
43
|
+
exports.getDateFromString = getDateFromString;
|
|
44
|
+
const getDaysBetweenDates = (startDate, endDate, { utc = true } = {}) => {
|
|
45
|
+
let dates = [];
|
|
46
|
+
let currDate = (0, exports.getDateTime)(startDate, { utc }).startOf('day');
|
|
47
|
+
let lastDate = (0, exports.getDateTime)(endDate, { utc }).endOf('day');
|
|
48
|
+
dates.push(currDate.toDate());
|
|
49
|
+
while (currDate.add(1, 'days').diff(lastDate) < 0) {
|
|
50
|
+
dates.push(currDate.clone().toDate());
|
|
51
|
+
}
|
|
52
|
+
return dates;
|
|
53
|
+
};
|
|
54
|
+
exports.getDaysBetweenDates = getDaysBetweenDates;
|
|
55
|
+
exports.Joi_date = Joi.extend(date_1.default);
|
|
56
|
+
const getENV = (str, def) => {
|
|
57
|
+
return process.env[str] || def;
|
|
58
|
+
};
|
|
59
|
+
exports.getENV = getENV;
|
|
60
|
+
const getSlug = (length = 8) => {
|
|
61
|
+
return new short_unique_id_1.default().randomUUID(length);
|
|
62
|
+
};
|
|
63
|
+
exports.getSlug = getSlug;
|
|
64
|
+
const getDateMysql = (date = undefined) => {
|
|
65
|
+
try {
|
|
66
|
+
return (0, exports.getDateTime)(date).format("YYYY-MM-DD HH:mm:ss");
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
return date;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
exports.getDateMysql = getDateMysql;
|
|
73
|
+
const getTimestampMysql = (date = undefined) => {
|
|
74
|
+
return ((0, exports.getDateTime)(date).unix());
|
|
75
|
+
};
|
|
76
|
+
exports.getTimestampMysql = getTimestampMysql;
|
|
77
|
+
const getDateTime = (time = undefined, { utc = true } = {}) => {
|
|
78
|
+
return utc ? moment(time).utc() : moment(time);
|
|
79
|
+
};
|
|
80
|
+
exports.getDateTime = getDateTime;
|
|
81
|
+
const getValueFunctionFile = (key) => {
|
|
82
|
+
return (value, { result = {}, fileUtil = undefined } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
83
|
+
if (value[key]) {
|
|
84
|
+
result[key] = yield fileUtil.findOneFileById(value[key]);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
exports.getValueFunctionFile = getValueFunctionFile;
|
|
89
|
+
const getValueFunctionLang = (key) => {
|
|
90
|
+
return (value = {}, { result = {}, transform = true, spread = false, spread_all = false, raw = false, lang_code = "" } = {}) => {
|
|
91
|
+
if (raw || typeof value[key] == "undefined")
|
|
92
|
+
return;
|
|
93
|
+
let obj = {}, bol = false;
|
|
94
|
+
try {
|
|
95
|
+
obj = JSON.parse(value[key]);
|
|
96
|
+
if (spread) {
|
|
97
|
+
result[key] = undefined;
|
|
98
|
+
result[key + "_" + lang_code] = (obj[lang_code] || obj['en']);
|
|
99
|
+
if (spread_all) {
|
|
100
|
+
Object.keys(obj).forEach((k) => Object.assign(result, { [key + "_" + k]: obj[k] }), {});
|
|
101
|
+
}
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
bol = true;
|
|
105
|
+
}
|
|
106
|
+
catch (err) { }
|
|
107
|
+
obj = obj || {};
|
|
108
|
+
if (!transform) {
|
|
109
|
+
result[key] = obj;
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
result[key] = bol ? (obj[lang_code] || obj['en']) : value[key];
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
exports.getValueFunctionLang = getValueFunctionLang;
|
|
116
|
+
const getValueByLang = (value, { result = {}, field = [], lang_code = "en", transform = true, raw = false } = {}) => {
|
|
117
|
+
for (let key of field) {
|
|
118
|
+
(0, exports.getValueFunctionLang)(key)(value, { result, lang_code, transform, raw });
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
};
|
|
122
|
+
exports.getValueByLang = getValueByLang;
|
|
123
|
+
exports.sleep = require('util').promisify(setTimeout);
|
|
124
|
+
const getUUID = () => (0, uuid_1.v4)();
|
|
125
|
+
exports.getUUID = getUUID;
|
|
126
|
+
const getUUID32 = () => (0, uuid_1.v4)().replace(/-/g, '');
|
|
127
|
+
exports.getUUID32 = getUUID32;
|
|
128
|
+
const getTokenDataFromTokenBySecret = (token, secret) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return jwt.verify(token, secret); });
|
|
129
|
+
exports.getTokenDataFromTokenBySecret = getTokenDataFromTokenBySecret;
|
|
130
|
+
const signToken = (secret, opt = {}) => {
|
|
131
|
+
return jwt.sign(Object.assign({}, opt), secret);
|
|
132
|
+
};
|
|
133
|
+
exports.signToken = signToken;
|
|
134
|
+
const removeHyphen = (str, replace = "", { encode = false, replace_word = "a" } = {}) => {
|
|
135
|
+
if (encode) {
|
|
136
|
+
str = str ? str.replace(/[`~!@#$%^&*()|+=?;:'",.<>\{\}\[\]\\\/]/gi, replace_word) : "";
|
|
137
|
+
if (str)
|
|
138
|
+
str = encodeURIComponent(str).replace(/%/g, '');
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
str = str ? str.replace(/\W/g, replace_word) : "";
|
|
142
|
+
}
|
|
143
|
+
return str ? str.replace(/-/g, replace).toLowerCase() : "";
|
|
144
|
+
};
|
|
145
|
+
exports.removeHyphen = removeHyphen;
|
|
146
|
+
const getMobileNumber = (str, { prefix = "+66" } = {}) => {
|
|
147
|
+
if (str) {
|
|
148
|
+
str = str.replace(/_/g, "").replace(/-/g, "").replace(/ /g, "");
|
|
149
|
+
if (str.indexOf("+") == -1 && str.length == 10) {
|
|
150
|
+
str = str.substr(1);
|
|
151
|
+
str = prefix + str;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return str;
|
|
155
|
+
};
|
|
156
|
+
exports.getMobileNumber = getMobileNumber;
|
|
157
|
+
const removeSpace = (str, replace = "") => {
|
|
158
|
+
return str ? str.replace(/ /g, replace) : "";
|
|
159
|
+
};
|
|
160
|
+
exports.removeSpace = removeSpace;
|
|
161
|
+
const removeAt = (str, replace = "_") => {
|
|
162
|
+
return str ? str.replace(/@/g, replace) : "";
|
|
163
|
+
};
|
|
164
|
+
exports.removeAt = removeAt;
|
|
165
|
+
const isNumber = (val, { negative = false } = {}) => {
|
|
166
|
+
let regx = new RegExp(/^\d+$/);
|
|
167
|
+
if (negative) {
|
|
168
|
+
regx = new RegExp(/^-?\d+$/);
|
|
169
|
+
}
|
|
170
|
+
return regx.test(val);
|
|
171
|
+
};
|
|
172
|
+
exports.isNumber = isNumber;
|
|
173
|
+
const isBase64 = (val) => {
|
|
174
|
+
let reg = new RegExp("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$");
|
|
175
|
+
return reg.test(val);
|
|
176
|
+
};
|
|
177
|
+
exports.isBase64 = isBase64;
|
|
178
|
+
const multiExecArray = (multi, { json_parse = false } = {}) => {
|
|
179
|
+
return new Promise((resolve, reject) => {
|
|
180
|
+
multi.exec((err, data) => {
|
|
181
|
+
if (json_parse) {
|
|
182
|
+
data = data.map(item => {
|
|
183
|
+
if (item instanceof Array)
|
|
184
|
+
item = item[1];
|
|
185
|
+
return JSON.parse(item);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
resolve(data);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
};
|
|
192
|
+
exports.multiExecArray = multiExecArray;
|
|
193
|
+
const randomRef = ({ length_digital = 3, length_alphabet = 1 } = {}) => {
|
|
194
|
+
let ref = "";
|
|
195
|
+
let array = (Array(length_alphabet).fill('a')).concat(Array(length_digital).fill('d'));
|
|
196
|
+
let possible_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
197
|
+
let possible_digital = "0123456789";
|
|
198
|
+
array = _.shuffle(array);
|
|
199
|
+
for (let i = 0; i < array.length; i++) {
|
|
200
|
+
if (array[i] == 'a') {
|
|
201
|
+
ref += possible_alphabet.charAt(Math.floor(Math.random() * possible_alphabet.length));
|
|
202
|
+
}
|
|
203
|
+
else if (array[i] == 'd') {
|
|
204
|
+
ref += possible_digital.charAt(Math.floor(Math.random() * possible_digital.length));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return ref;
|
|
208
|
+
};
|
|
209
|
+
exports.randomRef = randomRef;
|
|
210
|
+
const treeToArray = (items, array = [], level = 1) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
211
|
+
items.forEach(item => {
|
|
212
|
+
if (item['children']) {
|
|
213
|
+
array.push(Object.assign(Object.assign({}, item), { children: undefined }));
|
|
214
|
+
return (0, exports.treeToArray)(item['children'], array, level + 1);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
array.push(Object.assign({}, item));
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
return array;
|
|
221
|
+
});
|
|
222
|
+
exports.treeToArray = treeToArray;
|
|
223
|
+
const buildTree = (nodes, { index = undefined, field_children = "children", json_convert = false, key_index = "id", key_parent = "parent", root = undefined, to_array = false } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
224
|
+
if (!nodes) {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
if (json_convert) {
|
|
228
|
+
nodes = JSON.parse(JSON.stringify(nodes));
|
|
229
|
+
}
|
|
230
|
+
if (index)
|
|
231
|
+
root = [index];
|
|
232
|
+
let roots = [], children = {}, use_root = false;
|
|
233
|
+
let obj_root = root ? [].concat(root).reduce((total, item) => Object.assign(total, { [item]: {} }), {}) : {};
|
|
234
|
+
if (root instanceof Array) {
|
|
235
|
+
use_root = true;
|
|
236
|
+
}
|
|
237
|
+
for (let i = 0, len = nodes.length; i < len; ++i) {
|
|
238
|
+
let item = nodes[i], k = item[key_index], p = item[key_parent], c = use_root ? k : p, target = (!root && !p || obj_root[c]) ? roots : (children[p] || (children[p] = []));
|
|
239
|
+
if (use_root && obj_root[c]) {
|
|
240
|
+
children[p] = (children[p] || []).concat([item]);
|
|
241
|
+
if (obj_root[p]) {
|
|
242
|
+
obj_root[p][k] = true;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
target.push(item);
|
|
247
|
+
}
|
|
248
|
+
let findChildren = function (parent) {
|
|
249
|
+
if (children[parent[key_index]]) {
|
|
250
|
+
let results;
|
|
251
|
+
for (let i = 0, len = children[parent[key_index]].length; i < len; ++i) {
|
|
252
|
+
let item = children[parent[key_index]][i];
|
|
253
|
+
let obj_children = obj_root[parent[key_index]] || {};
|
|
254
|
+
if (!use_root || !Object.keys(obj_children).length || obj_children[item[key_index]]) {
|
|
255
|
+
obj_root[item[key_index]] = obj_root[item[key_index]] || {};
|
|
256
|
+
findChildren(item);
|
|
257
|
+
results = (results || []).concat([item]);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
parent[field_children] = results;
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
for (let i = 0, len = roots.length; i < len; ++i) {
|
|
264
|
+
findChildren(roots[i]);
|
|
265
|
+
}
|
|
266
|
+
if (index)
|
|
267
|
+
roots = roots[0];
|
|
268
|
+
if (roots && to_array) {
|
|
269
|
+
roots = yield (0, exports.treeToArray)([].concat(roots));
|
|
270
|
+
}
|
|
271
|
+
return roots;
|
|
272
|
+
});
|
|
273
|
+
exports.buildTree = buildTree;
|
|
274
|
+
const expandTree = (items, { array = [], parent = null, level = 1, prefix = "-", field_index = "id", field_children = "children", key_value = "value" } = {}) => {
|
|
275
|
+
let str = new Array(level + 1).join(prefix) + " ";
|
|
276
|
+
items.forEach(item => {
|
|
277
|
+
let value = str + item[key_value];
|
|
278
|
+
if (item[field_children]) {
|
|
279
|
+
array.push(Object.assign(Object.assign({}, item), { parent, [field_children]: undefined, [key_value]: value, has_children: true }));
|
|
280
|
+
let parent_id = item[field_index];
|
|
281
|
+
return (0, exports.expandTree)(item[field_children], { array, parent: parent_id, level: level + 1, prefix, field_index, field_children, key_value });
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
array.push(Object.assign(Object.assign({}, item), { parent, [key_value]: value, has_children: false }));
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
return array;
|
|
288
|
+
};
|
|
289
|
+
exports.expandTree = expandTree;
|
|
290
|
+
const convertToBigEndian = (text) => {
|
|
291
|
+
let buffer = Buffer.from(text, 'ucs2');
|
|
292
|
+
let l = buffer.length;
|
|
293
|
+
if (l & 0x01) {
|
|
294
|
+
throw new Error('Buffer length must be even');
|
|
295
|
+
}
|
|
296
|
+
for (let i = 0; i < l; i += 2) {
|
|
297
|
+
let a = buffer[i];
|
|
298
|
+
buffer[i] = buffer[i + 1];
|
|
299
|
+
buffer[i + 1] = a;
|
|
300
|
+
}
|
|
301
|
+
return buffer.toString('hex');
|
|
302
|
+
};
|
|
303
|
+
exports.convertToBigEndian = convertToBigEndian;
|
|
304
|
+
const slugify = (string = "", { lowercase = false } = {}) => {
|
|
305
|
+
if (!string || typeof (string) != 'string') {
|
|
306
|
+
return string;
|
|
307
|
+
}
|
|
308
|
+
if (lowercase) {
|
|
309
|
+
string = String(string).toLowerCase();
|
|
310
|
+
}
|
|
311
|
+
return String(string)
|
|
312
|
+
//.toLowerCase()
|
|
313
|
+
.replace(/[`~!@#$%^&*()|+=?;:'",.<>\{\}\[\]\\\/]/gi, '')
|
|
314
|
+
.replace(/\s+/g, '-')
|
|
315
|
+
.replace(/\-\-+/g, '-')
|
|
316
|
+
.replace(/^_+/, '')
|
|
317
|
+
.replace(/_+$/, '');
|
|
318
|
+
};
|
|
319
|
+
exports.slugify = slugify;
|
|
320
|
+
const slugifyField = (string = "") => {
|
|
321
|
+
if (!string || typeof (string) != 'string') {
|
|
322
|
+
return string;
|
|
323
|
+
}
|
|
324
|
+
return String(string)
|
|
325
|
+
.toLowerCase()
|
|
326
|
+
.replace(/\s+/g, "_")
|
|
327
|
+
.replace(/[^a-zA-Z0-9:]/g, "_")
|
|
328
|
+
.replace(/\_\_+/g, "_")
|
|
329
|
+
.replace(/^_+/, '')
|
|
330
|
+
.replace(/_+$/, '');
|
|
331
|
+
};
|
|
332
|
+
exports.slugifyField = slugifyField;
|
|
333
|
+
const stripHtml = (string) => {
|
|
334
|
+
if (!string || typeof (string) != 'string') {
|
|
335
|
+
return string;
|
|
336
|
+
}
|
|
337
|
+
return string
|
|
338
|
+
.replace(/<.*?>/g, '')
|
|
339
|
+
.replace(/\t/g, '')
|
|
340
|
+
.replace(' ', '')
|
|
341
|
+
.replace(/\n\n/g, ' ')
|
|
342
|
+
.replace(/\n/g, '');
|
|
343
|
+
};
|
|
344
|
+
exports.stripHtml = stripHtml;
|
|
345
|
+
const replaceColon = (string, { decode = false } = {}) => {
|
|
346
|
+
if (!string || typeof (string) != 'string') {
|
|
347
|
+
return string;
|
|
348
|
+
}
|
|
349
|
+
let regex = /:/g, replace = ":";
|
|
350
|
+
if (decode) {
|
|
351
|
+
regex = /:/g, replace = ":";
|
|
352
|
+
}
|
|
353
|
+
return string.replace(regex, replace);
|
|
354
|
+
};
|
|
355
|
+
exports.replaceColon = replaceColon;
|
|
356
|
+
const getParent = (nodes, { id = "", key_index = "id" } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
357
|
+
let obj = nodes.find(i => i[key_index] === Number(id));
|
|
358
|
+
if (obj) {
|
|
359
|
+
if (obj['parent'] === 0) {
|
|
360
|
+
return obj[key_index];
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
return (0, exports.getParent)(nodes, { id: obj['parent'] });
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return obj;
|
|
367
|
+
});
|
|
368
|
+
exports.getParent = getParent;
|
|
369
|
+
const getDomain = (domain) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
370
|
+
return domain.replace(/^(?:https?:\/\/)?(?:www\.)?/i, "").split('/')[0];
|
|
371
|
+
});
|
|
372
|
+
exports.getDomain = getDomain;
|
|
373
|
+
const getInstanceNumber = () => {
|
|
374
|
+
const key = (0, exports.getENV)('NODE_INSTANCE_KEY', 'NODE_APP_INSTANCE');
|
|
375
|
+
const split = (0, exports.getENV)('NODE_INSTANCE_SPLIT');
|
|
376
|
+
const index = (0, exports.getENV)('NODE_INSTANCE_INDEX');
|
|
377
|
+
let number = process.env[key];
|
|
378
|
+
if (split && number) {
|
|
379
|
+
let str = number.split(split);
|
|
380
|
+
if (str.length > 1)
|
|
381
|
+
number = str[index];
|
|
382
|
+
}
|
|
383
|
+
return number;
|
|
384
|
+
};
|
|
385
|
+
exports.getInstanceNumber = getInstanceNumber;
|
|
386
|
+
const xorEncode = (str, key = '') => {
|
|
387
|
+
if (!key)
|
|
388
|
+
key = (0, exports.getENV)('ENCRYPTION_KEY', 'password');
|
|
389
|
+
key = crypto.createHash('md5').update(key).digest('hex');
|
|
390
|
+
let rand = '';
|
|
391
|
+
while (rand.length < 32) {
|
|
392
|
+
rand += String(Math.floor(Math.random() * (1000 * 1000 * 1000)));
|
|
393
|
+
}
|
|
394
|
+
rand = sha1(rand.substring(0, 32));
|
|
395
|
+
let enc = '';
|
|
396
|
+
for (let i = 0; i < str.length; i++) {
|
|
397
|
+
enc += String.fromCharCode(rand.charCodeAt(i % rand.length)) + String.fromCharCode(rand.charCodeAt(i % rand.length) ^ str.charCodeAt(i));
|
|
398
|
+
}
|
|
399
|
+
return Buffer.from((0, exports.xorMerge)(enc, key)).toString('base64');
|
|
400
|
+
};
|
|
401
|
+
exports.xorEncode = xorEncode;
|
|
402
|
+
const xorDecode = (str, key = '') => {
|
|
403
|
+
if (!key)
|
|
404
|
+
key = (0, exports.getENV)('ENCRYPTION_KEY', 'password');
|
|
405
|
+
key = crypto.createHash('md5').update(key).digest('hex');
|
|
406
|
+
str = Buffer.from(str, 'base64').toString();
|
|
407
|
+
str = (0, exports.xorMerge)(str, key);
|
|
408
|
+
let dec = '';
|
|
409
|
+
for (let i = 0; i < str.length; i++) {
|
|
410
|
+
dec += String.fromCharCode(str.charCodeAt(i++) ^ str.charCodeAt(i));
|
|
411
|
+
}
|
|
412
|
+
return dec;
|
|
413
|
+
};
|
|
414
|
+
exports.xorDecode = xorDecode;
|
|
415
|
+
const xorMerge = (str, key) => {
|
|
416
|
+
let hash = sha1(key);
|
|
417
|
+
let result = '';
|
|
418
|
+
for (let i = 0; i < str.length; i++) {
|
|
419
|
+
result += String.fromCharCode(str.charCodeAt(i) ^ hash.charCodeAt(i % hash.length));
|
|
420
|
+
}
|
|
421
|
+
return result;
|
|
422
|
+
};
|
|
423
|
+
exports.xorMerge = xorMerge;
|
|
424
|
+
const convertCurrency = (data, currency = {}, { fields = [], fields_tax = [], fn_value = {}, fn_field = {}, fn_tax = {}, setting = false, vat_value = 0, tax_include = false, tax_dif = false, obj_format = {} } = {}) => {
|
|
425
|
+
let obj = {};
|
|
426
|
+
let exch_rate = Number(currency["exch_rate"] || 0) || 1;
|
|
427
|
+
let cur_format = currency["cur_format"] || "{{ price }} " + currency['title'];
|
|
428
|
+
let cur_decimal = currency['cur_decimal'] || "0";
|
|
429
|
+
let cur_format_num = currency['cur_format_num'] || "2";
|
|
430
|
+
if (currency['vat_value'] >= 0) {
|
|
431
|
+
vat_value = currency['vat_value'];
|
|
432
|
+
}
|
|
433
|
+
let tax_mod = 1 + (vat_value / 100);
|
|
434
|
+
let format = (currency['cur_comma'] != "0") ? '0,0' : '0';
|
|
435
|
+
let places = String("0").repeat(Number(cur_decimal));
|
|
436
|
+
format = format + "." + places;
|
|
437
|
+
let fnRound = Math.trunc;
|
|
438
|
+
let precision;
|
|
439
|
+
if (cur_format_num != "0") {
|
|
440
|
+
fnRound = Math.round;
|
|
441
|
+
if (cur_format_num == "2") {
|
|
442
|
+
precision = cur_decimal;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
let obj_tax = {};
|
|
446
|
+
if (fields_tax.length) {
|
|
447
|
+
obj_tax = fields_tax.reduce((total, item) => (Object.assign(total, { [item]: true })), {});
|
|
448
|
+
}
|
|
449
|
+
for (let i = 0; i <= fields.length; i++) {
|
|
450
|
+
let field = fields[i];
|
|
451
|
+
let value = data[field];
|
|
452
|
+
if (fn_value[field] && typeof fn_value[field] == "function") {
|
|
453
|
+
value = fn_value[field](data);
|
|
454
|
+
data[field] = value;
|
|
455
|
+
obj_format[field] = true;
|
|
456
|
+
}
|
|
457
|
+
if (value >= 0) {
|
|
458
|
+
if (obj_tax[field]) {
|
|
459
|
+
data[field + '_tax'] = value;
|
|
460
|
+
if (!tax_include) {
|
|
461
|
+
data[field + '_tax'] = Number((value * tax_mod).toFixed(5));
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
value = Number((value / tax_mod).toFixed(5));
|
|
465
|
+
data[field] = value;
|
|
466
|
+
}
|
|
467
|
+
fields.push(field + '_tax');
|
|
468
|
+
obj_format[field + '_tax'] = true;
|
|
469
|
+
if (tax_dif) {
|
|
470
|
+
data[field + '_tax_dif'] = data[field + '_tax'] - value;
|
|
471
|
+
fields.push(field + '_tax_dif');
|
|
472
|
+
obj_format[field + '_tax_dif'] = true;
|
|
473
|
+
}
|
|
474
|
+
if (fn_tax[field] && typeof fn_tax[field] == "function") {
|
|
475
|
+
data[field + '_tax'] = fn_tax[field](data);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (fn_field[field] && typeof fn_field[field] == "function") {
|
|
479
|
+
let option = fn_field[field](data);
|
|
480
|
+
if (option && option['key']) {
|
|
481
|
+
data[option['key']] = option['value'] || 0;
|
|
482
|
+
fields.push(option['key']);
|
|
483
|
+
obj_format[option['key']] = true;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
if (obj_format[field]) {
|
|
487
|
+
let _value = (Number(value).toFixed(5));
|
|
488
|
+
if (precision) {
|
|
489
|
+
_value = _.round(_value, precision);
|
|
490
|
+
}
|
|
491
|
+
data[field] = Number(numeral(_value).format('0.' + places, fnRound));
|
|
492
|
+
}
|
|
493
|
+
value = ((Number(value) * exch_rate)).toFixed(5) + "";
|
|
494
|
+
if (precision) {
|
|
495
|
+
value = _.round(value, precision);
|
|
496
|
+
}
|
|
497
|
+
let formatted = numeral(value).format(format, fnRound);
|
|
498
|
+
obj[field] = formatted;
|
|
499
|
+
obj[field + "_formatted"] = cur_format.replace("{{ price }}", formatted);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
;
|
|
503
|
+
if (setting) {
|
|
504
|
+
obj = Object.assign({ cur_format, format, precision, cur_format_num }, obj);
|
|
505
|
+
}
|
|
506
|
+
return obj;
|
|
507
|
+
};
|
|
508
|
+
exports.convertCurrency = convertCurrency;
|
package/utils/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './core';
|
|
2
|
+
export * from './config';
|
|
3
|
+
export * from './auth';
|
|
4
|
+
export * from './file';
|
|
5
|
+
export * from './language';
|
|
6
|
+
export * from './setting';
|
|
7
|
+
export * from './stream';
|
|
8
|
+
export * from './validator';
|
|
9
|
+
export * from './user';
|
|
10
|
+
export * from './currency';
|
package/utils/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./core"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./config"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./auth"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./file"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./language"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./setting"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./stream"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./validator"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./user"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./currency"), exports);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
interface OptionLang {
|
|
2
|
+
organ_id?: string | number;
|
|
3
|
+
[index: string]: any;
|
|
4
|
+
}
|
|
5
|
+
export declare class LanguageUtil {
|
|
6
|
+
ref: string;
|
|
7
|
+
option: OptionLang;
|
|
8
|
+
constructor(ref: string, option?: OptionLang);
|
|
9
|
+
getLangsAll({ where }?: {
|
|
10
|
+
where?: {};
|
|
11
|
+
}): Promise<import("sequelize").Model<any, any>[]>;
|
|
12
|
+
getLangsLive({ where, attr_include }?: {
|
|
13
|
+
where?: {};
|
|
14
|
+
attr_include?: any[];
|
|
15
|
+
}): Promise<Array<any>>;
|
|
16
|
+
existLang(lang_code: string): Promise<0 | 1>;
|
|
17
|
+
}
|
|
18
|
+
export {};
|