tiny-essentials 1.8.1 → 1.8.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/dist/TinyBasicsEs.js
CHANGED
|
@@ -2805,6 +2805,43 @@ function getAge(timeData = 0, now = null) {
|
|
|
2805
2805
|
return null;
|
|
2806
2806
|
}
|
|
2807
2807
|
|
|
2808
|
+
/**
|
|
2809
|
+
* Converts a byte value into a human-readable format with unit and value separated.
|
|
2810
|
+
*
|
|
2811
|
+
* @param {number} bytes - The number of bytes to format. Must be a non-negative number.
|
|
2812
|
+
* @param {number|null} [decimals=null] - The number of decimal places to include in the result. Defaults to null. If negative, it will be treated as 0. If null, no rounding is applied.
|
|
2813
|
+
* @param {string|null} [maxUnit=null] - Optional unit limit. If provided, restricts conversion to this unit at most (e.g., 'MB' prevents conversion to 'GB' or higher). Must be one of: 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'.
|
|
2814
|
+
* @returns {{ unit: string|null, value: number|null }} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
|
|
2815
|
+
*
|
|
2816
|
+
* @example
|
|
2817
|
+
* formatBytes(123456789);
|
|
2818
|
+
* // → { unit: 'MB', value: 117.74 }
|
|
2819
|
+
*
|
|
2820
|
+
* @example
|
|
2821
|
+
* formatBytes(1073741824, 2, 'MB');
|
|
2822
|
+
* // → { unit: 'MB', value: 1024 }
|
|
2823
|
+
*/
|
|
2824
|
+
function formatBytes(bytes, decimals = null, maxUnit = null) {
|
|
2825
|
+
if (typeof bytes !== 'number' || bytes < 0) return { unit: null, value: null };
|
|
2826
|
+
if (bytes === 0) return { unit: 'Bytes', value: 0 };
|
|
2827
|
+
|
|
2828
|
+
const k = 1024;
|
|
2829
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
2830
|
+
|
|
2831
|
+
const maxIndex = maxUnit && sizes.includes(maxUnit) ? sizes.indexOf(maxUnit) : sizes.length - 1;
|
|
2832
|
+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), maxIndex);
|
|
2833
|
+
|
|
2834
|
+
let value = bytes / Math.pow(k, i);
|
|
2835
|
+
|
|
2836
|
+
if (decimals !== null) {
|
|
2837
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
2838
|
+
value = parseFloat(value.toFixed(dm));
|
|
2839
|
+
}
|
|
2840
|
+
|
|
2841
|
+
const unit = sizes[i];
|
|
2842
|
+
return { unit, value };
|
|
2843
|
+
}
|
|
2844
|
+
|
|
2808
2845
|
;// ./src/v1/basics/text.mjs
|
|
2809
2846
|
/**
|
|
2810
2847
|
* Converts a string to title case where the first letter of each word is capitalized.
|
package/dist/TinyEssentials.js
CHANGED
|
@@ -2951,6 +2951,43 @@ function getAge(timeData = 0, now = null) {
|
|
|
2951
2951
|
return null;
|
|
2952
2952
|
}
|
|
2953
2953
|
|
|
2954
|
+
/**
|
|
2955
|
+
* Converts a byte value into a human-readable format with unit and value separated.
|
|
2956
|
+
*
|
|
2957
|
+
* @param {number} bytes - The number of bytes to format. Must be a non-negative number.
|
|
2958
|
+
* @param {number|null} [decimals=null] - The number of decimal places to include in the result. Defaults to null. If negative, it will be treated as 0. If null, no rounding is applied.
|
|
2959
|
+
* @param {string|null} [maxUnit=null] - Optional unit limit. If provided, restricts conversion to this unit at most (e.g., 'MB' prevents conversion to 'GB' or higher). Must be one of: 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'.
|
|
2960
|
+
* @returns {{ unit: string|null, value: number|null }} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
|
|
2961
|
+
*
|
|
2962
|
+
* @example
|
|
2963
|
+
* formatBytes(123456789);
|
|
2964
|
+
* // → { unit: 'MB', value: 117.74 }
|
|
2965
|
+
*
|
|
2966
|
+
* @example
|
|
2967
|
+
* formatBytes(1073741824, 2, 'MB');
|
|
2968
|
+
* // → { unit: 'MB', value: 1024 }
|
|
2969
|
+
*/
|
|
2970
|
+
function formatBytes(bytes, decimals = null, maxUnit = null) {
|
|
2971
|
+
if (typeof bytes !== 'number' || bytes < 0) return { unit: null, value: null };
|
|
2972
|
+
if (bytes === 0) return { unit: 'Bytes', value: 0 };
|
|
2973
|
+
|
|
2974
|
+
const k = 1024;
|
|
2975
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
2976
|
+
|
|
2977
|
+
const maxIndex = maxUnit && sizes.includes(maxUnit) ? sizes.indexOf(maxUnit) : sizes.length - 1;
|
|
2978
|
+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), maxIndex);
|
|
2979
|
+
|
|
2980
|
+
let value = bytes / Math.pow(k, i);
|
|
2981
|
+
|
|
2982
|
+
if (decimals !== null) {
|
|
2983
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
2984
|
+
value = parseFloat(value.toFixed(dm));
|
|
2985
|
+
}
|
|
2986
|
+
|
|
2987
|
+
const unit = sizes[i];
|
|
2988
|
+
return { unit, value };
|
|
2989
|
+
}
|
|
2990
|
+
|
|
2954
2991
|
;// ./src/v1/basics/text.mjs
|
|
2955
2992
|
/**
|
|
2956
2993
|
* Converts a string to title case where the first letter of each word is capitalized.
|
|
@@ -83,6 +83,44 @@ function getAge(timeData = 0, now = null) {
|
|
|
83
83
|
return null;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Converts a byte value into a human-readable format with unit and value separated.
|
|
88
|
+
*
|
|
89
|
+
* @param {number} bytes - The number of bytes to format. Must be a non-negative number.
|
|
90
|
+
* @param {number|null} [decimals=null] - The number of decimal places to include in the result. Defaults to null. If negative, it will be treated as 0. If null, no rounding is applied.
|
|
91
|
+
* @param {string|null} [maxUnit=null] - Optional unit limit. If provided, restricts conversion to this unit at most (e.g., 'MB' prevents conversion to 'GB' or higher). Must be one of: 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'.
|
|
92
|
+
* @returns {{ unit: string|null, value: number|null }} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* formatBytes(123456789);
|
|
96
|
+
* // → { unit: 'MB', value: 117.74 }
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* formatBytes(1073741824, 2, 'MB');
|
|
100
|
+
* // → { unit: 'MB', value: 1024 }
|
|
101
|
+
*/
|
|
102
|
+
function formatBytes(bytes, decimals = null, maxUnit = null) {
|
|
103
|
+
if (typeof bytes !== 'number' || bytes < 0) return { unit: null, value: null };
|
|
104
|
+
if (bytes === 0) return { unit: 'Bytes', value: 0 };
|
|
105
|
+
|
|
106
|
+
const k = 1024;
|
|
107
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
108
|
+
|
|
109
|
+
const maxIndex = maxUnit && sizes.includes(maxUnit) ? sizes.indexOf(maxUnit) : sizes.length - 1;
|
|
110
|
+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), maxIndex);
|
|
111
|
+
|
|
112
|
+
let value = bytes / Math.pow(k, i);
|
|
113
|
+
|
|
114
|
+
if (decimals !== null) {
|
|
115
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
116
|
+
value = parseFloat(value.toFixed(dm));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const unit = sizes[i];
|
|
120
|
+
return { unit, value };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
exports.formatBytes = formatBytes;
|
|
86
124
|
exports.getAge = getAge;
|
|
87
125
|
exports.getSimplePerc = getSimplePerc;
|
|
88
126
|
exports.ruleOfThree = ruleOfThree;
|
|
@@ -52,4 +52,24 @@ export function getSimplePerc(price: number, percentage: number): number;
|
|
|
52
52
|
* @returns {number|null} The age in years, or null if `timeData` is not provided or invalid.
|
|
53
53
|
*/
|
|
54
54
|
export function getAge(timeData?: number | string | Date, now?: Date | null): number | null;
|
|
55
|
+
/**
|
|
56
|
+
* Converts a byte value into a human-readable format with unit and value separated.
|
|
57
|
+
*
|
|
58
|
+
* @param {number} bytes - The number of bytes to format. Must be a non-negative number.
|
|
59
|
+
* @param {number|null} [decimals=null] - The number of decimal places to include in the result. Defaults to null. If negative, it will be treated as 0. If null, no rounding is applied.
|
|
60
|
+
* @param {string|null} [maxUnit=null] - Optional unit limit. If provided, restricts conversion to this unit at most (e.g., 'MB' prevents conversion to 'GB' or higher). Must be one of: 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'.
|
|
61
|
+
* @returns {{ unit: string|null, value: number|null }} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* formatBytes(123456789);
|
|
65
|
+
* // → { unit: 'MB', value: 117.74 }
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* formatBytes(1073741824, 2, 'MB');
|
|
69
|
+
* // → { unit: 'MB', value: 1024 }
|
|
70
|
+
*/
|
|
71
|
+
export function formatBytes(bytes: number, decimals?: number | null, maxUnit?: string | null): {
|
|
72
|
+
unit: string | null;
|
|
73
|
+
value: number | null;
|
|
74
|
+
};
|
|
55
75
|
//# sourceMappingURL=simpleMath.d.mts.map
|
|
@@ -73,3 +73,36 @@ export function getAge(timeData = 0, now = null) {
|
|
|
73
73
|
}
|
|
74
74
|
return null;
|
|
75
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Converts a byte value into a human-readable format with unit and value separated.
|
|
78
|
+
*
|
|
79
|
+
* @param {number} bytes - The number of bytes to format. Must be a non-negative number.
|
|
80
|
+
* @param {number|null} [decimals=null] - The number of decimal places to include in the result. Defaults to null. If negative, it will be treated as 0. If null, no rounding is applied.
|
|
81
|
+
* @param {string|null} [maxUnit=null] - Optional unit limit. If provided, restricts conversion to this unit at most (e.g., 'MB' prevents conversion to 'GB' or higher). Must be one of: 'Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'.
|
|
82
|
+
* @returns {{ unit: string|null, value: number|null }} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* formatBytes(123456789);
|
|
86
|
+
* // → { unit: 'MB', value: 117.74 }
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* formatBytes(1073741824, 2, 'MB');
|
|
90
|
+
* // → { unit: 'MB', value: 1024 }
|
|
91
|
+
*/
|
|
92
|
+
export function formatBytes(bytes, decimals = null, maxUnit = null) {
|
|
93
|
+
if (typeof bytes !== 'number' || bytes < 0)
|
|
94
|
+
return { unit: null, value: null };
|
|
95
|
+
if (bytes === 0)
|
|
96
|
+
return { unit: 'Bytes', value: 0 };
|
|
97
|
+
const k = 1024;
|
|
98
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
99
|
+
const maxIndex = maxUnit && sizes.includes(maxUnit) ? sizes.indexOf(maxUnit) : sizes.length - 1;
|
|
100
|
+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), maxIndex);
|
|
101
|
+
let value = bytes / Math.pow(k, i);
|
|
102
|
+
if (decimals !== null) {
|
|
103
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
104
|
+
value = parseFloat(value.toFixed(dm));
|
|
105
|
+
}
|
|
106
|
+
const unit = sizes[i];
|
|
107
|
+
return { unit, value };
|
|
108
|
+
}
|
|
@@ -63,3 +63,59 @@ If the `timeData` is not provided or is invalid, the function returns `null`.
|
|
|
63
63
|
```js
|
|
64
64
|
getAge('1990-01-01'); // → 35 (assuming the current year is 2025)
|
|
65
65
|
```
|
|
66
|
+
|
|
67
|
+
---
|
|
68
|
+
|
|
69
|
+
### 📦 `formatBytes(bytes, decimals, maxUnit)`
|
|
70
|
+
|
|
71
|
+
Converts a byte value into a human-readable format with the unit and value separated. It allows you to set the number of decimal places and restricts the conversion to a specified maximum unit (optional). 🌐
|
|
72
|
+
|
|
73
|
+
### Parameters:
|
|
74
|
+
|
|
75
|
+
- `bytes` (number) ➡️ **The number of bytes to format.**
|
|
76
|
+
Must be a non-negative number.
|
|
77
|
+
- `decimals` (number|null) ➡️ **The number of decimal places to include in the result.**
|
|
78
|
+
Defaults to `2`. If negative, it will be treated as `0`. If `null`, no rounding is applied and the full precision is used.
|
|
79
|
+
- `maxUnit` (string|null) ➡️ **Optional unit limit.**
|
|
80
|
+
If provided, restricts conversion to this unit at most (e.g., `'MB'` prevents conversion to `'GB'` or higher).
|
|
81
|
+
Must be one of:
|
|
82
|
+
`'Bytes'`, `'KB'`, `'MB'`, `'GB'`, `'TB'`, `'PB'`, `'EB'`, `'ZB'`, `'YB'`.
|
|
83
|
+
Defaults to `null`, meaning no restriction.
|
|
84
|
+
|
|
85
|
+
### Returns:
|
|
86
|
+
|
|
87
|
+
- **Object** with two properties:
|
|
88
|
+
- `unit`: (string|null) ➡️ **The unit of the value** (e.g., 'MB', 'GB', etc.).
|
|
89
|
+
- `value`: (number|null) ➡️ **The formatted byte value.**
|
|
90
|
+
If the input is invalid, returns `null` for both.
|
|
91
|
+
|
|
92
|
+
### Example Usage:
|
|
93
|
+
|
|
94
|
+
```js
|
|
95
|
+
formatBytes(123456789);
|
|
96
|
+
// Returns: { unit: 'MB', value: 117.74 }
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
```js
|
|
100
|
+
formatBytes(1073741824, 2, 'MB');
|
|
101
|
+
// Returns: { unit: 'MB', value: 1024 }
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
formatBytes(10485760);
|
|
106
|
+
// Returns: { unit: 'MB', value: 10 }
|
|
107
|
+
|
|
108
|
+
formatBytes(1073741824);
|
|
109
|
+
// Returns: { unit: 'GB', value: 1 }
|
|
110
|
+
|
|
111
|
+
formatBytes(1073741824, 2, 'MB');
|
|
112
|
+
// Returns: { unit: 'MB', value: 1024 }
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
---
|
|
116
|
+
|
|
117
|
+
### Notes:
|
|
118
|
+
|
|
119
|
+
* **Formatting:** Converts bytes to the most appropriate unit (from 'Bytes' to 'YB') based on the byte value.
|
|
120
|
+
* **Max Unit:** The `maxUnit` parameter allows you to limit the highest unit for conversion. If not provided, it will convert all the way up to 'YB'.
|
|
121
|
+
* **Decimals:** The result can be customized with a specified number of decimal places for precision. If `decimals` is `null`, no rounding is applied, and the full precision value is returned.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tiny-essentials",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
4
4
|
"description": "Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "npm run test:mjs && npm run test:cjs && npm run test:js",
|