diginext-utils 2.1.7 → 2.1.9

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.
Files changed (46) hide show
  1. package/README.md +162 -162
  2. package/package.json +1 -1
  3. package/src/Checker.ts +29 -29
  4. package/src/EventDispatcher.ts +62 -62
  5. package/src/FileUpload.ts +66 -66
  6. package/src/Slug.ts +365 -365
  7. package/src/Timer.ts +7 -7
  8. package/src/Validation.ts +36 -36
  9. package/src/array.ts +285 -285
  10. package/src/color.ts +87 -87
  11. package/src/console/enableConsole.ts +8 -8
  12. package/src/console/index.ts +17 -17
  13. package/src/console/log.ts +39 -39
  14. package/src/device/browser.ts +29 -29
  15. package/src/device/camera.ts +207 -207
  16. package/src/device/index.ts +230 -230
  17. package/src/device/os.ts +29 -29
  18. package/src/file/createDir.ts +14 -14
  19. package/src/file/fileMove.ts +32 -32
  20. package/src/file/findFilesByExt.ts +46 -46
  21. package/src/file/index.ts +7 -7
  22. package/src/images/index.ts +3 -3
  23. package/src/images/loadImage.ts +16 -16
  24. package/src/images/resize.ts +35 -35
  25. package/src/images/upload.ts +24 -24
  26. package/src/index.ts +39 -39
  27. package/src/json.ts +29 -29
  28. package/src/math/diffDate.ts +10 -10
  29. package/src/math/index.ts +90 -90
  30. package/src/math/positiveNumber.ts +12 -12
  31. package/src/name/en.ts +27 -27
  32. package/src/name/index.ts +8 -8
  33. package/src/name/vi.ts +23 -23
  34. package/src/object.ts +72 -72
  35. package/src/permission/index.ts +6 -6
  36. package/src/permission/requestCamera.ts +43 -43
  37. package/src/permission/requestDeviceOrientationControl.ts +33 -33
  38. package/src/response/index.ts +45 -45
  39. package/src/string/formatNumber.ts +12 -12
  40. package/src/string/generatePassword.ts +18 -18
  41. package/src/string/generateUUID.ts +37 -37
  42. package/src/string/getTextBetweenCharByIndex.ts +20 -20
  43. package/src/string/indexesOf.ts +15 -15
  44. package/src/string/makeDaySlug.ts +40 -40
  45. package/src/string/random.ts +33 -33
  46. package/src/string/trimNull.ts +16 -16
@@ -1,46 +1,46 @@
1
- var path = require("path");
2
- var fs = require("fs");
3
-
4
- /**
5
- * This is an alias of "findFileByExt()"
6
- */
7
- export const forEachFileByExt = (base: string, ext: string, cb: (path: string) => {}) => {
8
- var walk = function (directoryName: string) {
9
- try {
10
- fs.readdir(directoryName, function (e: any, files: string[]) {
11
- if (e) {
12
- console.log("Error: ", e);
13
- return;
14
- }
15
- files.forEach(function (file: string) {
16
- var fullPath = path.join(directoryName, file);
17
- fs.stat(fullPath, function (e: any, f: any) {
18
- if (e) {
19
- console.log("Error: ", e);
20
- return;
21
- }
22
- if (f.isDirectory()) {
23
- walk(fullPath);
24
- } else {
25
- if (fullPath.toLowerCase().indexOf(ext.toLowerCase()) > -1) {
26
- if (cb) cb(fullPath);
27
- }
28
- }
29
- });
30
- });
31
- });
32
- } catch (error) {
33
- console.log("error", error);
34
- }
35
- };
36
- walk(base);
37
- };
38
-
39
- /**
40
- * This is an alias of "forEachFileByExt()"
41
- */
42
- export const findFileByExt = forEachFileByExt;
43
-
44
- const fileExt = { findFileByExt, forEachFileByExt };
45
-
46
- export default fileExt;
1
+ var path = require("path");
2
+ var fs = require("fs");
3
+
4
+ /**
5
+ * This is an alias of "findFileByExt()"
6
+ */
7
+ export const forEachFileByExt = (base: string, ext: string, cb: (path: string) => {}) => {
8
+ var walk = function (directoryName: string) {
9
+ try {
10
+ fs.readdir(directoryName, function (e: any, files: string[]) {
11
+ if (e) {
12
+ console.log("Error: ", e);
13
+ return;
14
+ }
15
+ files.forEach(function (file: string) {
16
+ var fullPath = path.join(directoryName, file);
17
+ fs.stat(fullPath, function (e: any, f: any) {
18
+ if (e) {
19
+ console.log("Error: ", e);
20
+ return;
21
+ }
22
+ if (f.isDirectory()) {
23
+ walk(fullPath);
24
+ } else {
25
+ if (fullPath.toLowerCase().indexOf(ext.toLowerCase()) > -1) {
26
+ if (cb) cb(fullPath);
27
+ }
28
+ }
29
+ });
30
+ });
31
+ });
32
+ } catch (error) {
33
+ console.log("error", error);
34
+ }
35
+ };
36
+ walk(base);
37
+ };
38
+
39
+ /**
40
+ * This is an alias of "forEachFileByExt()"
41
+ */
42
+ export const findFileByExt = forEachFileByExt;
43
+
44
+ const fileExt = { findFileByExt, forEachFileByExt };
45
+
46
+ export default fileExt;
package/src/file/index.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { createDir } from "./createDir";
2
- import { fileMove } from "./fileMove";
3
- import fileExt from "./findFilesByExt";
4
-
5
- const xfile = { createDir, fileMove, ...fileExt };
6
-
7
- export default xfile;
1
+ import { createDir } from "./createDir";
2
+ import { fileMove } from "./fileMove";
3
+ import fileExt from "./findFilesByExt";
4
+
5
+ const xfile = { createDir, fileMove, ...fileExt };
6
+
7
+ export default xfile;
@@ -1,3 +1,3 @@
1
- // export { default as AdminIcon } from "diginext-dashkit/dist/Icon";
2
- export { upload } from "./upload";
3
- export { resize } from "./resize";
1
+ // export { default as AdminIcon } from "diginext-dashkit/dist/Icon";
2
+ export { upload } from "./upload";
3
+ export { resize } from "./resize";
@@ -1,16 +1,16 @@
1
- export default async function loadImage(list: Array<string>) {
2
- //
3
- if (!list?.length) return;
4
-
5
- return await Promise.all(
6
- list.map((url) => {
7
- return new Promise((resolve, reject) => {
8
- const img = new Image();
9
- img.onload = resolve;
10
- img.onerror = resolve;
11
- img.src = url;
12
- return;
13
- });
14
- })
15
- );
16
- }
1
+ export default async function loadImage(list: Array<string>) {
2
+ //
3
+ if (!list?.length) return;
4
+
5
+ return await Promise.all(
6
+ list.map((url) => {
7
+ return new Promise((resolve, reject) => {
8
+ const img = new Image();
9
+ img.onload = resolve;
10
+ img.onerror = resolve;
11
+ img.src = url;
12
+ return;
13
+ });
14
+ })
15
+ );
16
+ }
@@ -1,35 +1,35 @@
1
- import loadImage from "blueimp-load-image";
2
-
3
- export const resize = async (file: File) => {
4
- if (!file) {
5
- console.error("NO FILE AVAIABLE!");
6
- return;
7
- }
8
-
9
- const { type } = file;
10
-
11
- const data = await loadImage(file, {
12
- //
13
- meta: false,
14
- canvas: true,
15
- maxWidth: 2048,
16
- maxHeight: 2048,
17
- });
18
-
19
- const blob = await new Promise(function (resolve) {
20
- try {
21
- const canvas = data.image as any;
22
- canvas.toBlob(function (blob: Blob) {
23
- resolve(blob);
24
- }, type);
25
- } catch (error) {
26
- console.error(`diginext-utils/src/images/resize.ts canvas.toBlob error`, error);
27
- }
28
- });
29
-
30
- if (blob) return blob;
31
-
32
- return data;
33
- };
34
-
35
- export default resize;
1
+ import loadImage from "blueimp-load-image";
2
+
3
+ export const resize = async (file: File) => {
4
+ if (!file) {
5
+ console.error("NO FILE AVAIABLE!");
6
+ return;
7
+ }
8
+
9
+ const { type } = file;
10
+
11
+ const data = await loadImage(file, {
12
+ //
13
+ meta: false,
14
+ canvas: true,
15
+ maxWidth: 2048,
16
+ maxHeight: 2048,
17
+ });
18
+
19
+ const blob = await new Promise(function (resolve) {
20
+ try {
21
+ const canvas = data.image as any;
22
+ canvas.toBlob(function (blob: Blob) {
23
+ resolve(blob);
24
+ }, type);
25
+ } catch (error) {
26
+ console.error(`diginext-utils/src/images/resize.ts canvas.toBlob error`, error);
27
+ }
28
+ });
29
+
30
+ if (blob) return blob;
31
+
32
+ return data;
33
+ };
34
+
35
+ export default resize;
@@ -1,24 +1,24 @@
1
- import { getFailedResponse } from "./../response/index";
2
- import resize from "./resize";
3
- import { isImage } from "./../string/url";
4
-
5
- export const upload = async (file: File) => {
6
- const { name } = file;
7
-
8
- if (!isImage(name)) {
9
- const err = "Please Choose Image!";
10
- console.error(err);
11
- return getFailedResponse(err);
12
- }
13
-
14
- const blob = (await resize(file)) as any;
15
- const url = URL.createObjectURL(blob as Blob);
16
- blob.name = name;
17
-
18
- return {
19
- blob,
20
- url,
21
- };
22
- };
23
-
24
- export default upload;
1
+ import { getFailedResponse } from "./../response/index";
2
+ import resize from "./resize";
3
+ import { isImage } from "./../string/url";
4
+
5
+ export const upload = async (file: File) => {
6
+ const { name } = file;
7
+
8
+ if (!isImage(name)) {
9
+ const err = "Please Choose Image!";
10
+ console.error(err);
11
+ return getFailedResponse(err);
12
+ }
13
+
14
+ const blob = (await resize(file)) as any;
15
+ const url = URL.createObjectURL(blob as Blob);
16
+ blob.name = name;
17
+
18
+ return {
19
+ blob,
20
+ url,
21
+ };
22
+ };
23
+
24
+ export default upload;
package/src/index.ts CHANGED
@@ -1,39 +1,39 @@
1
- import * as array from "./array";
2
- import * as device from "./device";
3
- import * as console from "./console";
4
- import * as browser from "./device/browser";
5
- import * as camera from "./device/camera";
6
- import * as math from "./math";
7
- import * as xname from "./name";
8
- import * as object from "./object";
9
- import * as string from "./string";
10
- import * as url from "./string/url";
11
- import * as createDir from "./file/createDir";
12
- import * as fileMove from "./file/fileMove";
13
- import * as findFilesByExt from "./file/findFilesByExt";
14
- import * as Timer from "./Timer";
15
- import * as requestCamera from "./permission/requestCamera";
16
- import * as requestDeviceOrientationControl from "./permission/requestDeviceOrientationControl";
17
- import * as enableConsole from "./console/enableConsole";
18
-
19
- const utils = {
20
- xname,
21
- array,
22
- device,
23
- console,
24
- browser,
25
- camera,
26
- math,
27
- object,
28
- string,
29
- url,
30
- createDir,
31
- fileMove,
32
- findFilesByExt,
33
- Timer,
34
- requestCamera,
35
- requestDeviceOrientationControl,
36
- enableConsole,
37
- };
38
-
39
- export default utils;
1
+ import * as array from "./array";
2
+ import * as device from "./device";
3
+ import * as console from "./console";
4
+ import * as browser from "./device/browser";
5
+ import * as camera from "./device/camera";
6
+ import * as math from "./math";
7
+ import * as xname from "./name";
8
+ import * as object from "./object";
9
+ import * as string from "./string";
10
+ import * as url from "./string/url";
11
+ import * as createDir from "./file/createDir";
12
+ import * as fileMove from "./file/fileMove";
13
+ import * as findFilesByExt from "./file/findFilesByExt";
14
+ import * as Timer from "./Timer";
15
+ import * as requestCamera from "./permission/requestCamera";
16
+ import * as requestDeviceOrientationControl from "./permission/requestDeviceOrientationControl";
17
+ import * as enableConsole from "./console/enableConsole";
18
+
19
+ const utils = {
20
+ xname,
21
+ array,
22
+ device,
23
+ console,
24
+ browser,
25
+ camera,
26
+ math,
27
+ object,
28
+ string,
29
+ url,
30
+ createDir,
31
+ fileMove,
32
+ findFilesByExt,
33
+ Timer,
34
+ requestCamera,
35
+ requestDeviceOrientationControl,
36
+ enableConsole,
37
+ };
38
+
39
+ export default utils;
package/src/json.ts CHANGED
@@ -1,29 +1,29 @@
1
- /**
2
- * Check if the object or string is in JSON format
3
- */
4
- export const isValid = (content: object | string) => {
5
- if (typeof content == "object") {
6
- try {
7
- content = JSON.stringify(content);
8
- } catch (err) {
9
- return false;
10
- }
11
- }
12
-
13
- if (typeof content == "string") {
14
- try {
15
- content = JSON.parse(content);
16
- } catch (err) {
17
- return false;
18
- }
19
- }
20
-
21
- if (typeof content != "object") {
22
- return false;
23
- }
24
- return true;
25
- };
26
-
27
- const xjson = { isValid };
28
-
29
- export default xjson;
1
+ /**
2
+ * Check if the object or string is in JSON format
3
+ */
4
+ export const isValid = (content: object | string) => {
5
+ if (typeof content == "object") {
6
+ try {
7
+ content = JSON.stringify(content);
8
+ } catch (err) {
9
+ return false;
10
+ }
11
+ }
12
+
13
+ if (typeof content == "string") {
14
+ try {
15
+ content = JSON.parse(content);
16
+ } catch (err) {
17
+ return false;
18
+ }
19
+ }
20
+
21
+ if (typeof content != "object") {
22
+ return false;
23
+ }
24
+ return true;
25
+ };
26
+
27
+ const xjson = { isValid };
28
+
29
+ export default xjson;
@@ -1,10 +1,10 @@
1
- /**
2
- * Returns amount of different days between 2 dates
3
- */
4
- export function diffDate(date1: string, date2: string) {
5
- date1 = date1 || new Date().toString();
6
- date2 = date2 || new Date().toString();
7
- return (new Date(date2).getTime() - new Date(date1).getTime()) / (24 * 60 * 60 * 1000);
8
- }
9
-
10
- export default diffDate;
1
+ /**
2
+ * Returns amount of different days between 2 dates
3
+ */
4
+ export function diffDate(date1: string, date2: string) {
5
+ date1 = date1 || new Date().toString();
6
+ date2 = date2 || new Date().toString();
7
+ return (new Date(date2).getTime() - new Date(date1).getTime()) / (24 * 60 * 60 * 1000);
8
+ }
9
+
10
+ export default diffDate;
package/src/math/index.ts CHANGED
@@ -1,90 +1,90 @@
1
- const DEG2RAD = Math.PI / 180;
2
- const RAD2DEG = 180 / Math.PI;
3
-
4
- export const randRound = (number: number) => {
5
- return Math.round(Math.random() * number);
6
- };
7
-
8
- export const rand = (number: number) => {
9
- return (Math.random() - Math.random()) * number;
10
- };
11
-
12
- export const randHalt = (number: number) => {
13
- var rand = Math.random() - Math.random();
14
- var res;
15
- if (rand > 0) {
16
- res = rand * (number / 2) + number / 2;
17
- } else {
18
- res = rand * (number / 2) - number / 2;
19
- }
20
- return Math.abs(res);
21
- };
22
-
23
- export const randInt = (low: number, high: number) => {
24
- return low + Math.floor(Math.random() * (high - low + 1));
25
- };
26
-
27
- export const randFloat = (low: number, high: number) => {
28
- return low + Math.random() * (high - low);
29
- };
30
-
31
- export const degToRad = (degrees: number) => {
32
- return degrees * DEG2RAD;
33
- };
34
-
35
- export const radToDeg = (radians: number) => {
36
- return radians * RAD2DEG;
37
- };
38
-
39
- export const clamp = (value: number, min: number, max: number) => {
40
- const result = Math.max(min, Math.min(max, value));
41
- return Number.isFinite(result) ? result : 0;
42
- };
43
-
44
- export const degBetweenPoints360 = (cx: number, cy: number, ex: number, ey: number) => {
45
- let theta = degBetweenPoints(cx, cy, ex, ey); // range (-180, 180]
46
- if (theta < 0) theta = 360 + theta; // range [0, 360)
47
- return Number.isFinite(theta) ? theta : 0;
48
- };
49
-
50
- export const degBetweenPoints = (cx: number, cy: number, ex: number, ey: number) => {
51
- const dy = ey - cy;
52
- const dx = ex - cx;
53
- let theta = Math.atan2(dy, dx); // range (-PI, PI]
54
- theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
55
- return Number.isFinite(theta) ? theta : 0;
56
- };
57
-
58
- export const angleBetweenPoints = (cx: number, cy: number, ex: number, ey: number) => {
59
- const dy = ey - cy;
60
- const dx = ex - cx;
61
- let theta = Math.atan2(dy, dx); // range (-PI, PI]
62
- theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
63
- return Number.isFinite(theta) ? theta : 0;
64
- };
65
-
66
- export const distance2Point = (x1: number, y1: number, x2: number, y2: number) => {
67
- const dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
68
- return Number.isFinite(dist) ? dist : 0;
69
- };
70
-
71
- import { diffDate } from "./diffDate";
72
- import { positiveNumber } from "./positiveNumber";
73
-
74
- const xmath = {
75
- rand,
76
- randRound,
77
- randHalt,
78
- randInt,
79
- randFloat,
80
- degToRad,
81
- radToDeg,
82
- degBetweenPoints360,
83
- degBetweenPoints,
84
- angleBetweenPoints,
85
- distance2Point,
86
- diffDate,
87
- positiveNumber,
88
- };
89
-
90
- export default xmath;
1
+ const DEG2RAD = Math.PI / 180;
2
+ const RAD2DEG = 180 / Math.PI;
3
+
4
+ export const randRound = (number: number) => {
5
+ return Math.round(Math.random() * number);
6
+ };
7
+
8
+ export const rand = (number: number) => {
9
+ return (Math.random() - Math.random()) * number;
10
+ };
11
+
12
+ export const randHalt = (number: number) => {
13
+ var rand = Math.random() - Math.random();
14
+ var res;
15
+ if (rand > 0) {
16
+ res = rand * (number / 2) + number / 2;
17
+ } else {
18
+ res = rand * (number / 2) - number / 2;
19
+ }
20
+ return Math.abs(res);
21
+ };
22
+
23
+ export const randInt = (low: number, high: number) => {
24
+ return low + Math.floor(Math.random() * (high - low + 1));
25
+ };
26
+
27
+ export const randFloat = (low: number, high: number) => {
28
+ return low + Math.random() * (high - low);
29
+ };
30
+
31
+ export const degToRad = (degrees: number) => {
32
+ return degrees * DEG2RAD;
33
+ };
34
+
35
+ export const radToDeg = (radians: number) => {
36
+ return radians * RAD2DEG;
37
+ };
38
+
39
+ export const clamp = (value: number, min: number, max: number) => {
40
+ const result = Math.max(min, Math.min(max, value));
41
+ return Number.isFinite(result) ? result : 0;
42
+ };
43
+
44
+ export const degBetweenPoints360 = (cx: number, cy: number, ex: number, ey: number) => {
45
+ let theta = degBetweenPoints(cx, cy, ex, ey); // range (-180, 180]
46
+ if (theta < 0) theta = 360 + theta; // range [0, 360)
47
+ return Number.isFinite(theta) ? theta : 0;
48
+ };
49
+
50
+ export const degBetweenPoints = (cx: number, cy: number, ex: number, ey: number) => {
51
+ const dy = ey - cy;
52
+ const dx = ex - cx;
53
+ let theta = Math.atan2(dy, dx); // range (-PI, PI]
54
+ theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
55
+ return Number.isFinite(theta) ? theta : 0;
56
+ };
57
+
58
+ export const angleBetweenPoints = (cx: number, cy: number, ex: number, ey: number) => {
59
+ const dy = ey - cy;
60
+ const dx = ex - cx;
61
+ let theta = Math.atan2(dy, dx); // range (-PI, PI]
62
+ theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
63
+ return Number.isFinite(theta) ? theta : 0;
64
+ };
65
+
66
+ export const distance2Point = (x1: number, y1: number, x2: number, y2: number) => {
67
+ const dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
68
+ return Number.isFinite(dist) ? dist : 0;
69
+ };
70
+
71
+ import { diffDate } from "./diffDate";
72
+ import { positiveNumber } from "./positiveNumber";
73
+
74
+ const xmath = {
75
+ rand,
76
+ randRound,
77
+ randHalt,
78
+ randInt,
79
+ randFloat,
80
+ degToRad,
81
+ radToDeg,
82
+ degBetweenPoints360,
83
+ degBetweenPoints,
84
+ angleBetweenPoints,
85
+ distance2Point,
86
+ diffDate,
87
+ positiveNumber,
88
+ };
89
+
90
+ export default xmath;
@@ -1,12 +1,12 @@
1
- import { isNull } from "../object";
2
-
3
- export function positiveNumber(number: number) {
4
- if (isNull(number)) return 0;
5
- if (!Number.isFinite(Number(number))) return 0;
6
- number = Number(number);
7
- if (number < 0) return 0;
8
-
9
- return number;
10
- }
11
-
12
- export default positiveNumber;
1
+ import { isNull } from "../object";
2
+
3
+ export function positiveNumber(number: number) {
4
+ if (isNull(number)) return 0;
5
+ if (!Number.isFinite(Number(number))) return 0;
6
+ number = Number(number);
7
+ if (number < 0) return 0;
8
+
9
+ return number;
10
+ }
11
+
12
+ export default positiveNumber;