tiny-essentials 1.8.1 → 1.8.3

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.
@@ -2203,8 +2203,8 @@ async function asyncReplace(str, regex, asyncFn) {
2203
2203
  * This implementation ensures a uniform distribution of permutations.
2204
2204
  * Original algorithm source: StackOverflow (link above).
2205
2205
  *
2206
- * @param {string[]} items - The array to shuffle.
2207
- * @returns {string[]} The same array instance, now shuffled in place.
2206
+ * @param {any[]} items - The array to shuffle.
2207
+ * @returns {any[]} The same array instance, now shuffled in place.
2208
2208
  */
2209
2209
  function shuffleArray(items) {
2210
2210
  let currentIndex = items.length,
@@ -2805,6 +2805,49 @@ function getAge(timeData = 0, now = null) {
2805
2805
  return null;
2806
2806
  }
2807
2807
 
2808
+ /**
2809
+ * @typedef {Object} FormattedByteResult
2810
+ * @property {string|null} unit - The resulting unit (e.g., 'MB', 'GB') or null if input is invalid.
2811
+ * @property {number|null} value - The numerical value in the chosen unit, or null if input is invalid.
2812
+ */
2813
+
2814
+ /**
2815
+ * Converts a byte value into a human-readable format with unit and value separated.
2816
+ *
2817
+ * @param {number} bytes - The number of bytes to format. Must be a non-negative number.
2818
+ * @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.
2819
+ * @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'.
2820
+ * @returns {FormattedByteResult} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
2821
+ *
2822
+ * @example
2823
+ * formatBytes(123456789);
2824
+ * // → { unit: 'MB', value: 117.74 }
2825
+ *
2826
+ * @example
2827
+ * formatBytes(1073741824, 2, 'MB');
2828
+ * // → { unit: 'MB', value: 1024 }
2829
+ */
2830
+ function formatBytes(bytes, decimals = null, maxUnit = null) {
2831
+ if (typeof bytes !== 'number' || bytes < 0) return { unit: null, value: null };
2832
+ if (bytes === 0) return { unit: 'Bytes', value: 0 };
2833
+
2834
+ const k = 1024;
2835
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
2836
+
2837
+ const maxIndex = maxUnit && sizes.includes(maxUnit) ? sizes.indexOf(maxUnit) : sizes.length - 1;
2838
+ const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), maxIndex);
2839
+
2840
+ let value = bytes / Math.pow(k, i);
2841
+
2842
+ if (decimals !== null) {
2843
+ const dm = decimals < 0 ? 0 : decimals;
2844
+ value = parseFloat(value.toFixed(dm));
2845
+ }
2846
+
2847
+ const unit = sizes[i];
2848
+ return { unit, value };
2849
+ }
2850
+
2808
2851
  ;// ./src/v1/basics/text.mjs
2809
2852
  /**
2810
2853
  * Converts a string to title case where the first letter of each word is capitalized.
@@ -2349,8 +2349,8 @@ class TinyLevelUp {
2349
2349
  * This implementation ensures a uniform distribution of permutations.
2350
2350
  * Original algorithm source: StackOverflow (link above).
2351
2351
  *
2352
- * @param {string[]} items - The array to shuffle.
2353
- * @returns {string[]} The same array instance, now shuffled in place.
2352
+ * @param {any[]} items - The array to shuffle.
2353
+ * @returns {any[]} The same array instance, now shuffled in place.
2354
2354
  */
2355
2355
  function shuffleArray(items) {
2356
2356
  let currentIndex = items.length,
@@ -2951,6 +2951,49 @@ function getAge(timeData = 0, now = null) {
2951
2951
  return null;
2952
2952
  }
2953
2953
 
2954
+ /**
2955
+ * @typedef {Object} FormattedByteResult
2956
+ * @property {string|null} unit - The resulting unit (e.g., 'MB', 'GB') or null if input is invalid.
2957
+ * @property {number|null} value - The numerical value in the chosen unit, or null if input is invalid.
2958
+ */
2959
+
2960
+ /**
2961
+ * Converts a byte value into a human-readable format with unit and value separated.
2962
+ *
2963
+ * @param {number} bytes - The number of bytes to format. Must be a non-negative number.
2964
+ * @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.
2965
+ * @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'.
2966
+ * @returns {FormattedByteResult} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
2967
+ *
2968
+ * @example
2969
+ * formatBytes(123456789);
2970
+ * // → { unit: 'MB', value: 117.74 }
2971
+ *
2972
+ * @example
2973
+ * formatBytes(1073741824, 2, 'MB');
2974
+ * // → { unit: 'MB', value: 1024 }
2975
+ */
2976
+ function formatBytes(bytes, decimals = null, maxUnit = null) {
2977
+ if (typeof bytes !== 'number' || bytes < 0) return { unit: null, value: null };
2978
+ if (bytes === 0) return { unit: 'Bytes', value: 0 };
2979
+
2980
+ const k = 1024;
2981
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
2982
+
2983
+ const maxIndex = maxUnit && sizes.includes(maxUnit) ? sizes.indexOf(maxUnit) : sizes.length - 1;
2984
+ const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), maxIndex);
2985
+
2986
+ let value = bytes / Math.pow(k, i);
2987
+
2988
+ if (decimals !== null) {
2989
+ const dm = decimals < 0 ? 0 : decimals;
2990
+ value = parseFloat(value.toFixed(dm));
2991
+ }
2992
+
2993
+ const unit = sizes[i];
2994
+ return { unit, value };
2995
+ }
2996
+
2954
2997
  ;// ./src/v1/basics/text.mjs
2955
2998
  /**
2956
2999
  * Converts a string to title case where the first letter of each word is capitalized.
@@ -8,8 +8,8 @@
8
8
  * This implementation ensures a uniform distribution of permutations.
9
9
  * Original algorithm source: StackOverflow (link above).
10
10
  *
11
- * @param {string[]} items - The array to shuffle.
12
- * @returns {string[]} The same array instance, now shuffled in place.
11
+ * @param {any[]} items - The array to shuffle.
12
+ * @returns {any[]} The same array instance, now shuffled in place.
13
13
  */
14
14
  function shuffleArray(items) {
15
15
  let currentIndex = items.length,
@@ -4,8 +4,8 @@
4
4
  * This implementation ensures a uniform distribution of permutations.
5
5
  * Original algorithm source: StackOverflow (link above).
6
6
  *
7
- * @param {string[]} items - The array to shuffle.
8
- * @returns {string[]} The same array instance, now shuffled in place.
7
+ * @param {any[]} items - The array to shuffle.
8
+ * @returns {any[]} The same array instance, now shuffled in place.
9
9
  */
10
- export function shuffleArray(items: string[]): string[];
10
+ export function shuffleArray(items: any[]): any[];
11
11
  //# sourceMappingURL=array.d.mts.map
@@ -5,8 +5,8 @@
5
5
  * This implementation ensures a uniform distribution of permutations.
6
6
  * Original algorithm source: StackOverflow (link above).
7
7
  *
8
- * @param {string[]} items - The array to shuffle.
9
- * @returns {string[]} The same array instance, now shuffled in place.
8
+ * @param {any[]} items - The array to shuffle.
9
+ * @returns {any[]} The same array instance, now shuffled in place.
10
10
  */
11
11
  export function shuffleArray(items) {
12
12
  let currentIndex = items.length, randomIndex;
@@ -83,6 +83,50 @@ function getAge(timeData = 0, now = null) {
83
83
  return null;
84
84
  }
85
85
 
86
+ /**
87
+ * @typedef {Object} FormattedByteResult
88
+ * @property {string|null} unit - The resulting unit (e.g., 'MB', 'GB') or null if input is invalid.
89
+ * @property {number|null} value - The numerical value in the chosen unit, or null if input is invalid.
90
+ */
91
+
92
+ /**
93
+ * Converts a byte value into a human-readable format with unit and value separated.
94
+ *
95
+ * @param {number} bytes - The number of bytes to format. Must be a non-negative number.
96
+ * @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.
97
+ * @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'.
98
+ * @returns {FormattedByteResult} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
99
+ *
100
+ * @example
101
+ * formatBytes(123456789);
102
+ * // → { unit: 'MB', value: 117.74 }
103
+ *
104
+ * @example
105
+ * formatBytes(1073741824, 2, 'MB');
106
+ * // → { unit: 'MB', value: 1024 }
107
+ */
108
+ function formatBytes(bytes, decimals = null, maxUnit = null) {
109
+ if (typeof bytes !== 'number' || bytes < 0) return { unit: null, value: null };
110
+ if (bytes === 0) return { unit: 'Bytes', value: 0 };
111
+
112
+ const k = 1024;
113
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
114
+
115
+ const maxIndex = maxUnit && sizes.includes(maxUnit) ? sizes.indexOf(maxUnit) : sizes.length - 1;
116
+ const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), maxIndex);
117
+
118
+ let value = bytes / Math.pow(k, i);
119
+
120
+ if (decimals !== null) {
121
+ const dm = decimals < 0 ? 0 : decimals;
122
+ value = parseFloat(value.toFixed(dm));
123
+ }
124
+
125
+ const unit = sizes[i];
126
+ return { unit, value };
127
+ }
128
+
129
+ exports.formatBytes = formatBytes;
86
130
  exports.getAge = getAge;
87
131
  exports.getSimplePerc = getSimplePerc;
88
132
  exports.ruleOfThree = ruleOfThree;
@@ -52,4 +52,36 @@ 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
+ * @typedef {Object} FormattedByteResult
57
+ * @property {string|null} unit - The resulting unit (e.g., 'MB', 'GB') or null if input is invalid.
58
+ * @property {number|null} value - The numerical value in the chosen unit, or null if input is invalid.
59
+ */
60
+ /**
61
+ * Converts a byte value into a human-readable format with unit and value separated.
62
+ *
63
+ * @param {number} bytes - The number of bytes to format. Must be a non-negative number.
64
+ * @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.
65
+ * @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'.
66
+ * @returns {FormattedByteResult} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
67
+ *
68
+ * @example
69
+ * formatBytes(123456789);
70
+ * // → { unit: 'MB', value: 117.74 }
71
+ *
72
+ * @example
73
+ * formatBytes(1073741824, 2, 'MB');
74
+ * // → { unit: 'MB', value: 1024 }
75
+ */
76
+ export function formatBytes(bytes: number, decimals?: number | null, maxUnit?: string | null): FormattedByteResult;
77
+ export type FormattedByteResult = {
78
+ /**
79
+ * - The resulting unit (e.g., 'MB', 'GB') or null if input is invalid.
80
+ */
81
+ unit: string | null;
82
+ /**
83
+ * - The numerical value in the chosen unit, or null if input is invalid.
84
+ */
85
+ value: number | null;
86
+ };
55
87
  //# sourceMappingURL=simpleMath.d.mts.map
@@ -73,3 +73,41 @@ export function getAge(timeData = 0, now = null) {
73
73
  }
74
74
  return null;
75
75
  }
76
+ /**
77
+ * @typedef {Object} FormattedByteResult
78
+ * @property {string|null} unit - The resulting unit (e.g., 'MB', 'GB') or null if input is invalid.
79
+ * @property {number|null} value - The numerical value in the chosen unit, or null if input is invalid.
80
+ */
81
+ /**
82
+ * Converts a byte value into a human-readable format with unit and value separated.
83
+ *
84
+ * @param {number} bytes - The number of bytes to format. Must be a non-negative number.
85
+ * @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.
86
+ * @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'.
87
+ * @returns {FormattedByteResult} An object with the converted value and its corresponding unit. Returns nulls if input is invalid.
88
+ *
89
+ * @example
90
+ * formatBytes(123456789);
91
+ * // → { unit: 'MB', value: 117.74 }
92
+ *
93
+ * @example
94
+ * formatBytes(1073741824, 2, 'MB');
95
+ * // → { unit: 'MB', value: 1024 }
96
+ */
97
+ export function formatBytes(bytes, decimals = null, maxUnit = null) {
98
+ if (typeof bytes !== 'number' || bytes < 0)
99
+ return { unit: null, value: null };
100
+ if (bytes === 0)
101
+ return { unit: 'Bytes', value: 0 };
102
+ const k = 1024;
103
+ const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
104
+ const maxIndex = maxUnit && sizes.includes(maxUnit) ? sizes.indexOf(maxUnit) : sizes.length - 1;
105
+ const i = Math.min(Math.floor(Math.log(bytes) / Math.log(k)), maxIndex);
106
+ let value = bytes / Math.pow(k, i);
107
+ if (decimals !== null) {
108
+ const dm = decimals < 0 ? 0 : decimals;
109
+ value = parseFloat(value.toFixed(dm));
110
+ }
111
+ const unit = sizes[i];
112
+ return { unit, value };
113
+ }
@@ -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.1",
3
+ "version": "1.8.3",
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",