made-data-converters
Version:
This package includes:
57 lines • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.reverseString = exports.toBoolean = exports.toNumber = exports.truncate = void 0;
/**
* Truncates a string value to the specified length with an ellipsis.
* @param {string} value - The value to truncate.
* @param {number} maxLength - The maximum length of the value.
* @return {string} A truncated string with ellipsis if the value's length is greater than the max length.
*/
function truncate(value, maxLength) {
if (isNullOrWhiteSpace(value) || value.length <= maxLength) {
return value;
}
const suffix = "...";
return value.substring(0, maxLength - suffix.length) + suffix;
}
exports.truncate = truncate;
/**
* Converts a string value to a number value.
* @param {string} value - The string value to convert.
* @return {number} The converted value as a number.
*/
function toNumber(value) {
if (isNullOrWhiteSpace(value)) {
return 0;
}
return value.match(/^[-+]?[0-9]*\.?[0-9]+$/)
? parseFloat(value)
: 0;
}
exports.toNumber = toNumber;
/**
* Converts a string value to a boolean value.
* @param {string} value - The string value to convert.
* @return {boolean} The converted value as a boolean.
*/
function toBoolean(value) {
if (isNullOrWhiteSpace(value)) {
return false;
}
return value === 'true' || value === '1' || value === 'on' || value === 'checked' || value === 'selected' || value === 'yes';
}
exports.toBoolean = toBoolean;
;
/**
* Reverses a string value.
* @param {string} value - The string value to reverse.
* @return {string} The reversed string value.
*/
function reverseString(value) {
return value.split('').reverse().join('');
}
exports.reverseString = reverseString;
function isNullOrWhiteSpace(value) {
return value === null || value.match(/^ *$/) !== null;
}
//# sourceMappingURL=index.js.map