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.
package/src/Uuid.js ADDED
@@ -0,0 +1,66 @@
1
+ export default function generateUUID() {
2
+ // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
3
+ var lut = [];
4
+ for (var i = 0; i < 256; i++) {
5
+ lut[i] = (i < 16 ? "0" : "") + i.toString(16);
6
+ }
7
+
8
+ function _generateUUID() {
9
+ var d0 = (Math.random() * 0xffffffff) | 0;
10
+ var d1 = (Math.random() * 0xffffffff) | 0;
11
+ var d2 = (Math.random() * 0xffffffff) | 0;
12
+ var d3 = (Math.random() * 0xffffffff) | 0;
13
+ var uuid =
14
+ lut[d0 & 0xff] +
15
+ lut[(d0 >> 8) & 0xff] +
16
+ lut[(d0 >> 16) & 0xff] +
17
+ lut[(d0 >> 24) & 0xff] +
18
+ "-" +
19
+ lut[d1 & 0xff] +
20
+ lut[(d1 >> 8) & 0xff] +
21
+ "-" +
22
+ lut[((d1 >> 16) & 0x0f) | 0x40] +
23
+ lut[(d1 >> 24) & 0xff] +
24
+ "-" +
25
+ lut[(d2 & 0x3f) | 0x80] +
26
+ lut[(d2 >> 8) & 0xff] +
27
+ "-" +
28
+ lut[(d2 >> 16) & 0xff] +
29
+ lut[(d2 >> 24) & 0xff] +
30
+ lut[d3 & 0xff] +
31
+ lut[(d3 >> 8) & 0xff] +
32
+ lut[(d3 >> 16) & 0xff] +
33
+ lut[(d3 >> 24) & 0xff];
34
+
35
+ // .toUpperCase() here flattens concatenated strings to save heap memory space.
36
+ return uuid.toUpperCase();
37
+ }
38
+
39
+ return _generateUUID();
40
+ }
41
+
42
+ // export default {
43
+ // get generateUUID() {
44
+ // // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
45
+ // var lut = [];
46
+ // for (var i = 0; i < 256; i++) {
47
+ // lut[i] = (i < 16 ? '0' : '') + (i).toString(16);
48
+ // }
49
+
50
+ // function _generateUUID() {
51
+ // var d0 = Math.random() * 0xffffffff | 0;
52
+ // var d1 = Math.random() * 0xffffffff | 0;
53
+ // var d2 = Math.random() * 0xffffffff | 0;
54
+ // var d3 = Math.random() * 0xffffffff | 0;
55
+ // var uuid = lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff] + '-' +
56
+ // lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + '-' + lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + '-' +
57
+ // lut[d2 & 0x3f | 0x80] + lut[d2 >> 8 & 0xff] + '-' + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] +
58
+ // lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff];
59
+
60
+ // // .toUpperCase() here flattens concatenated strings to save heap memory space.
61
+ // return uuid.toUpperCase();
62
+ // };
63
+
64
+ // return _generateUUID()
65
+ // },
66
+ // }
@@ -0,0 +1,35 @@
1
+ export default {
2
+ imageValidate(file) {
3
+ // console.log("imageValidate")
4
+ var maxSize = 10;
5
+ var fileSize = 1024 * 1024 * maxSize; // 10mb
6
+ var fileTypes = ["image/png", "image/jpeg"];
7
+ // var isFileValid = true;
8
+ if (!file || fileTypes.indexOf(file.type) < 0) {
9
+ alert("Vui lòng upload ảnh đúng định dạng jpg hay png");
10
+ return false;
11
+ } else if (file.size > fileSize) {
12
+ alert("Vui lòng upload ảnh có dung lượng thấp hơn " + maxSize + "mb");
13
+ return false;
14
+ }
15
+ return true;
16
+ },
17
+
18
+ videoValidate(file) {
19
+ // console.log("videoValidate")
20
+
21
+ var maxSize = 15;
22
+ var fileSize = 1024 * 1024 * maxSize; // 10mb
23
+ var fileTypes = "video";
24
+ // console.log(file.type)
25
+ // var isFileValid = true;
26
+ if (!file || file.type.indexOf(fileTypes) < 0) {
27
+ alert("Vui lòng upload video");
28
+ return false;
29
+ } else if (file.size > fileSize) {
30
+ alert("Vui lòng upload video có dung lượng thấp hơn " + maxSize + "mb");
31
+ return false;
32
+ }
33
+ return true;
34
+ },
35
+ };
@@ -0,0 +1,301 @@
1
+ import { randInt } from "../math";
2
+
3
+ /**
4
+ *
5
+ * @param {Array} array
6
+ * @param {string} key
7
+ * @returns {Number}
8
+ */
9
+ export const sumArray = (array, key) => {
10
+ if (!array) {
11
+ console.warn("ARRAY NOT EXITED !");
12
+ return 0;
13
+ }
14
+ if (key) return array.reduce((c, v) => c + v[key], 0);
15
+ else return array.reduce((c, v) => c + v, 0);
16
+ };
17
+
18
+ /**
19
+ *
20
+ * @param {Array} array
21
+ * @param {string} key
22
+ * @returns {Number}
23
+ */
24
+ export const averageArray = (array, key) => {
25
+ if (!array) {
26
+ console.warn("ARRAY NOT EXITED !");
27
+ return 0;
28
+ }
29
+ return this.sum(array, key) / array.length || 0;
30
+ };
31
+
32
+ /**
33
+ *
34
+ * @param {Array} array
35
+ * @param {string} key
36
+ * @returns {Number}
37
+ */
38
+ export const minArray = (array, key) => {
39
+ if (!array) {
40
+ console.warn("ARRAY NOT EXITED !");
41
+ return 0;
42
+ }
43
+ if (array.length > 0) {
44
+ if (key) return array.reduce((c, v) => (c < v[key] ? c : v[key]));
45
+ else return array.reduce((c, v) => (c < v ? c : v));
46
+ }
47
+ return 0;
48
+ };
49
+
50
+ /**
51
+ *
52
+ * @param {Array} array
53
+ * @param {string} key
54
+ * @returns {Number}
55
+ */
56
+ export const maxArray = (array, key) => {
57
+ if (!array) {
58
+ console.warn("ARRAY NOT EXITED !");
59
+ return 0;
60
+ }
61
+ if (array.length > 0) {
62
+ if (key) return array.reduce((c, v) => (c > v[key] ? c : v[key]));
63
+ else return array.reduce((c, v) => (c > v ? c : v));
64
+ }
65
+ return 0;
66
+ };
67
+
68
+ /**
69
+ *
70
+ * @param {Array} array
71
+ * @param {string} key
72
+ * @returns {Array}
73
+ */
74
+ export const sortElementByString = (array, key) => {
75
+ if (!Array.isArray(array)) return [];
76
+ if (key)
77
+ return array.sort((x, y) => {
78
+ var a = x[key].toUpperCase(),
79
+ b = y[key].toUpperCase();
80
+ return a == b ? 0 : a > b ? 1 : -1;
81
+ });
82
+ console.log("NO KEY");
83
+ };
84
+
85
+ /**
86
+ *
87
+ * @param {Array} array
88
+ * @param {string} key
89
+ * @returns {Array}
90
+ */
91
+ export const sortElementByNumber = (array, key) => {
92
+ if (!Array.isArray(array)) return [];
93
+ if (key)
94
+ return array.sort((a, b) => {
95
+ return a[key] - b[key];
96
+ });
97
+ console.log("NO KEY");
98
+ };
99
+
100
+ /**
101
+ *
102
+ * @param {Array} array
103
+ * @returns {any}
104
+ */
105
+ export const firstElement = (array) => {
106
+ if (array) if (array.length || array.length > 0) return array[0];
107
+ return null;
108
+ };
109
+
110
+ /**
111
+ *
112
+ * @param {Array} array
113
+ * @returns {any}
114
+ */
115
+ export const lastElement = (array) => {
116
+ if (array) if (array.length || array.length > 0) return array[array.length - 1];
117
+ return null;
118
+ };
119
+
120
+ /**
121
+ *
122
+ * @param {Array} array
123
+ * @returns {any}
124
+ */
125
+ export const randomIndex = (array) => {
126
+ if (array) return randInt(0, array.length - 1);
127
+ return -1;
128
+ };
129
+
130
+ /**
131
+ *
132
+ * @param {Array} array
133
+ * @returns {any}
134
+ */
135
+ export const randomElement = (array) => {
136
+ if (array) return array[randomIndex(array)];
137
+ return null;
138
+ };
139
+
140
+ /**
141
+ * Remove same elements from 2 arrays
142
+ * @param {Array} list1
143
+ * @param {Array} list2
144
+ * @param {string} key
145
+ * @returns {Array}
146
+ */
147
+ export const mergeAndMakeUniqueElement = (list1, list2, key) => {
148
+ if (!list1 || !list2) return;
149
+
150
+ if (key) {
151
+ return list1
152
+ .concat(list2)
153
+ .filter((item, index, self) => {
154
+ return self.findIndex(x => x[key] == item[key]) === index
155
+ })
156
+ }
157
+ else {
158
+ return list1.concat(list2)
159
+ .filter((x, index, self) => {
160
+ return self.indexOf(x) === index
161
+ })
162
+ }
163
+
164
+ };
165
+
166
+ /**
167
+ * check target == toMatch
168
+ * @param {Array} target
169
+ * @param {Array} toMatch
170
+ * @returns {Boolean}
171
+ */
172
+ export const allMatchInArray = (target, toMatch) => {
173
+ if (!target || !toMatch) return false;
174
+ const found = toMatch.every((item) => {
175
+ return target.includes(item);
176
+ });
177
+ return found;
178
+ };
179
+
180
+
181
+
182
+ /**
183
+ *
184
+ * @param {any} item
185
+ * @param {Array} array
186
+ * @returns {Array}
187
+ */
188
+ export const removeItem = (item, array) => {
189
+ const index = array.indexOf(item);
190
+ if (index == -1) {
191
+ console.warn("[ArrayExtra.removeItem] Item not found.");
192
+ return array;
193
+ }
194
+ array.splice(index, 1);
195
+ return array;
196
+ };
197
+
198
+ /**
199
+ *
200
+ * @param {string} key
201
+ * @param {any} value
202
+ * @param {Array} array
203
+ * @returns {Array}
204
+ */
205
+ export const removeItemByKey = (key, value, array) => {
206
+ const foundIndex = array.findIndex((item) => {
207
+ return item[key] == value;
208
+ });
209
+
210
+ if (foundIndex < 0) {
211
+ console.warn("[ArrayExtra.removeItemByKey] Item not found.", key, value);
212
+ return array;
213
+ }
214
+ array.splice(foundIndex, 1);
215
+ return array;
216
+ };
217
+
218
+ /**
219
+ * Get an array with shuffle element
220
+ * @param {Array} array
221
+ * @param {Number} n
222
+ * @returns {Array}
223
+ */
224
+ export const getRandom = (array, n) => {
225
+ var result = new Array(n),
226
+ len = array.length,
227
+ taken = new Array(len);
228
+ if (n > len) throw new RangeError("getRandom: more elements taken than available");
229
+ while (n--) {
230
+ var x = Math.floor(Math.random() * len);
231
+ result[n] = array[x in taken ? taken[x] : x];
232
+ taken[x] = --len in taken ? taken[len] : len;
233
+ }
234
+ return result;
235
+ };
236
+
237
+ /**
238
+ * Get an array with shuffle element
239
+ * @param {Array} array
240
+ * @param {Number} n
241
+ * @returns {Array}
242
+ */
243
+ export const getHalfRandom = (array, n) => {
244
+ var n = Math.ceil(array.length / 2);
245
+ return array.getRandom(n);
246
+ };
247
+
248
+ /**
249
+ * Make array shuffle itself
250
+ * @param {Array} array
251
+ * @returns {Array}
252
+ */
253
+ export const shuffle = (array) => {
254
+ var i = array.length,
255
+ j,
256
+ temp;
257
+ if (i == 0) return array;
258
+ while (--i) {
259
+ j = Math.floor(Math.random() * (i + 1));
260
+ temp = array[i];
261
+ array[i] = array[j];
262
+ array[j] = temp;
263
+ }
264
+ return array;
265
+ };
266
+
267
+ /**
268
+ *
269
+ * @param {Array} array
270
+ * @param {Number} oldIndex
271
+ * @param {Number} newIndex
272
+ * @returns {Array}
273
+ */
274
+ export const moveIndex = (array, oldIndex, newIndex) => {
275
+ if (newIndex >= array.length) {
276
+ var k = newIndex - array.length + 1;
277
+ while (k--) {
278
+ array.push(undefined);
279
+ }
280
+ }
281
+ array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
282
+ return array;
283
+ };
284
+
285
+
286
+ export const moveArray = (array, oldIndex, newIndex) => {
287
+ while (oldIndex < 0) {
288
+ oldIndex += array.length;
289
+ }
290
+ while (newIndex < 0) {
291
+ newIndex += array.length;
292
+ }
293
+ if (newIndex >= array.length) {
294
+ var k = newIndex - array.length;
295
+ while (k-- + 1) {
296
+ array.push(999);
297
+ }
298
+ }
299
+ array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
300
+ return array;
301
+ };
@@ -0,0 +1,13 @@
1
+ const fs = require("fs");
2
+
3
+ /**
4
+ *
5
+ * @param {string} path
6
+ */
7
+ export default function createDir(path) {
8
+ if (fs.existsSync(path)) {
9
+ console.log("directory already exited !");
10
+ return true;
11
+ }
12
+ fs.mkdirSync(path, { recursive: true });
13
+ }
@@ -0,0 +1,35 @@
1
+ const fs = require("fs");
2
+
3
+ /**
4
+ *
5
+ * @param {string} oldPath
6
+ * @param {string} newPath
7
+ * @param {Function} callback
8
+ */
9
+ export default function fileMove(oldPath, newPath, callback) {
10
+ fs.rename(oldPath, newPath, function (err) {
11
+ if (err) {
12
+ if (err.code === "EXDEV") {
13
+ copy();
14
+ } else {
15
+ callback(err);
16
+ }
17
+ return;
18
+ }
19
+ callback();
20
+ });
21
+
22
+ function copy() {
23
+ var readStream = fs.createReadStream(oldPath);
24
+ var writeStream = fs.createWriteStream(newPath);
25
+
26
+ readStream.on("error", callback);
27
+ writeStream.on("error", callback);
28
+
29
+ readStream.on("close", function () {
30
+ fs.unlink(oldPath, callback);
31
+ });
32
+
33
+ readStream.pipe(writeStream);
34
+ }
35
+ }
@@ -0,0 +1,42 @@
1
+ var path = require("path");
2
+ var fs = require("fs");
3
+
4
+ /**
5
+ *
6
+ * @param {string} base
7
+ * @param {string} ext
8
+ * @param {Function} cb
9
+ */
10
+ const forEachFileByExt = (base, ext, cb) => {
11
+ var walk = function (directoryName) {
12
+ try {
13
+ fs.readdir(directoryName, function (e, files) {
14
+ if (e) {
15
+ console.log("Error: ", e);
16
+ return;
17
+ }
18
+ files.forEach(function (file) {
19
+ var fullPath = path.join(directoryName, file);
20
+ fs.stat(fullPath, function (e, f) {
21
+ if (e) {
22
+ console.log("Error: ", e);
23
+ return;
24
+ }
25
+ if (f.isDirectory()) {
26
+ walk(fullPath);
27
+ } else {
28
+ if (fullPath.toLowerCase().indexOf(ext.toLowerCase()) > -1) {
29
+ if (cb) cb(fullPath);
30
+ }
31
+ }
32
+ });
33
+ });
34
+ });
35
+ } catch (error) {
36
+ console.log("error", error);
37
+ }
38
+ };
39
+ walk(base);
40
+ };
41
+
42
+ export default forEachFileByExt;
@@ -0,0 +1,58 @@
1
+ import JSZip from "jszip";
2
+ import ObjectExtra from "plugins/utils/ObjectExtra";
3
+ import createDir from "../file/createDir";
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+
7
+ /**
8
+ *
9
+ * @param {string} zipPath
10
+ * @param {string} directory
11
+ * @param {Function} forEach
12
+ */
13
+ const extractZip = async (zipPath, directory, forEach) => {
14
+ // let id = -1;
15
+ // let isCallcb = false;
16
+ fs.readFile(zipPath, async function (err, data) {
17
+ if (err) throw err;
18
+ JSZip.loadAsync(data)
19
+ .then(async function (zip) {
20
+ const _length = ObjectExtra.toArray(zip).length;
21
+ zip.forEach((filePath, fileObj) => {
22
+ console.log(filePath);
23
+ var fileName = path.resolve(directory, filePath);
24
+ if (fileName.indexOf(".DS_Store") >= 0) {
25
+ return;
26
+ }
27
+ if (fileName.indexOf("__MACOSX/") >= 0) {
28
+ return;
29
+ }
30
+
31
+ // console.log(fileName)
32
+ if (fileObj.dir) {
33
+ createDir(fileName);
34
+ if (forEach) forEach(fileName, false);
35
+ } else {
36
+ try {
37
+ fileObj.async("nodebuffer").then((buff) => {
38
+ fs.writeFileSync(fileName, buff);
39
+ if (forEach) forEach(fileName, true);
40
+
41
+ // id++;
42
+ // if (id >= _length) if (callback && !isCallcb) {
43
+ // console.log("4")
44
+ // isCallcb = true;
45
+ // if (callback) callback();
46
+ // }
47
+ });
48
+ } catch (error) {
49
+ cosole.log("extractZip ERROR", error);
50
+ }
51
+ }
52
+ });
53
+ })
54
+ .then(function () {});
55
+ });
56
+ };
57
+
58
+ export default extractZip;
@@ -0,0 +1,29 @@
1
+ export const isPotrait = (params) => {
2
+ if (typeof window == "undefined") return false;
3
+
4
+ if (!window.orientation) return window.matchMedia("(orientation: portrait)").matches;
5
+ return !(window.orientation === 90 || window.orientation === -90);
6
+ };
7
+
8
+ export const isLandscape = (params) => {
9
+ return !isPotrait();
10
+ };
11
+
12
+ export const ua = (params) => {
13
+ if (typeof navigator == "undefined") return null;
14
+ if (typeof window == "undefined") return null;
15
+
16
+ return navigator.userAgent || navigator.vendor || window.opera;
17
+ };
18
+
19
+ export const isFacebookWebview = (params) => {
20
+ var ua = ua();
21
+ if (ua) return ua.indexOf("FBAN") > -1 || ua.indexOf("FBAV") > -1;
22
+ };
23
+
24
+ export const isInAppWebview = (params) => {
25
+ const rules = ["WebView", "(iPhone|iPod|iPad)(?!.*Safari/)", "Android.*(wv|.0.0.0)"];
26
+ const regex = new RegExp(`(${rules.join("|")})`, "ig");
27
+ if (ua()) return Boolean(ua().match(regex));
28
+ return false;
29
+ };