@clerk/shared
Version:
Internal package utils used by the Clerk SDKs
117 lines (116 loc) • 3.65 kB
JavaScript
//#region src/underscore.ts
/**
* Convert words to a sentence.
*
* @param items - An array of words to be joined.
* @returns A string with the items joined by a comma and the last item joined by ", or".
*/
const toSentence = (items) => {
if (items.length == 0) return "";
if (items.length == 1) return items[0];
let sentence = items.slice(0, -1).join(", ");
sentence += `, or ${items.slice(-1)}`;
return sentence;
};
const IP_V4_ADDRESS_REGEX = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
/**
* Checks if a string is a valid IPv4 address.
*
* @returns True if the string is a valid IPv4 address, false otherwise.
*/
function isIPV4Address(str) {
return IP_V4_ADDRESS_REGEX.test(str || "");
}
/**
* Converts the first character of a string to uppercase.
*
* @param str - The string to be converted.
* @returns The modified string with the rest of the string unchanged.
*
* @example
* ```ts
* titleize('hello world') // 'Hello world'
* ```
*/
function titleize(str) {
const s = str || "";
return s.charAt(0).toUpperCase() + s.slice(1);
}
/**
* Converts a string from snake_case to camelCase.
*/
function snakeToCamel(str) {
return str ? str.replace(/([-_][a-z])/g, (match) => match.toUpperCase().replace(/-|_/, "")) : "";
}
/**
* Converts a string from camelCase to snake_case.
*/
function camelToSnake(str) {
return str ? str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`) : "";
}
const createDeepObjectTransformer = (transform) => {
const deepTransform = (obj) => {
if (!obj) return obj;
if (Array.isArray(obj)) return obj.map((el) => {
if (typeof el === "object" || Array.isArray(el)) return deepTransform(el);
return el;
});
const copy = { ...obj };
const keys = Object.keys(copy);
for (const oldName of keys) {
const newName = transform(oldName.toString());
if (newName !== oldName) {
copy[newName] = copy[oldName];
delete copy[oldName];
}
if (typeof copy[newName] === "object") copy[newName] = deepTransform(copy[newName]);
}
return copy;
};
return deepTransform;
};
/**
* Transforms camelCased objects/ arrays to snake_cased.
* This function recursively traverses all objects and arrays of the passed value
* camelCased keys are removed.
*
* @function
*/
const deepCamelToSnake = createDeepObjectTransformer(camelToSnake);
/**
* Transforms snake_cased objects/ arrays to camelCased.
* This function recursively traverses all objects and arrays of the passed value
* camelCased keys are removed.
*
* @function
*/
const deepSnakeToCamel = createDeepObjectTransformer(snakeToCamel);
/**
* A function to determine if a value is truthy.
*
* @returns True for `true`, true, positive numbers. False for `false`, false, 0, negative integers and anything else.
*/
function isTruthy(value) {
if (typeof value === `boolean`) return value;
if (value === void 0 || value === null) return false;
if (typeof value === `string`) {
if (value.toLowerCase() === `true`) return true;
if (value.toLowerCase() === `false`) return false;
}
const number = parseInt(value, 10);
if (isNaN(number)) return false;
if (number > 0) return true;
return false;
}
/**
* Get all non-undefined values from an object.
*/
function getNonUndefinedValues(obj) {
return Object.entries(obj).reduce((acc, [key, value]) => {
if (value !== void 0) acc[key] = value;
return acc;
}, {});
}
//#endregion
export { camelToSnake, deepCamelToSnake, deepSnakeToCamel, getNonUndefinedValues, isIPV4Address, isTruthy, snakeToCamel, titleize, toSentence };
//# sourceMappingURL=underscore.mjs.map