@akadenia/helpers 1.2.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/text.js ADDED
@@ -0,0 +1,256 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateWordFromId = exports.generateIDFromWord = exports.isValidEmail = exports.enforceCharacterLimit = exports.capitalizeText = exports.handleNullDisplay = exports.acronymToKebabCase = exports.isAcronym = exports.convertKebabToCamelCase = exports.convertCamelToKebabCase = exports.convertCamelToSnakeCase = exports.convertSnakeToCamelCase = exports.pluralizeOnCondition = exports.replaceUnderscoreWithSpaces = exports.replaceSpacesWithUnderscore = exports.fileNameFromPath = exports.truncateText = exports.formatPosition = exports.uuidv4 = exports.convertKeyCasing = void 0;
4
+ const _1 = require("./");
5
+ // Internal Helper Functions At The Top
6
+ /**
7
+ *
8
+ * @function
9
+ * @param {T} input - The string, object, or array of objects that needs to have its casing changed
10
+ * @param {"toSnakeCase" | "toCamelCase"} type - The type of casing the string, object, or array of objects should be converted to (snake_case or camelCase)
11
+ * @returns {T} - A new string, object, or array of objects in the specified casing
12
+ */
13
+ const convertKeyCasing = (input, type) => {
14
+ if (typeof input === "string") {
15
+ const transformedString = type === "toSnakeCase"
16
+ ? input.replace(/([A-Z0-9])/g, "_$1").toLowerCase()
17
+ : input.toLowerCase().replace(/(_\w)/g, (m) => m[1].toUpperCase());
18
+ return transformedString;
19
+ }
20
+ else if (_1.ObjectHelpers.isPureObject(input)) {
21
+ const newObj = {};
22
+ for (const key in input) {
23
+ if (Object.prototype.hasOwnProperty.call(input, key)) {
24
+ const newKey = type === "toSnakeCase" ? key.replace(/([A-Z])/g, "_$1").toLowerCase() : key.replace(/(_\w)/g, (m) => m[1].toUpperCase());
25
+ if (_1.ObjectHelpers.isPureObject(input[key]) || Array.isArray(input[key])) {
26
+ newObj[newKey] = (0, exports.convertKeyCasing)(input[key], type);
27
+ }
28
+ else {
29
+ newObj[newKey] = input[key];
30
+ }
31
+ }
32
+ }
33
+ return newObj;
34
+ }
35
+ else if (Array.isArray(input)) {
36
+ return input.map((item) => (0, exports.convertKeyCasing)(item, type));
37
+ }
38
+ return input;
39
+ };
40
+ exports.convertKeyCasing = convertKeyCasing;
41
+ /**
42
+ *
43
+ * @function
44
+ * @returns {string} - A randomly generated string.
45
+ */
46
+ const uuidv4 = () => {
47
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
48
+ const r = (Math.random() * 16) | 0;
49
+ const v = c === "x" ? r : (r & 0x3) | 0x8;
50
+ return v.toString(16);
51
+ });
52
+ };
53
+ exports.uuidv4 = uuidv4;
54
+ /**
55
+ *
56
+ * @function
57
+ * @param {number} position - A position number.
58
+ * @returns {string} - A string representing the position with the appropriate suffix (st, nd, rd, or th) e.g "1st", "2nd", "3rd", "4th", etc.
59
+ */
60
+ const formatPosition = (position) => {
61
+ let postfix = "th";
62
+ if (position === 1) {
63
+ postfix = "st";
64
+ }
65
+ else if (position === 2) {
66
+ postfix = "nd";
67
+ }
68
+ else if (position === 3) {
69
+ postfix = "rd";
70
+ }
71
+ return `${position}${postfix}`;
72
+ };
73
+ exports.formatPosition = formatPosition;
74
+ /**
75
+ *
76
+ * @function
77
+ * @param {string} text - The text to be truncated
78
+ * @param {number} characterLimit - The maximum number of characters that the text can have
79
+ * @returns {string} - The truncated text with "..." added at the end if the text has exceeded the character limit
80
+ */
81
+ const truncateText = (text, characterLimit) => text.length > characterLimit ? text.substring(0, characterLimit - 3) + "..." : text;
82
+ exports.truncateText = truncateText;
83
+ /**
84
+ *
85
+ * @function
86
+ * @param {string} path - The path of the file
87
+ * @returns {string} - The file name
88
+ */
89
+ const fileNameFromPath = (path) => path.substring(path.lastIndexOf("/") + 1);
90
+ exports.fileNameFromPath = fileNameFromPath;
91
+ /**
92
+ *
93
+ * @function
94
+ * @param {string} s - The string that needs to have spaces replaced with underscores
95
+ * @returns {string} - The string with spaces replaced with underscores
96
+ */
97
+ const replaceSpacesWithUnderscore = (s) => (s === null || s === void 0 ? void 0 : s.trim().replace(/\s/g, "_")) || "";
98
+ exports.replaceSpacesWithUnderscore = replaceSpacesWithUnderscore;
99
+ /**
100
+ *
101
+ * @function
102
+ * @param {string} s - The string that needs to have underscores replaced with spaces
103
+ * @returns {string} - The string with underscores replaced with spaces
104
+ */
105
+ const replaceUnderscoreWithSpaces = (s) => (s === null || s === void 0 ? void 0 : s.trim().replace(/_/g, " ")) || "";
106
+ exports.replaceUnderscoreWithSpaces = replaceUnderscoreWithSpaces;
107
+ /**
108
+ *
109
+ * @function
110
+ * @param {string} word - The word that needs to be pluralized
111
+ * @param {boolean} condition - The condition that determines if the word needs to be pluralized
112
+ * @returns {string} - The word with "s" added to the end if the condition is true
113
+ */
114
+ const pluralizeOnCondition = (word, condition) => {
115
+ return condition ? `${word}s` : word;
116
+ };
117
+ exports.pluralizeOnCondition = pluralizeOnCondition;
118
+ /**
119
+ * @function
120
+ * @param {Object | Array<Object> | string} data - The object or array of objects to convert key cases
121
+ * @returns {Object | Array<Object> | string} - The object or array of objects with the keys in camelCase
122
+ */
123
+ const convertSnakeToCamelCase = (data) => {
124
+ return (0, exports.convertKeyCasing)(data, "toCamelCase");
125
+ };
126
+ exports.convertSnakeToCamelCase = convertSnakeToCamelCase;
127
+ /**
128
+ * @function
129
+ * @param {Object | Array<Object> | string} data - The object or array of objects to convert key cases
130
+ * @returns {Object | Array<Object> | string} - The object or array of objects with the keys in snake_case
131
+ */
132
+ const convertCamelToSnakeCase = (data) => {
133
+ return (0, exports.convertKeyCasing)(data, "toSnakeCase");
134
+ };
135
+ exports.convertCamelToSnakeCase = convertCamelToSnakeCase;
136
+ /**
137
+ * Convert camel case to kebab case
138
+ * @function
139
+ * @param {string} word - The word needed to be converted to kebab case from camel case
140
+ * @returns {string} - The word returned as kebab case
141
+ */
142
+ function convertCamelToKebabCase(word) {
143
+ return word.replace(/[A-Z]+(?![a-z])|[A-Z]/g, ($, ofs) => (ofs ? "-" : "") + $.toLowerCase());
144
+ }
145
+ exports.convertCamelToKebabCase = convertCamelToKebabCase;
146
+ /**
147
+ * Convert kebab case to camel case
148
+ * @function
149
+ * @param {string} word - The word needed to be converted from kebab case to camel case
150
+ * @returns {string} - The word returned as camel case
151
+ */
152
+ function convertKebabToCamelCase(word) {
153
+ return word
154
+ .split("-")
155
+ .map((token) => (0, exports.capitalizeText)(token))
156
+ .join("");
157
+ }
158
+ exports.convertKebabToCamelCase = convertKebabToCamelCase;
159
+ /**
160
+ * Validate if a word is acronym
161
+ * @function
162
+ * @param {string} word - The word that is being validated as acronym
163
+ * @returns {string} - The boolean value when the condition is met
164
+ */
165
+ function isAcronym(word) {
166
+ return word.toUpperCase() === word;
167
+ }
168
+ exports.isAcronym = isAcronym;
169
+ /**
170
+ * Convert acronym to kebab case
171
+ * @function
172
+ * @param {string} word - The acronym to be converted to kebab case
173
+ * @returns {string} - The word returned as camel case
174
+ */
175
+ function acronymToKebabCase(word) {
176
+ if (!isAcronym(word)) {
177
+ throw new Error(`The text passed: ${word} is not an acronym.`);
178
+ }
179
+ return word
180
+ .split("")
181
+ .map((token) => token.toLowerCase())
182
+ .join("-");
183
+ }
184
+ exports.acronymToKebabCase = acronymToKebabCase;
185
+ /**
186
+ * @function
187
+ * @param value The string to be displayed
188
+ * @returns The passed string or the default string if the passed string is null or undefined
189
+ */
190
+ const handleNullDisplay = (value, defaultValue = "N/A") => value !== null && value !== void 0 ? value : defaultValue;
191
+ exports.handleNullDisplay = handleNullDisplay;
192
+ /**
193
+ * @function
194
+ * @param text The string to be capitalized
195
+ * @returns The string in a capitalized form
196
+ */
197
+ const capitalizeText = (text) => {
198
+ if (!text)
199
+ return "";
200
+ return text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
201
+ };
202
+ exports.capitalizeText = capitalizeText;
203
+ /**
204
+ * Enforces a character limit on a given string.
205
+ * @function
206
+ * @param {object} options - The options for the function.
207
+ * @param {string} options.text - The text to limit.
208
+ * @param {number} options.characterLimit - The maximum number of characters allowed.
209
+ * @param {function} options.onCharacterLimit - The function to call when the character limit is exceeded.
210
+ * @returns {string} The original string if it is shorter than the character limit, or a truncated version of the string if it is longer.
211
+ */
212
+ const enforceCharacterLimit = ({ text, characterLimit, onCharacterLimit, }) => {
213
+ if (text.length > characterLimit) {
214
+ onCharacterLimit();
215
+ return text.slice(0, characterLimit);
216
+ }
217
+ return text;
218
+ };
219
+ exports.enforceCharacterLimit = enforceCharacterLimit;
220
+ /**
221
+ * Validate email if its a proper email or not
222
+ * @function
223
+ * @param email The email to be validated
224
+ * @returns The boolean value when the condition is met
225
+ */
226
+ const isValidEmail = (email) => {
227
+ const emailRegex = /^[\w-.]+@([\w-]+\.)+[\w-]{2,}$/;
228
+ const result = email === null || email === void 0 ? void 0 : email.match(emailRegex);
229
+ return !!result;
230
+ };
231
+ exports.isValidEmail = isValidEmail;
232
+ /**
233
+ * Generate ID from word
234
+ * @function
235
+ * @param word The word needed to generate ID from
236
+ * @returns The generated ID from the word
237
+ */
238
+ function generateIDFromWord(text) {
239
+ return text
240
+ .split(" ")
241
+ .map((word) => word.toLowerCase())
242
+ .join("-");
243
+ }
244
+ exports.generateIDFromWord = generateIDFromWord;
245
+ /**
246
+ * Get the word from the generated ID
247
+ * @function
248
+ * @param id The id that is needed to get the word from.
249
+ * @param customList The word lists that are used to check the original words from.
250
+ * @returns The word that is got from the id
251
+ */
252
+ function generateWordFromId(id, customList = {}) {
253
+ var _a;
254
+ return (_a = customList[id]) !== null && _a !== void 0 ? _a : id.split("-").map(exports.capitalizeText).join(" ");
255
+ }
256
+ exports.generateWordFromId = generateWordFromId;
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@akadenia/helpers",
3
+ "version": "1.2.2",
4
+ "description": "Akadenia helpers",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "keywords": [
8
+ "helpers",
9
+ "akadenia"
10
+ ],
11
+ "scripts": {
12
+ "build": "rm -rf dist && tsc -p tsconfig.json",
13
+ "test": "jest",
14
+ "format": "prettier --write \"./**/*.{ts,tsx,js,jsx,json,yml}\"",
15
+ "lint": "prettier --check \"./**/*.{ts,tsx,js,jsx,json,yml}\"",
16
+ "prepare": "npm run build",
17
+ "version": "git add -A src",
18
+ "updateLink": "npm run build && npm rm @akadenia/helpers -g && npm link",
19
+ "generate:docs": "jsdoc2md dist/*.js"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/akadenia/AkadeniaHelpers.git"
27
+ },
28
+ "author": "Akadenia",
29
+ "license": "MIT",
30
+ "bugs": {
31
+ "url": "https://github.com/akadenia/AkadeniaHelpers/issues"
32
+ },
33
+ "homepage": "https://github.com/akadenia/AkadeniaHelpers#readme",
34
+ "devDependencies": {
35
+ "@commitlint/cli": "^18.6.1",
36
+ "@commitlint/config-conventional": "^18.6.2",
37
+ "@jest/globals": "^29.7.0",
38
+ "@semantic-release/changelog": "^6.0.3",
39
+ "@semantic-release/exec": "^6.0.3",
40
+ "@types/node": "^18.16.1",
41
+ "husky": "^9.0.11",
42
+ "jest": "^29.7.0",
43
+ "jsdoc-to-markdown": "^8.0.1",
44
+ "prettier": "^3.2.5",
45
+ "semantic-release": "22.0.12",
46
+ "ts-jest": "^29.1.2",
47
+ "typescript": "^5.4.3"
48
+ }
49
+ }