nhb-toolbox 4.10.54 → 4.10.56
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/dist/cjs/number/percent.js +18 -7
- package/dist/cjs/number/range.js +5 -5
- package/dist/dts/number/percent.d.ts.map +1 -1
- package/dist/dts/number/range.d.ts +2 -2
- package/dist/dts/number/range.d.ts.map +1 -1
- package/dist/dts/number/types.d.ts +10 -9
- package/dist/dts/number/types.d.ts.map +1 -1
- package/dist/esm/number/percent.js +18 -7
- package/dist/esm/number/range.js +5 -5
- package/package.json +1 -1
|
@@ -18,27 +18,38 @@ const guards_1 = require("./guards");
|
|
|
18
18
|
* @returns The calculated number rounded to three decimal places, or `NaN` if input is invalid.
|
|
19
19
|
*/
|
|
20
20
|
function calculatePercentage(options) {
|
|
21
|
+
const { roundTo = 3 } = options;
|
|
22
|
+
/**
|
|
23
|
+
* - Rounds a number to the specified number of decimal places.
|
|
24
|
+
*
|
|
25
|
+
* @param num - The number to round.
|
|
26
|
+
* @returns The rounded number.
|
|
27
|
+
*/
|
|
28
|
+
const _roundNumber = (num) => {
|
|
29
|
+
const factor = Math.pow(10, roundTo);
|
|
30
|
+
return Math.round(num * factor) / factor;
|
|
31
|
+
};
|
|
21
32
|
switch (options?.mode) {
|
|
22
33
|
case 'get-percent': {
|
|
23
34
|
const { part, total } = options;
|
|
24
35
|
if ((0, guards_1.areInvalidNumbers)(part, total) || total === 0) {
|
|
25
36
|
return NaN;
|
|
26
37
|
}
|
|
27
|
-
return
|
|
38
|
+
return _roundNumber((part / total) * 100);
|
|
28
39
|
}
|
|
29
40
|
case 'get-value': {
|
|
30
41
|
const { percentage, total } = options;
|
|
31
42
|
if ((0, guards_1.areInvalidNumbers)(percentage, total) || total === 0) {
|
|
32
43
|
return NaN;
|
|
33
44
|
}
|
|
34
|
-
return
|
|
45
|
+
return _roundNumber((percentage / 100) * total);
|
|
35
46
|
}
|
|
36
47
|
case 'get-original': {
|
|
37
48
|
const { percentage, value } = options;
|
|
38
49
|
if ((0, guards_1.areInvalidNumbers)(percentage, value) || percentage === 0) {
|
|
39
50
|
return NaN;
|
|
40
51
|
}
|
|
41
|
-
return
|
|
52
|
+
return _roundNumber((value / percentage) * 100);
|
|
42
53
|
}
|
|
43
54
|
case 'get-change-percent': {
|
|
44
55
|
const { oldValue, newValue } = options;
|
|
@@ -46,7 +57,7 @@ function calculatePercentage(options) {
|
|
|
46
57
|
return NaN;
|
|
47
58
|
}
|
|
48
59
|
const change = ((newValue - oldValue) / oldValue) * 100;
|
|
49
|
-
return
|
|
60
|
+
return _roundNumber(change);
|
|
50
61
|
}
|
|
51
62
|
case 'apply-percent-change': {
|
|
52
63
|
const { baseValue, percentage } = options;
|
|
@@ -54,7 +65,7 @@ function calculatePercentage(options) {
|
|
|
54
65
|
return NaN;
|
|
55
66
|
}
|
|
56
67
|
const value = baseValue * (1 + percentage / 100);
|
|
57
|
-
return
|
|
68
|
+
return _roundNumber(value);
|
|
58
69
|
}
|
|
59
70
|
case 'get-percent-difference': {
|
|
60
71
|
const { value1, value2 } = options;
|
|
@@ -66,14 +77,14 @@ function calculatePercentage(options) {
|
|
|
66
77
|
return NaN;
|
|
67
78
|
}
|
|
68
79
|
const diff = (Math.abs(value1 - value2) / avg) * 100;
|
|
69
|
-
return
|
|
80
|
+
return _roundNumber(diff);
|
|
70
81
|
}
|
|
71
82
|
case 'inverse-percent': {
|
|
72
83
|
const { part, total } = options;
|
|
73
84
|
if ((0, guards_1.areInvalidNumbers)(part, total) || part === 0) {
|
|
74
85
|
return NaN;
|
|
75
86
|
}
|
|
76
|
-
return
|
|
87
|
+
return _roundNumber((total / part) * 100);
|
|
77
88
|
}
|
|
78
89
|
default:
|
|
79
90
|
return NaN;
|
package/dist/cjs/number/range.js
CHANGED
|
@@ -16,7 +16,7 @@ const prime_1 = require("./prime");
|
|
|
16
16
|
* @returns Either a string or an array of numbers.
|
|
17
17
|
*/
|
|
18
18
|
function getNumbersInRange(type = 'any', options) {
|
|
19
|
-
const {
|
|
19
|
+
const { getAsString = false, min = 0, max = 100, includeMin = true, includeMax = true, separator = ', ', multiplesOf, } = options || {};
|
|
20
20
|
let output = [];
|
|
21
21
|
/**
|
|
22
22
|
* Helper function to apply range and get array of numbers in that range.
|
|
@@ -42,8 +42,8 @@ function getNumbersInRange(type = 'any', options) {
|
|
|
42
42
|
}
|
|
43
43
|
return numbers;
|
|
44
44
|
};
|
|
45
|
-
if (type === 'prime' &&
|
|
46
|
-
console.warn('Warning: The "
|
|
45
|
+
if (type === 'prime' && multiplesOf !== undefined) {
|
|
46
|
+
console.warn('Warning: The "multiplesOf" option is ignored when the type is "prime"!');
|
|
47
47
|
}
|
|
48
48
|
switch (type) {
|
|
49
49
|
case 'random':
|
|
@@ -71,9 +71,9 @@ function getNumbersInRange(type = 'any', options) {
|
|
|
71
71
|
break;
|
|
72
72
|
}
|
|
73
73
|
if (type !== 'prime') {
|
|
74
|
-
output = (0, helpers_1._applyMultiples)(output,
|
|
74
|
+
output = (0, helpers_1._applyMultiples)(output, multiplesOf);
|
|
75
75
|
}
|
|
76
|
-
return
|
|
76
|
+
return getAsString ?
|
|
77
77
|
(0, utils_1.convertArrayToString)(output, separator)
|
|
78
78
|
: output;
|
|
79
79
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"percent.d.ts","sourceRoot":"","sources":["../../../src/number/percent.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,iBAAiB,GAAG,MAAM,
|
|
1
|
+
{"version":3,"file":"percent.d.ts","sourceRoot":"","sources":["../../../src/number/percent.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,iBAAiB,GAAG,MAAM,CAqGtE"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NumberType, RangedNumbers, RangeOptions } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* * Function to get numbers within a range based on the provided `NumberType` and options.
|
|
4
4
|
* * Returns either a string or an array of numbers based on the `getAs` property in options.
|
|
@@ -7,5 +7,5 @@ import type { GetAs, NumberType, RangedNumbers, RangeOptions } from './types';
|
|
|
7
7
|
* @param options - Options to configure number generation, including range and formatting.
|
|
8
8
|
* @returns Either a string or an array of numbers.
|
|
9
9
|
*/
|
|
10
|
-
export declare function getNumbersInRange<T extends
|
|
10
|
+
export declare function getNumbersInRange<T extends boolean = false>(type?: NumberType, options?: RangeOptions<T>): RangedNumbers<T>;
|
|
11
11
|
//# sourceMappingURL=range.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../../src/number/range.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../../src/number/range.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvE;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,OAAO,GAAG,KAAK,EAC1D,IAAI,GAAE,UAAkB,EACxB,OAAO,CAAC,EAAE,YAAY,CAAC,CAAC,CAAC,GACvB,aAAa,CAAC,CAAC,CAAC,CA2FlB"}
|
|
@@ -22,19 +22,17 @@ export interface DecimalOptions<T extends boolean | undefined = false> {
|
|
|
22
22
|
export type ConvertedDecimal<T> = T extends true ? `${number}` : number;
|
|
23
23
|
/** - Type of numbers to generate */
|
|
24
24
|
export type NumberType = 'any' | 'natural' | 'odd' | 'even' | 'prime' | 'random';
|
|
25
|
-
/** - Output format for the generated numbers */
|
|
26
|
-
export type GetAs = 'array' | 'string';
|
|
27
25
|
/** - Options for generating numbers in a range */
|
|
28
|
-
export interface RangeOptions<T extends
|
|
29
|
-
/** Separator for the string format if `
|
|
30
|
-
separator?: string;
|
|
26
|
+
export interface RangeOptions<T extends boolean = false> extends RandomNumberOptions {
|
|
27
|
+
/** Separator for the string format if `getAsString` is `'true'`. Defaults to `, `. */
|
|
28
|
+
separator?: T extends true ? string : never;
|
|
31
29
|
/** The multiples of which number to consider in the result. */
|
|
32
30
|
multiplesOf?: number;
|
|
33
|
-
/** The format for the result -
|
|
34
|
-
|
|
31
|
+
/** The format for the result - `{ getAsString: true }` returns strings with custom separator and `false` returns array of numbers. Default is `false`. */
|
|
32
|
+
getAsString?: T;
|
|
35
33
|
}
|
|
36
34
|
/** - The return type of the `getNumbersInRange` function */
|
|
37
|
-
export type RangedNumbers<T extends
|
|
35
|
+
export type RangedNumbers<T extends boolean = false> = T extends true ? string : number[];
|
|
38
36
|
/** List of ISO 4217 currency codes. */
|
|
39
37
|
export type CurrencyCode = keyof typeof CURRENCY_LOCALES | (typeof CURRENCY_CODES)[number];
|
|
40
38
|
/** - List of all supported BCP 47 locales */
|
|
@@ -118,7 +116,10 @@ export interface InversePercentageOptions {
|
|
|
118
116
|
total: number;
|
|
119
117
|
}
|
|
120
118
|
/** * Options for calculating percentages and related values. */
|
|
121
|
-
export type PercentageOptions = GetPercentOptions | GetValueOptions | GetOriginalOptions | GetChangeOptions | ApplyChangeOptions | GetDifferenceOptions | InversePercentageOptions
|
|
119
|
+
export type PercentageOptions = (GetPercentOptions | GetValueOptions | GetOriginalOptions | GetChangeOptions | ApplyChangeOptions | GetDifferenceOptions | InversePercentageOptions) & {
|
|
120
|
+
/** The number of decimal places to round the result to. */
|
|
121
|
+
roundTo?: number;
|
|
122
|
+
};
|
|
122
123
|
/** * Static methods from `Unit` class that accept a single number argument and return a number. */
|
|
123
124
|
export type UnitNumberMethods = {
|
|
124
125
|
[K in keyof typeof Unit]: (typeof Unit)[K] extends ((value: number) => number) ? K : never;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/number/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,EAClB,oBAAoB,EACpB,KAAK,EACL,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEnC,4CAA4C;AAC5C,MAAM,WAAW,mBAAmB;IACnC,oCAAoC;IACpC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,kCAAkC;IAClC,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,kDAAkD;AAClD,MAAM,WAAW,cAAc,CAAC,CAAC,SAAS,OAAO,GAAG,SAAS,GAAG,KAAK;IACpE,6DAA6D;IAC7D,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,CAAC,CAAC;CACb;AAED,4DAA4D;AAC5D,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAAI,CAAC,SAAS,IAAI,GAAG,GAAG,MAAM,EAAE,GAAG,MAAM,CAAC;AAExE,oCAAoC;AACpC,MAAM,MAAM,UAAU,GACnB,KAAK,GACL,SAAS,GACT,KAAK,GACL,MAAM,GACN,OAAO,GACP,QAAQ,CAAC;AAEZ,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/number/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,kBAAkB,EAClB,oBAAoB,EACpB,KAAK,EACL,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,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K,CAAC,CAAC,OAAO,CAAC,CAAC;AAEhD,8BAA8B;AAC9B,MAAM,MAAM,QAAQ,GAAG,MAAM,OAAO,kBAAkB,CAAC"}
|
|
@@ -15,27 +15,38 @@ import { areInvalidNumbers } from './guards';
|
|
|
15
15
|
* @returns The calculated number rounded to three decimal places, or `NaN` if input is invalid.
|
|
16
16
|
*/
|
|
17
17
|
export function calculatePercentage(options) {
|
|
18
|
+
const { roundTo = 3 } = options;
|
|
19
|
+
/**
|
|
20
|
+
* - Rounds a number to the specified number of decimal places.
|
|
21
|
+
*
|
|
22
|
+
* @param num - The number to round.
|
|
23
|
+
* @returns The rounded number.
|
|
24
|
+
*/
|
|
25
|
+
const _roundNumber = (num) => {
|
|
26
|
+
const factor = Math.pow(10, roundTo);
|
|
27
|
+
return Math.round(num * factor) / factor;
|
|
28
|
+
};
|
|
18
29
|
switch (options?.mode) {
|
|
19
30
|
case 'get-percent': {
|
|
20
31
|
const { part, total } = options;
|
|
21
32
|
if (areInvalidNumbers(part, total) || total === 0) {
|
|
22
33
|
return NaN;
|
|
23
34
|
}
|
|
24
|
-
return
|
|
35
|
+
return _roundNumber((part / total) * 100);
|
|
25
36
|
}
|
|
26
37
|
case 'get-value': {
|
|
27
38
|
const { percentage, total } = options;
|
|
28
39
|
if (areInvalidNumbers(percentage, total) || total === 0) {
|
|
29
40
|
return NaN;
|
|
30
41
|
}
|
|
31
|
-
return
|
|
42
|
+
return _roundNumber((percentage / 100) * total);
|
|
32
43
|
}
|
|
33
44
|
case 'get-original': {
|
|
34
45
|
const { percentage, value } = options;
|
|
35
46
|
if (areInvalidNumbers(percentage, value) || percentage === 0) {
|
|
36
47
|
return NaN;
|
|
37
48
|
}
|
|
38
|
-
return
|
|
49
|
+
return _roundNumber((value / percentage) * 100);
|
|
39
50
|
}
|
|
40
51
|
case 'get-change-percent': {
|
|
41
52
|
const { oldValue, newValue } = options;
|
|
@@ -43,7 +54,7 @@ export function calculatePercentage(options) {
|
|
|
43
54
|
return NaN;
|
|
44
55
|
}
|
|
45
56
|
const change = ((newValue - oldValue) / oldValue) * 100;
|
|
46
|
-
return
|
|
57
|
+
return _roundNumber(change);
|
|
47
58
|
}
|
|
48
59
|
case 'apply-percent-change': {
|
|
49
60
|
const { baseValue, percentage } = options;
|
|
@@ -51,7 +62,7 @@ export function calculatePercentage(options) {
|
|
|
51
62
|
return NaN;
|
|
52
63
|
}
|
|
53
64
|
const value = baseValue * (1 + percentage / 100);
|
|
54
|
-
return
|
|
65
|
+
return _roundNumber(value);
|
|
55
66
|
}
|
|
56
67
|
case 'get-percent-difference': {
|
|
57
68
|
const { value1, value2 } = options;
|
|
@@ -63,14 +74,14 @@ export function calculatePercentage(options) {
|
|
|
63
74
|
return NaN;
|
|
64
75
|
}
|
|
65
76
|
const diff = (Math.abs(value1 - value2) / avg) * 100;
|
|
66
|
-
return
|
|
77
|
+
return _roundNumber(diff);
|
|
67
78
|
}
|
|
68
79
|
case 'inverse-percent': {
|
|
69
80
|
const { part, total } = options;
|
|
70
81
|
if (areInvalidNumbers(part, total) || part === 0) {
|
|
71
82
|
return NaN;
|
|
72
83
|
}
|
|
73
|
-
return
|
|
84
|
+
return _roundNumber((total / part) * 100);
|
|
74
85
|
}
|
|
75
86
|
default:
|
|
76
87
|
return NaN;
|
package/dist/esm/number/range.js
CHANGED
|
@@ -13,7 +13,7 @@ import { isPrime } from './prime';
|
|
|
13
13
|
* @returns Either a string or an array of numbers.
|
|
14
14
|
*/
|
|
15
15
|
export function getNumbersInRange(type = 'any', options) {
|
|
16
|
-
const {
|
|
16
|
+
const { getAsString = false, min = 0, max = 100, includeMin = true, includeMax = true, separator = ', ', multiplesOf, } = options || {};
|
|
17
17
|
let output = [];
|
|
18
18
|
/**
|
|
19
19
|
* Helper function to apply range and get array of numbers in that range.
|
|
@@ -39,8 +39,8 @@ export function getNumbersInRange(type = 'any', options) {
|
|
|
39
39
|
}
|
|
40
40
|
return numbers;
|
|
41
41
|
};
|
|
42
|
-
if (type === 'prime' &&
|
|
43
|
-
console.warn('Warning: The "
|
|
42
|
+
if (type === 'prime' && multiplesOf !== undefined) {
|
|
43
|
+
console.warn('Warning: The "multiplesOf" option is ignored when the type is "prime"!');
|
|
44
44
|
}
|
|
45
45
|
switch (type) {
|
|
46
46
|
case 'random':
|
|
@@ -68,9 +68,9 @@ export function getNumbersInRange(type = 'any', options) {
|
|
|
68
68
|
break;
|
|
69
69
|
}
|
|
70
70
|
if (type !== 'prime') {
|
|
71
|
-
output = _applyMultiples(output,
|
|
71
|
+
output = _applyMultiples(output, multiplesOf);
|
|
72
72
|
}
|
|
73
|
-
return
|
|
73
|
+
return getAsString ?
|
|
74
74
|
convertArrayToString(output, separator)
|
|
75
75
|
: output;
|
|
76
76
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nhb-toolbox",
|
|
3
|
-
"version": "4.10.
|
|
3
|
+
"version": "4.10.56",
|
|
4
4
|
"description": "A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|