data-validator-js
Version:
Validation Methods for all types of Data
111 lines (110 loc) • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var ConverterUtils = /** @class */ (function () {
function ConverterUtils() {
}
/**
* ConvertToCapitals - Convert the value to UpperCase
* @param value String which we need to check
*/
ConverterUtils.ConvertToCapitals = function (value) {
return value.toUpperCase();
};
/**
* ConvertToLowerCase - Convert the value to LowerCase
* @param value String which we need to check
*/
ConverterUtils.ConvertToLowerCase = function (value) {
return value.toLowerCase();
};
/**
* ConvertToCamelCase - Convert the value to CamelCase
* @param value String which we need to check
*/
ConverterUtils.ConvertToCamelCase = function (value) {
return (' ' + value).toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, function (match, chr) {
return chr.toUpperCase();
});
};
/**
* ConvertToString - Convert the value to String
* @param value String which we need to check
*/
ConverterUtils.ConvertToString = function (value) {
if (typeof value === 'object') {
return JSON.stringify(value);
}
else {
return String(value);
}
};
/**
* ConvertToNumber - Convert the value to Number
* @param value String which we need to check
*/
ConverterUtils.ConvertToNumber = function (value) {
// let result = Number(value)
return Number(value);
};
/**
* ConvertToTitleCase - Convert the value to TitleCase
* @param value String which we need to check
*/
ConverterUtils.ConvertToTitleCase = function (value) {
return value.replace(/([^\W_]+[^\s-]*) */g, function (txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
};
/**
* ConvertToLowerCamelCase - Convert the value to LowerCamelCase
* @param value String which we need to check
*/
ConverterUtils.ConvertToLowerCamelCase = function (value) {
return value.replace(/\w\S*/g, function (chr) {
return chr.charAt(0).toLowerCase() + chr.charAt(1).toUpperCase() + chr.substr(2).toLowerCase();
});
};
/**
* ConvertToSnakeCase - Convert the value to SnakeCase
* @param value String which we need to check
*/
ConverterUtils.ConvertToSnakeCase = function (value) {
return value
.trim()
.toLowerCase()
.replace(/\s+/g, '_');
};
/**
* ConvertToRandomCase - Convert the value to RandomCase
* @param value String which we need to check
*/
ConverterUtils.ConvertToRandomCase = function (value) {
return value
.split('')
.map(function (chr) {
var chance = Math.round(Math.random());
return (chr = chance ? chr.toUpperCase() : chr.toLowerCase());
})
.join('');
};
/**
* ConvertToToggleCase - Convert the value to ToggleCase
* @param value String which we need to check
*/
ConverterUtils.ConvertToToggleCase = function (value) {
return value.replace(/([a-z]+)|([A-Z]+)/g, function (match, chr) {
return chr ? match.toUpperCase() : match.toLowerCase();
});
};
/**
* ConvertToSentenceCase - Convert the value to SentenceCase
* @param value String which we need to check
*/
ConverterUtils.ConvertToSentenceCase = function (value) {
return value.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g, function (c) {
return c.toUpperCase();
});
};
return ConverterUtils;
}());
exports.default = ConverterUtils;