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,40 @@
1
+ export const isNull = (object) => {
2
+ if (typeof object == "undefined") return true;
3
+
4
+ if (Array.isArray(object)) return object.length == 0;
5
+ return object == null;
6
+ };
7
+
8
+ export const toBool = (object) => {
9
+ if (isNull(object)) return false;
10
+ object = object.toString();
11
+
12
+ return object === "true" || object == "1";
13
+ };
14
+
15
+ export const toInt = (object) => {
16
+ if (isNull(object)) return 0;
17
+ object = object.toString();
18
+
19
+ return parseInt(object, 10);
20
+ };
21
+
22
+ export const toFloat = (object) => {
23
+ if (isNull(object)) return 0;
24
+ object = object.toString();
25
+
26
+ return parseFloat(object);
27
+ };
28
+
29
+ /**
30
+ * Convert value in object to array
31
+ * @param {object} obj
32
+ * @returns {Array}
33
+ */
34
+ export const toArray = (obj) => {
35
+ const array = [];
36
+ for (const key in obj) {
37
+ array.push(obj[key]);
38
+ }
39
+ return array;
40
+ };
@@ -0,0 +1,43 @@
1
+ const requestCamera = ({ audio = true, video = true }) => {
2
+ if (typeof window == "undefined") return false;
3
+
4
+ return new Promise((resolve, reject) => {
5
+ // Older browsers might not implement mediaDevices at all, so we set an empty object first
6
+ if (navigator.mediaDevices === undefined) {
7
+ navigator.mediaDevices = {};
8
+ }
9
+
10
+ // Some browsers partially implement mediaDevices. We can't just assign an object
11
+ // with getUserMedia as it would overwrite existing properties.
12
+ // Here, we will just add the getUserMedia property if it's missing.
13
+ if (navigator.mediaDevices.getUserMedia === undefined) {
14
+ navigator.mediaDevices.getUserMedia = function (constraints) {
15
+ // First get ahold of the legacy getUserMedia, if present
16
+ var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
17
+
18
+ // Some browsers just don't implement it - return a rejected promise with an error
19
+ // to keep a consistent interface
20
+ if (!getUserMedia) {
21
+ return Promise.reject(new Error("getUserMedia is not implemented in this browser"));
22
+ }
23
+
24
+ // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
25
+ return new Promise(function (resolve, reject) {
26
+ getUserMedia.call(navigator, constraints, resolve, reject);
27
+ });
28
+ };
29
+ }
30
+
31
+ navigator.mediaDevices
32
+ .getUserMedia({ audio, video })
33
+ .then(function (stream) {
34
+ resolve(true);
35
+ })
36
+ .catch(function (err) {
37
+ console.log(err.name + ": " + err.message);
38
+ resolve(false);
39
+ });
40
+ });
41
+ };
42
+
43
+ export default requestCamera;
@@ -0,0 +1,32 @@
1
+ import { isAndroid } from "../device";
2
+
3
+ const requestDeviceOrientationControl = () => {
4
+ if (typeof window == "undefined") return false;
5
+ return new Promise((resolve, reject) => {
6
+ if (isAndroid()) resolve(true);
7
+
8
+ if (typeof DeviceMotionEvent != "undefined" && DeviceMotionEvent.requestPermission) {
9
+ // (optional) Do something before API request prompt.
10
+ DeviceOrientationEvent.requestPermission()
11
+ .then((response) => {
12
+ // (optional) Do something after API prompt dismissed.
13
+ if (response == "granted") {
14
+ resolve(true);
15
+ // resolve({ status: true })
16
+ } else {
17
+ resolve(false);
18
+ // resolve({ status: false, reason: "DeviceMotionEvent is not support" })
19
+ }
20
+ })
21
+ .catch((response) => {
22
+ resolve(false);
23
+ // resolve({ status: false, reason: response })
24
+ });
25
+ } else {
26
+ resolve(false);
27
+ // resolve({ status: false, reason: "DeviceMotionEvent is not defined" })
28
+ }
29
+ });
30
+ };
31
+
32
+ export default requestDeviceOrientationControl;
@@ -0,0 +1,194 @@
1
+ // import { log } from "../../helper/log";
2
+ // import { startCase, toLower } from "lodash";
3
+ import { isNull } from "../object";
4
+
5
+ export const textLowCase = "abcdefghijklmnopqrstuvwxyz";
6
+ export const numeric = "0123456789";
7
+ export const punctuation = "!@#$%^&*()_+~|}{[];?><,./-=";
8
+ export const allCharacter = `đĐaáàảãạăắằẳẵặâấầẩẫậeéèẻẽẹêếềểễệoóòỏõọôốồổỗộơớờởỡợiíìỉĩịuúùủũụưứừửữựyýỳỷỹỵAÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬEÉÈẺẼẸÊẾỀỂỄỆOÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢIÍÌỈĨỊUÚÙỦŨỤƯỨỪỬỮỰYÝỲ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijklmnopqrstuvwxyz{|}`;
9
+
10
+ /**
11
+ * allCharacter = `đĐaáàảãạăắằẳẵặâấầẩẫậeéèẻẽẹêếềểễệoóòỏõọôốồổỗộơớờởỡợiíìỉĩịuúùủũụưứừửữựyýỳỷỹỵAÁÀẢÃẠĂẮẰẲẴẶÂẤẦẨẪẬEÉÈẺẼẸÊẾỀỂỄỆOÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢIÍÌỈĨỊUÚÙỦŨỤƯỨỪỬỮỰYÝỲ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_\`abcdefghijklmnopqrstuvwxyz{|}`;
12
+ * @param {number} length
13
+ * @returns {string}
14
+ */
15
+ export const randAllCharacterByLength = (length = 0) => {
16
+ return randomStringByLength(length, allCharacter);
17
+ };
18
+
19
+ /**
20
+ * @param {number} length
21
+ * @param {textLowCase | numeric | punctuation | allCharacter} str
22
+ * @returns {string}
23
+ */
24
+ export const randomStringByLength = (length, str = textLowCase) => {
25
+ let result = "";
26
+
27
+ for (let i = 0; i < length; i++) {
28
+ result += str.charAt(Math.floor(Math.random() * str.length));
29
+ }
30
+ return result;
31
+ };
32
+
33
+ /**
34
+ * Get string between str1 and str2 from text
35
+ * @param {string} text
36
+ * @param {string} str1
37
+ * @param {string} str2
38
+ * @return {string}
39
+ */
40
+ export const getBetween = (text, str1, str2 = "") => {
41
+ if (!text) return "";
42
+ if (text.indexOf(str1) <= -1) return "";
43
+
44
+ const firstIndex = text.indexOf(str1) + str1.length;
45
+ const secondIndex = str2 ? text.indexOf(str2, firstIndex) : text.length;
46
+ return text.substring(firstIndex, secondIndex);
47
+ };
48
+
49
+ /**
50
+ * Convert object to string
51
+ * @param {*} object
52
+ * @return {string}
53
+ */
54
+ export const makeString = (object = "") => {
55
+ /// Ensure some object is a coerced to a string
56
+ if (isNull(object)) return "";
57
+
58
+ return "" + object;
59
+ };
60
+
61
+ /**
62
+ * toUpperCase
63
+ * @param {*} str
64
+ * @return {string}
65
+ */
66
+ export const toUpperCase = (str = "") => {
67
+ return makeString(str).toUpperCase();
68
+ };
69
+
70
+ /**
71
+ * toLowerCase
72
+ * @param {*} str
73
+ * @return {string}
74
+ */
75
+ export const toLowerCase = (str = "") => {
76
+ return makeString(str).toLowerCase();
77
+ };
78
+
79
+ /**
80
+ * toLowerCase
81
+ * @param {*} str
82
+ * @return {string}
83
+ */
84
+ export const titleize = (str = "") => {
85
+ const regString = new RegExp(/(?:^|\s|-)\S/g);
86
+ return toLowerCase(str).replace(regString, function (c) {
87
+ return c.toUpperCase();
88
+ });
89
+ };
90
+
91
+ /**
92
+ * Convert only first charater to UpperCase
93
+ * @param {*} str
94
+ * @return {string}
95
+ */
96
+ export const capitalize = (str, lowercaseRest = 1) => {
97
+ str = makeString(str);
98
+ const remainingChars = !lowercaseRest ? str.slice(1) : str.slice(1).toLowerCase();
99
+
100
+ return str.charAt(0).toUpperCase() + remainingChars;
101
+ };
102
+
103
+ /**
104
+ * Convert first character from every single words to UpperCase
105
+ * @param {*} str
106
+ * @return {string}
107
+ */
108
+ export const capitalizeName = (str) => {
109
+ str = makeString(str);
110
+
111
+ str = str.trim();
112
+ str = str.replace(/^\s+|\s+$/gm, "");
113
+
114
+ str = str.toLowerCase();
115
+
116
+ const arr = str.split(" ");
117
+
118
+ str = arr
119
+ .map((item) => {
120
+ return capitalize(item);
121
+ })
122
+ .filter((x) => x)
123
+ .join(" ");
124
+
125
+ return str;
126
+ };
127
+
128
+ /**
129
+ *
130
+ * @param {string} s - Input string
131
+ * @param {{delimiter,limit,lowercase,replacements,transliterate}} opt - Option
132
+ * @return {string}
133
+ */
134
+ export const clearUnicodeCharacters = (s, opt = {}) => {
135
+ // log(`clearUnicodeCharacters >>> `, s);
136
+ s = s.toString();
137
+ // opt = Object(opt);
138
+
139
+ var defaults = {
140
+ delimiter: " ",
141
+ limit: undefined,
142
+ lowercase: true,
143
+ replacements: {},
144
+ transliterate: typeof XRegExp === "undefined" ? true : false,
145
+ };
146
+
147
+ // Merge options
148
+ for (var k in defaults) {
149
+ if (!opt.hasOwnProperty(k)) {
150
+ opt[k] = defaults[k];
151
+ }
152
+ }
153
+
154
+ //prettier-ignore
155
+ const char_map={À:"A",Á:"A",Â:"A",Ã:"A",Ä:"A",Å:"A",Æ:"AE",Ç:"C",È:"E",É:"E",Ê:"E",Ë:"E",Ì:"I",Í:"I",Î:"I",Ï:"I",Ð:"D",Ñ:"N",Ò:"O",Ó:"O",Ô:"O",Õ:"O",Ö:"O",Ő:"O",Ø:"O",Ù:"U",Ú:"U",Û:"U",Ü:"U",Ű:"U",Ý:"Y",Þ:"TH",ß:"ss",à:"a",á:"a",â:"a",ã:"a",ä:"a",å:"a",æ:"ae",ç:"c",è:"e",é:"e",ê:"e",ë:"e",ì:"i",í:"i",î:"i",ï:"i",ð:"d",ñ:"n",ò:"o",ó:"o",ô:"o",õ:"o",ö:"o",ő:"o",ø:"o",ù:"u",ú:"u",û:"u",ü:"u",ű:"u",ý:"y",þ:"th",ÿ:"y","©":"(c)",Α:"A",Β:"B",Γ:"G",Δ:"D",Ε:"E",Ζ:"Z",Η:"H",Θ:"8",Ι:"I",Κ:"K",Λ:"L",Μ:"M",Ν:"N",Ξ:"3",Ο:"O",Π:"P",Ρ:"R",Σ:"S",Τ:"T",Υ:"Y",Φ:"F",Χ:"X",Ψ:"PS",Ω:"W",Ά:"A",Έ:"E",Ί:"I",Ό:"O",Ύ:"Y",Ή:"H",Ώ:"W",Ϊ:"I",Ϋ:"Y",α:"a",β:"b",γ:"g",δ:"d",ε:"e",ζ:"z",η:"h",θ:"8",ι:"i",κ:"k",λ:"l",μ:"m",ν:"n",ξ:"3",ο:"o",π:"p",ρ:"r",σ:"s",τ:"t",υ:"y",φ:"f",χ:"x",ψ:"ps",ω:"w",ά:"a",έ:"e",ί:"i",ό:"o",ύ:"y",ή:"h",ώ:"w",ς:"s",ϊ:"i",ΰ:"y",ϋ:"y",ΐ:"i",Ş:"S",İ:"I",Ç:"C",Ü:"U",Ö:"O",Ğ:"G",ş:"s",ı:"i",ç:"c",ü:"u",ö:"o",ğ:"g",А:"A",Б:"B",В:"V",Г:"G",Д:"D",Е:"E",Ё:"Yo",Ж:"Zh",З:"Z",И:"I",Й:"J",К:"K",Л:"L",М:"M",Н:"N",О:"O",П:"P",Р:"R",С:"S",Т:"T",У:"U",Ф:"F",Х:"H",Ц:"C",Ч:"Ch",Ш:"Sh",Щ:"Sh",Ъ:"",Ы:"Y",Ь:"",Э:"E",Ю:"Yu",Я:"Ya",а:"a",б:"b",в:"v",г:"g",д:"d",е:"e",ё:"yo",ж:"zh",з:"z",и:"i",й:"j",к:"k",л:"l",м:"m",н:"n",о:"o",п:"p",р:"r",с:"s",т:"t",у:"u",ф:"f",х:"h",ц:"c",ч:"ch",ш:"sh",щ:"sh",ъ:"",ы:"y",ь:"",э:"e",ю:"yu",я:"ya",Є:"Ye",І:"I",Ї:"Yi",Ґ:"G",є:"ye",і:"i",ї:"yi",ґ:"g",Č:"C",Ď:"D",Ě:"E",Ň:"N",Ř:"R",Š:"S",Ť:"T",Ů:"U",Ž:"Z",č:"c",ď:"d",ě:"e",ň:"n",ř:"r",š:"s",ť:"t",ů:"u",ž:"z",Ą:"A",Ć:"C",Ę:"e",Ł:"L",Ń:"N",Ó:"o",Ś:"S",Ź:"Z",Ż:"Z",ą:"a",ć:"c",ę:"e",ł:"l",ń:"n",ó:"o",ś:"s",ź:"z",ż:"z",Ā:"A",Č:"C",Ē:"E",Ģ:"G",Ī:"i",Ķ:"k",Ļ:"L",Ņ:"N",Š:"S",Ū:"u",Ž:"Z",ā:"a",č:"c",ē:"e",ģ:"g",ī:"i",ķ:"k",ļ:"l",ņ:"n",š:"s",ū:"u",ž:"z",};
156
+
157
+ // Vietnamese
158
+ s = s.replace(/á|à|ả|ạ|ã|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ/gi, "a");
159
+ s = s.replace(/é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ/gi, "e");
160
+ s = s.replace(/i|í|ì|ỉ|ĩ|ị/gi, "i");
161
+ s = s.replace(/ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ/gi, "o");
162
+ s = s.replace(/ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự/gi, "u");
163
+ s = s.replace(/ý|ỳ|ỷ|ỹ|ỵ/gi, "y");
164
+ s = s.replace(/đ/gi, "d");
165
+ s = "@" + s + "@";
166
+ s = s.replace(/\@\-|\-\@|\@/gi, "");
167
+
168
+ // Make custom replacements
169
+ for (var k in opt.replacements) {
170
+ s = s.replace(RegExp(k, "g"), opt.replacements[k]);
171
+ }
172
+
173
+ // Transliterate characters to ASCII
174
+ if (opt.transliterate) {
175
+ for (var k in char_map) {
176
+ s = s.replace(RegExp(k, "g"), char_map[k]);
177
+ }
178
+ }
179
+
180
+ // Replace non-alphanumeric characters with our delimiter
181
+ var alnum = typeof XRegExp === "undefined" ? RegExp("[^a-z0-9]+", "ig") : XRegExp("[^\\p{L}\\p{N}]+", "ig");
182
+ s = s.replace(alnum, opt.delimiter);
183
+
184
+ // Remove duplicate delimiters
185
+ s = s.replace(RegExp("[" + opt.delimiter + "]{2,}", "g"), opt.delimiter);
186
+
187
+ // Truncate slug to max. characters
188
+ s = s.substring(0, opt.limit);
189
+
190
+ // Remove delimiter from ends
191
+ s = s.replace(RegExp("(^" + opt.delimiter + "|" + opt.delimiter + "$)", "g"), "");
192
+
193
+ return opt.lowercase ? s.toLowerCase() : s;
194
+ };