@nativerent/js-utils 1.0.0 → 1.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.
- package/README.md +3 -1
- package/dist/index.d.ts +139 -5
- package/dist/index.js +357 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +319 -2
- package/dist/index.mjs.map +1 -1
- package/jest.config.mjs +5 -0
- package/package.json +6 -2
- package/src/index.ts +563 -12
- package/src/types.d.ts +7 -1
- package/tests/tests.ts +521 -0
package/README.md
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
type
|
|
1
|
+
type FlattenableObject = { [key: string]: any };
|
|
2
|
+
|
|
3
|
+
type Primitive = boolean | number | string;
|
|
4
|
+
|
|
5
|
+
type SimpleObject = {
|
|
6
|
+
[key: string]: Primitive | Primitive[] | null | undefined;
|
|
7
|
+
};
|
|
2
8
|
|
|
3
9
|
declare function debounce(fn: Function, delay: number): () => void;
|
|
4
10
|
declare function throttle(fn: Function, delay: number): (...args: any[]) => void;
|
|
@@ -29,10 +35,23 @@ declare function objectHasProp(obj: any, prop: string | number | symbol): boolea
|
|
|
29
35
|
declare function getObjectKeys(object: object): string[];
|
|
30
36
|
/**
|
|
31
37
|
* Works with primitive objects, see JSDoc @param
|
|
32
|
-
*
|
|
33
|
-
* @param object {[key: string]: SimpleObject | { [key: string]: SimpleObject }}
|
|
34
38
|
*/
|
|
35
|
-
declare function objectToQueryString(object:
|
|
39
|
+
declare function objectToQueryString(object: {
|
|
40
|
+
[key: string]: SimpleObject | {
|
|
41
|
+
[key: string]: SimpleObject;
|
|
42
|
+
};
|
|
43
|
+
}): string;
|
|
44
|
+
declare function countObjectInnerLength(object: {
|
|
45
|
+
[key: string | number]: any;
|
|
46
|
+
}): any;
|
|
47
|
+
/**
|
|
48
|
+
* Check if the element is in viewport
|
|
49
|
+
*/
|
|
50
|
+
declare const isElementVisible: (element: HTMLElement, minVisiblePercent?: number) => boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Find a DOM element where an ad unit should be rendered to
|
|
53
|
+
*/
|
|
54
|
+
declare function getHtmlElement(id: string): HTMLElement | null;
|
|
36
55
|
declare function decodeSafeURL(url: string): string;
|
|
37
56
|
declare function getSafeURL(url: string): string;
|
|
38
57
|
declare function parseURL(url: string): URL;
|
|
@@ -70,11 +89,126 @@ declare function createScriptElement(js: string, inline?: boolean, attributes?:
|
|
|
70
89
|
* Create a <style> element
|
|
71
90
|
*/
|
|
72
91
|
declare function createStyleElement(css: string | null): HTMLElement;
|
|
92
|
+
/**
|
|
93
|
+
* Create a <link> element
|
|
94
|
+
*/
|
|
95
|
+
declare function createLinkElement(href: string): HTMLLinkElement;
|
|
73
96
|
/**
|
|
74
97
|
* Create svg elements
|
|
75
98
|
*/
|
|
76
99
|
declare function createSvgElement(content: string, attributes: {
|
|
77
100
|
[key: string]: string;
|
|
78
101
|
}): SVGSVGElement;
|
|
102
|
+
/**
|
|
103
|
+
* Append a new script element to the DOM head
|
|
104
|
+
*/
|
|
105
|
+
declare function insertJs(js: string, inline?: boolean): void;
|
|
106
|
+
/**
|
|
107
|
+
* Append a new style element to the DOM head
|
|
108
|
+
*/
|
|
109
|
+
declare const insertCss: (css: string, className?: string, external?: boolean) => void;
|
|
110
|
+
/**
|
|
111
|
+
* Get screen sizes
|
|
112
|
+
*/
|
|
113
|
+
declare function getScreenSize(): {
|
|
114
|
+
width: number;
|
|
115
|
+
height: number;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Get a random integer between min and max
|
|
119
|
+
*/
|
|
120
|
+
declare function getRandomInt(min?: number, max?: number): number;
|
|
121
|
+
/**
|
|
122
|
+
* Create an image element to use as a tracking pixel
|
|
123
|
+
*/
|
|
124
|
+
declare function createPixelElement(src: string): HTMLImageElement;
|
|
125
|
+
/**
|
|
126
|
+
* Implementation of Java's String.hashCode() method
|
|
127
|
+
*/
|
|
128
|
+
declare function hashCode(str: string): number;
|
|
129
|
+
/**
|
|
130
|
+
* Executes a callback as soon as DOM is interactive
|
|
131
|
+
*/
|
|
132
|
+
declare function onDOMReady(callback: Function, ...args: any[]): void;
|
|
133
|
+
/**
|
|
134
|
+
* Calculate the percentage of two values
|
|
135
|
+
*
|
|
136
|
+
* @param a
|
|
137
|
+
* @param b
|
|
138
|
+
* @return number
|
|
139
|
+
*/
|
|
140
|
+
declare function toPercent(a: number, b: number): number;
|
|
141
|
+
/**
|
|
142
|
+
* Make a camelCase string
|
|
143
|
+
*
|
|
144
|
+
* @param string
|
|
145
|
+
* @return string
|
|
146
|
+
*/
|
|
147
|
+
declare function toCamelCase(string: string): string;
|
|
148
|
+
/**
|
|
149
|
+
* Make a snake_case string
|
|
150
|
+
*
|
|
151
|
+
* @param string
|
|
152
|
+
* @return string
|
|
153
|
+
*/
|
|
154
|
+
declare function toSnakeCase(string: string): string;
|
|
155
|
+
/**
|
|
156
|
+
* Transform the first letter of the given string to the upper case
|
|
157
|
+
*
|
|
158
|
+
* @param string
|
|
159
|
+
* @return string
|
|
160
|
+
*/
|
|
161
|
+
declare function capitalize(string: string): string;
|
|
162
|
+
/**
|
|
163
|
+
* Registry independent string sorting
|
|
164
|
+
*
|
|
165
|
+
* @param first
|
|
166
|
+
* @param second
|
|
167
|
+
*/
|
|
168
|
+
declare function sortByAlphabet(first: string, second: string): number;
|
|
169
|
+
/**
|
|
170
|
+
* @param length
|
|
171
|
+
* @return string of max length 100
|
|
172
|
+
*/
|
|
173
|
+
declare function getRandomStr(length?: number): string;
|
|
174
|
+
declare function getRandomItem(items: any[]): any;
|
|
175
|
+
/**
|
|
176
|
+
* Summarize elements of the given array
|
|
177
|
+
*
|
|
178
|
+
* @param values
|
|
179
|
+
* @return number | string - a summary of all the numbers in the array or an empty string if
|
|
180
|
+
* there was only NaNs
|
|
181
|
+
*/
|
|
182
|
+
declare function sumArray(values: Array<any>): number | string;
|
|
183
|
+
/**
|
|
184
|
+
* Returns an array of the object keys which values are not present in filter
|
|
185
|
+
*
|
|
186
|
+
* @param obj
|
|
187
|
+
* @param values
|
|
188
|
+
*/
|
|
189
|
+
declare function filterObjectKeysByValues(obj: {
|
|
190
|
+
[key: string]: any;
|
|
191
|
+
}, values: Array<string | number | boolean | null | undefined>): string[];
|
|
192
|
+
declare function getFromObject(object: object, key: string): any;
|
|
193
|
+
declare function fireBlurEvent(element: string | Element | null, delay?: number): void;
|
|
194
|
+
declare function fireInputEvent(element: string | Element | null, delay?: number): void;
|
|
195
|
+
declare function fireEvent(eventName: string, element: string | Element | null, delay?: number): void;
|
|
196
|
+
declare function hasObjectChanged(object: {
|
|
197
|
+
[key: string]: any;
|
|
198
|
+
}, values: any[]): boolean;
|
|
199
|
+
declare function roundBigNum(number: number): number;
|
|
200
|
+
declare function extractNumbers(value: any): string;
|
|
201
|
+
declare function removeSpaces(value: any): string;
|
|
202
|
+
declare function roundUp(num: number, precision: number): number;
|
|
203
|
+
declare function roundDown(num: number, precision: number): number;
|
|
204
|
+
declare function areDiff(val1?: Primitive | null, val2?: Primitive | null, strict?: boolean): boolean;
|
|
205
|
+
declare function flattenObject(obj: FlattenableObject, prefix?: string): FlattenableObject;
|
|
206
|
+
declare function flattenObjectAsArray(obj: FlattenableObject): FlattenableObject;
|
|
207
|
+
declare function parseObjectPathStr(pathStr: string): string[];
|
|
208
|
+
declare function formatNumber(num: number | string, fractionDigits?: number): string;
|
|
209
|
+
declare function formatNumberWithSign(num: number | string, fractionDigits?: number): string;
|
|
210
|
+
declare function formatPercent(num: number | string): string;
|
|
211
|
+
declare function formatWithSign(num: number | string, numString?: string): string;
|
|
212
|
+
declare function autoSizeText(el: HTMLElement, height: number, minFontSize: number, maxFontSize?: number): void;
|
|
79
213
|
|
|
80
|
-
export { arrayDiff, arrayIntersect, createHtmlElement, createScriptElement, createStyleElement, createSvgElement, debounce, decodeSafeURL, deepCloneObject, encodeQueryString, flatHtmlAttributes, getNumericStyleProp, getObjectKeys, getSafeURL, injectScript, injectStyleLink, insertHtmlElements, isBool, isDefined, isFn, isHTMLElement, isNullOrUndef, isNum, isObject, isStr, isString, isUndef, objectHasProp, objectToQueryString, parseURL, stringToHtmlElements, throttle, toBinaryStr };
|
|
214
|
+
export { areDiff, arrayDiff, arrayIntersect, autoSizeText, capitalize, countObjectInnerLength, createHtmlElement, createLinkElement, createPixelElement, createScriptElement, createStyleElement, createSvgElement, debounce, decodeSafeURL, deepCloneObject, encodeQueryString, extractNumbers, filterObjectKeysByValues, fireBlurEvent, fireEvent, fireInputEvent, flatHtmlAttributes, flattenObject, flattenObjectAsArray, formatNumber, formatNumberWithSign, formatPercent, formatWithSign, getFromObject, getHtmlElement, getNumericStyleProp, getObjectKeys, getRandomInt, getRandomItem, getRandomStr, getSafeURL, getScreenSize, hasObjectChanged, hashCode, injectScript, injectStyleLink, insertCss, insertHtmlElements, insertJs, isBool, isDefined, isElementVisible, isFn, isHTMLElement, isNullOrUndef, isNum, isObject, isStr, isString, isUndef, objectHasProp, objectToQueryString, onDOMReady, parseObjectPathStr, parseURL, removeSpaces, roundBigNum, roundDown, roundUp, sortByAlphabet, stringToHtmlElements, sumArray, throttle, toBinaryStr, toCamelCase, toPercent, toSnakeCase };
|
package/dist/index.js
CHANGED
|
@@ -79,11 +79,44 @@ function objectToQueryString(object) {
|
|
|
79
79
|
if (Array.isArray(v)) {
|
|
80
80
|
return v.map((item) => `${k}[]=${encodeURIComponent(item)}`).join("&");
|
|
81
81
|
} else if (isObject(v)) {
|
|
82
|
-
|
|
82
|
+
return `${k}=${encodeURIComponent(JSON.stringify(v))}`;
|
|
83
83
|
}
|
|
84
84
|
return `${k}=${encodeURIComponent(v || "")}`;
|
|
85
85
|
}).join("&");
|
|
86
86
|
}
|
|
87
|
+
function countObjectInnerLength(object) {
|
|
88
|
+
const obj = Object.values(object);
|
|
89
|
+
return obj.length === 1 ? obj[0].length : obj.reduce((acc, cur) => {
|
|
90
|
+
if (Array.isArray(acc)) {
|
|
91
|
+
acc = acc.length;
|
|
92
|
+
}
|
|
93
|
+
if (cur) {
|
|
94
|
+
acc += cur.length;
|
|
95
|
+
}
|
|
96
|
+
return acc;
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const isElementVisible = (element, minVisiblePercent = 50) => {
|
|
100
|
+
const elementCoords = element.getBoundingClientRect();
|
|
101
|
+
const elementHeight = elementCoords.height;
|
|
102
|
+
if (!elementHeight) {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
const elementTop = elementCoords.top;
|
|
106
|
+
const elementBottom = elementCoords.bottom;
|
|
107
|
+
const viewPortHeight = window.innerHeight;
|
|
108
|
+
const elementVisibleHeight = elementTop > 0 ? (
|
|
109
|
+
// the element is in or below viewport
|
|
110
|
+
viewPortHeight - elementTop
|
|
111
|
+
) : (
|
|
112
|
+
// the element is still in or above viewport
|
|
113
|
+
elementBottom
|
|
114
|
+
);
|
|
115
|
+
return elementVisibleHeight / elementHeight * 100 >= minVisiblePercent;
|
|
116
|
+
};
|
|
117
|
+
function getHtmlElement(id) {
|
|
118
|
+
return document.getElementById(id);
|
|
119
|
+
}
|
|
87
120
|
function decodeSafeURL(url) {
|
|
88
121
|
try {
|
|
89
122
|
return decodeURI(url);
|
|
@@ -222,6 +255,12 @@ function createStyleElement(css) {
|
|
|
222
255
|
}
|
|
223
256
|
return element;
|
|
224
257
|
}
|
|
258
|
+
function createLinkElement(href) {
|
|
259
|
+
const element = createHtmlElement("link");
|
|
260
|
+
element.rel = "stylesheet";
|
|
261
|
+
element.href = href;
|
|
262
|
+
return element;
|
|
263
|
+
}
|
|
225
264
|
function createSvgElement(content, attributes) {
|
|
226
265
|
const attrs = getObjectKeys(attributes);
|
|
227
266
|
const element = document.createElementNS("http://www.w3.org/2000/svg", "svg");
|
|
@@ -233,10 +272,294 @@ function createSvgElement(content, attributes) {
|
|
|
233
272
|
element.innerHTML = content;
|
|
234
273
|
return element;
|
|
235
274
|
}
|
|
275
|
+
function insertJs(js, inline = false) {
|
|
276
|
+
const element = createScriptElement(js, inline);
|
|
277
|
+
document.head.appendChild(element);
|
|
278
|
+
}
|
|
279
|
+
const insertCss = (css, className, external = false) => {
|
|
280
|
+
const element = external ? createLinkElement(css) : createStyleElement(css);
|
|
281
|
+
if (className) {
|
|
282
|
+
element.className = className;
|
|
283
|
+
}
|
|
284
|
+
document.head.appendChild(element);
|
|
285
|
+
};
|
|
286
|
+
function getScreenSize() {
|
|
287
|
+
let width, height;
|
|
288
|
+
if (window.screen) {
|
|
289
|
+
width = screen.width;
|
|
290
|
+
height = screen.height;
|
|
291
|
+
} else {
|
|
292
|
+
width = window.innerWidth;
|
|
293
|
+
height = window.innerHeight;
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
width,
|
|
297
|
+
height
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
function getRandomInt(min = 0, max = 4294967295) {
|
|
301
|
+
min = Math.ceil(min);
|
|
302
|
+
max = Math.floor(max);
|
|
303
|
+
return Math.floor(Math.random() * (max - min)) + min;
|
|
304
|
+
}
|
|
305
|
+
function createPixelElement(src) {
|
|
306
|
+
const image = document.createElement("img");
|
|
307
|
+
image.src = src;
|
|
308
|
+
image.width = 1;
|
|
309
|
+
image.height = 1;
|
|
310
|
+
image.style.position = "absolute";
|
|
311
|
+
image.style.left = "-99999px";
|
|
312
|
+
return image;
|
|
313
|
+
}
|
|
314
|
+
function hashCode(str) {
|
|
315
|
+
let hash = 0, chr;
|
|
316
|
+
for (let i = 0; i < str.length; i++) {
|
|
317
|
+
chr = str.charCodeAt(i);
|
|
318
|
+
hash = (hash << 5) - hash + chr;
|
|
319
|
+
hash |= 0;
|
|
320
|
+
}
|
|
321
|
+
return hash >>> 0;
|
|
322
|
+
}
|
|
323
|
+
function onDOMReady(callback, ...args) {
|
|
324
|
+
if (document.readyState === "interactive" || document.readyState === "complete") {
|
|
325
|
+
callback(...args);
|
|
326
|
+
} else {
|
|
327
|
+
document.addEventListener("DOMContentLoaded", () => callback(...args));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function toPercent(a, b) {
|
|
331
|
+
return b > 0 ? a / b * 100 : 0;
|
|
332
|
+
}
|
|
333
|
+
function toCamelCase(string) {
|
|
334
|
+
return string.split(new RegExp(/[-_.]/)).reduce(
|
|
335
|
+
(a, b) => a + b.charAt(0).toUpperCase() + b.slice(1)
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
function toSnakeCase(string) {
|
|
339
|
+
return toCamelCase(string).replace(/(^[A-Z])?([A-Z])/gm, "$1_$2").toLowerCase();
|
|
340
|
+
}
|
|
341
|
+
function capitalize(string) {
|
|
342
|
+
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
343
|
+
}
|
|
344
|
+
function sortByAlphabet(first, second) {
|
|
345
|
+
return first.toLowerCase() > second.toLowerCase() ? 1 : -1;
|
|
346
|
+
}
|
|
347
|
+
function getRandomStr(length) {
|
|
348
|
+
length = length ? length > 100 ? 100 : length : 10;
|
|
349
|
+
let str = Math.random().toString(36).substring(2);
|
|
350
|
+
while (str.length < length) {
|
|
351
|
+
str += Math.random().toString(36).substring(2);
|
|
352
|
+
}
|
|
353
|
+
const result = str.slice(-length);
|
|
354
|
+
return isNaN(Number(result)) ? result : getRandomStr(length);
|
|
355
|
+
}
|
|
356
|
+
function getRandomItem(items) {
|
|
357
|
+
return items[Math.floor(Math.random() * items.length)];
|
|
358
|
+
}
|
|
359
|
+
function sumArray(values) {
|
|
360
|
+
if (values.every((value) => isNaN(value))) {
|
|
361
|
+
return "";
|
|
362
|
+
} else {
|
|
363
|
+
return values.reduce((sum, curr) => {
|
|
364
|
+
const value = Number(curr);
|
|
365
|
+
if (!isNaN(value)) {
|
|
366
|
+
return sum + value;
|
|
367
|
+
} else {
|
|
368
|
+
return sum;
|
|
369
|
+
}
|
|
370
|
+
}, 0);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
function filterObjectKeysByValues(obj, values) {
|
|
374
|
+
values = Array.isArray(values) ? values : Array(values);
|
|
375
|
+
const keys = [];
|
|
376
|
+
for (const key in obj) {
|
|
377
|
+
if (!values.includes(obj[key])) {
|
|
378
|
+
keys.push(key);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return keys;
|
|
382
|
+
}
|
|
383
|
+
function getFromObject(object, key) {
|
|
384
|
+
return key.split(".").reduce((a, b) => {
|
|
385
|
+
return a[b];
|
|
386
|
+
}, object);
|
|
387
|
+
}
|
|
388
|
+
function fireBlurEvent(element, delay = 0) {
|
|
389
|
+
fireEvent("blur", element, delay);
|
|
390
|
+
}
|
|
391
|
+
function fireInputEvent(element, delay = 0) {
|
|
392
|
+
fireEvent("input", element, delay);
|
|
393
|
+
}
|
|
394
|
+
function fireEvent(eventName, element, delay = 0) {
|
|
395
|
+
setTimeout(() => {
|
|
396
|
+
if (isString(element)) {
|
|
397
|
+
element = document.querySelector(element);
|
|
398
|
+
}
|
|
399
|
+
if (!isNullOrUndef(element)) {
|
|
400
|
+
element.dispatchEvent(
|
|
401
|
+
new Event(eventName, {
|
|
402
|
+
bubbles: true
|
|
403
|
+
})
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
}, delay);
|
|
407
|
+
}
|
|
408
|
+
function hasObjectChanged(object, values) {
|
|
409
|
+
let hasChanged = false;
|
|
410
|
+
for (const key in object) {
|
|
411
|
+
let oldVal = object[key];
|
|
412
|
+
let [, newVal] = values.find(([newKey]) => key === newKey);
|
|
413
|
+
if (isObject(oldVal)) {
|
|
414
|
+
if (!isObject(newVal)) {
|
|
415
|
+
hasChanged = true;
|
|
416
|
+
} else {
|
|
417
|
+
hasChanged = hasObjectChanged(oldVal, Object.entries(newVal));
|
|
418
|
+
}
|
|
419
|
+
} else if (Array.isArray(newVal) || Array.isArray(oldVal)) {
|
|
420
|
+
newVal = Array.isArray(newVal) ? newVal : [];
|
|
421
|
+
oldVal = Array.isArray(oldVal) ? oldVal : [];
|
|
422
|
+
hasChanged = arrayDiff(newVal, oldVal).length > 0;
|
|
423
|
+
} else {
|
|
424
|
+
hasChanged = areDiff(newVal, oldVal);
|
|
425
|
+
}
|
|
426
|
+
if (hasChanged) {
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return hasChanged;
|
|
431
|
+
}
|
|
432
|
+
function roundBigNum(number) {
|
|
433
|
+
const digitsNum = Math.trunc(number).toString().length;
|
|
434
|
+
let roundTo = 0;
|
|
435
|
+
switch (true) {
|
|
436
|
+
case (digitsNum > 2 && digitsNum < 5):
|
|
437
|
+
roundTo = 100;
|
|
438
|
+
break;
|
|
439
|
+
case digitsNum >= 5:
|
|
440
|
+
roundTo = 1e4;
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
return roundTo ? Math.round(number / roundTo) * roundTo : number;
|
|
444
|
+
}
|
|
445
|
+
function extractNumbers(value) {
|
|
446
|
+
if (!isStr(value) && !isNum(value)) {
|
|
447
|
+
return "";
|
|
448
|
+
}
|
|
449
|
+
return value.toString().replace(/[^0-9]/g, "");
|
|
450
|
+
}
|
|
451
|
+
function removeSpaces(value) {
|
|
452
|
+
if (!isStr(value) && !isNum(value)) {
|
|
453
|
+
return "";
|
|
454
|
+
}
|
|
455
|
+
return value.toString().replace(/\s/g, "");
|
|
456
|
+
}
|
|
457
|
+
function roundUp(num, precision) {
|
|
458
|
+
const multiplier = Number("1".padEnd(precision + 1, "0"));
|
|
459
|
+
return Math.ceil(num * multiplier) / multiplier;
|
|
460
|
+
}
|
|
461
|
+
function roundDown(num, precision) {
|
|
462
|
+
const multiplier = Number("1".padEnd(precision + 1, "0"));
|
|
463
|
+
return Math.floor(num * multiplier) / multiplier;
|
|
464
|
+
}
|
|
465
|
+
function areDiff(val1, val2, strict) {
|
|
466
|
+
if (strict) {
|
|
467
|
+
return val1 !== val2;
|
|
468
|
+
}
|
|
469
|
+
if (Boolean(val1) || Boolean(val2)) {
|
|
470
|
+
return val1 != val2;
|
|
471
|
+
}
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
function flattenObject(obj, prefix = "") {
|
|
475
|
+
return Object.keys(obj).reduce((acc, k) => {
|
|
476
|
+
const pre = prefix.length ? prefix + "." : "";
|
|
477
|
+
if (isObject(obj[k]) || Array.isArray(obj[k])) {
|
|
478
|
+
Object.assign(acc, flattenObject(obj[k], pre + k));
|
|
479
|
+
} else {
|
|
480
|
+
acc[pre + k] = obj[k];
|
|
481
|
+
}
|
|
482
|
+
return acc;
|
|
483
|
+
}, {});
|
|
484
|
+
}
|
|
485
|
+
function flattenObjectAsArray(obj) {
|
|
486
|
+
const result = {};
|
|
487
|
+
const flat = flattenObject(obj);
|
|
488
|
+
Object.keys(flat).forEach((key) => {
|
|
489
|
+
const newKey = key.replaceAll(/\.([^.]+)/g, "[$1]");
|
|
490
|
+
result[newKey] = flat[key];
|
|
491
|
+
});
|
|
492
|
+
return result;
|
|
493
|
+
}
|
|
494
|
+
function parseObjectPathStr(pathStr) {
|
|
495
|
+
const pathParts = pathStr.replace(/\[/g, ".").replace(/\]/g, "").split(".");
|
|
496
|
+
if (pathParts.length > 1 && pathParts[1].includes("[")) {
|
|
497
|
+
return [pathParts[0], ...parseObjectPathStr(pathParts[1])];
|
|
498
|
+
}
|
|
499
|
+
return pathParts;
|
|
500
|
+
}
|
|
501
|
+
function formatNumber(num, fractionDigits) {
|
|
502
|
+
fractionDigits = isNullOrUndef(fractionDigits) ? 2 : fractionDigits;
|
|
503
|
+
return new Intl.NumberFormat("ru-RU", {
|
|
504
|
+
style: "decimal",
|
|
505
|
+
minimumFractionDigits: fractionDigits,
|
|
506
|
+
maximumFractionDigits: fractionDigits
|
|
507
|
+
}).format(Number(num)).replace(",", ".").replace(/\u00A0/g, " ");
|
|
508
|
+
}
|
|
509
|
+
function formatNumberWithSign(num, fractionDigits) {
|
|
510
|
+
return formatWithSign(num, formatNumber(num, fractionDigits));
|
|
511
|
+
}
|
|
512
|
+
function formatPercent(num) {
|
|
513
|
+
return formatNumber(num) + "%";
|
|
514
|
+
}
|
|
515
|
+
function formatWithSign(num, numString) {
|
|
516
|
+
numString = numString ? numString : num.toString();
|
|
517
|
+
return (Number(num) < 0 ? "" : "+") + numString;
|
|
518
|
+
}
|
|
519
|
+
function autoSizeText(el, height, minFontSize, maxFontSize = 50) {
|
|
520
|
+
let attempts = 30;
|
|
521
|
+
const resizeText = () => {
|
|
522
|
+
if (getTextHeight() === 0) {
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
while (attempts && getTextHeight() > height) {
|
|
526
|
+
attempts--;
|
|
527
|
+
reduceText();
|
|
528
|
+
}
|
|
529
|
+
while (attempts && getTextHeight() < height) {
|
|
530
|
+
attempts--;
|
|
531
|
+
enlargeText();
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
const reduceText = () => {
|
|
535
|
+
const fontSize = getNumericStyleProp("fontSize", el);
|
|
536
|
+
const newFontSize = fontSize - 1;
|
|
537
|
+
if (fontSize > 1 && newFontSize >= minFontSize) {
|
|
538
|
+
el.style.fontSize = `${newFontSize}px`;
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
const enlargeText = () => {
|
|
542
|
+
const fontSize = getNumericStyleProp("fontSize", el);
|
|
543
|
+
const newFontSize = fontSize + 1;
|
|
544
|
+
if (newFontSize <= maxFontSize) {
|
|
545
|
+
el.style.fontSize = `${newFontSize}px`;
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
const getTextHeight = () => Math.floor(
|
|
549
|
+
el.scrollHeight - getNumericStyleProp("paddingTop", el) - getNumericStyleProp("paddingBottom", el)
|
|
550
|
+
);
|
|
551
|
+
resizeText();
|
|
552
|
+
}
|
|
236
553
|
|
|
554
|
+
exports.areDiff = areDiff;
|
|
237
555
|
exports.arrayDiff = arrayDiff;
|
|
238
556
|
exports.arrayIntersect = arrayIntersect;
|
|
557
|
+
exports.autoSizeText = autoSizeText;
|
|
558
|
+
exports.capitalize = capitalize;
|
|
559
|
+
exports.countObjectInnerLength = countObjectInnerLength;
|
|
239
560
|
exports.createHtmlElement = createHtmlElement;
|
|
561
|
+
exports.createLinkElement = createLinkElement;
|
|
562
|
+
exports.createPixelElement = createPixelElement;
|
|
240
563
|
exports.createScriptElement = createScriptElement;
|
|
241
564
|
exports.createStyleElement = createStyleElement;
|
|
242
565
|
exports.createSvgElement = createSvgElement;
|
|
@@ -244,15 +567,37 @@ exports.debounce = debounce;
|
|
|
244
567
|
exports.decodeSafeURL = decodeSafeURL;
|
|
245
568
|
exports.deepCloneObject = deepCloneObject;
|
|
246
569
|
exports.encodeQueryString = encodeQueryString;
|
|
570
|
+
exports.extractNumbers = extractNumbers;
|
|
571
|
+
exports.filterObjectKeysByValues = filterObjectKeysByValues;
|
|
572
|
+
exports.fireBlurEvent = fireBlurEvent;
|
|
573
|
+
exports.fireEvent = fireEvent;
|
|
574
|
+
exports.fireInputEvent = fireInputEvent;
|
|
247
575
|
exports.flatHtmlAttributes = flatHtmlAttributes;
|
|
576
|
+
exports.flattenObject = flattenObject;
|
|
577
|
+
exports.flattenObjectAsArray = flattenObjectAsArray;
|
|
578
|
+
exports.formatNumber = formatNumber;
|
|
579
|
+
exports.formatNumberWithSign = formatNumberWithSign;
|
|
580
|
+
exports.formatPercent = formatPercent;
|
|
581
|
+
exports.formatWithSign = formatWithSign;
|
|
582
|
+
exports.getFromObject = getFromObject;
|
|
583
|
+
exports.getHtmlElement = getHtmlElement;
|
|
248
584
|
exports.getNumericStyleProp = getNumericStyleProp;
|
|
249
585
|
exports.getObjectKeys = getObjectKeys;
|
|
586
|
+
exports.getRandomInt = getRandomInt;
|
|
587
|
+
exports.getRandomItem = getRandomItem;
|
|
588
|
+
exports.getRandomStr = getRandomStr;
|
|
250
589
|
exports.getSafeURL = getSafeURL;
|
|
590
|
+
exports.getScreenSize = getScreenSize;
|
|
591
|
+
exports.hasObjectChanged = hasObjectChanged;
|
|
592
|
+
exports.hashCode = hashCode;
|
|
251
593
|
exports.injectScript = injectScript;
|
|
252
594
|
exports.injectStyleLink = injectStyleLink;
|
|
595
|
+
exports.insertCss = insertCss;
|
|
253
596
|
exports.insertHtmlElements = insertHtmlElements;
|
|
597
|
+
exports.insertJs = insertJs;
|
|
254
598
|
exports.isBool = isBool;
|
|
255
599
|
exports.isDefined = isDefined;
|
|
600
|
+
exports.isElementVisible = isElementVisible;
|
|
256
601
|
exports.isFn = isFn;
|
|
257
602
|
exports.isHTMLElement = isHTMLElement;
|
|
258
603
|
exports.isNullOrUndef = isNullOrUndef;
|
|
@@ -263,8 +608,19 @@ exports.isString = isString;
|
|
|
263
608
|
exports.isUndef = isUndef;
|
|
264
609
|
exports.objectHasProp = objectHasProp;
|
|
265
610
|
exports.objectToQueryString = objectToQueryString;
|
|
611
|
+
exports.onDOMReady = onDOMReady;
|
|
612
|
+
exports.parseObjectPathStr = parseObjectPathStr;
|
|
266
613
|
exports.parseURL = parseURL;
|
|
614
|
+
exports.removeSpaces = removeSpaces;
|
|
615
|
+
exports.roundBigNum = roundBigNum;
|
|
616
|
+
exports.roundDown = roundDown;
|
|
617
|
+
exports.roundUp = roundUp;
|
|
618
|
+
exports.sortByAlphabet = sortByAlphabet;
|
|
267
619
|
exports.stringToHtmlElements = stringToHtmlElements;
|
|
620
|
+
exports.sumArray = sumArray;
|
|
268
621
|
exports.throttle = throttle;
|
|
269
622
|
exports.toBinaryStr = toBinaryStr;
|
|
623
|
+
exports.toCamelCase = toCamelCase;
|
|
624
|
+
exports.toPercent = toPercent;
|
|
625
|
+
exports.toSnakeCase = toSnakeCase;
|
|
270
626
|
//# sourceMappingURL=index.js.map
|