diginext-utils 2.0.0 → 2.0.2

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 (111) hide show
  1. package/dist/Checker.d.ts +6 -0
  2. package/dist/Checker.js +27 -0
  3. package/dist/Checker.mjs +25 -0
  4. package/dist/EventDispatcher.d.ts +13 -0
  5. package/dist/EventDispatcher.js +51 -0
  6. package/dist/EventDispatcher.mjs +51 -0
  7. package/dist/FileUpload.d.ts +12 -0
  8. package/dist/FileUpload.js +60 -0
  9. package/dist/FileUpload.mjs +60 -0
  10. package/dist/Slug.d.ts +5 -0
  11. package/dist/Slug.js +329 -0
  12. package/dist/Slug.mjs +327 -0
  13. package/dist/Timer.d.ts +5 -0
  14. package/dist/Timer.js +11 -0
  15. package/dist/Timer.mjs +2 -0
  16. package/dist/Validation.d.ts +6 -0
  17. package/dist/Validation.js +33 -0
  18. package/dist/Validation.mjs +33 -0
  19. package/dist/array.d.ts +93 -0
  20. package/dist/array.js +210 -0
  21. package/dist/array.mjs +3 -0
  22. package/dist/backend/file/createDir.d.ts +6 -0
  23. package/dist/backend/file/createDir.js +21 -0
  24. package/dist/backend/file/createDir.mjs +13 -0
  25. package/dist/backend/file/fileMove.d.ts +6 -0
  26. package/dist/backend/file/fileMove.js +37 -0
  27. package/dist/backend/file/fileMove.mjs +29 -0
  28. package/dist/backend/file/findFilesByExt.d.ts +6 -0
  29. package/dist/backend/file/findFilesByExt.js +48 -0
  30. package/dist/backend/file/findFilesByExt.mjs +40 -0
  31. package/dist/chunk-5AL36RZR.mjs +47 -0
  32. package/dist/chunk-7IYY7BKN.mjs +136 -0
  33. package/dist/chunk-ACLBWYEO.mjs +85 -0
  34. package/dist/chunk-AHFJBIQJ.mjs +9 -0
  35. package/dist/chunk-AKU6F3WT.mjs +9 -0
  36. package/dist/chunk-FOWYH6LL.mjs +32 -0
  37. package/dist/chunk-FTE6FZSH.mjs +17 -0
  38. package/dist/chunk-JKYSZLBF.mjs +27 -0
  39. package/dist/chunk-LXQIKFED.mjs +13 -0
  40. package/dist/chunk-Q5R25OIS.mjs +185 -0
  41. package/dist/chunk-R2BH5I6O.mjs +9 -0
  42. package/dist/chunk-UKRVCU5M.mjs +166 -0
  43. package/dist/chunk-VFKAZAWF.mjs +40 -0
  44. package/dist/chunk-VZGV2BTL.mjs +27 -0
  45. package/dist/chunk-Y4ZTQWPA.mjs +68 -0
  46. package/dist/color.d.ts +12 -0
  47. package/dist/color.js +69 -0
  48. package/dist/color.mjs +62 -0
  49. package/dist/console/enableConsole.d.ts +3 -0
  50. package/dist/console/enableConsole.js +15 -0
  51. package/dist/console/enableConsole.mjs +2 -0
  52. package/dist/console/index.d.ts +5 -0
  53. package/dist/console/index.js +19 -0
  54. package/dist/console/index.mjs +2 -0
  55. package/dist/device/browser.d.ts +7 -0
  56. package/dist/device/browser.js +40 -0
  57. package/dist/device/browser.mjs +2 -0
  58. package/dist/device/camera.d.ts +16 -0
  59. package/dist/device/camera.js +164 -0
  60. package/dist/device/camera.mjs +3 -0
  61. package/dist/device.d.ts +6 -0
  62. package/dist/device.js +173 -0
  63. package/dist/device.mjs +2 -0
  64. package/dist/index.d.ts +13 -280
  65. package/dist/index.mjs +14 -820
  66. package/dist/math/diffDate.d.ts +6 -0
  67. package/dist/math/diffDate.js +10 -0
  68. package/dist/math/diffDate.mjs +10 -0
  69. package/dist/math/positiveNumber.d.ts +3 -0
  70. package/dist/math/positiveNumber.js +28 -0
  71. package/dist/math/positiveNumber.mjs +16 -0
  72. package/dist/math.d.ts +55 -0
  73. package/dist/math.js +51 -0
  74. package/dist/math.mjs +2 -0
  75. package/dist/object.d.ts +25 -0
  76. package/dist/object.js +56 -0
  77. package/dist/object.mjs +2 -0
  78. package/dist/permission/requestCamera.d.ts +6 -0
  79. package/dist/permission/requestCamera.js +29 -0
  80. package/dist/permission/requestCamera.mjs +2 -0
  81. package/dist/permission/requestDeviceOrientationControl.d.ts +3 -0
  82. package/dist/permission/requestDeviceOrientationControl.js +176 -0
  83. package/dist/permission/requestDeviceOrientationControl.mjs +3 -0
  84. package/dist/string/formatNumber.d.ts +7 -0
  85. package/dist/string/formatNumber.js +28 -0
  86. package/dist/string/formatNumber.mjs +10 -0
  87. package/dist/string/generatePassword.d.ts +10 -0
  88. package/dist/string/generatePassword.js +27 -0
  89. package/dist/string/generatePassword.mjs +16 -0
  90. package/dist/string/generateUUID.d.ts +3 -0
  91. package/dist/string/generateUUID.js +14 -0
  92. package/dist/string/generateUUID.mjs +14 -0
  93. package/dist/string/name/en.d.ts +3 -0
  94. package/dist/string/name/en.js +19 -0
  95. package/dist/string/name/en.mjs +17 -0
  96. package/dist/string/name/vi.d.ts +3 -0
  97. package/dist/string/name/vi.js +37 -0
  98. package/dist/string/name/vi.mjs +20 -0
  99. package/dist/string/random.d.ts +11 -0
  100. package/dist/string/random.js +26 -0
  101. package/dist/string/random.mjs +2 -0
  102. package/dist/string/trimNull.d.ts +3 -0
  103. package/dist/string/trimNull.js +20 -0
  104. package/dist/string/trimNull.mjs +14 -0
  105. package/dist/string/url.d.ts +33 -0
  106. package/dist/string/url.js +89 -0
  107. package/dist/string/url.mjs +3 -0
  108. package/dist/string.d.ts +35 -0
  109. package/dist/string.js +107 -0
  110. package/dist/string.mjs +3 -0
  111. package/package.json +2 -5
package/dist/array.js ADDED
@@ -0,0 +1,210 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ // src/math.ts
6
+ var randInt = (low, high) => {
7
+ return low + Math.floor(Math.random() * (high - low + 1));
8
+ };
9
+
10
+ // src/array.ts
11
+ var sumArray = (array, key) => {
12
+ if (!array) {
13
+ console.warn("ARRAY NOT EXITED !");
14
+ return 0;
15
+ }
16
+ if (key)
17
+ return array.reduce((c, v) => c + v[key], 0);
18
+ else
19
+ return array.reduce((c, v) => c + v, 0);
20
+ };
21
+ var averageArray = (array, key) => {
22
+ if (!array) {
23
+ console.warn("ARRAY NOT EXITED !");
24
+ return 0;
25
+ }
26
+ return sumArray(array, key) / array.length || 0;
27
+ };
28
+ var minArray = (array, key) => {
29
+ if (!array) {
30
+ console.warn("ARRAY NOT EXITED !");
31
+ return 0;
32
+ }
33
+ if (array.length > 0) {
34
+ if (key)
35
+ return array.reduce((c, v) => c < v[key] ? c : v[key]);
36
+ else
37
+ return array.reduce((c, v) => c < v ? c : v);
38
+ }
39
+ return 0;
40
+ };
41
+ var maxArray = (array, key) => {
42
+ if (!array) {
43
+ console.warn("ARRAY NOT EXITED !");
44
+ return 0;
45
+ }
46
+ if (array.length > 0) {
47
+ if (key)
48
+ return array.reduce((c, v) => c > v[key] ? c : v[key]);
49
+ else
50
+ return array.reduce((c, v) => c > v ? c : v);
51
+ }
52
+ return 0;
53
+ };
54
+ var sortElementByString = (array, key) => {
55
+ if (!Array.isArray(array))
56
+ return [];
57
+ if (key)
58
+ return array.sort((x, y) => {
59
+ var a = x[key].toUpperCase(), b = y[key].toUpperCase();
60
+ return a == b ? 0 : a > b ? 1 : -1;
61
+ });
62
+ console.log("NO KEY");
63
+ };
64
+ var sortElementByNumber = (array, key) => {
65
+ if (!Array.isArray(array))
66
+ return [];
67
+ if (key)
68
+ return array.sort((a, b) => {
69
+ return a[key] - b[key];
70
+ });
71
+ console.log("NO KEY");
72
+ };
73
+ var firstElement = (array) => {
74
+ if (array) {
75
+ if (array.length || array.length > 0)
76
+ return array[0];
77
+ }
78
+ return null;
79
+ };
80
+ var lastElement = (array) => {
81
+ if (array) {
82
+ if (array.length || array.length > 0)
83
+ return array[array.length - 1];
84
+ }
85
+ return null;
86
+ };
87
+ var randomIndex = (array) => {
88
+ if (array)
89
+ return randInt(0, array.length - 1);
90
+ return -1;
91
+ };
92
+ var randomElement = (array) => {
93
+ if (array)
94
+ return array[randomIndex(array)];
95
+ return null;
96
+ };
97
+ var mergeAndMakeUniqueElement = (list1, list2, key) => {
98
+ if (!list1 || !list2)
99
+ return;
100
+ if (key) {
101
+ return list1.concat(list2).filter((item, index, self) => {
102
+ return self.findIndex((x) => x[key] == item[key]) === index;
103
+ });
104
+ } else {
105
+ return list1.concat(list2).filter((x, index, self) => {
106
+ return self.indexOf(x) === index;
107
+ });
108
+ }
109
+ };
110
+ var allMatchInArray = (target, toMatch) => {
111
+ if (!target || !toMatch)
112
+ return false;
113
+ const found = toMatch.every((item) => {
114
+ return target.includes(item);
115
+ });
116
+ return found;
117
+ };
118
+ var removeItem = (item, array) => {
119
+ const index = array.indexOf(item);
120
+ if (index == -1) {
121
+ console.warn("[ArrayExtra.removeItem] Item not found.");
122
+ return array;
123
+ }
124
+ array.splice(index, 1);
125
+ return array;
126
+ };
127
+ var removeItemByKey = (key, value, array) => {
128
+ const foundIndex = array.findIndex((item) => {
129
+ return item[key] == value;
130
+ });
131
+ if (foundIndex < 0) {
132
+ console.warn("[ArrayExtra.removeItemByKey] Item not found.", key, value);
133
+ return array;
134
+ }
135
+ array.splice(foundIndex, 1);
136
+ return array;
137
+ };
138
+ var getRandom = (array, n) => {
139
+ var result = new Array(n), len = array.length, taken = new Array(len);
140
+ if (n > len)
141
+ throw new RangeError("getRandom: more elements taken than available");
142
+ while (n--) {
143
+ var x = Math.floor(Math.random() * len);
144
+ result[n] = array[x in taken ? taken[x] : x];
145
+ taken[x] = --len in taken ? taken[len] : len;
146
+ }
147
+ return result;
148
+ };
149
+ var getHalfRandom = (array, n) => {
150
+ var n = Math.ceil(array.length / 2);
151
+ return getRandom(array, n);
152
+ };
153
+ var shuffle = (array) => {
154
+ var i = array.length, j, temp;
155
+ if (array.length < 1)
156
+ return array;
157
+ while (--i) {
158
+ j = Math.floor(Math.random() * (i + 1));
159
+ temp = array[i];
160
+ array[i] = array[j];
161
+ array[j] = temp;
162
+ }
163
+ return array;
164
+ };
165
+ var moveIndex = (array, oldIndex, newIndex) => {
166
+ if (newIndex >= array.length) {
167
+ var k = newIndex - array.length + 1;
168
+ while (k--) {
169
+ array.push(void 0);
170
+ }
171
+ }
172
+ array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
173
+ return array;
174
+ };
175
+ var moveArray = (array, oldIndex, newIndex) => {
176
+ while (oldIndex < 0) {
177
+ oldIndex += array.length;
178
+ }
179
+ while (newIndex < 0) {
180
+ newIndex += array.length;
181
+ }
182
+ if (newIndex >= array.length) {
183
+ var k = newIndex - array.length;
184
+ while (k-- + 1) {
185
+ array.push(999);
186
+ }
187
+ }
188
+ array.splice(newIndex, 0, array.splice(oldIndex, 1)[0]);
189
+ return array;
190
+ };
191
+
192
+ exports.allMatchInArray = allMatchInArray;
193
+ exports.averageArray = averageArray;
194
+ exports.firstElement = firstElement;
195
+ exports.getHalfRandom = getHalfRandom;
196
+ exports.getRandom = getRandom;
197
+ exports.lastElement = lastElement;
198
+ exports.maxArray = maxArray;
199
+ exports.mergeAndMakeUniqueElement = mergeAndMakeUniqueElement;
200
+ exports.minArray = minArray;
201
+ exports.moveArray = moveArray;
202
+ exports.moveIndex = moveIndex;
203
+ exports.randomElement = randomElement;
204
+ exports.randomIndex = randomIndex;
205
+ exports.removeItem = removeItem;
206
+ exports.removeItemByKey = removeItemByKey;
207
+ exports.shuffle = shuffle;
208
+ exports.sortElementByNumber = sortElementByNumber;
209
+ exports.sortElementByString = sortElementByString;
210
+ exports.sumArray = sumArray;
package/dist/array.mjs ADDED
@@ -0,0 +1,3 @@
1
+ export { allMatchInArray, averageArray, firstElement, getHalfRandom, getRandom, lastElement, maxArray, mergeAndMakeUniqueElement, minArray, moveArray, moveIndex, randomElement, randomIndex, removeItem, removeItemByKey, shuffle, sortElementByNumber, sortElementByString, sumArray } from './chunk-Q5R25OIS.mjs';
2
+ import './chunk-VFKAZAWF.mjs';
3
+ import './chunk-AKU6F3WT.mjs';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Create new directory
3
+ */
4
+ declare function createDir(path: string): boolean;
5
+
6
+ export { createDir as default };
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
4
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
5
+ }) : x)(function(x) {
6
+ if (typeof require !== "undefined")
7
+ return require.apply(this, arguments);
8
+ throw new Error('Dynamic require of "' + x + '" is not supported');
9
+ });
10
+
11
+ // src/backend/file/createDir.ts
12
+ var fs = __require("fs");
13
+ function createDir(path) {
14
+ if (fs.existsSync(path)) {
15
+ console.log("directory already exited !");
16
+ return true;
17
+ }
18
+ fs.mkdirSync(path, { recursive: true });
19
+ }
20
+
21
+ module.exports = createDir;
@@ -0,0 +1,13 @@
1
+ import { __require } from '../../chunk-AKU6F3WT.mjs';
2
+
3
+ // src/backend/file/createDir.ts
4
+ var fs = __require("fs");
5
+ function createDir(path) {
6
+ if (fs.existsSync(path)) {
7
+ console.log("directory already exited !");
8
+ return true;
9
+ }
10
+ fs.mkdirSync(path, { recursive: true });
11
+ }
12
+
13
+ export { createDir as default };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Move file to another location.
3
+ */
4
+ declare function fileMove(oldPath: string, newPath: string, callback: any): void;
5
+
6
+ export { fileMove as default };
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
4
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
5
+ }) : x)(function(x) {
6
+ if (typeof require !== "undefined")
7
+ return require.apply(this, arguments);
8
+ throw new Error('Dynamic require of "' + x + '" is not supported');
9
+ });
10
+
11
+ // src/backend/file/fileMove.ts
12
+ var fs = __require("fs");
13
+ function fileMove(oldPath, newPath, callback) {
14
+ fs.rename(oldPath, newPath, function(err) {
15
+ if (err) {
16
+ if (err.code === "EXDEV") {
17
+ copy();
18
+ } else {
19
+ callback(err);
20
+ }
21
+ return;
22
+ }
23
+ callback();
24
+ });
25
+ function copy() {
26
+ var readStream = fs.createReadStream(oldPath);
27
+ var writeStream = fs.createWriteStream(newPath);
28
+ readStream.on("error", callback);
29
+ writeStream.on("error", callback);
30
+ readStream.on("close", function() {
31
+ fs.unlink(oldPath, callback);
32
+ });
33
+ readStream.pipe(writeStream);
34
+ }
35
+ }
36
+
37
+ module.exports = fileMove;
@@ -0,0 +1,29 @@
1
+ import { __require } from '../../chunk-AKU6F3WT.mjs';
2
+
3
+ // src/backend/file/fileMove.ts
4
+ var fs = __require("fs");
5
+ function fileMove(oldPath, newPath, callback) {
6
+ fs.rename(oldPath, newPath, function(err) {
7
+ if (err) {
8
+ if (err.code === "EXDEV") {
9
+ copy();
10
+ } else {
11
+ callback(err);
12
+ }
13
+ return;
14
+ }
15
+ callback();
16
+ });
17
+ function copy() {
18
+ var readStream = fs.createReadStream(oldPath);
19
+ var writeStream = fs.createWriteStream(newPath);
20
+ readStream.on("error", callback);
21
+ writeStream.on("error", callback);
22
+ readStream.on("close", function() {
23
+ fs.unlink(oldPath, callback);
24
+ });
25
+ readStream.pipe(writeStream);
26
+ }
27
+ }
28
+
29
+ export { fileMove as default };
@@ -0,0 +1,6 @@
1
+ /**
2
+ *
3
+ */
4
+ declare const forEachFileByExt: (base: string, ext: string, cb: (path: string) => {}) => void;
5
+
6
+ export { forEachFileByExt as default };
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
4
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
5
+ }) : x)(function(x) {
6
+ if (typeof require !== "undefined")
7
+ return require.apply(this, arguments);
8
+ throw new Error('Dynamic require of "' + x + '" is not supported');
9
+ });
10
+
11
+ // src/backend/file/findFilesByExt.ts
12
+ var path = __require("path");
13
+ var fs = __require("fs");
14
+ var forEachFileByExt = (base, ext, cb) => {
15
+ var walk = function(directoryName) {
16
+ try {
17
+ fs.readdir(directoryName, function(e, files) {
18
+ if (e) {
19
+ console.log("Error: ", e);
20
+ return;
21
+ }
22
+ files.forEach(function(file) {
23
+ var fullPath = path.join(directoryName, file);
24
+ fs.stat(fullPath, function(e2, f) {
25
+ if (e2) {
26
+ console.log("Error: ", e2);
27
+ return;
28
+ }
29
+ if (f.isDirectory()) {
30
+ walk(fullPath);
31
+ } else {
32
+ if (fullPath.toLowerCase().indexOf(ext.toLowerCase()) > -1) {
33
+ if (cb)
34
+ cb(fullPath);
35
+ }
36
+ }
37
+ });
38
+ });
39
+ });
40
+ } catch (error) {
41
+ console.log("error", error);
42
+ }
43
+ };
44
+ walk(base);
45
+ };
46
+ var findFilesByExt_default = forEachFileByExt;
47
+
48
+ module.exports = findFilesByExt_default;
@@ -0,0 +1,40 @@
1
+ import { __require } from '../../chunk-AKU6F3WT.mjs';
2
+
3
+ // src/backend/file/findFilesByExt.ts
4
+ var path = __require("path");
5
+ var fs = __require("fs");
6
+ var forEachFileByExt = (base, ext, cb) => {
7
+ var walk = function(directoryName) {
8
+ try {
9
+ fs.readdir(directoryName, function(e, files) {
10
+ if (e) {
11
+ console.log("Error: ", e);
12
+ return;
13
+ }
14
+ files.forEach(function(file) {
15
+ var fullPath = path.join(directoryName, file);
16
+ fs.stat(fullPath, function(e2, f) {
17
+ if (e2) {
18
+ console.log("Error: ", e2);
19
+ return;
20
+ }
21
+ if (f.isDirectory()) {
22
+ walk(fullPath);
23
+ } else {
24
+ if (fullPath.toLowerCase().indexOf(ext.toLowerCase()) > -1) {
25
+ if (cb)
26
+ cb(fullPath);
27
+ }
28
+ }
29
+ });
30
+ });
31
+ });
32
+ } catch (error) {
33
+ console.log("error", error);
34
+ }
35
+ };
36
+ walk(base);
37
+ };
38
+ var findFilesByExt_default = forEachFileByExt;
39
+
40
+ export { findFilesByExt_default as default };
@@ -0,0 +1,47 @@
1
+ // src/object.ts
2
+ var isNull = (object) => {
3
+ if (typeof object == "undefined")
4
+ return true;
5
+ if (object == "")
6
+ return true;
7
+ if (Array.isArray(object))
8
+ return object.length == 0;
9
+ if (JSON.stringify(object) == "{}")
10
+ return true;
11
+ return object == null;
12
+ };
13
+ var toBool = (object) => {
14
+ if (isNull(object))
15
+ return false;
16
+ object = object.toString();
17
+ return object === "true" || object == "1";
18
+ };
19
+ var toInt = (object) => {
20
+ if (isNull(object))
21
+ return 0;
22
+ object = object.toString();
23
+ return parseInt(object, 10);
24
+ };
25
+ var toFloat = (object) => {
26
+ if (isNull(object))
27
+ return 0;
28
+ object = object.toString();
29
+ return parseFloat(object);
30
+ };
31
+ var toArray = (obj) => {
32
+ if (isNull(obj))
33
+ return [];
34
+ if (!Array.isArray(obj)) {
35
+ obj = [obj];
36
+ }
37
+ return obj;
38
+ };
39
+ var objectToArray = (obj) => {
40
+ const array = [];
41
+ for (const key in obj) {
42
+ array.push(obj[key]);
43
+ }
44
+ return array;
45
+ };
46
+
47
+ export { isNull, objectToArray, toArray, toBool, toFloat, toInt };
@@ -0,0 +1,136 @@
1
+ import { requestCamera_default } from './chunk-JKYSZLBF.mjs';
2
+
3
+ // src/device/camera.ts
4
+ function getWebcam({
5
+ container,
6
+ facingMode = "environment",
7
+ audio = false
8
+ }) {
9
+ if (typeof window == "undefined")
10
+ return;
11
+ container = container || document.body;
12
+ return new Promise(async (resolve, reject) => {
13
+ const video = document.createElement("video");
14
+ video.style.position = "absolute";
15
+ video.style.top = "0";
16
+ video.style.left = "0";
17
+ video.style.width = "100%";
18
+ video.style.height = "100%";
19
+ container.append(video);
20
+ let inputCameras = [];
21
+ let currentCamera;
22
+ const requestedMediaConstraints = {
23
+ video: {
24
+ facingMode: { exact: facingMode }
25
+ },
26
+ audio
27
+ };
28
+ console.log("[Camera.js] Requesting:", requestedMediaConstraints);
29
+ const res = await requestCamera_default({ audio });
30
+ if (!res) {
31
+ setTimeout(function() {
32
+ handleError({ name: "Error", message: "NotSupported" });
33
+ }, 50);
34
+ }
35
+ navigator.mediaDevices.enumerateDevices().then(parseDevices).catch(handleError);
36
+ function parseDevices(devices) {
37
+ inputCameras = [];
38
+ let backCameras = [];
39
+ devices.forEach(function(device) {
40
+ if (device.kind == "videoinput" && typeof device.deviceId != "undefined" && device.deviceId != "") {
41
+ inputCameras.push(device);
42
+ }
43
+ });
44
+ console.log("[Camera.js] inputCameras:", inputCameras);
45
+ if (inputCameras.length > 0) {
46
+ let cams = "";
47
+ let backCamera;
48
+ inputCameras.map((cam, index) => {
49
+ cams += `[${cam.deviceId}] ${cam.kind} | ${cam.label}
50
+ `;
51
+ let label = cam.label.toLowerCase();
52
+ if (label.indexOf("back") > -1 || label.indexOf("facetime") > -1) {
53
+ backCamera = cam;
54
+ backCameras.push(backCamera);
55
+ }
56
+ });
57
+ if (backCameras.length > 1) {
58
+ backCamera = backCameras[backCameras.length - 1];
59
+ }
60
+ console.log(`[Camera.js] All input sources:`, cams);
61
+ console.log(`[Camera.js] This device has ${backCameras.length} back camera${backCameras.length > 1 ? "s" : ""}.`);
62
+ console.log("[Camera.js] backCameras:", JSON.stringify(backCameras));
63
+ currentCamera = backCamera;
64
+ getStreamOfCameraId(backCamera.deviceId).then(onStreamReceived).catch((e) => {
65
+ if (backCameras.length > 1) {
66
+ backCamera = backCameras[backCameras.length - 2];
67
+ getStreamOfCameraId(backCamera.deviceId).then(onStreamReceived).catch(handleError);
68
+ } else if (backCameras.length > 2) {
69
+ backCamera = backCameras[backCameras.length - 3];
70
+ getStreamOfCameraId(backCamera.deviceId).then(onStreamReceived).catch(handleError);
71
+ } else {
72
+ handleError(e);
73
+ }
74
+ });
75
+ } else {
76
+ navigator.mediaDevices.getUserMedia(requestedMediaConstraints).then(onStreamReceived).catch(handleError);
77
+ }
78
+ }
79
+ function onStreamReceived(stream2) {
80
+ if (inputCameras.length == 0) {
81
+ console.log("[Camera.js] Not found any back cameras, request again?");
82
+ navigator.mediaDevices.enumerateDevices().then(parseDevices).catch(handleError);
83
+ return;
84
+ }
85
+ playWebcamVideo(stream2);
86
+ }
87
+ function playWebcamVideo(_stream) {
88
+ if ("srcObject" in video) {
89
+ video.srcObject = _stream;
90
+ } else {
91
+ video.src = window.URL.createObjectURL(_stream);
92
+ }
93
+ video.setAttribute("playsinline", "true");
94
+ video.setAttribute("muted", "true");
95
+ video.setAttribute("autoplay", "true");
96
+ video.muted = true;
97
+ video.autoplay = true;
98
+ video.style.objectFit = "cover";
99
+ video.play();
100
+ video.addEventListener("canplay", function(e) {
101
+ });
102
+ video.addEventListener("canplaythrough", function(e) {
103
+ });
104
+ video.addEventListener("error", (e) => console.log("[Camera.js] <video> error:", e));
105
+ video.addEventListener("stalled", function(e) {
106
+ console.log("[Camera.js] <video> stalled:", e);
107
+ reject();
108
+ });
109
+ video.addEventListener("loadedmetadata", function(e) {
110
+ if (currentCamera.label.toLowerCase().indexOf("facetime") > -1) {
111
+ video.style.transform = "scaleX(-1)";
112
+ video.style.webkitTransform = "scaleX(-1)";
113
+ }
114
+ video.play();
115
+ resolve(video);
116
+ });
117
+ }
118
+ function getStreamOfCameraId(id) {
119
+ const constraints = {
120
+ video: { deviceId: { exact: id } },
121
+ audio: false
122
+ };
123
+ return new Promise((resolve2, reject2) => {
124
+ navigator.mediaDevices.getUserMedia(constraints).then(resolve2).catch(reject2);
125
+ });
126
+ }
127
+ function handleError(err) {
128
+ console.error(err);
129
+ const errMsg = "[Camera.js] " + err.name + ": " + err.message;
130
+ console.error(errMsg);
131
+ reject();
132
+ }
133
+ });
134
+ }
135
+
136
+ export { getWebcam };