@vpmedia/simplify 1.61.0 โ 1.62.0
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/CHANGELOG.md +10 -0
- package/package.json +1 -1
- package/src/index.js +1 -1
- package/src/util/async.test.js +1 -1
- package/src/util/error.js +7 -0
- package/src/util/error.test.js +30 -17
- package/src/util/state.test.js +43 -41
- package/src/util/string.test.js +47 -51
- package/types/index.d.ts +1 -1
- package/types/util/error.d.ts +1 -0
- package/types/util/error.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -20,7 +20,7 @@ export { typeChecker } from './typecheck/TypeChecker.js';
|
|
|
20
20
|
export { TypeCheckError } from './typecheck/TypeCheckError.js';
|
|
21
21
|
export { typeCheck, typeCheckArray, typeCheckEnum } from './typecheck/util.js';
|
|
22
22
|
export { delayPromise, loadJSON } from './util/async.js';
|
|
23
|
-
export { getErrorDetails } from './util/error.js';
|
|
23
|
+
export { getErrorDetails, getTypedError } from './util/error.js';
|
|
24
24
|
export { FetchError, fetchRetry, HTTP_0_ANY } from './util/fetch.js';
|
|
25
25
|
export {
|
|
26
26
|
addFloat,
|
package/src/util/async.test.js
CHANGED
|
@@ -10,7 +10,7 @@ describe('delayPromise', () => {
|
|
|
10
10
|
expect(end - start).toBeGreaterThanOrEqual(9);
|
|
11
11
|
});
|
|
12
12
|
|
|
13
|
-
test('
|
|
13
|
+
test('delayPromise with zero delay', async () => {
|
|
14
14
|
const start = Date.now();
|
|
15
15
|
await delayPromise(0);
|
|
16
16
|
const end = Date.now();
|
package/src/util/error.js
CHANGED
|
@@ -22,3 +22,10 @@ export const getErrorDetails = (error, excludes = ['stack']) => {
|
|
|
22
22
|
}
|
|
23
23
|
return errorDetails;
|
|
24
24
|
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get typed error from an unknown type.
|
|
28
|
+
* @param {unknown} error - The error to cast.
|
|
29
|
+
* @returns {Error} The typed error object.
|
|
30
|
+
*/
|
|
31
|
+
export const getTypedError = (error) => (error instanceof Error ? error : new Error(String(error)));
|
package/src/util/error.test.js
CHANGED
|
@@ -1,20 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
/* eslint-disable unicorn/no-useless-undefined */
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
import { getErrorDetails, getTypedError } from './error.js';
|
|
4
|
+
|
|
5
|
+
describe('error', () => {
|
|
6
|
+
test('getErrorDetails', () => {
|
|
7
|
+
const error = new Error('Test error', { cause: 'Test cause' });
|
|
8
|
+
const errorDetails = getErrorDetails(error);
|
|
9
|
+
expect(errorDetails.type).toBe('Error');
|
|
10
|
+
expect(errorDetails.message).toBe('Test error');
|
|
11
|
+
expect(errorDetails.cause).toBe('Test cause');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('getErrorDetails with Error cause', () => {
|
|
15
|
+
const error = new SyntaxError('Test error', { cause: new TypeError('Cause error') });
|
|
16
|
+
const errorDetails = getErrorDetails(error);
|
|
17
|
+
expect(errorDetails.type).toBe('SyntaxError');
|
|
18
|
+
expect(errorDetails.message).toBe('Test error');
|
|
19
|
+
expect(errorDetails.cause instanceof Error).toBe(true);
|
|
20
|
+
if (errorDetails.cause instanceof Error) {
|
|
21
|
+
expect(errorDetails.cause.message).toBe('Cause error');
|
|
22
|
+
}
|
|
23
|
+
});
|
|
10
24
|
|
|
11
|
-
test('
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
25
|
+
test('getTypedError', () => {
|
|
26
|
+
expect(getTypedError(new Error('Error message')).message).toBe('Error message');
|
|
27
|
+
expect(getTypedError('Error message').message).toBe('Error message');
|
|
28
|
+
expect(getTypedError(1).message).toBe('1');
|
|
29
|
+
expect(getTypedError(true).message).toBe('true');
|
|
30
|
+
expect(getTypedError(null).message).toBe('null');
|
|
31
|
+
expect(getTypedError(undefined).message).toBe('undefined');
|
|
32
|
+
});
|
|
20
33
|
});
|
package/src/util/state.test.js
CHANGED
|
@@ -1,45 +1,47 @@
|
|
|
1
1
|
import { serverDataToState } from './state.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
3
|
+
describe('state', () => {
|
|
4
|
+
test('serverDataToState() recursive', () => {
|
|
5
|
+
const state = serverDataToState(
|
|
6
|
+
{
|
|
7
|
+
my_array: [{ key_a: 'value1' }],
|
|
8
|
+
my_data: { key_a: 'value1' },
|
|
9
|
+
my_list: [1, 2, 3],
|
|
10
|
+
my_null: null,
|
|
11
|
+
my_number: 1000,
|
|
12
|
+
my_string: 'a',
|
|
13
|
+
my_var: 'test',
|
|
14
|
+
},
|
|
15
|
+
true
|
|
16
|
+
);
|
|
17
|
+
expect(state.myArray[0].keyA).toBe('value1');
|
|
18
|
+
expect(state.myData.keyA).toBe('value1');
|
|
19
|
+
expect(state.myList[0]).toBe(1);
|
|
20
|
+
expect(state.myNull).toBe(null);
|
|
21
|
+
expect(state.myNumber).toBe(1000);
|
|
22
|
+
expect(state.myString).toBe('a');
|
|
23
|
+
expect(state.myVar).toBe('test');
|
|
24
|
+
});
|
|
24
25
|
|
|
25
|
-
test('serverDataToState() non-recursive', () => {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
26
|
+
test('serverDataToState() non-recursive', () => {
|
|
27
|
+
const state = serverDataToState(
|
|
28
|
+
{
|
|
29
|
+
my_array: [{ key_a: 'value1' }],
|
|
30
|
+
my_data: { key_a: 'value1' },
|
|
31
|
+
my_list: [1, 2, 3],
|
|
32
|
+
my_null: null,
|
|
33
|
+
my_number: 1000,
|
|
34
|
+
my_string: 'a',
|
|
35
|
+
my_var: 'test',
|
|
36
|
+
},
|
|
37
|
+
false
|
|
38
|
+
);
|
|
39
|
+
expect(state.myArray[0].key_a).toBe('value1');
|
|
40
|
+
expect(state.myData.key_a).toBe('value1');
|
|
41
|
+
expect(state.myList[0]).toBe(1);
|
|
42
|
+
expect(state.myNull).toBe(null);
|
|
43
|
+
expect(state.myNumber).toBe(1000);
|
|
44
|
+
expect(state.myString).toBe('a');
|
|
45
|
+
expect(state.myVar).toBe('test');
|
|
46
|
+
});
|
|
45
47
|
});
|
package/src/util/string.test.js
CHANGED
|
@@ -1,70 +1,66 @@
|
|
|
1
1
|
/* eslint-disable unicorn/no-useless-undefined */
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
underscoreToCamelCase,
|
|
5
|
+
capitalize,
|
|
6
|
+
addLeadingZero,
|
|
7
|
+
getTypeFromValue,
|
|
8
|
+
getDisplayValue,
|
|
9
|
+
saveAsFile,
|
|
10
|
+
} from './string.js';
|
|
4
11
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
describe('string', () => {
|
|
13
|
+
test('addLeadingZero', () => {
|
|
14
|
+
expect(addLeadingZero(1)).toBe('01');
|
|
15
|
+
expect(addLeadingZero('1')).toBe('01');
|
|
16
|
+
expect(addLeadingZero(1, 3)).toBe('001');
|
|
17
|
+
expect(addLeadingZero('21')).toBe('21');
|
|
18
|
+
expect(addLeadingZero(21)).toBe('21');
|
|
19
|
+
expect(addLeadingZero(null)).toBe(null);
|
|
20
|
+
expect(addLeadingZero(undefined)).toBe(null);
|
|
21
|
+
});
|
|
14
22
|
|
|
15
|
-
|
|
16
|
-
test('Capitalizes first letter of string', () => {
|
|
23
|
+
test('capitalize', () => {
|
|
17
24
|
expect(capitalize('test')).toBe('Test');
|
|
18
25
|
expect(capitalize('TEST')).toBe('Test');
|
|
19
26
|
expect(capitalize('tEST')).toBe('Test');
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test('Handles null input', () => {
|
|
23
27
|
expect(capitalize(null)).toBe(null);
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('Handles empty string', () => {
|
|
27
28
|
expect(capitalize('')).toBe('');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('Handles empty string with whitespace', () => {
|
|
31
29
|
expect(capitalize(' ')).toBe(' ');
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('Handles single character', () => {
|
|
35
30
|
expect(capitalize('a')).toBe('A');
|
|
36
31
|
expect(capitalize('A')).toBe('A');
|
|
32
|
+
expect(capitalize('test123')).toBe('Test123');
|
|
37
33
|
});
|
|
38
34
|
|
|
39
|
-
test('
|
|
40
|
-
expect(
|
|
35
|
+
test('underscoreToCamelCase', () => {
|
|
36
|
+
expect(underscoreToCamelCase('test')).toBe('test');
|
|
37
|
+
expect(underscoreToCamelCase('test_variable')).toBe('testVariable');
|
|
38
|
+
expect(underscoreToCamelCase('test_variable_name')).toBe('testVariableName');
|
|
41
39
|
});
|
|
42
|
-
});
|
|
43
40
|
|
|
44
|
-
test('
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
});
|
|
41
|
+
test('getTypeFromValue', () => {
|
|
42
|
+
expect(getTypeFromValue('test')).toBe('string');
|
|
43
|
+
expect(getTypeFromValue(() => null)).toBe('function');
|
|
44
|
+
expect(getTypeFromValue([])).toBe('array');
|
|
45
|
+
expect(getTypeFromValue({})).toBe('object');
|
|
46
|
+
expect(getTypeFromValue(new Date())).toBe('date');
|
|
47
|
+
expect(getTypeFromValue(null)).toBe('null');
|
|
48
|
+
expect(getTypeFromValue(true)).toBe('boolean');
|
|
49
|
+
expect(getTypeFromValue(undefined)).toBe('undefined');
|
|
50
|
+
});
|
|
49
51
|
|
|
50
|
-
test('
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
});
|
|
52
|
+
test('getDisplayValue', () => {
|
|
53
|
+
expect(getDisplayValue('test')).toBe('"test"');
|
|
54
|
+
expect(getDisplayValue(() => null)).toBe('() => null');
|
|
55
|
+
expect(getDisplayValue([])).toBe('[]');
|
|
56
|
+
expect(getDisplayValue({})).toBe('{}');
|
|
57
|
+
expect(getDisplayValue(new Date())).not.toBe(null);
|
|
58
|
+
expect(getDisplayValue(null)).toBe('null');
|
|
59
|
+
expect(getDisplayValue(true)).toBe('true');
|
|
60
|
+
expect(getDisplayValue(undefined)).toBe('undefined');
|
|
61
|
+
});
|
|
60
62
|
|
|
61
|
-
test('
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
expect(getDisplayValue([])).toBe('[]');
|
|
65
|
-
expect(getDisplayValue({})).toBe('{}');
|
|
66
|
-
expect(getDisplayValue(new Date())).not.toBe(null);
|
|
67
|
-
expect(getDisplayValue(null)).toBe('null');
|
|
68
|
-
expect(getDisplayValue(true)).toBe('true');
|
|
69
|
-
expect(getDisplayValue(undefined)).toBe('undefined');
|
|
63
|
+
test('saveAsFile', () => {
|
|
64
|
+
expect(() => saveAsFile('test.txt', 'test content')).not.toThrowError(Error);
|
|
65
|
+
});
|
|
70
66
|
});
|
package/types/index.d.ts
CHANGED
|
@@ -10,12 +10,12 @@ export { OpenTelemetryLogHandler } from "./logging/OpenTelemetryLogHandler.js";
|
|
|
10
10
|
export { SentryLogHandler } from "./logging/SentryLogHandler.js";
|
|
11
11
|
export { typeChecker } from "./typecheck/TypeChecker.js";
|
|
12
12
|
export { TypeCheckError } from "./typecheck/TypeCheckError.js";
|
|
13
|
-
export { getErrorDetails } from "./util/error.js";
|
|
14
13
|
export { serverDataToState } from "./util/state.js";
|
|
15
14
|
export { formatLogMessage, getLogLevelName } from "./logging/util.js";
|
|
16
15
|
export { addPageLifecycleCallback, getDocumentState, getPageLifecycleEventEmitter, getPageLifecycleState, initPageLifecycle, isPageLifecycleInitialized } from "./pagelifecycle/util.js";
|
|
17
16
|
export { typeCheck, typeCheckArray, typeCheckEnum } from "./typecheck/util.js";
|
|
18
17
|
export { delayPromise, loadJSON } from "./util/async.js";
|
|
18
|
+
export { getErrorDetails, getTypedError } from "./util/error.js";
|
|
19
19
|
export { FetchError, fetchRetry, HTTP_0_ANY } from "./util/fetch.js";
|
|
20
20
|
export { addFloat, deg2rad, fixFloat, fixFloatPrecision, getRandomInt, isEqual, isGreater, isGreaterOrEqual, isInRange, isLess, isLessOrEqual, subFloat } from "./util/number.js";
|
|
21
21
|
export { purgeObject, deepMerge, getObjArrayPropSum, getObjValueByPath, setObjValueByPath } from "./util/object.js";
|
package/types/util/error.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/util/error.js"],"names":[],"mappings":"AAMO,uCAJI,KAAK,aACL,MAAM,EAAE,GACN,MAAM,CAmBlB"}
|
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../src/util/error.js"],"names":[],"mappings":"AAMO,uCAJI,KAAK,aACL,MAAM,EAAE,GACN,MAAM,CAmBlB;AAOM,qCAHI,OAAO,GACL,KAAK,CAEiF"}
|