diginext-utils 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.
@@ -0,0 +1,30 @@
1
+ import _ from "lodash";
2
+
3
+ export default class FileUtils {
4
+ static getExtensionVideo(file) {
5
+ var extension = "";
6
+
7
+ if (file.type.indexOf("quicktime") >= 0) extension = ".mov";
8
+ else if (file.type.indexOf("avi") >= 0) extension = ".avi";
9
+ else if (file.type.indexOf("mpeg") >= 0) extension = ".mpg";
10
+ else if (file.type.indexOf("wmv") >= 0) extension = ".wmv";
11
+ else if (file.type.indexOf("ogg") >= 0) extension = ".ogg";
12
+ else if (file.type.indexOf("webm") >= 0) extension = ".webm";
13
+ else if (file.type.indexOf("mpeg-4") >= 0) extension = ".mp4";
14
+ else if (file.type.indexOf("mp4") >= 0) extension = ".mp4";
15
+
16
+ return extension;
17
+ }
18
+
19
+ static getFileExtension(file) {
20
+ const fn = file.name || file.originalFilename;
21
+ let _f = fn.includes(".") ? fn.split(".") : [""];
22
+ return _f[_f.length - 1];
23
+ }
24
+
25
+ static getFileNameByUrl(urlStr) {
26
+ let _u = urlStr.toString();
27
+ let _a = _u.includes("/") ? _u.split("/") : [];
28
+ return _.last(_a);
29
+ }
30
+ }
@@ -0,0 +1,172 @@
1
+ var DEG2RAD = Math.PI / 180;
2
+ var RAD2DEG = 180 / Math.PI;
3
+
4
+ export default {
5
+ isRotateLeft(a, b, c) {
6
+ return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x) > 0;
7
+ },
8
+
9
+ deg_between_points_360(cx, cy, ex, ey) {
10
+ var theta = this.angleLine(cx, cy, ex, ey); // range (-180, 180]
11
+ if (theta < 0) theta = 360 + theta; // range [0, 360)
12
+ return theta;
13
+ },
14
+
15
+ deg_between_points(cx, cy, ex, ey) {
16
+ var dy = ey - cy;
17
+ var dx = ex - cx;
18
+ var theta = Math.atan2(dy, dx); // range (-PI, PI]
19
+ theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
20
+ return theta;
21
+ },
22
+
23
+ angle_between_points(cx, cy, ex, ey) {
24
+ var dy = ey - cy;
25
+ var dx = ex - cx;
26
+ var theta = Math.atan2(dy, dx); // range (-PI, PI]
27
+ // theta *= 180 / Math.PI; // rads to degs, range (-180, 180]
28
+ return theta;
29
+ },
30
+
31
+ distance2Point(x1, y1, x2, y2) {
32
+ var dist = Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
33
+ return dist;
34
+ },
35
+
36
+ randRound: function (number) {
37
+ return Math.round(Math.random() * number);
38
+ },
39
+
40
+ rand(number) {
41
+ return (Math.random() - Math.random()) * number;
42
+ },
43
+
44
+ randHalt(number) {
45
+ var rand = Math.random() - Math.random();
46
+ var res;
47
+ if (rand > 0) {
48
+ res = rand * (number / 2) + number / 2;
49
+ } else {
50
+ res = rand * (number / 2) - number / 2;
51
+ }
52
+ return Math.abs(res);
53
+ },
54
+
55
+ clamp(value, min, max) {
56
+ return Math.max(min, Math.min(max, value));
57
+ },
58
+
59
+ // compute euclidian modulo of m % n
60
+ // https://en.wikipedia.org/wiki/Modulo_operation
61
+
62
+ euclideanModulo(n, m) {
63
+ return ((n % m) + m) % m;
64
+ },
65
+
66
+ // Linear mapping from range <a1, a2> to range <b1, b2>
67
+
68
+ mapLinear(x, a1, a2, b1, b2) {
69
+ return b1 + ((x - a1) * (b2 - b1)) / (a2 - a1);
70
+ },
71
+
72
+ // https://en.wikipedia.org/wiki/Linear_interpolation
73
+
74
+ lerp(x, y, t) {
75
+ return (1 - t) * x + t * y;
76
+ },
77
+
78
+ // http://en.wikipedia.org/wiki/Smoothstep
79
+
80
+ smoothstep(x, min, max) {
81
+ if (x <= min) return 0;
82
+ if (x >= max) return 1;
83
+
84
+ x = (x - min) / (max - min);
85
+
86
+ return x * x * (3 - 2 * x);
87
+ },
88
+
89
+ smootherstep(x, min, max) {
90
+ if (x <= min) return 0;
91
+ if (x >= max) return 1;
92
+
93
+ x = (x - min) / (max - min);
94
+
95
+ return x * x * x * (x * (x * 6 - 15) + 10);
96
+ },
97
+
98
+ // Random integer from <low, high> interval
99
+
100
+ randInt(low, high) {
101
+ return low + Math.floor(Math.random() * (high - low + 1));
102
+ },
103
+
104
+ // Random float from <low, high> interval
105
+
106
+ randFloat(low, high) {
107
+ return low + Math.random() * (high - low);
108
+ },
109
+
110
+ // Random float from <-range/2, range/2> interval
111
+
112
+ randFloatSpread(range) {
113
+ return range * (0.5 - Math.random());
114
+ },
115
+
116
+ rotationDegToRad(rotation) {
117
+ return {
118
+ x: this.degToRad(rotation.x),
119
+ y: this.degToRad(rotation.y),
120
+ z: this.degToRad(rotation.z),
121
+ };
122
+ },
123
+
124
+ degToRad(degrees) {
125
+ return degrees * DEG2RAD;
126
+ },
127
+
128
+ radToDeg(radians) {
129
+ return radians * RAD2DEG;
130
+ },
131
+
132
+ isPowerOfTwo(value) {
133
+ return (value & (value - 1)) === 0 && value !== 0;
134
+ },
135
+
136
+ nearestPowerOfTwo(value) {
137
+ return Math.pow(2, Math.round(Math.log(value) / Math.LN2));
138
+ },
139
+
140
+ nextPowerOfTwo(value) {
141
+ value--;
142
+ value |= value >> 1;
143
+ value |= value >> 2;
144
+ value |= value >> 4;
145
+ value |= value >> 8;
146
+ value |= value >> 16;
147
+ value++;
148
+
149
+ return value;
150
+ },
151
+
152
+ circleByPercentRadius(percent, radius) {
153
+ const theta = ((percent * 360 - 90) * Math.PI) / 180;
154
+
155
+ const x = radius * Math.cos(theta);
156
+ const y = -radius * Math.sin(theta);
157
+ return { x, y };
158
+ },
159
+
160
+ /**
161
+ *
162
+ * @param {Array} array
163
+ */
164
+ arrayToVector(array = [0, 0, 0]) {
165
+ const keys = ["x", "y", "z", "w"];
166
+ const result = {};
167
+ array.map((item, index) => {
168
+ result[keys[index]] = item;
169
+ });
170
+ return result;
171
+ },
172
+ };
package/src/OS.js ADDED
@@ -0,0 +1,203 @@
1
+ export default (options) => {
2
+ const isIos = () => {
3
+ if (typeof window.deviceInfo == "undefined") check();
4
+ var os = window.deviceInfo.os.toLowerCase();
5
+
6
+ return os.includes("ios");
7
+ };
8
+
9
+ const isAndroid = () => {
10
+ if (typeof window.deviceInfo == "undefined") check();
11
+ var os = window.deviceInfo.os.toLowerCase();
12
+
13
+ return os.includes("android");
14
+ };
15
+
16
+ const check = () => {
17
+ option = option != undefined ? option : {};
18
+
19
+ var callback = option.hasOwnProperty("callback") ? option.callback : null;
20
+
21
+ if (typeof window.deviceInfo == "undefined") {
22
+ var unknown = "-";
23
+
24
+ // screen
25
+ var screenSize = "";
26
+ if (screen.width) {
27
+ var width = screen.width ? screen.width : "";
28
+ var height = screen.height ? screen.height : "";
29
+ screenSize += "" + width + " x " + height;
30
+ }
31
+
32
+ // browser
33
+ var nVer = navigator.appVersion;
34
+ var nAgt = navigator.userAgent;
35
+ var browser = navigator.appName;
36
+ var version = "" + parseFloat(navigator.appVersion);
37
+ var majorVersion = parseInt(navigator.appVersion, 10);
38
+ var nameOffset, verOffset, ix;
39
+
40
+ // Opera
41
+ if ((verOffset = nAgt.indexOf("Opera")) != -1) {
42
+ browser = "Opera";
43
+ version = nAgt.substring(verOffset + 6);
44
+ if ((verOffset = nAgt.indexOf("Version")) != -1) {
45
+ version = nAgt.substring(verOffset + 8);
46
+ }
47
+ }
48
+ // Opera Next
49
+ if ((verOffset = nAgt.indexOf("OPR")) != -1) {
50
+ browser = "Opera";
51
+ version = nAgt.substring(verOffset + 4);
52
+ }
53
+ // Edge
54
+ else if ((verOffset = nAgt.indexOf("Edge")) != -1) {
55
+ browser = "Microsoft Edge";
56
+ version = nAgt.substring(verOffset + 5);
57
+ }
58
+ // MSIE
59
+ else if ((verOffset = nAgt.indexOf("MSIE")) != -1) {
60
+ browser = "Microsoft Internet Explorer";
61
+ version = nAgt.substring(verOffset + 5);
62
+ }
63
+ // Chrome
64
+ else if ((verOffset = nAgt.indexOf("Chrome")) != -1) {
65
+ browser = "Chrome";
66
+ version = nAgt.substring(verOffset + 7);
67
+ }
68
+ // Safari
69
+ else if ((verOffset = nAgt.indexOf("Safari")) != -1) {
70
+ browser = "Safari";
71
+ version = nAgt.substring(verOffset + 7);
72
+ if ((verOffset = nAgt.indexOf("Version")) != -1) {
73
+ version = nAgt.substring(verOffset + 8);
74
+ }
75
+ }
76
+ // Firefox
77
+ else if ((verOffset = nAgt.indexOf("Firefox")) != -1) {
78
+ browser = "Firefox";
79
+ version = nAgt.substring(verOffset + 8);
80
+ }
81
+ // MSIE 11+
82
+ else if (nAgt.indexOf("Trident/") != -1) {
83
+ browser = "Microsoft Internet Explorer";
84
+ version = nAgt.substring(nAgt.indexOf("rv:") + 3);
85
+ }
86
+ // Other browsers
87
+ else if ((nameOffset = nAgt.lastIndexOf(" ") + 1) < (verOffset = nAgt.lastIndexOf("/"))) {
88
+ browser = nAgt.substring(nameOffset, verOffset);
89
+ version = nAgt.substring(verOffset + 1);
90
+ if (browser.toLowerCase() == browser.toUpperCase()) {
91
+ browser = navigator.appName;
92
+ }
93
+ }
94
+ // trim the version string
95
+ if ((ix = version.indexOf(";")) != -1) version = version.substring(0, ix);
96
+ if ((ix = version.indexOf(" ")) != -1) version = version.substring(0, ix);
97
+ if ((ix = version.indexOf(")")) != -1) version = version.substring(0, ix);
98
+
99
+ majorVersion = parseInt("" + version, 10);
100
+ if (isNaN(majorVersion)) {
101
+ version = "" + parseFloat(navigator.appVersion);
102
+ majorVersion = parseInt(navigator.appVersion, 10);
103
+ }
104
+
105
+ // mobile version
106
+ var mobile = /Mobile|mini|Fennec|Android|iP(ad|od|hone)/.test(nVer);
107
+
108
+ // cookie
109
+ var cookieEnabled = navigator.cookieEnabled ? true : false;
110
+
111
+ if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled) {
112
+ document.cookie = "testcookie";
113
+ cookieEnabled = document.cookie.indexOf("testcookie") != -1 ? true : false;
114
+ }
115
+
116
+ // system
117
+ var os = unknown;
118
+ var clientStrings = [
119
+ { s: "Windows 10", r: /(Windows 10.0|Windows NT 10.0)/ },
120
+ { s: "Windows 8.1", r: /(Windows 8.1|Windows NT 6.3)/ },
121
+ { s: "Windows 8", r: /(Windows 8|Windows NT 6.2)/ },
122
+ { s: "Windows 7", r: /(Windows 7|Windows NT 6.1)/ },
123
+ { s: "Windows Vista", r: /Windows NT 6.0/ },
124
+ { s: "Windows Server 2003", r: /Windows NT 5.2/ },
125
+ { s: "Windows XP", r: /(Windows NT 5.1|Windows XP)/ },
126
+ { s: "Windows 2000", r: /(Windows NT 5.0|Windows 2000)/ },
127
+ { s: "Windows ME", r: /(Win 9x 4.90|Windows ME)/ },
128
+ { s: "Windows 98", r: /(Windows 98|Win98)/ },
129
+ { s: "Windows 95", r: /(Windows 95|Win95|Windows_95)/ },
130
+ { s: "Windows NT 4.0", r: /(Windows NT 4.0|WinNT4.0|WinNT|Windows NT)/ },
131
+ { s: "Windows CE", r: /Windows CE/ },
132
+ { s: "Windows 3.11", r: /Win16/ },
133
+ { s: "Android", r: /Android/ },
134
+ { s: "Open BSD", r: /OpenBSD/ },
135
+ { s: "Sun OS", r: /SunOS/ },
136
+ { s: "Linux", r: /(Linux|X11)/ },
137
+ { s: "iOS", r: /(iPhone|iPad|iPod)/ },
138
+ { s: "Mac OS X", r: /Mac OS X/ },
139
+ { s: "Mac OS", r: /(MacPPC|MacIntel|Mac_PowerPC|Macintosh)/ },
140
+ { s: "QNX", r: /QNX/ },
141
+ { s: "UNIX", r: /UNIX/ },
142
+ { s: "BeOS", r: /BeOS/ },
143
+ { s: "OS/2", r: /OS\/2/ },
144
+ { s: "Search Bot", r: /(nuhk|Googlebot|Yammybot|Openbot|Slurp|MSNBot|Ask Jeeves\/Teoma|ia_archiver)/ },
145
+ ];
146
+ for (var id in clientStrings) {
147
+ var cs = clientStrings[id];
148
+ if (cs.r.test(nAgt)) {
149
+ os = cs.s;
150
+ break;
151
+ }
152
+ }
153
+
154
+ var osVersion = unknown;
155
+
156
+ if (/Windows/.test(os)) {
157
+ osVersion = /Windows (.*)/.exec(os)[1];
158
+ os = "Windows";
159
+ }
160
+
161
+ switch (os) {
162
+ case "Mac OS X":
163
+ osVersion = /Mac OS X (10[\.\_\d]+)/.exec(nAgt)[1];
164
+ break;
165
+
166
+ case "Android":
167
+ osVersion = /Android ([\.\_\d]+)/.exec(nAgt)[1];
168
+ break;
169
+
170
+ case "iOS":
171
+ osVersion = /OS (\d+)_(\d+)_?(\d+)?/.exec(nVer);
172
+ osVersion = osVersion[1] + "." + osVersion[2] + "." + (osVersion[3] | 0);
173
+ break;
174
+ }
175
+
176
+ // flash (you'll need to include swfobject)
177
+ /* script src="//ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" */
178
+ var flashVersion = "no check";
179
+ if (typeof swfobject != "undefined") {
180
+ var fv = swfobject.getFlashPlayerVersion();
181
+ if (fv.major > 0) {
182
+ flashVersion = fv.major + "." + fv.minor + " r" + fv.release;
183
+ } else {
184
+ flashVersion = unknown;
185
+ }
186
+ }
187
+
188
+ window.deviceInfo = {
189
+ screen: screenSize,
190
+ browser: browser,
191
+ browserVersion: version,
192
+ browserMajorVersion: majorVersion,
193
+ mobile: mobile,
194
+ os: os,
195
+ osVersion: osVersion,
196
+ cookies: cookieEnabled,
197
+ flashVersion: flashVersion,
198
+ };
199
+ }
200
+
201
+ if (callback != null) callback(window.deviceInfo);
202
+ };
203
+ };
@@ -0,0 +1,109 @@
1
+ import ArrayExtra from "./ArrayExtra";
2
+
3
+ export default class ObjectExtra {
4
+ static getKeyByValue(object, value) {
5
+ return Object.keys(object).find((key) => object[key] === value);
6
+ }
7
+
8
+ static randomValue(obj) {
9
+ return obj[this.randomKey(obj)];
10
+ }
11
+
12
+ static randomKey(obj) {
13
+ var keys = Object.keys(obj);
14
+ return keys[(keys.length * Math.random()) << 0];
15
+ }
16
+
17
+ static randomElementIndex(obj) {
18
+ var keys = Object.keys(obj);
19
+ return (keys.length * Math.random()) << 0;
20
+ }
21
+
22
+ static getObjectLength(obj) {
23
+ var keys = Object.keys(obj);
24
+ return keys.length;
25
+ }
26
+
27
+ /**
28
+ *
29
+ * @param {Object} obj
30
+ * @param {Function} cb
31
+ */
32
+ static forEach(obj, cb) {
33
+ const keys = Object.keys(obj);
34
+
35
+ keys.map((key, index) => {
36
+ if (cb) cb(key, obj[key], index);
37
+ });
38
+ }
39
+
40
+ /**
41
+ *
42
+ * @param {Object} object
43
+ * @returns {Array} list keys
44
+ */
45
+ static toArray(obj) {
46
+ let array = [];
47
+ for (const key in obj) {
48
+ array.push(obj[key]);
49
+ }
50
+ return array;
51
+ }
52
+
53
+ /**
54
+ *
55
+ * @param {Object} object
56
+ * @returns {Object}
57
+ */
58
+ static sortObjectByKey(object) {
59
+ let array = [];
60
+
61
+ for (const key in object) {
62
+ array.push({
63
+ key: key,
64
+ value: object[key],
65
+ });
66
+ }
67
+
68
+ array = ArrayExtra.sortElementByString(array, "key");
69
+
70
+ let result = {};
71
+
72
+ array.forEach((item) => {
73
+ result[item.key] = item.value;
74
+ });
75
+
76
+ return result;
77
+ }
78
+
79
+ //
80
+
81
+ static isNull(object) {
82
+ if (typeof object == "undefined") {
83
+ return true;
84
+ }
85
+ if (Array.isArray(object)) return object.length == 0;
86
+ return object == null;
87
+ }
88
+
89
+ static toBool(object) {
90
+ if (this.isNull(object)) return false;
91
+ object = object.toString();
92
+
93
+ return object === "true" || object == "1";
94
+ }
95
+
96
+ static toInt(object) {
97
+ if (this.isNull(object)) return 0;
98
+ object = object.toString();
99
+
100
+ return parseInt(object, 10);
101
+ }
102
+
103
+ static toFloat(object) {
104
+ if (this.isNull(object)) return 0;
105
+ object = object.toString();
106
+
107
+ return parseFloat(object);
108
+ }
109
+ }
@@ -0,0 +1,25 @@
1
+ import React from "react";
2
+
3
+ export const isClassComponent = (component) => {
4
+ return typeof component === "function" && !!component.prototype.isReactComponent;
5
+ };
6
+
7
+ export const isFunctionComponent = (component) => {
8
+ return typeof component === "function" && String(component).includes("return React.createElement");
9
+ };
10
+
11
+ export const isReactComponent = (component) => {
12
+ return isClassComponent(component) || isFunctionComponent(component);
13
+ };
14
+
15
+ export const isElement = (element) => {
16
+ return React.isValidElement(element);
17
+ };
18
+
19
+ export const isDOMTypeElement = (element) => {
20
+ return isElement(element) && typeof element.type === "string";
21
+ };
22
+
23
+ export const isCompositeTypeElement = (element) => {
24
+ return isElement(element) && typeof element.type === "function";
25
+ };
@@ -0,0 +1,31 @@
1
+ import React, { useEffect } from "react";
2
+
3
+ /**
4
+ *
5
+ * @param {Element} target DOM element or window | document , default is window
6
+ * @param {Function} callback handle function
7
+ * @param {String} eventName Event name
8
+ */
9
+ const useEventListener = (eventName = "load", callback, options) => {
10
+ useEffect(() => {
11
+ options = options || {};
12
+ let target = options["target"] ? options.target : window || document;
13
+
14
+ if (typeof target === "string" && typeof document !== "undefined") {
15
+ target = document.querySelector(target);
16
+ }
17
+
18
+ if (!target) {
19
+ // console.log("[useEventListener] target is not defined!");
20
+ return;
21
+ }
22
+ // console.log("[useEventListener]", target);
23
+ target.addEventListener(eventName, callback);
24
+
25
+ return () => {
26
+ target.removeEventListener(eventName, callback);
27
+ };
28
+ }, [eventName, callback]);
29
+ };
30
+
31
+ export default useEventListener;