@krainovsd/js-helpers 0.16.4 → 0.16.5
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/lib/cjs/index.cjs +176 -14
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/esm/api/api.js +2 -2
- package/lib/esm/api/before/oauth-before-handler.js +2 -2
- package/lib/esm/api/oauth.js +2 -2
- package/lib/esm/lib/browser/download-file.js +2 -2
- package/lib/esm/lib/lodash/get-by-path.js +66 -3
- package/lib/esm/lib/lodash/get-by-path.js.map +1 -1
- package/lib/esm/lib/lodash/set-by-path.js +107 -7
- package/lib/esm/lib/lodash/set-by-path.js.map +1 -1
- package/lib/esm/lib/utils/speed-test.js +4 -4
- package/lib/esm/lib/utils/speed-test.js.map +1 -1
- package/lib/index.d.ts +3 -3
- package/package.json +2 -4
package/lib/esm/api/api.js
CHANGED
|
@@ -9,8 +9,8 @@ import '../lib/date/is-tomorrow.js';
|
|
|
9
9
|
import '../lib/date/is-yesterday.js';
|
|
10
10
|
import { wait } from '../lib/utils/wait.js';
|
|
11
11
|
import { createURLWithQueries } from '../lib/utils/create-url-with-queries.js';
|
|
12
|
-
import 'lodash/get';
|
|
13
|
-
import 'lodash/set';
|
|
12
|
+
import '../lib/lodash/get-by-path.js';
|
|
13
|
+
import '../lib/lodash/set-by-path.js';
|
|
14
14
|
import { REQUEST_ERROR, RESPONSE_DATA_SYMBOL } from './api.constants.js';
|
|
15
15
|
import { refetchAfterOauth } from './oauth.js';
|
|
16
16
|
|
|
@@ -4,8 +4,8 @@ import 'dayjs';
|
|
|
4
4
|
import '../../lib/date/is-today.js';
|
|
5
5
|
import '../../lib/date/is-tomorrow.js';
|
|
6
6
|
import '../../lib/date/is-yesterday.js';
|
|
7
|
-
import 'lodash/get';
|
|
8
|
-
import 'lodash/set';
|
|
7
|
+
import '../../lib/lodash/get-by-path.js';
|
|
8
|
+
import '../../lib/lodash/set-by-path.js';
|
|
9
9
|
import { startWith } from '../../lib/utils/start-with.js';
|
|
10
10
|
import { OAUTH_STATE } from '../api.constants.js';
|
|
11
11
|
import { getOauthTokenFromOtherWindow } from '../oauth.js';
|
package/lib/esm/api/oauth.js
CHANGED
|
@@ -6,8 +6,8 @@ import '../lib/date/is-tomorrow.js';
|
|
|
6
6
|
import '../lib/date/is-yesterday.js';
|
|
7
7
|
import { isArray } from '../lib/typings/is-array.js';
|
|
8
8
|
import { isString } from '../lib/typings/is-string.js';
|
|
9
|
-
import 'lodash/get';
|
|
10
|
-
import 'lodash/set';
|
|
9
|
+
import '../lib/lodash/get-by-path.js';
|
|
10
|
+
import '../lib/lodash/set-by-path.js';
|
|
11
11
|
import { getQueryValues } from '../lib/browser/get-query-values.js';
|
|
12
12
|
import { OAUTH_STATE } from './api.constants.js';
|
|
13
13
|
|
|
@@ -3,8 +3,8 @@ import 'dayjs';
|
|
|
3
3
|
import '../date/is-today.js';
|
|
4
4
|
import '../date/is-tomorrow.js';
|
|
5
5
|
import '../date/is-yesterday.js';
|
|
6
|
-
import 'lodash/get';
|
|
7
|
-
import 'lodash/set';
|
|
6
|
+
import '../lodash/get-by-path.js';
|
|
7
|
+
import '../lodash/set-by-path.js';
|
|
8
8
|
import { getFileNameFromHeader } from '../utils/get-file-name-from-header.js';
|
|
9
9
|
|
|
10
10
|
function downloadFile({ data, fileName, mimeType }) {
|
|
@@ -1,12 +1,75 @@
|
|
|
1
|
-
import get from 'lodash/get';
|
|
2
1
|
import { isArray } from '../typings/is-array.js';
|
|
3
2
|
import { isString } from '../typings/is-string.js';
|
|
4
3
|
import { isObject } from '../typings/is-object.js';
|
|
5
4
|
|
|
5
|
+
/* eslint-disable max-depth */
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
8
|
+
const startArray = "[".codePointAt(0);
|
|
9
|
+
const endArray = "]".codePointAt(0);
|
|
10
|
+
const separator = ".".codePointAt(0);
|
|
11
|
+
const numbers = new Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].map((n) => n.codePointAt(0)));
|
|
6
12
|
function getByPath(data, path, defaultValue) {
|
|
7
|
-
if ((!isObject(data) && !isArray(data))
|
|
13
|
+
if (!isString(path) || (!isObject(data) && !isArray(data)))
|
|
8
14
|
return defaultValue;
|
|
9
|
-
|
|
15
|
+
let cursor = 0;
|
|
16
|
+
let pathStart = 0;
|
|
17
|
+
let arrayPathStart = -1;
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
let extractedData = data;
|
|
20
|
+
try {
|
|
21
|
+
while (cursor < path.length) {
|
|
22
|
+
const char = path.codePointAt(cursor);
|
|
23
|
+
if (char === separator) {
|
|
24
|
+
arrayPathStart = -1;
|
|
25
|
+
if (pathStart === cursor) {
|
|
26
|
+
pathStart++;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
if (extractedData == undefined || typeof extractedData !== "object")
|
|
30
|
+
return defaultValue;
|
|
31
|
+
extractedData = extractedData[path.slice(pathStart, cursor)];
|
|
32
|
+
pathStart = cursor + 1;
|
|
33
|
+
}
|
|
34
|
+
/** start array path if not started yet */
|
|
35
|
+
}
|
|
36
|
+
else if (char === startArray && arrayPathStart === -1) {
|
|
37
|
+
arrayPathStart = cursor;
|
|
38
|
+
/** end array if its started min 2 char ago */
|
|
39
|
+
}
|
|
40
|
+
else if (char === endArray && arrayPathStart !== -1 && cursor - arrayPathStart > 1) {
|
|
41
|
+
/** before array was an object path */
|
|
42
|
+
if (pathStart < arrayPathStart) {
|
|
43
|
+
if (extractedData == undefined || typeof extractedData !== "object")
|
|
44
|
+
return defaultValue;
|
|
45
|
+
extractedData = extractedData[path.slice(pathStart, arrayPathStart)];
|
|
46
|
+
}
|
|
47
|
+
const index = +path.slice(arrayPathStart + 1, cursor);
|
|
48
|
+
if (!Array.isArray(extractedData))
|
|
49
|
+
return defaultValue;
|
|
50
|
+
extractedData = extractedData[index];
|
|
51
|
+
arrayPathStart = -1;
|
|
52
|
+
pathStart = cursor + 1;
|
|
53
|
+
/** if array started but char is not number */
|
|
54
|
+
}
|
|
55
|
+
else if (arrayPathStart !== -1 && !numbers.has(char)) {
|
|
56
|
+
arrayPathStart = -1;
|
|
57
|
+
}
|
|
58
|
+
cursor++;
|
|
59
|
+
}
|
|
60
|
+
if (pathStart < path.length) {
|
|
61
|
+
if (extractedData == undefined || typeof extractedData !== "object")
|
|
62
|
+
return defaultValue;
|
|
63
|
+
extractedData = extractedData[path.slice(pathStart, cursor)];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
return defaultValue;
|
|
68
|
+
}
|
|
69
|
+
if (extractedData === undefined) {
|
|
70
|
+
return defaultValue;
|
|
71
|
+
}
|
|
72
|
+
return extractedData;
|
|
10
73
|
}
|
|
11
74
|
|
|
12
75
|
export { getByPath };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-by-path.js","sources":["../../../../src/lib/lodash/get-by-path.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"get-by-path.js","sources":["../../../../src/lib/lodash/get-by-path.ts"],"sourcesContent":["/* eslint-disable max-depth */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-non-null-assertion */\nimport { isArray, isObject, isString } from \"../typings\";\n\nconst startArray = \"[\".codePointAt(0)!;\nconst endArray = \"]\".codePointAt(0)!;\nconst separator = \".\".codePointAt(0)!;\nconst numbers = new Set(\n [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"].map((n) => n.codePointAt(0)!),\n);\nexport function getByPath<T, D>(data: unknown, path: string | undefined, defaultValue?: D): T | D {\n if (!isString(path) || (!isObject(data) && !isArray(data))) return defaultValue as D;\n\n let cursor = 0;\n let pathStart = 0;\n let arrayPathStart = -1;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let extractedData: any = data;\n\n try {\n while (cursor < path.length) {\n const char = path.codePointAt(cursor)!;\n\n if (char === separator) {\n arrayPathStart = -1;\n if (pathStart === cursor) {\n pathStart++;\n } else {\n if (extractedData == undefined || typeof extractedData !== \"object\")\n return defaultValue as D;\n extractedData = extractedData[path.slice(pathStart, cursor)];\n\n pathStart = cursor + 1;\n }\n /** start array path if not started yet */\n } else if (char === startArray && arrayPathStart === -1) {\n arrayPathStart = cursor;\n /** end array if its started min 2 char ago */\n } else if (char === endArray && arrayPathStart !== -1 && cursor - arrayPathStart > 1) {\n /** before array was an object path */\n if (pathStart < arrayPathStart) {\n if (extractedData == undefined || typeof extractedData !== \"object\")\n return defaultValue as D;\n extractedData = extractedData[path.slice(pathStart, arrayPathStart)];\n }\n\n const index = +path.slice(arrayPathStart + 1, cursor);\n if (!Array.isArray(extractedData)) return defaultValue as D;\n extractedData = extractedData[index];\n\n arrayPathStart = -1;\n pathStart = cursor + 1;\n\n /** if array started but char is not number */\n } else if (arrayPathStart !== -1 && !numbers.has(char)) {\n arrayPathStart = -1;\n }\n cursor++;\n }\n\n if (pathStart < path.length) {\n if (extractedData == undefined || typeof extractedData !== \"object\") return defaultValue as D;\n extractedData = extractedData[path.slice(pathStart, cursor)];\n }\n } catch {\n return defaultValue as D;\n }\n\n if (extractedData === undefined) {\n return defaultValue as D;\n }\n\n return extractedData as T;\n}\n"],"names":[],"mappings":";;;;AAAA;AACA;AACA;AAGA,MAAM,UAAU,GAAG,GAAG,CAAC,WAAW,CAAC,CAAC,CAAE;AACtC,MAAM,QAAQ,GAAG,GAAG,CAAC,WAAW,CAAC,CAAC,CAAE;AACpC,MAAM,SAAS,GAAG,GAAG,CAAC,WAAW,CAAC,CAAC,CAAE;AACrC,MAAM,OAAO,GAAG,IAAI,GAAG,CACrB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC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I,OAAO,aAAa,KAAK,QAAQ;AACjE,wBAAA,OAAO,YAAiB;AAC1B,oBAAA,aAAa,GAAG,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;;AAGtE,gBAAA,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,cAAc,GAAG,CAAC,EAAE,MAAM,CAAC;AACrD,gBAAA,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC;AAAE,oBAAA,OAAO,YAAiB;AAC3D,gBAAA,aAAa,GAAG,aAAa,CAAC,KAAK,CAAC;gBAEpC,cAAc,GAAG,CAAC,CAAC;AACnB,gBAAA,SAAS,GAAG,MAAM,GAAG,CAAC;;;AAGjB,iBAAA,IAAI,cAAc,KAAK,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;gBACtD,cAAc,GAAG,CAAC,CAAC;;AAErB,YAAA,MAAM,EAAE;;AAGV,QAAA,IAAI,SAAS,GAAG,IAAI,CAAC,MAAM,EAAE;AAC3B,YAAA,IAAI,aAAa,IAAI,SAAS,IAAI,OAAO,aAAa,KAAK,QAAQ;AAAE,gBAAA,OAAO,YAAiB;AAC7F,YAAA,aAAa,GAAG,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;;;AAE9D,IAAA,MAAM;AACN,QAAA,OAAO,YAAiB;;AAG1B,IAAA,IAAI,aAAa,KAAK,SAAS,EAAE;AAC/B,QAAA,OAAO,YAAiB;;AAG1B,IAAA,OAAO,aAAkB;AAC3B;;;;"}
|
|
@@ -1,16 +1,116 @@
|
|
|
1
|
-
import set from 'lodash/set';
|
|
2
1
|
import { isArray } from '../typings/is-array.js';
|
|
2
|
+
import { isString } from '../typings/is-string.js';
|
|
3
3
|
import { isObject } from '../typings/is-object.js';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
/* eslint-disable max-depth */
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
8
|
+
const startArray = "[".codePointAt(0);
|
|
9
|
+
const endArray = "]".codePointAt(0);
|
|
10
|
+
const separator = ".".codePointAt(0);
|
|
11
|
+
const numbers = new Set(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].map((n) => n.codePointAt(0)));
|
|
12
|
+
function setByPath(data, path, settableValue) {
|
|
13
|
+
if (!isString(path) || (!isObject(data) && !isArray(data)))
|
|
14
|
+
return false;
|
|
15
|
+
let cursor = 0;
|
|
16
|
+
let pathStart = 0;
|
|
17
|
+
let arrayPathStart = -1;
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
19
|
+
let extractedData = data;
|
|
20
|
+
let prevPath;
|
|
6
21
|
try {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
22
|
+
while (cursor < path.length) {
|
|
23
|
+
const char = path.codePointAt(cursor);
|
|
24
|
+
/** get */
|
|
25
|
+
if (char === separator) {
|
|
26
|
+
arrayPathStart = -1;
|
|
27
|
+
if (pathStart === cursor) {
|
|
28
|
+
pathStart++;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
if (prevPath) {
|
|
32
|
+
const object = {};
|
|
33
|
+
extractedData[prevPath] = object;
|
|
34
|
+
extractedData = object;
|
|
35
|
+
}
|
|
36
|
+
if (cursor === path.length - 1) {
|
|
37
|
+
extractedData[path.slice(pathStart, cursor)] = settableValue;
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const currentPath = path.slice(pathStart, cursor);
|
|
41
|
+
const extractedValue = extractedData[currentPath];
|
|
42
|
+
if (extractedValue == undefined) {
|
|
43
|
+
prevPath = currentPath;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
extractedData = extractedValue;
|
|
47
|
+
}
|
|
48
|
+
pathStart = cursor + 1;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** start array path if not started yet */
|
|
52
|
+
}
|
|
53
|
+
else if (char === startArray && arrayPathStart === -1) {
|
|
54
|
+
arrayPathStart = cursor;
|
|
55
|
+
/** end array if its started min 2 char ago */
|
|
56
|
+
}
|
|
57
|
+
else if (char === endArray && arrayPathStart !== -1 && cursor - arrayPathStart > 1) {
|
|
58
|
+
/** before array was an object path */
|
|
59
|
+
if (pathStart < arrayPathStart) {
|
|
60
|
+
if (prevPath) {
|
|
61
|
+
const object = {};
|
|
62
|
+
extractedData[prevPath] = object;
|
|
63
|
+
extractedData = object;
|
|
64
|
+
}
|
|
65
|
+
const currentPath = path.slice(pathStart, arrayPathStart);
|
|
66
|
+
const extractedValue = extractedData[currentPath];
|
|
67
|
+
if (extractedValue == undefined) {
|
|
68
|
+
prevPath = currentPath;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
extractedData = extractedValue;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (prevPath) {
|
|
75
|
+
const array = [];
|
|
76
|
+
extractedData[prevPath] = array;
|
|
77
|
+
extractedData = array;
|
|
78
|
+
}
|
|
79
|
+
const index = +path.slice(arrayPathStart + 1, cursor);
|
|
80
|
+
if (cursor === path.length - 1) {
|
|
81
|
+
extractedData[index] = settableValue;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const extractedValue = extractedData[index];
|
|
85
|
+
if (extractedValue == undefined) {
|
|
86
|
+
prevPath = index;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
extractedData = extractedValue;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
arrayPathStart = -1;
|
|
93
|
+
pathStart = cursor + 1;
|
|
94
|
+
/** if array started but char is not number */
|
|
95
|
+
}
|
|
96
|
+
else if (arrayPathStart !== -1 && !numbers.has(char)) {
|
|
97
|
+
arrayPathStart = -1;
|
|
98
|
+
}
|
|
99
|
+
cursor++;
|
|
100
|
+
}
|
|
101
|
+
if (pathStart < path.length) {
|
|
102
|
+
if (prevPath) {
|
|
103
|
+
const object = {};
|
|
104
|
+
extractedData[prevPath] = object;
|
|
105
|
+
extractedData = object;
|
|
106
|
+
}
|
|
107
|
+
extractedData[path.slice(pathStart, cursor)] = settableValue;
|
|
108
|
+
}
|
|
10
109
|
}
|
|
11
|
-
catch
|
|
12
|
-
|
|
110
|
+
catch {
|
|
111
|
+
return false;
|
|
13
112
|
}
|
|
113
|
+
return true;
|
|
14
114
|
}
|
|
15
115
|
|
|
16
116
|
export { setByPath };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"set-by-path.js","sources":["../../../../src/lib/lodash/set-by-path.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"set-by-path.js","sources":["../../../../src/lib/lodash/set-by-path.ts"],"sourcesContent":["/* eslint-disable max-depth */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-non-null-assertion */\nimport { isArray, isObject } from \"../typings\";\nimport { isString } from \"../typings\";\n\nconst startArray = \"[\".codePointAt(0)!;\nconst endArray = \"]\".codePointAt(0)!;\nconst separator = \".\".codePointAt(0)!;\nconst numbers = new Set(\n [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"].map((n) => n.codePointAt(0)!),\n);\nexport function setByPath(data: unknown, path: string | undefined, settableValue: unknown) {\n if (!isString(path) || (!isObject(data) && !isArray(data))) return false;\n\n let cursor = 0;\n let pathStart = 0;\n let arrayPathStart = -1;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n let extractedData: any = data;\n let prevPath: string | number | undefined;\n\n try {\n while (cursor < path.length) {\n const char = path.codePointAt(cursor)!;\n /** get */\n if (char === separator) {\n arrayPathStart = -1;\n if (pathStart === cursor) {\n pathStart++;\n } else {\n if (prevPath) {\n const object = {};\n extractedData[prevPath] = object;\n extractedData = object;\n }\n\n if (cursor === path.length - 1) {\n extractedData[path.slice(pathStart, cursor)] = settableValue;\n } else {\n const currentPath = path.slice(pathStart, cursor);\n const extractedValue = extractedData[currentPath];\n if (extractedValue == undefined) {\n prevPath = currentPath;\n } else {\n extractedData = extractedValue;\n }\n pathStart = cursor + 1;\n }\n }\n\n /** start array path if not started yet */\n } else if (char === startArray && arrayPathStart === -1) {\n arrayPathStart = cursor;\n /** end array if its started min 2 char ago */\n } else if (char === endArray && arrayPathStart !== -1 && cursor - arrayPathStart > 1) {\n /** before array was an object path */\n if (pathStart < arrayPathStart) {\n if (prevPath) {\n const object = {};\n extractedData[prevPath] = object;\n extractedData = object;\n }\n const currentPath = path.slice(pathStart, arrayPathStart);\n const extractedValue = extractedData[currentPath];\n if (extractedValue == undefined) {\n prevPath = currentPath;\n } else {\n extractedData = extractedValue;\n }\n }\n\n if (prevPath) {\n const array: unknown[] = [];\n extractedData[prevPath] = array;\n extractedData = array;\n }\n const index = +path.slice(arrayPathStart + 1, cursor);\n if (cursor === path.length - 1) {\n extractedData[index] = settableValue;\n } else {\n const extractedValue = extractedData[index];\n if (extractedValue == undefined) {\n prevPath = index;\n } else {\n extractedData = extractedValue;\n }\n }\n arrayPathStart = -1;\n pathStart = cursor + 1;\n\n /** if array started but char is not number */\n } else if (arrayPathStart !== -1 && !numbers.has(char)) {\n arrayPathStart = -1;\n }\n cursor++;\n }\n\n if (pathStart < path.length) {\n if (prevPath) {\n const object = {};\n extractedData[prevPath] = object;\n extractedData = object;\n }\n extractedData[path.slice(pathStart, cursor)] = settableValue;\n }\n } catch {\n return false;\n }\n\n return true;\n}\n"],"names":[],"mappings":";;;;AAAA;AACA;AACA;AAIA,MAAM,UAAU,GAAG,GAAG,CAAC,WAAW,CAAC,CAAC,CAAE;AACtC,MAAM,QAAQ,GAAG,GAAG,CAAC,WAAW,CAAC,CAAC,CAAE;AACpC,MAAM,SAAS,GAAG,GAAG,CAAC,WAAW,CAAC,CAAC,CAAE;AACrC,MAAM,OAAO,GAAG,IAAI,GAAG,CACrB,CAAC,GAAG,E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3C,oBAAA,IAAI,cAAc,IAAI,SAAS,EAAE;wBAC/B,QAAQ,GAAG,KAAK;;yBACX;wBACL,aAAa,GAAG,cAAc;;;gBAGlC,cAAc,GAAG,CAAC,CAAC;AACnB,gBAAA,SAAS,GAAG,MAAM,GAAG,CAAC;;;AAGjB,iBAAA,IAAI,cAAc,KAAK,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE;gBACtD,cAAc,GAAG,CAAC,CAAC;;AAErB,YAAA,MAAM,EAAE;;AAGV,QAAA,IAAI,SAAS,GAAG,IAAI,CAAC,MAAM,EAAE;YAC3B,IAAI,QAAQ,EAAE;gBACZ,MAAM,MAAM,GAAG,EAAE;AACjB,gBAAA,aAAa,CAAC,QAAQ,CAAC,GAAG,MAAM;gBAChC,aAAa,GAAG,MAAM;;AAExB,YAAA,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,GAAG,aAAa;;;AAE9D,IAAA,MAAM;AACN,QAAA,OAAO,KAAK;;AAGd,IAAA,OAAO,IAAI;AACb;;;;"}
|
|
@@ -4,11 +4,11 @@ function speedTest(cb, opts) {
|
|
|
4
4
|
if (type === "performance") {
|
|
5
5
|
const start = performance.now();
|
|
6
6
|
if (iterations <= 1) {
|
|
7
|
-
result = cb();
|
|
7
|
+
result = cb(0);
|
|
8
8
|
}
|
|
9
9
|
else {
|
|
10
10
|
for (let i = 0; i < iterations; i++) {
|
|
11
|
-
result = cb();
|
|
11
|
+
result = cb(i);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
const end = performance.now();
|
|
@@ -19,11 +19,11 @@ function speedTest(cb, opts) {
|
|
|
19
19
|
// eslint-disable-next-line no-console
|
|
20
20
|
console.time(name);
|
|
21
21
|
if (iterations <= 1) {
|
|
22
|
-
result = cb();
|
|
22
|
+
result = cb(0);
|
|
23
23
|
}
|
|
24
24
|
else {
|
|
25
25
|
for (let i = 0; i < iterations; i++) {
|
|
26
|
-
result = cb();
|
|
26
|
+
result = cb(i);
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
// eslint-disable-next-line no-console
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"speed-test.js","sources":["../../../../src/lib/utils/speed-test.ts"],"sourcesContent":["type SpeedTestOptions = {\n name: string;\n iterations?: number;\n type?: \"time\" | \"performance\";\n};\n\nexport function speedTest<T>(cb: () => T, opts: SpeedTestOptions): T {\n const { name, iterations = 10000, type = \"performance\" } = opts;\n let result: T | undefined;\n\n if (type === \"performance\") {\n const start = performance.now();\n\n if (iterations <= 1) {\n result = cb();\n } else {\n for (let i = 0; i < iterations; i++) {\n result = cb();\n }\n }\n\n const end = performance.now();\n // eslint-disable-next-line no-console\n console.log(`${name}: `, end - start);\n } else {\n // eslint-disable-next-line no-console\n console.time(name);\n\n if (iterations <= 1) {\n result = cb();\n } else {\n for (let i = 0; i < iterations; i++) {\n result = cb();\n }\n }\n\n // eslint-disable-next-line no-console\n console.timeEnd(name);\n }\n\n return result as T;\n}\n"],"names":[],"mappings":"AAMgB,SAAA,SAAS,CAAI,
|
|
1
|
+
{"version":3,"file":"speed-test.js","sources":["../../../../src/lib/utils/speed-test.ts"],"sourcesContent":["type SpeedTestOptions = {\n name: string;\n iterations?: number;\n type?: \"time\" | \"performance\";\n};\n\nexport function speedTest<T>(cb: (i: number) => T, opts: SpeedTestOptions): T {\n const { name, iterations = 10000, type = \"performance\" } = opts;\n let result: T | undefined;\n\n if (type === \"performance\") {\n const start = performance.now();\n\n if (iterations <= 1) {\n result = cb(0);\n } else {\n for (let i = 0; i < iterations; i++) {\n result = cb(i);\n }\n }\n\n const end = performance.now();\n // eslint-disable-next-line no-console\n console.log(`${name}: `, end - start);\n } else {\n // eslint-disable-next-line no-console\n console.time(name);\n\n if (iterations <= 1) {\n result = cb(0);\n } else {\n for (let i = 0; i < iterations; i++) {\n result = cb(i);\n }\n }\n\n // eslint-disable-next-line no-console\n console.timeEnd(name);\n }\n\n return result as T;\n}\n"],"names":[],"mappings":"AAMgB,SAAA,SAAS,CAAI,EAAoB,EAAE,IAAsB,EAAA;AACvE,IAAA,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,KAAK,EAAE,IAAI,GAAG,aAAa,EAAE,GAAG,IAAI;AAC/D,IAAA,IAAI,MAAqB;AAEzB,IAAA,IAAI,IAAI,KAAK,aAAa,EAAE;AAC1B,QAAA,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;AAE/B,QAAA,IAAI,UAAU,IAAI,CAAC,EAAE;AACnB,YAAA,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC;;aACT;AACL,YAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,EAAE,CAAC,EAAE,EAAE;AACnC,gBAAA,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC;;;AAIlB,QAAA,MAAM,GAAG,GAAG,WAAW,CAAC,GAAG,EAAE;;QAE7B,OAAO,CAAC,GAAG,CAAC,CAAG,EAAA,IAAI,CAAI,EAAA,CAAA,EAAE,GAAG,GAAG,KAAK,CAAC;;SAChC;;AAEL,QAAA,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC;AAElB,QAAA,IAAI,UAAU,IAAI,CAAC,EAAE;AACnB,YAAA,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC;;aACT;AACL,YAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,UAAU,EAAE,CAAC,EAAE,EAAE;AACnC,gBAAA,MAAM,GAAG,EAAE,CAAC,CAAC,CAAC;;;;AAKlB,QAAA,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC;;AAGvB,IAAA,OAAO,MAAW;AACpB;;;;"}
|
package/lib/index.d.ts
CHANGED
|
@@ -389,7 +389,7 @@ type SpeedTestOptions = {
|
|
|
389
389
|
iterations?: number;
|
|
390
390
|
type?: "time" | "performance";
|
|
391
391
|
};
|
|
392
|
-
declare function speedTest<T>(cb: () => T, opts: SpeedTestOptions): T;
|
|
392
|
+
declare function speedTest<T>(cb: (i: number) => T, opts: SpeedTestOptions): T;
|
|
393
393
|
|
|
394
394
|
declare function createGlobalId(): number;
|
|
395
395
|
|
|
@@ -431,9 +431,9 @@ declare function isDate(value: unknown): value is Date;
|
|
|
431
431
|
|
|
432
432
|
declare function isPrimitive(value: unknown): value is string | number | boolean | null | undefined;
|
|
433
433
|
|
|
434
|
-
declare function getByPath<T, D
|
|
434
|
+
declare function getByPath<T, D>(data: unknown, path: string | undefined, defaultValue?: D): T | D;
|
|
435
435
|
|
|
436
|
-
declare function setByPath(data:
|
|
436
|
+
declare function setByPath(data: unknown, path: string | undefined, settableValue: unknown): boolean;
|
|
437
437
|
|
|
438
438
|
declare function cloneDeep<T>(value: T): T;
|
|
439
439
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@krainovsd/js-helpers",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.5",
|
|
4
4
|
"description": "Krainov helpers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "KrainovSD <denislosev48@gmail.com>",
|
|
@@ -45,8 +45,7 @@
|
|
|
45
45
|
"test-coverage": "jest --coverage"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"dayjs": "1.11.13"
|
|
49
|
-
"lodash": "4.17.21"
|
|
48
|
+
"dayjs": "^1.11.13"
|
|
50
49
|
},
|
|
51
50
|
"devDependencies": {
|
|
52
51
|
"@krainovsd/presets": "0.3.6",
|
|
@@ -59,7 +58,6 @@
|
|
|
59
58
|
"@swc/core": "1.11.5",
|
|
60
59
|
"@swc/jest": "0.2.37",
|
|
61
60
|
"@types/jest": "29.5.14",
|
|
62
|
-
"@types/lodash": "4.17.14",
|
|
63
61
|
"@types/node": "22.13.8",
|
|
64
62
|
"jest": "29.7.0",
|
|
65
63
|
"node-fetch": "2.7.0",
|