@akadenia/helpers 1.7.3 → 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/dist/file.d.ts CHANGED
@@ -8,3 +8,12 @@
8
8
  * checkFileExtension("test.txt", ["md"]) // false
9
9
  */
10
10
  export declare const checkFileExtension: (filePath: string, validExtensions: string[]) => boolean;
11
+ /**
12
+ * Format a file size to a human readable string
13
+ * @param bytes The file size in bytes
14
+ * @returns The formatted file size
15
+ * @example
16
+ * formatFileSize(1024) // "1.0 KB"
17
+ * formatFileSize(1024 * 1024) // "1.0 MB"
18
+ */
19
+ export declare const formatFileSize: (bytes: number | null) => string;
package/dist/file.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkFileExtension = void 0;
3
+ exports.formatFileSize = exports.checkFileExtension = void 0;
4
4
  /**
5
5
  * Check if a file path has a valid extension
6
6
  * @param filePath The file path to check
@@ -15,3 +15,21 @@ const checkFileExtension = (filePath, validExtensions) => {
15
15
  return !!fileExtension && validExtensions.includes(fileExtension);
16
16
  };
17
17
  exports.checkFileExtension = checkFileExtension;
18
+ /**
19
+ * Format a file size to a human readable string
20
+ * @param bytes The file size in bytes
21
+ * @returns The formatted file size
22
+ * @example
23
+ * formatFileSize(1024) // "1.0 KB"
24
+ * formatFileSize(1024 * 1024) // "1.0 MB"
25
+ */
26
+ const formatFileSize = (bytes) => {
27
+ if (!bytes || bytes <= 0)
28
+ return "0 B";
29
+ const units = ["B", "KB", "MB", "GB", "TB"];
30
+ const unitIndex = Math.floor(Math.log2(bytes) / 10);
31
+ const clampedIndex = Math.min(unitIndex, units.length - 1);
32
+ const size = bytes / Math.pow(1024, clampedIndex);
33
+ return `${size.toFixed(clampedIndex === 0 ? 0 : 1)} ${units[clampedIndex]}`;
34
+ };
35
+ exports.formatFileSize = formatFileSize;
package/dist/object.d.ts CHANGED
@@ -74,3 +74,10 @@ export declare const findEntry: <EntryType>(array: EntryType[], predicate: (item
74
74
  export declare function convertArrayToMap<T extends object>(arrayData: T[], keyProp: keyof T): {
75
75
  [key: string]: T;
76
76
  };
77
+ /**
78
+ * Converts object keys from camelCase to kebab-case
79
+ * @template T extends Record<string, any> - The type of the object
80
+ * @param {T | undefined} obj - The object to convert
81
+ * @returns {Record<string, any> | undefined} - The object with kebab-case keys or undefined
82
+ */
83
+ export declare const convertObjectKeysToKebabCase: <T extends Record<string, any>>(obj: T | undefined) => Record<string, any> | undefined;
package/dist/object.js CHANGED
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findEntry = exports.objectPropHasValue = exports.parseCookie = exports.isPureObject = void 0;
3
+ exports.convertObjectKeysToKebabCase = exports.findEntry = exports.objectPropHasValue = exports.parseCookie = exports.isPureObject = void 0;
4
4
  exports.filterObjectsByProperty = filterObjectsByProperty;
5
5
  exports.containsSubObject = containsSubObject;
6
6
  exports.findObjectBySubObject = findObjectBySubObject;
7
7
  exports.filterObjectsBySubObject = filterObjectsBySubObject;
8
8
  exports.convertArrayToMap = convertArrayToMap;
9
+ const text_1 = require("./text");
9
10
  /**
10
11
  *
11
12
  * @function
@@ -121,3 +122,15 @@ function convertArrayToMap(arrayData, keyProp) {
121
122
  }, dataAsObject);
122
123
  return dataAsObject;
123
124
  }
125
+ /**
126
+ * Converts object keys from camelCase to kebab-case
127
+ * @template T extends Record<string, any> - The type of the object
128
+ * @param {T | undefined} obj - The object to convert
129
+ * @returns {Record<string, any> | undefined} - The object with kebab-case keys or undefined
130
+ */
131
+ const convertObjectKeysToKebabCase = (obj) => {
132
+ if (!obj)
133
+ return undefined;
134
+ return Object.entries(obj).reduce((acc, [key, value]) => (Object.assign(Object.assign({}, acc), { [(0, text_1.convertCamelToKebabCase)(key)]: value })), {});
135
+ };
136
+ exports.convertObjectKeysToKebabCase = convertObjectKeysToKebabCase;
package/dist/text.d.ts CHANGED
@@ -82,6 +82,13 @@ export declare function convertCamelToKebabCase(word: string): string;
82
82
  * @returns {string} - The word returned as camel case
83
83
  */
84
84
  export declare function convertKebabToCamelCase(word: string): string;
85
+ /**
86
+ * Convert camel case to readable text
87
+ * @function
88
+ * @param {string} name - The name to be converted to readable text
89
+ * @returns {string} - The readable text
90
+ */
91
+ export declare function convertCamelCaseToReadableText(name: string): string;
85
92
  /**
86
93
  * Generate acronym from text
87
94
  * @param term term to be converted to an acronym
package/dist/text.js CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.abbreviateNumber = exports.isValidEmail = exports.enforceCharacterLimit = exports.capitalizeText = exports.handleNullDisplay = exports.convertCamelToSnakeCase = exports.convertSnakeToCamelCase = exports.pluralizeOnCondition = exports.replaceUnderscoreWithSpaces = exports.replaceSpacesWithUnderscore = exports.fileNameFromPath = exports.truncateText = exports.formatPosition = exports.uuidv4 = exports.convertKeyCasing = void 0;
4
4
  exports.convertCamelToKebabCase = convertCamelToKebabCase;
5
5
  exports.convertKebabToCamelCase = convertKebabToCamelCase;
6
+ exports.convertCamelCaseToReadableText = convertCamelCaseToReadableText;
6
7
  exports.generateAcronym = generateAcronym;
7
8
  exports.isAcronym = isAcronym;
8
9
  exports.acronymToKebabCase = acronymToKebabCase;
@@ -121,7 +122,22 @@ exports.replaceUnderscoreWithSpaces = replaceUnderscoreWithSpaces;
121
122
  * @returns {string} - The word with "s" added to the end if the condition is true
122
123
  */
123
124
  const pluralizeOnCondition = (word, condition) => {
124
- return condition ? `${word}s` : word;
125
+ if (!condition)
126
+ return word;
127
+ const lowerCasedWord = word.toLowerCase();
128
+ // Handle words ending with 'y' (e.g., 'library' -> 'libraries')
129
+ if (lowerCasedWord.endsWith("y")) {
130
+ return `${word.slice(0, -1)}ies`;
131
+ }
132
+ // Handle words ending with 's', 'sh', or 'ch' (e.g., 'class' -> 'classes')
133
+ if (lowerCasedWord.endsWith("s") ||
134
+ lowerCasedWord.endsWith("sh") ||
135
+ lowerCasedWord.endsWith("ch") ||
136
+ lowerCasedWord.endsWith("x")) {
137
+ return `${word}es`;
138
+ }
139
+ // Default pluralization by adding 's'
140
+ return `${word}s`;
125
141
  };
126
142
  exports.pluralizeOnCondition = pluralizeOnCondition;
127
143
  /**
@@ -163,6 +179,19 @@ function convertKebabToCamelCase(word) {
163
179
  .map((token) => (0, exports.capitalizeText)(token))
164
180
  .join("");
165
181
  }
182
+ /**
183
+ * Convert camel case to readable text
184
+ * @function
185
+ * @param {string} name - The name to be converted to readable text
186
+ * @returns {string} - The readable text
187
+ */
188
+ function convertCamelCaseToReadableText(name) {
189
+ return name
190
+ .replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
191
+ .replace(/([a-z\d])([A-Z])/g, "$1 $2")
192
+ .replace(/^./, (c) => c.toUpperCase())
193
+ .trim();
194
+ }
166
195
  /**
167
196
  * Generate acronym from text
168
197
  * @param term term to be converted to an acronym
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akadenia/helpers",
3
- "version": "1.7.3",
3
+ "version": "1.9.0",
4
4
  "description": "Akadenia helpers",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -9,7 +9,7 @@
9
9
  "akadenia"
10
10
  ],
11
11
  "scripts": {
12
- "build": "rm -rf dist && tsc -p tsconfig.json",
12
+ "build": "rimraf dist && tsc -p tsconfig.json",
13
13
  "test": "jest",
14
14
  "format": "prettier --write \"./**/*.{ts,tsx,js,jsx,json,yml}\"",
15
15
  "lint": "prettier --check \"./**/*.{ts,tsx,js,jsx,json,yml}\"",
@@ -42,6 +42,7 @@
42
42
  "jest": "^29.7.0",
43
43
  "jsdoc-to-markdown": "^8.0.3",
44
44
  "prettier": "^3.3.3",
45
+ "rimraf": "^6.0.1",
45
46
  "semantic-release": "22.0.12",
46
47
  "ts-jest": "^29.2.5",
47
48
  "typescript": "^5.6.2"