@instawork/design-system
Version:
The design system for Instawork's web apps
33 lines • 1.18 kB
JavaScript
export function isArrayLike(obj) {
return (obj === null || obj === void 0 ? void 0 : obj.hasOwnProperty('length')) || false;
}
/**
* Returns `false` if the object has a length or value equal to +/-1; otherwise, `true`
* @param obj - A numeric value, array, or other object with a "length" property
*/
export function isPlural(obj) {
if (!obj) {
return true;
}
if (typeof obj === 'number') {
return Math.abs(obj) !== 1;
}
if (typeof obj === 'string') {
const parsed = parseFloat(obj);
if (!isNaN(parsed)) {
return isPlural(parsed);
}
}
return isArrayLike(obj) ? isPlural(obj.length) : true;
}
/**
* Returns the value passed for `plural` if the `obj` is plural according to {@link isPlural}; otherwise, returns the
* value passed for `singular`.
* @param obj - A numeric value, array, or other object with a "length" property
* @param singular - The string to return if `obj` is not plural
* @param plural - The string to return if `obj` is plural
*/
export function pluralize(obj, singular, plural) {
return isPlural(obj) ? plural : singular;
}
//# sourceMappingURL=pluralize.js.map