@vpmedia/simplify 1.7.1 → 1.9.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/README.md +1 -1
- package/eslint.config.js +0 -4
- package/package.json +6 -6
- package/src/index.js +2 -0
- package/src/logging/Logger.js +17 -1
- package/src/util/fixFloatPrecision.js +8 -0
- package/src/util/fixFloatPrecision.test.js +5 -0
- package/src/util/purgeObject.test.js +8 -0
- package/src/util/safeFloat.js +31 -0
- package/src/util/safeFloat.test.js +13 -0
- package/types/index.d.ts +2 -0
- package/types/util/cloneObject.d.ts +7 -0
- package/types/util/cloneObject.d.ts.map +1 -0
- package/types/util/fixFloatPrecision.d.ts +7 -0
- package/types/util/fixFloatPrecision.d.ts.map +1 -0
- package/types/util/safeFloat.d.ts +22 -0
- package/types/util/safeFloat.d.ts.map +1 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @vpmedia/simplify
|
|
2
2
|
|
|
3
|
-
[](https://badge.fury.io/js/@vpmedia%2Fsimplify)
|
|
4
4
|
[](https://github.com/vpmedia/simplify/actions/workflows/ci.yml)
|
|
5
5
|
|
|
6
6
|
@vpmedia/simplify TBD
|
package/eslint.config.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import js from '@eslint/js';
|
|
2
|
-
// import importPlugin from 'eslint-plugin-import';
|
|
3
2
|
import jsdocPlugin from 'eslint-plugin-jsdoc';
|
|
4
3
|
import unicornPlugin from 'eslint-plugin-unicorn';
|
|
5
4
|
import globals from 'globals';
|
|
@@ -37,7 +36,6 @@ export default [
|
|
|
37
36
|
},
|
|
38
37
|
files: ['**/*.{js,jsx,mjs,cjs,ts,tsx}'],
|
|
39
38
|
plugins: {
|
|
40
|
-
// import: importPlugin,
|
|
41
39
|
jsdoc: jsdocPlugin,
|
|
42
40
|
unicorn: unicornPlugin,
|
|
43
41
|
},
|
|
@@ -52,7 +50,6 @@ export default [
|
|
|
52
50
|
rules: {
|
|
53
51
|
...js.configs.recommended.rules,
|
|
54
52
|
...jsdocPlugin.configs['flat/recommended'].rules,
|
|
55
|
-
// ...importPlugin.configs.recommended.rules,
|
|
56
53
|
...unicornPlugin.configs['flat/recommended'].rules,
|
|
57
54
|
'unicorn/filename-case': 'off',
|
|
58
55
|
'unicorn/no-null': 'off',
|
|
@@ -62,7 +59,6 @@ export default [
|
|
|
62
59
|
'no-unused-vars': 'off',
|
|
63
60
|
'prefer-arrow-callback': 'error',
|
|
64
61
|
'prefer-template': 'error',
|
|
65
|
-
// 'import/extensions': ['warn', 'always'],
|
|
66
62
|
'jsdoc/require-jsdoc': [
|
|
67
63
|
'error',
|
|
68
64
|
{
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vpmedia/simplify",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0",
|
|
4
4
|
"description": "@vpmedia/simplify",
|
|
5
5
|
"author": "Andras Csizmadia <andras@vpmedia.hu> (www.vpmedia.hu)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -19,19 +19,19 @@
|
|
|
19
19
|
"types": "./types/index.d.ts",
|
|
20
20
|
"type": "module",
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@babel/preset-env": "^7.25.
|
|
23
|
-
"@eslint/js": "^9.
|
|
22
|
+
"@babel/preset-env": "^7.25.8",
|
|
23
|
+
"@eslint/js": "^9.12.0",
|
|
24
24
|
"@jest/globals": "^29.7.0",
|
|
25
25
|
"@types/jest": "^29.5.13",
|
|
26
26
|
"eslint": "^9.12.0",
|
|
27
|
-
"eslint-plugin-jsdoc": "^50.3.
|
|
27
|
+
"eslint-plugin-jsdoc": "^50.3.2",
|
|
28
28
|
"eslint-plugin-unicorn": "^56.0.0",
|
|
29
|
-
"globals": "^15.
|
|
29
|
+
"globals": "^15.11.0",
|
|
30
30
|
"jest": "^29.7.0",
|
|
31
31
|
"jest-environment-jsdom": "^29.7.0",
|
|
32
32
|
"lefthook": "^1.7.18",
|
|
33
33
|
"prettier": "^3.3.3",
|
|
34
|
-
"typescript": "^5.6.
|
|
34
|
+
"typescript": "^5.6.3"
|
|
35
35
|
},
|
|
36
36
|
"scripts": {
|
|
37
37
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests",
|
package/src/index.js
CHANGED
|
@@ -6,10 +6,12 @@ export { capitalize } from './util/capitalize.js';
|
|
|
6
6
|
export { deg2rad } from './util/deg2rad.js';
|
|
7
7
|
export { delayPromise } from './util/delayPromise.js';
|
|
8
8
|
export { FetchError, HTTP_0_ANY, fetchRetry } from './util/fetchRetry.js';
|
|
9
|
+
export { fixFloatPrecision } from './util/fixFloatPrecision.js';
|
|
9
10
|
export { getObjValueByPath } from './util/getObjValueByPath.js';
|
|
10
11
|
export { getRandomInt } from './util/getRandomInt.js';
|
|
11
12
|
export { getURLParam } from './util/getURLParam.js';
|
|
12
13
|
export { purgeObject } from './util/purgeObject.js';
|
|
14
|
+
export { addFloat, fixFloat, subFloat } from './util/safeFloat.js';
|
|
13
15
|
export { sanitizeURLParam } from './util/sanitizeURLParam.js';
|
|
14
16
|
export { saveAsFile } from './util/saveAsFile.js';
|
|
15
17
|
export { setObjValueByPath } from './util/setObjValueByPath.js';
|
package/src/logging/Logger.js
CHANGED
|
@@ -9,9 +9,13 @@ export const LEVEL_SILENT = 0;
|
|
|
9
9
|
|
|
10
10
|
export class Logger {
|
|
11
11
|
/**
|
|
12
|
-
* @type {(error) => {}}
|
|
12
|
+
* @type {(error: Error) => {}}
|
|
13
13
|
*/
|
|
14
14
|
static exceptionHandler = null;
|
|
15
|
+
/**
|
|
16
|
+
* @type {(level: string, message: string, extraData: object) => {}}
|
|
17
|
+
*/
|
|
18
|
+
static suppressedLogHandler = null;
|
|
15
19
|
|
|
16
20
|
/**
|
|
17
21
|
* Creates a new Logger instance.
|
|
@@ -33,6 +37,9 @@ export class Logger {
|
|
|
33
37
|
*/
|
|
34
38
|
debug(message, extraData = null) {
|
|
35
39
|
if (this.level < LEVEL_DEBUG) {
|
|
40
|
+
if (Logger.suppressedLogHandler) {
|
|
41
|
+
Logger.suppressedLogHandler('debug', message, extraData);
|
|
42
|
+
}
|
|
36
43
|
return;
|
|
37
44
|
}
|
|
38
45
|
const logMessage = `${Date.now()} [${this.name}] ${message}`;
|
|
@@ -50,6 +57,9 @@ export class Logger {
|
|
|
50
57
|
*/
|
|
51
58
|
info(message, extraData = null) {
|
|
52
59
|
if (this.level < LEVEL_INFO) {
|
|
60
|
+
if (Logger.suppressedLogHandler) {
|
|
61
|
+
Logger.suppressedLogHandler('info', message, extraData);
|
|
62
|
+
}
|
|
53
63
|
return;
|
|
54
64
|
}
|
|
55
65
|
const logMessage = `${Date.now()} [${this.name}] ${message}`;
|
|
@@ -67,6 +77,9 @@ export class Logger {
|
|
|
67
77
|
*/
|
|
68
78
|
warn(message, extraData = null) {
|
|
69
79
|
if (this.level < LEVEL_WARN) {
|
|
80
|
+
if (Logger.suppressedLogHandler) {
|
|
81
|
+
Logger.suppressedLogHandler('warning', message, extraData);
|
|
82
|
+
}
|
|
70
83
|
return;
|
|
71
84
|
}
|
|
72
85
|
const logMessage = `${Date.now()} [${this.name}] ${message}`;
|
|
@@ -84,6 +97,9 @@ export class Logger {
|
|
|
84
97
|
*/
|
|
85
98
|
error(message, extraData = null) {
|
|
86
99
|
if (this.level < LEVEL_ERROR) {
|
|
100
|
+
if (Logger.suppressedLogHandler) {
|
|
101
|
+
Logger.suppressedLogHandler('error', message, extraData);
|
|
102
|
+
}
|
|
87
103
|
return;
|
|
88
104
|
}
|
|
89
105
|
const logMessage = `${Date.now()} [${this.name}] ${message}`;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convenience method for floating point precision handling.
|
|
3
|
+
* @param {number} value - The number to process.
|
|
4
|
+
* @param {number} p - The precision. Defaults to 2.
|
|
5
|
+
* @returns {number} The processed value.
|
|
6
|
+
*/
|
|
7
|
+
export function fixFloat(value, p = 2) {
|
|
8
|
+
return Number.parseFloat(value.toFixed(p));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Adds two value with floating point precision.
|
|
13
|
+
* @param {number} a - The number a.
|
|
14
|
+
* @param {number} b - The number b.
|
|
15
|
+
* @returns {number} The processed value.
|
|
16
|
+
*/
|
|
17
|
+
export function addFloat(a, b) {
|
|
18
|
+
const p = 100;
|
|
19
|
+
return fixFloat((a * p + b * p) / p);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Substracts two value with floating point precision.
|
|
24
|
+
* @param {number} a - The number a.
|
|
25
|
+
* @param {number} b - The number b.
|
|
26
|
+
* @returns {number} The processed value.
|
|
27
|
+
*/
|
|
28
|
+
export function subFloat(a, b) {
|
|
29
|
+
const p = 100;
|
|
30
|
+
return fixFloat((a * p - b * p) / p);
|
|
31
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { addFloat, fixFloat, subFloat } from './safeFloat.js';
|
|
2
|
+
|
|
3
|
+
test('fixFloat()', () => {
|
|
4
|
+
expect(fixFloat(0.20000000000000004)).toBe(0.2);
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
test('addFloat()', () => {
|
|
8
|
+
expect(addFloat(0.20000000000000004, 0.1000001)).toBe(0.3);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test('subFloat()', () => {
|
|
12
|
+
expect(subFloat(0.20000000000000004, 0.1000001)).toBe(0.1);
|
|
13
|
+
});
|
package/types/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ export { addLeadingZero } from "./util/addLeadingZero.js";
|
|
|
4
4
|
export { capitalize } from "./util/capitalize.js";
|
|
5
5
|
export { deg2rad } from "./util/deg2rad.js";
|
|
6
6
|
export { delayPromise } from "./util/delayPromise.js";
|
|
7
|
+
export { fixFloatPrecision } from "./util/fixFloatPrecision.js";
|
|
7
8
|
export { getObjValueByPath } from "./util/getObjValueByPath.js";
|
|
8
9
|
export { getRandomInt } from "./util/getRandomInt.js";
|
|
9
10
|
export { getURLParam } from "./util/getURLParam.js";
|
|
@@ -13,4 +14,5 @@ export { saveAsFile } from "./util/saveAsFile.js";
|
|
|
13
14
|
export { setObjValueByPath } from "./util/setObjValueByPath.js";
|
|
14
15
|
export { underscoreToCamelCase } from "./util/underscoreToCamelCase.js";
|
|
15
16
|
export { FetchError, HTTP_0_ANY, fetchRetry } from "./util/fetchRetry.js";
|
|
17
|
+
export { addFloat, fixFloat, subFloat } from "./util/safeFloat.js";
|
|
16
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cloneObject.d.ts","sourceRoot":"","sources":["../../src/util/cloneObject.js"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,oCAHW,MAAM,GACJ,MAAM,CAKlB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fixFloatPrecision.d.ts","sourceRoot":"","sources":["../../src/util/fixFloatPrecision.js"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,yCAHW,MAAM,GACJ,MAAM,CAIlB"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convenience method for floating point precision handling.
|
|
3
|
+
* @param {number} value - The number to process.
|
|
4
|
+
* @param {number} p - The precision. Defaults to 2.
|
|
5
|
+
* @returns {number} The processed value.
|
|
6
|
+
*/
|
|
7
|
+
export function fixFloat(value: number, p?: number): number;
|
|
8
|
+
/**
|
|
9
|
+
* Adds two value with floating point precision.
|
|
10
|
+
* @param {number} a - The number a.
|
|
11
|
+
* @param {number} b - The number b.
|
|
12
|
+
* @returns {number} The processed value.
|
|
13
|
+
*/
|
|
14
|
+
export function addFloat(a: number, b: number): number;
|
|
15
|
+
/**
|
|
16
|
+
* Substracts two value with floating point precision.
|
|
17
|
+
* @param {number} a - The number a.
|
|
18
|
+
* @param {number} b - The number b.
|
|
19
|
+
* @returns {number} The processed value.
|
|
20
|
+
*/
|
|
21
|
+
export function subFloat(a: number, b: number): number;
|
|
22
|
+
//# sourceMappingURL=safeFloat.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safeFloat.d.ts","sourceRoot":"","sources":["../../src/util/safeFloat.js"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,gCAJW,MAAM,MACN,MAAM,GACJ,MAAM,CAIlB;AAED;;;;;GAKG;AACH,4BAJW,MAAM,KACN,MAAM,GACJ,MAAM,CAKlB;AAED;;;;;GAKG;AACH,4BAJW,MAAM,KACN,MAAM,GACJ,MAAM,CAKlB"}
|