@zuzjs/core 0.1.3 → 0.1.5
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/index.d.ts +35 -45
- package/dist/index.js +374 -148
- package/dist/regexps.d.ts +3 -0
- package/dist/regexps.js +9 -0
- package/dist/types.d.ts +21 -17
- package/dist/types.js +8 -1
- package/dist/withGlobals.d.ts +25 -0
- package/dist/withGlobals.js +79 -0
- package/package.json +27 -12
package/dist/index.d.ts
CHANGED
|
@@ -1,46 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const
|
|
9
|
-
export declare const
|
|
10
|
-
export declare const
|
|
11
|
-
export declare const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
import { AxiosProgressEvent } from "axios";
|
|
2
|
+
import { dynamicObject, FormatNumberParams, sortOptions } from "./types";
|
|
3
|
+
export declare const __SALT: string;
|
|
4
|
+
export { default as "_" } from "./withGlobals";
|
|
5
|
+
export declare const numberInRange: (min: number, max: number) => number;
|
|
6
|
+
export declare const toHash: (n: number, len?: number, SALT?: string | null) => string;
|
|
7
|
+
export declare const fromHash: (str: string, SALT?: string | null) => number;
|
|
8
|
+
export declare const MD5: (str: string) => string;
|
|
9
|
+
export declare const uuid: (len: number) => string;
|
|
10
|
+
export declare const ucfirst: (o: any) => string;
|
|
11
|
+
export declare const urldecode: (str: string) => string;
|
|
12
|
+
export declare const urlencode: (str: string) => string;
|
|
13
|
+
export declare const pluralize: (word: string, count: number) => string;
|
|
14
|
+
export declare const isHexColor: (color: string) => boolean;
|
|
15
|
+
export declare const isRgbaColor: (color: string) => boolean;
|
|
16
|
+
export declare const isHslColor: (color: string) => boolean;
|
|
17
|
+
export declare const isColor: (color: string) => boolean;
|
|
18
|
+
export declare const hexToRgba: (hex: string, alpha?: number) => string;
|
|
19
|
+
export declare const removeDuplicates: <T>(array: T[]) => T[];
|
|
20
|
+
export declare const withPost: <T>(uri: string, data: dynamicObject | FormData | string, timeout?: number, ignoreKind?: boolean, headers?: dynamicObject, onProgress?: (ev: AxiosProgressEvent) => void) => Promise<T>;
|
|
21
|
+
export declare const withGet: <T>(uri: string, timeout?: number, ignoreKind?: boolean) => Promise<T>;
|
|
22
|
+
export declare const withTime: (fun: (...args: any[]) => any) => {
|
|
23
|
+
result: any;
|
|
24
|
+
executionTime: number;
|
|
23
25
|
};
|
|
24
|
-
export declare const
|
|
25
|
-
export declare const
|
|
26
|
-
export declare const
|
|
27
|
-
export declare const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
export declare const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
* @function equals
|
|
36
|
-
* @function isString
|
|
37
|
-
* @function isNumber
|
|
38
|
-
* @function isObject
|
|
39
|
-
* @function isArray
|
|
40
|
-
* @function isEmpty
|
|
41
|
-
* @function isNotEmpty
|
|
42
|
-
* @function isEmail
|
|
43
|
-
* @function isUrl
|
|
44
|
-
*
|
|
45
|
-
*/
|
|
46
|
-
export declare const extendGlobals: () => void;
|
|
26
|
+
export declare const time: (stamp?: number, format?: string) => string;
|
|
27
|
+
export declare const timeSince: (stamp: number) => string;
|
|
28
|
+
export declare const arrayRand: (arr: any[]) => any;
|
|
29
|
+
export declare const formatNumber: ({ number, locale, style, decimal, currency }: FormatNumberParams) => string;
|
|
30
|
+
export declare const formatSize: (bytes: number | string) => string;
|
|
31
|
+
export declare const copyToClipboard: (text: string) => Promise<unknown>;
|
|
32
|
+
export declare const natsort: (options?: sortOptions) => (a: string | number, b: string | number) => number;
|
|
33
|
+
export declare const camelCase: (str: string, ucf?: boolean) => string;
|
|
34
|
+
export declare const camelCaseToDash: (str: string) => string;
|
|
35
|
+
export declare const clamp: (value: number, min: number, max: number) => number;
|
|
36
|
+
export declare const slugify: (text: string, separator?: string) => string;
|
package/dist/index.js
CHANGED
|
@@ -1,115 +1,98 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.slugify = exports.clamp = exports.camelCaseToDash = exports.camelCase = exports.natsort = exports.copyToClipboard = exports.formatSize = exports.formatNumber = exports.arrayRand = exports.timeSince = exports.time = exports.withTime = exports.withGet = exports.withPost = exports.removeDuplicates = exports.hexToRgba = exports.isColor = exports.isHslColor = exports.isRgbaColor = exports.isHexColor = exports.pluralize = exports.urlencode = exports.urldecode = exports.ucfirst = exports.uuid = exports.MD5 = exports.fromHash = exports.toHash = exports.numberInRange = exports["_"] = exports.__SALT = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const hashids_1 = __importDefault(require("hashids"));
|
|
9
|
+
const js_cookie_1 = __importDefault(require("js-cookie"));
|
|
10
|
+
const md5_1 = __importDefault(require("md5"));
|
|
11
|
+
const moment_1 = __importDefault(require("moment"));
|
|
12
|
+
const regexps_1 = require("./regexps");
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const withGlobals_1 = __importDefault(require("./withGlobals"));
|
|
15
|
+
exports.__SALT = `zuzjs-core`;
|
|
16
|
+
var withGlobals_2 = require("./withGlobals");
|
|
17
|
+
Object.defineProperty(exports, "_", { enumerable: true, get: function () { return __importDefault(withGlobals_2).default; } });
|
|
18
|
+
const numberInRange = (min, max) => {
|
|
19
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
20
20
|
};
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (_time.length > 1) { // If time format correct
|
|
29
|
-
__ = _time.slice(1); // Remove full string match value
|
|
30
|
-
__[5] = +_time[0] < 12 ? 'AM' : 'PM'; // Set AM/PM
|
|
31
|
-
__[0] = +_time[0] % 12 || 12;
|
|
21
|
+
exports.numberInRange = numberInRange;
|
|
22
|
+
const toHash = (n, len = 6, SALT = null) => new hashids_1.default(SALT || exports.__SALT, len).encode(n);
|
|
23
|
+
exports.toHash = toHash;
|
|
24
|
+
const fromHash = (str, SALT = null) => {
|
|
25
|
+
try {
|
|
26
|
+
const n = new hashids_1.default(SALT || exports.__SALT, +process.env.HASHIDS_LENGTH).decode(str);
|
|
27
|
+
return n.length >= 0 ? Number(n[0]) : 0;
|
|
32
28
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
export const findParent = (node, classes, depth = 1) => {
|
|
66
|
-
var n, i = node.parentNode, o = 0, a = 0;
|
|
67
|
-
if (null === i || !i.classList || i.classList.length == 0)
|
|
68
|
-
return null;
|
|
69
|
-
for (n = classes.length; a < n; ++a)
|
|
70
|
-
i.classList.contains(classes[a]) && (o += 1);
|
|
71
|
-
return o === n ? i : 0 < depth ? findParent(i, classes, depth - 1) : null;
|
|
72
|
-
};
|
|
73
|
-
export const ucfirst = (str) => {
|
|
74
|
-
return str = str.trim(),
|
|
75
|
-
str.charAt(0).toUpperCase() + str.substring(1);
|
|
76
|
-
};
|
|
77
|
-
const buildFormData = (data) => {
|
|
78
|
-
var formData = new FormData();
|
|
79
|
-
var _data = Cookies.get();
|
|
80
|
-
Object.keys(_data).map(k => formData.append(k, _data[k]));
|
|
81
|
-
Object.keys(data).filter(x => x != 'files').map(k => formData.append(k, data[k]));
|
|
82
|
-
if ('files' in data)
|
|
83
|
-
[data['files']].map((f) => formData.append('files[]', f));
|
|
84
|
-
return formData;
|
|
85
|
-
};
|
|
86
|
-
/**
|
|
87
|
-
* HTTP Requests
|
|
88
|
-
*/
|
|
89
|
-
export const withRest = async (uri, data, timeout = 60, fd, progress, bearer = `__ha`) => {
|
|
90
|
-
var Bearer = getCookie(bearer) || `${uuid(8)}^${uuid(8)}`;
|
|
91
|
-
var isWindow = typeof window !== 'undefined';
|
|
92
|
-
var cancelToken = null;
|
|
93
|
-
if (isWindow) {
|
|
94
|
-
window.__restToken = axios.CancelToken.source();
|
|
95
|
-
cancelToken = window.__restToken?.token;
|
|
29
|
+
catch (e) {
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
exports.fromHash = fromHash;
|
|
34
|
+
const MD5 = (str) => (0, md5_1.default)(str);
|
|
35
|
+
exports.MD5 = MD5;
|
|
36
|
+
const uuid = (len) => (0, exports.toHash)((0, exports.numberInRange)(11111111111, 999999999999));
|
|
37
|
+
exports.uuid = uuid;
|
|
38
|
+
const ucfirst = (o) => `${o.charAt(0).toUpperCase()}${o.substring(1, o.length)}`;
|
|
39
|
+
exports.ucfirst = ucfirst;
|
|
40
|
+
const urldecode = (str) => decodeURIComponent(str.replace(/\+/g, '%20'));
|
|
41
|
+
exports.urldecode = urldecode;
|
|
42
|
+
const urlencode = (str) => encodeURIComponent(str);
|
|
43
|
+
exports.urlencode = urlencode;
|
|
44
|
+
const pluralize = (word, count) => `${word}${count !== 1 ? 's' : ''}`;
|
|
45
|
+
exports.pluralize = pluralize;
|
|
46
|
+
const isHexColor = (color) => regexps_1.hexColorRegex.test(color);
|
|
47
|
+
exports.isHexColor = isHexColor;
|
|
48
|
+
const isRgbaColor = (color) => regexps_1.rgbaColorRegex.test(color);
|
|
49
|
+
exports.isRgbaColor = isRgbaColor;
|
|
50
|
+
const isHslColor = (color) => regexps_1.hslColorRegex.test(color);
|
|
51
|
+
exports.isHslColor = isHslColor;
|
|
52
|
+
// Function to validate a color string
|
|
53
|
+
const isColor = (color) => (0, exports.isHexColor)(color) || (0, exports.isRgbaColor)(color) || (0, exports.isHslColor)(color);
|
|
54
|
+
exports.isColor = isColor;
|
|
55
|
+
const hexToRgba = (hex, alpha = 1) => {
|
|
56
|
+
// Remove the hash symbol if present
|
|
57
|
+
hex = hex.replace(/^#/, '');
|
|
58
|
+
// If shorthand hex (#RGB), expand it to full form (#RRGGBB)
|
|
59
|
+
if (hex.length === 3) {
|
|
60
|
+
hex = hex.split('').map(char => char + char).join('');
|
|
96
61
|
}
|
|
97
|
-
|
|
62
|
+
// Convert to integer values for RGB
|
|
63
|
+
const bigint = parseInt(hex, 16);
|
|
64
|
+
const r = (bigint >> 16) & 255;
|
|
65
|
+
const g = (bigint >> 8) & 255;
|
|
66
|
+
const b = bigint & 255;
|
|
67
|
+
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
|
68
|
+
};
|
|
69
|
+
exports.hexToRgba = hexToRgba;
|
|
70
|
+
const removeDuplicates = (array) => {
|
|
71
|
+
return array.reduce((accumulator, currentValue) => {
|
|
72
|
+
if (!accumulator.includes(currentValue)) {
|
|
73
|
+
accumulator.push(currentValue);
|
|
74
|
+
}
|
|
75
|
+
return accumulator;
|
|
76
|
+
}, []);
|
|
77
|
+
};
|
|
78
|
+
exports.removeDuplicates = removeDuplicates;
|
|
79
|
+
const withPost = async (uri, data, timeout = 60, ignoreKind = false, headers, onProgress) => {
|
|
80
|
+
const _cookies = js_cookie_1.default.get();
|
|
81
|
+
if (data instanceof FormData) {
|
|
82
|
+
for (const c in _cookies) {
|
|
83
|
+
data.append(c, _cookies[c]);
|
|
84
|
+
}
|
|
98
85
|
return new Promise((resolve, reject) => {
|
|
99
|
-
|
|
100
|
-
method:
|
|
86
|
+
(0, axios_1.default)({
|
|
87
|
+
method: 'post',
|
|
101
88
|
url: uri,
|
|
102
|
-
data:
|
|
103
|
-
timeout:
|
|
104
|
-
cancelToken: cancelToken,
|
|
89
|
+
data: data,
|
|
90
|
+
timeout: timeout * 1000,
|
|
105
91
|
headers: {
|
|
106
92
|
'Content-Type': 'multipart/form-data',
|
|
107
|
-
|
|
93
|
+
...(headers || {})
|
|
108
94
|
},
|
|
109
|
-
onUploadProgress: ev =>
|
|
110
|
-
//TODO: Add progress
|
|
111
|
-
// if(progress) progress(ev.)
|
|
112
|
-
}
|
|
95
|
+
onUploadProgress: ev => onProgress && onProgress(ev)
|
|
113
96
|
})
|
|
114
97
|
.then(resp => {
|
|
115
98
|
if (resp.data && "kind" in resp.data) {
|
|
@@ -122,58 +105,301 @@ export const withRest = async (uri, data, timeout = 60, fd, progress, bearer = `
|
|
|
122
105
|
.catch(err => reject(err));
|
|
123
106
|
});
|
|
124
107
|
}
|
|
108
|
+
else if ((0, withGlobals_1.default)(data).isString()) {
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
axios_1.default.post(uri, data, {
|
|
111
|
+
timeout: 1000 * timeout,
|
|
112
|
+
headers: {
|
|
113
|
+
'Content-Type': 'application/json',
|
|
114
|
+
...(headers || {})
|
|
115
|
+
}
|
|
116
|
+
})
|
|
117
|
+
.then(resp => {
|
|
118
|
+
if (resp.data && (ignoreKind || ("kind" in resp.data))) {
|
|
119
|
+
resolve(resp.data);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
reject(resp.data);
|
|
123
|
+
}
|
|
124
|
+
})
|
|
125
|
+
.catch(err => {
|
|
126
|
+
if (err?.response?.data)
|
|
127
|
+
reject(err.response.data);
|
|
128
|
+
else
|
|
129
|
+
reject(err.code && err.code == `ERR_NETWORK` ? { error: err.code, message: navigator.onLine ? `Unable to connect to the server. It may be temporarily down.` : `Network error: Unable to connect. Please check your internet connection and try again.` } : err);
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
else if (typeof data === "object" && !Array.isArray(data) && data !== null) {
|
|
134
|
+
return new Promise((resolve, reject) => {
|
|
135
|
+
axios_1.default.post(uri, {
|
|
136
|
+
...data,
|
|
137
|
+
..._cookies,
|
|
138
|
+
__stmp: new Date().getTime() / 1000
|
|
139
|
+
}, {
|
|
140
|
+
timeout: 1000 * timeout,
|
|
141
|
+
headers: {
|
|
142
|
+
'Content-Type': 'application/json',
|
|
143
|
+
...(headers || {})
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
.then(resp => {
|
|
147
|
+
if (resp.data && (ignoreKind || ("kind" in resp.data))) {
|
|
148
|
+
resolve(resp.data);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
reject(resp.data);
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
.catch(err => {
|
|
155
|
+
if (err?.response?.data)
|
|
156
|
+
reject(err.response.data);
|
|
157
|
+
else
|
|
158
|
+
reject(err.code && err.code == `ERR_NETWORK` ? { error: err.code, message: navigator.onLine ? `Unable to connect to the server. It may be temporarily down.` : `Network error: Unable to connect. Please check your internet connection and try again.` } : err);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
}
|
|
125
162
|
return new Promise((resolve, reject) => {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
cancelToken: cancelToken
|
|
137
|
-
})
|
|
138
|
-
.then(resp => {
|
|
139
|
-
if (resp.data && "kind" in resp.data) {
|
|
163
|
+
reject();
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
exports.withPost = withPost;
|
|
167
|
+
const withGet = async (uri, timeout = 60, ignoreKind = false) => {
|
|
168
|
+
return new Promise((resolve, reject) => {
|
|
169
|
+
axios_1.default
|
|
170
|
+
.get(uri, { timeout: timeout * 1000 })
|
|
171
|
+
.then((resp) => {
|
|
172
|
+
if (resp.data && (ignoreKind || "kind" in resp.data)) {
|
|
140
173
|
resolve(resp.data);
|
|
141
174
|
}
|
|
142
175
|
else {
|
|
143
176
|
reject(resp.data);
|
|
144
177
|
}
|
|
145
178
|
})
|
|
146
|
-
.catch(err =>
|
|
179
|
+
.catch((err) => {
|
|
180
|
+
if (err?.response?.data)
|
|
181
|
+
reject(err.response.data);
|
|
182
|
+
else
|
|
183
|
+
reject(err.code === `ERR_NETWORK`
|
|
184
|
+
? {
|
|
185
|
+
error: err.code,
|
|
186
|
+
message: navigator.onLine
|
|
187
|
+
? `Unable to connect to the server. It may be temporarily down.`
|
|
188
|
+
: `Network error: Unable to connect. Please check your internet connection and try again.`,
|
|
189
|
+
}
|
|
190
|
+
: err);
|
|
191
|
+
});
|
|
147
192
|
});
|
|
148
193
|
};
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
194
|
+
exports.withGet = withGet;
|
|
195
|
+
const withTime = (fun) => {
|
|
196
|
+
const start = new Date().getTime();
|
|
197
|
+
const result = fun();
|
|
198
|
+
const end = new Date().getTime();
|
|
199
|
+
return {
|
|
200
|
+
result,
|
|
201
|
+
executionTime: end - start
|
|
202
|
+
};
|
|
203
|
+
};
|
|
204
|
+
exports.withTime = withTime;
|
|
205
|
+
const time = (stamp, format) => {
|
|
206
|
+
return stamp ?
|
|
207
|
+
moment_1.default.unix(+stamp / 1000).format(format || `YYYY-MM-DD HH:mm:ss`)
|
|
208
|
+
: (0, moment_1.default)().format(format || `YYYY-MM-DD HH:mm:ss`);
|
|
209
|
+
};
|
|
210
|
+
exports.time = time;
|
|
211
|
+
const timeSince = (stamp) => (0, moment_1.default)(stamp).fromNow();
|
|
212
|
+
exports.timeSince = timeSince;
|
|
213
|
+
const arrayRand = (arr) => arr[Math.floor(Math.random() * arr.length)];
|
|
214
|
+
exports.arrayRand = arrayRand;
|
|
215
|
+
const formatNumber = ({ number, locale = 'en-US', style = `decimal`, decimal = 2, currency }) => {
|
|
216
|
+
if (style === 'currency' && !currency) {
|
|
217
|
+
throw new TypeError('Currency code is required with currency style.');
|
|
218
|
+
}
|
|
219
|
+
if (currency) {
|
|
220
|
+
const { code, style: currencyStyle, symbol } = currency;
|
|
221
|
+
const out = new Intl.NumberFormat(locale, {
|
|
222
|
+
style: `currency`,
|
|
223
|
+
currency: code,
|
|
224
|
+
currencyDisplay: currencyStyle,
|
|
225
|
+
minimumFractionDigits: +number % 1 > 0 ? decimal : 0,
|
|
226
|
+
maximumFractionDigits: +number % 1 > 0 ? decimal : 0
|
|
227
|
+
}).format(+number);
|
|
228
|
+
return symbol ? out.replace(new RegExp(`\\${code}`, 'g'), symbol) : out;
|
|
229
|
+
}
|
|
230
|
+
return new Intl.NumberFormat(locale, {
|
|
231
|
+
style,
|
|
232
|
+
minimumFractionDigits: +number % 1 > 0 ? 2 : 0,
|
|
233
|
+
maximumFractionDigits: +number % 1 > 0 ? 2 : 0
|
|
234
|
+
}).format(+number);
|
|
235
|
+
};
|
|
236
|
+
exports.formatNumber = formatNumber;
|
|
237
|
+
const formatSize = (bytes) => {
|
|
238
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
239
|
+
const _bytes = `string` == typeof bytes ? parseFloat(bytes) : bytes;
|
|
240
|
+
if (_bytes == 0)
|
|
241
|
+
return '0 Byte';
|
|
242
|
+
const _i = Math.floor(Math.log(_bytes) / Math.log(1024));
|
|
243
|
+
const i = `string` == typeof _i ? parseInt(_i) : _i;
|
|
244
|
+
const nx = _bytes / Math.pow(1024, i);
|
|
245
|
+
return nx.toFixed(2) + ' ' + sizes[i];
|
|
246
|
+
};
|
|
247
|
+
exports.formatSize = formatSize;
|
|
248
|
+
const copyToClipboard = (text) => {
|
|
249
|
+
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
250
|
+
return navigator.clipboard.writeText(text);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
return new Promise((resolve, reject) => {
|
|
254
|
+
const textarea = document.createElement("textarea");
|
|
255
|
+
textarea.value = text;
|
|
256
|
+
textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge.
|
|
257
|
+
document.body.appendChild(textarea);
|
|
258
|
+
textarea.focus();
|
|
259
|
+
textarea.select();
|
|
260
|
+
try {
|
|
261
|
+
document.execCommand("copy");
|
|
262
|
+
resolve(`Copied to clipboard`);
|
|
263
|
+
}
|
|
264
|
+
catch (err) {
|
|
265
|
+
// console.error("Fallback: Oops, unable to copy", err);
|
|
266
|
+
reject(err);
|
|
267
|
+
}
|
|
268
|
+
document.body.removeChild(textarea);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
exports.copyToClipboard = copyToClipboard;
|
|
273
|
+
const natsort = (options = {
|
|
274
|
+
direction: types_1.SORT.Asc,
|
|
275
|
+
caseSensitive: false,
|
|
276
|
+
}) => {
|
|
277
|
+
const ore = /^0/;
|
|
278
|
+
const sre = /\s+/g;
|
|
279
|
+
const tre = /^\s+|\s+$/g;
|
|
280
|
+
// unicode
|
|
281
|
+
const ure = /[^\x00-\x80]/;
|
|
282
|
+
// hex
|
|
283
|
+
const hre = /^0x[0-9a-f]+$/i;
|
|
284
|
+
// numeric
|
|
285
|
+
const nre = /(0x[\da-fA-F]+|(^[\+\-]?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?(?=\D|\s|$))|\d+)/g;
|
|
286
|
+
// datetime
|
|
287
|
+
const dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/; // tslint:disable-line
|
|
288
|
+
const GREATER = options.direction == types_1.SORT.Desc ? -1 : 1;
|
|
289
|
+
const SMALLER = -GREATER;
|
|
290
|
+
const _normalize = !options.caseSensitive
|
|
291
|
+
? (s) => s.toString().toLowerCase().replace(tre, '')
|
|
292
|
+
: (s) => (`${s}`).replace(tre, '');
|
|
293
|
+
const _tokenize = (s) => {
|
|
294
|
+
return s.replace(nre, '\0$1\0')
|
|
295
|
+
.replace(/\0$/, '')
|
|
296
|
+
.replace(/^\0/, '')
|
|
297
|
+
.split('\0');
|
|
177
298
|
};
|
|
299
|
+
const _parse = (s, l) => {
|
|
300
|
+
return (!s.match(ore) || l === 1) &&
|
|
301
|
+
parseFloat(s)
|
|
302
|
+
|| s.replace(sre, ' ').replace(tre, '')
|
|
303
|
+
|| 0;
|
|
304
|
+
};
|
|
305
|
+
return function (a, b) {
|
|
306
|
+
const aa = _normalize(a);
|
|
307
|
+
const bb = _normalize(b);
|
|
308
|
+
if (!aa && !bb) {
|
|
309
|
+
return 0;
|
|
310
|
+
}
|
|
311
|
+
if (!aa && bb) {
|
|
312
|
+
return SMALLER;
|
|
313
|
+
}
|
|
314
|
+
if (aa && !bb) {
|
|
315
|
+
return GREATER;
|
|
316
|
+
}
|
|
317
|
+
const aArr = _tokenize(aa);
|
|
318
|
+
const bArr = _tokenize(bb);
|
|
319
|
+
// hex or date detection
|
|
320
|
+
const aHex = aa.match(hre);
|
|
321
|
+
const bHex = bb.match(hre);
|
|
322
|
+
const av = (aHex && bHex) ? parseInt(aHex[0], 16) : (aArr.length !== 1 && Date.parse(aa));
|
|
323
|
+
const bv = (aHex && bHex)
|
|
324
|
+
? parseInt(bHex[0], 16)
|
|
325
|
+
: av && bb.match(dre) && Date.parse(bb) || null;
|
|
326
|
+
// try and sort Hex codes or Dates
|
|
327
|
+
if (bv) {
|
|
328
|
+
if (av === bv) {
|
|
329
|
+
return 0;
|
|
330
|
+
}
|
|
331
|
+
if (typeof av === 'number' && typeof bv === 'number' && av < bv) {
|
|
332
|
+
return SMALLER;
|
|
333
|
+
}
|
|
334
|
+
if (typeof av === 'number' && av > bv) {
|
|
335
|
+
return GREATER;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const al = aArr.length;
|
|
339
|
+
const bl = bArr.length;
|
|
340
|
+
// handle numeric strings and default strings
|
|
341
|
+
for (let i = 0, l = Math.max(al, bl); i < l; i += 1) {
|
|
342
|
+
const af = _parse(aArr[i] || '', al);
|
|
343
|
+
const bf = _parse(bArr[i] || '', bl);
|
|
344
|
+
if (isNaN(af) !== isNaN(bf)) {
|
|
345
|
+
return isNaN(af) ? GREATER : SMALLER;
|
|
346
|
+
}
|
|
347
|
+
if (ure.test(af + bf) && af.localeCompare) {
|
|
348
|
+
const comp = af.localeCompare(bf);
|
|
349
|
+
if (comp > 0) {
|
|
350
|
+
return GREATER;
|
|
351
|
+
}
|
|
352
|
+
if (comp < 0) {
|
|
353
|
+
return SMALLER;
|
|
354
|
+
}
|
|
355
|
+
if (i === l - 1) {
|
|
356
|
+
return 0;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (af < bf) {
|
|
360
|
+
return SMALLER;
|
|
361
|
+
}
|
|
362
|
+
if (af > bf) {
|
|
363
|
+
return GREATER;
|
|
364
|
+
}
|
|
365
|
+
if (`${af}` < `${bf}`) {
|
|
366
|
+
return SMALLER;
|
|
367
|
+
}
|
|
368
|
+
if (`${af}` > `${bf}`) {
|
|
369
|
+
return GREATER;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return 0;
|
|
373
|
+
};
|
|
374
|
+
};
|
|
375
|
+
exports.natsort = natsort;
|
|
376
|
+
const camelCase = (str, ucf = false) => {
|
|
377
|
+
return str
|
|
378
|
+
.toLowerCase()
|
|
379
|
+
.split(/[^a-zA-Z0-9]+/) // Split by any non-alphanumeric character
|
|
380
|
+
.map((word, index) => index === 0
|
|
381
|
+
? ucf ? (0, exports.ucfirst)(word) : word
|
|
382
|
+
: (0, exports.ucfirst)(word))
|
|
383
|
+
.join('');
|
|
384
|
+
};
|
|
385
|
+
exports.camelCase = camelCase;
|
|
386
|
+
const camelCaseToDash = (str) => str.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
387
|
+
exports.camelCaseToDash = camelCaseToDash;
|
|
388
|
+
const clamp = (value, min, max) => {
|
|
389
|
+
return Math.min(Math.max(value, min), max);
|
|
390
|
+
};
|
|
391
|
+
exports.clamp = clamp;
|
|
392
|
+
const slugify = (text, separator = "-") => {
|
|
393
|
+
if (undefined == text) {
|
|
394
|
+
console.log(text, `is undefined`);
|
|
395
|
+
return ``;
|
|
396
|
+
}
|
|
397
|
+
return text
|
|
398
|
+
.normalize("NFKD") // Normalize accents (e.g., é → e)
|
|
399
|
+
.replace(/[\u0300-\u036f]/g, "") // Remove diacritic marks
|
|
400
|
+
.toLowerCase()
|
|
401
|
+
.replace(/[^a-z0-9\p{L}\p{N}]+/gu, separator) // Keep letters/numbers from all languages
|
|
402
|
+
.replace(new RegExp(`\\${separator}{2,}`, "g"), separator) // Remove duplicate separators
|
|
403
|
+
.replace(new RegExp(`^\\${separator}|\\${separator}$`, "g"), ""); // Trim separators from ends
|
|
178
404
|
};
|
|
179
|
-
|
|
405
|
+
exports.slugify = slugify;
|
package/dist/regexps.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hslColorRegex = exports.rgbaColorRegex = exports.hexColorRegex = void 0;
|
|
4
|
+
// Hex color regex (#RGB, #RRGGBB)
|
|
5
|
+
exports.hexColorRegex = /^#([A-Fa-f0-9]{3}){1,2}$/;
|
|
6
|
+
// RGBA color regex (rgba(255, 255, 255, 1))
|
|
7
|
+
exports.rgbaColorRegex = /^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(,\s*((0|1|0?\.\d+)\s*))?\)$/;
|
|
8
|
+
// HSL color regex (hsl(360, 100%, 100%))
|
|
9
|
+
exports.hslColorRegex = /^hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)$/;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
isEmail(v: any): boolean;
|
|
15
|
-
isUrl(v: any): boolean;
|
|
16
|
-
}
|
|
1
|
+
export type dynamicObject = {
|
|
2
|
+
[x: string]: any;
|
|
3
|
+
};
|
|
4
|
+
export interface FormatNumberParams {
|
|
5
|
+
number: number | string;
|
|
6
|
+
locale: string;
|
|
7
|
+
style?: `decimal` | `currency` | `percent`;
|
|
8
|
+
decimal?: number;
|
|
9
|
+
currency?: {
|
|
10
|
+
code: string;
|
|
11
|
+
style: `symbol` | `code` | `name`;
|
|
12
|
+
symbol?: string;
|
|
13
|
+
};
|
|
17
14
|
}
|
|
18
|
-
export {
|
|
15
|
+
export declare enum SORT {
|
|
16
|
+
Asc = "ASC",
|
|
17
|
+
Desc = "DESC"
|
|
18
|
+
}
|
|
19
|
+
export type sortOptions = {
|
|
20
|
+
direction?: SORT;
|
|
21
|
+
caseSensitive?: boolean;
|
|
22
|
+
};
|
package/dist/types.js
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
declare class withGlobals {
|
|
2
|
+
_: any;
|
|
3
|
+
constructor(value: any);
|
|
4
|
+
isTypeof(v: any): boolean;
|
|
5
|
+
isFunction(): boolean;
|
|
6
|
+
isArray(): boolean;
|
|
7
|
+
isNull(): boolean;
|
|
8
|
+
isString(): boolean;
|
|
9
|
+
isNumber(): boolean;
|
|
10
|
+
isObject(): boolean;
|
|
11
|
+
isEmpty(): boolean;
|
|
12
|
+
isEmail(): boolean;
|
|
13
|
+
isUrl(): boolean;
|
|
14
|
+
toLowerCase(): this;
|
|
15
|
+
equals(v: any): boolean;
|
|
16
|
+
ucfirst(): this;
|
|
17
|
+
formatString(v: string | number, ...vv: (string | number)[]): this;
|
|
18
|
+
camelCase(): this;
|
|
19
|
+
value(): any;
|
|
20
|
+
valueOf(): any;
|
|
21
|
+
toString(): string;
|
|
22
|
+
[Symbol.toPrimitive](hint: string): string | number | boolean;
|
|
23
|
+
}
|
|
24
|
+
declare const _: <T>(value: T) => withGlobals;
|
|
25
|
+
export default _;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class withGlobals {
|
|
4
|
+
_;
|
|
5
|
+
constructor(value) {
|
|
6
|
+
this._ = value;
|
|
7
|
+
}
|
|
8
|
+
isTypeof(v) {
|
|
9
|
+
return typeof this._ === typeof v;
|
|
10
|
+
}
|
|
11
|
+
isFunction() {
|
|
12
|
+
return typeof this._ === "function";
|
|
13
|
+
}
|
|
14
|
+
isArray() {
|
|
15
|
+
return Array.isArray(this._);
|
|
16
|
+
}
|
|
17
|
+
isNull() {
|
|
18
|
+
return this._ === null;
|
|
19
|
+
}
|
|
20
|
+
isString() {
|
|
21
|
+
return typeof this._ === "string";
|
|
22
|
+
}
|
|
23
|
+
isNumber() {
|
|
24
|
+
return /^[+-]?\d+(\.\d+)?$/.test(this._);
|
|
25
|
+
}
|
|
26
|
+
isObject() {
|
|
27
|
+
return typeof this._ === "object" && !Array.isArray(this._) && this._ !== null;
|
|
28
|
+
}
|
|
29
|
+
isEmpty() {
|
|
30
|
+
if (Array.isArray(this._))
|
|
31
|
+
return this._.length === 0;
|
|
32
|
+
if (typeof this._ === "object" && this._ !== null)
|
|
33
|
+
return Object.keys(this._).length === 0;
|
|
34
|
+
return this._ === "" || String(this._).length === 0;
|
|
35
|
+
}
|
|
36
|
+
isEmail() {
|
|
37
|
+
return typeof this._ === "string" && /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this._);
|
|
38
|
+
}
|
|
39
|
+
isUrl() {
|
|
40
|
+
return typeof this._ === "string" && /^(https?:\/\/)?(www\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(\/[^\s]*)?$/.test(this._);
|
|
41
|
+
}
|
|
42
|
+
toLowerCase() {
|
|
43
|
+
this._ = typeof this._ === "string" ? this._.toLowerCase() : String(this._).toLowerCase();
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
equals(v) { return this._ === v; }
|
|
47
|
+
ucfirst() {
|
|
48
|
+
this._ = typeof this._ === "string" ? this._.charAt(0).toUpperCase() + this._.slice(1) : this._;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
formatString(v, ...vv) {
|
|
52
|
+
if (typeof this._ !== "string")
|
|
53
|
+
this._ = "";
|
|
54
|
+
const values = [v, ...vv];
|
|
55
|
+
this._ = this._.replace(/%(\d+)/g, (inp, index) => values[Number(index)]?.toString() || `%${index}`);
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
camelCase() {
|
|
59
|
+
this._ = typeof this._ === "string"
|
|
60
|
+
? this._
|
|
61
|
+
.split(/[^a-zA-Z0-9]+/)
|
|
62
|
+
.map((word, index) => index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1))
|
|
63
|
+
.join("")
|
|
64
|
+
: this._;
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
value() { return this._; }
|
|
68
|
+
valueOf() { return this._; }
|
|
69
|
+
toString() { return String(this._); }
|
|
70
|
+
[Symbol.toPrimitive](hint) {
|
|
71
|
+
if (hint === "number")
|
|
72
|
+
return Number(this._);
|
|
73
|
+
if (hint === "boolean")
|
|
74
|
+
return Boolean(this._);
|
|
75
|
+
return String(this._);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const _ = (value) => new withGlobals(value);
|
|
79
|
+
exports.default = _;
|
package/package.json
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zuzjs/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"keywords": [
|
|
5
|
-
"
|
|
5
|
+
"core",
|
|
6
6
|
"zuz",
|
|
7
|
-
"zuz.js"
|
|
7
|
+
"zuz.js",
|
|
8
|
+
"zuz orm",
|
|
9
|
+
"zuzjs"
|
|
8
10
|
],
|
|
9
|
-
"description": "ZuzJS Core",
|
|
11
|
+
"description": "ZuzJS Core Library",
|
|
10
12
|
"author": "Zuz.js Team <support@zuz.com.pk>",
|
|
11
13
|
"license": "MIT",
|
|
12
|
-
"type": "
|
|
14
|
+
"type": "commonjs",
|
|
13
15
|
"main": "dist/index.js",
|
|
16
|
+
"bin": {
|
|
17
|
+
"zorm": "dist/bin.js"
|
|
18
|
+
},
|
|
14
19
|
"exports": {
|
|
15
20
|
".": "./dist/index.js"
|
|
16
21
|
},
|
|
@@ -18,17 +23,27 @@
|
|
|
18
23
|
"dist"
|
|
19
24
|
],
|
|
20
25
|
"scripts": {
|
|
21
|
-
"dev": "tsc -d -w -p tsconfig.json"
|
|
22
|
-
"build": "tsc -b tsconfig.json"
|
|
26
|
+
"dev": "tsc -d -w -p tsconfig.json"
|
|
23
27
|
},
|
|
24
28
|
"engines": {
|
|
25
29
|
"node": ">=18.17.0"
|
|
26
30
|
},
|
|
31
|
+
"sideEffects": [
|
|
32
|
+
"reflect-metadata"
|
|
33
|
+
],
|
|
27
34
|
"dependencies": {
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
35
|
+
"@types/hashids": "^2.0.1",
|
|
36
|
+
"@types/md5": "^2.3.5",
|
|
37
|
+
"axios": "^1.8.4",
|
|
38
|
+
"hashids": "^2.3.0",
|
|
39
|
+
"js-cookie": "^3.0.5",
|
|
40
|
+
"md5": "^2.3.0",
|
|
41
|
+
"moment": "^2.30.1",
|
|
42
|
+
"nanoid": "^5.1.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"husky": "^9.1.7",
|
|
46
|
+
"ts-node": "^10.9.2",
|
|
47
|
+
"typescript": "^5.7.3"
|
|
33
48
|
}
|
|
34
49
|
}
|