@rzl-zone/utils-js
Version:
A modern, lightweight set of JavaScript utility functions with TypeScript support for everyday development, crafted to enhance code readability and maintainability.
96 lines (89 loc) • 3.24 kB
JavaScript
/*!
* ====================================================
* Rzl Utils-JS.
* ----------------------------------------------------
* Version: 3.11.0.
* Author: Rizalvin Dwiky.
* Repository: https://github.com/rzl-zone/utils-js.
* ====================================================
*/
import { isEmptyString } from './chunk-ULQPCIA2.js';
import { isNonEmptyString, isPlainObject, isString, isNumber, getPreciseType } from './chunk-MSUW5VHZ.js';
var capitalizeFirst = (string, options = {
lowerCaseNextRest: true,
trim: false
}) => {
if (!isNonEmptyString(string)) return "";
if (!isPlainObject(options)) {
options = {};
}
const lowerCaseNextRest = options.lowerCaseNextRest !== false;
const trim = options.trim === true;
if (trim) string = string.trim();
return string[0].toUpperCase() + (lowerCaseNextRest ? string.slice(1).toLowerCase() : string.slice(1));
};
var capitalizeWords = (value, options = {
collapseSpaces: false,
trim: false
}) => {
if (!isNonEmptyString(value)) return "";
let result = value;
if (!isPlainObject(options)) {
options = {};
}
const collapseSpaces = options.collapseSpaces === true;
const trim = options.trim === true;
if (trim) {
result = result.trim();
}
if (collapseSpaces) {
const leadingSpaces = result.match(/^\s*/)?.[0] ?? "";
const trailingSpaces = result.match(/\s*$/)?.[0] ?? "";
result = result.trim().replace(/\s+/g, " ");
result = `${leadingSpaces}${result}${trailingSpaces}`;
}
return result.toLowerCase().split(" ").map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join(" ");
};
function stripHtmlTags(input) {
if (!isString(input)) {
return void 0;
}
if (isEmptyString(input)) {
return "";
}
const stripped = input.replace(/<\/?[a-zA-Z][^<>]*\/?>/g, " ").trim();
const cleaned = stripped.replace(/\s+/g, " ").trim();
return cleaned;
}
var getInitialsName = (name) => {
if (!isNonEmptyString(name)) return "";
name = name.replace(/\s+/g, " ").trim();
const nameParts = name.split(" ");
if (nameParts.length > 1) {
return nameParts[0][0] + nameParts[1][0].toUpperCase();
}
return name.length > 1 ? name.substring(0, 2).toUpperCase() : (
// First two letters for single-word names
name[0].toUpperCase()
);
};
var replaceAt = (index, originalString, replaceTo) => {
if (!isNumber(index) || !isString(replaceTo) || !isString(originalString)) {
throw new TypeError(
`First parameter (\`index\`) must be of type \`number\`, second parameter (\`originalString\`) and third parameter (\`replaceTo\`) must be of type \`string\`, but received: "['index': \`${getPreciseType(
index
)}\`, 'originalString': \`${getPreciseType(
originalString
)}\`, 'replaceTo': \`${getPreciseType(replaceTo)}\`]".`
);
}
if (index < 0 || index >= originalString.length) {
throw new RangeError(
`First parameter (\`index\`) is out of range from second parameter (\`originalString\`).`
);
}
return originalString.slice(0, index) + // Extract before the index
replaceTo + // Insert replacement
originalString.slice(index + 1);
};
export { capitalizeFirst, capitalizeWords, getInitialsName, replaceAt, stripHtmlTags };