string-kit-js
Version:
[](https://www.npmjs.com/package/string-kit-js) [](https://opensource.org/licenses/MIT)
91 lines (75 loc) • 2.08 kB
JavaScript
function isBlank(str) {
return str === null || str === undefined || (typeof str === "string" && str.trim() === "");
}
function isNotBlank(str) {
return !isBlank(str);
}
function isEmpty(str) {
return str === null || str === undefined || str === "";
}
function isNotEmpty(str) {
return !isEmpty(str);
}
function equals(str1, str2) {
return str1 === str2;
}
function equalsIgnoreCase(str1, str2) {
if (str1 == null || str2 == null) return false;
return String(str1).toLowerCase() === String(str2).toLowerCase();
}
function contains(str, substr) {
if (str == null || substr == null) return false;
return String(str).includes(substr);
}
function startsWith(str, prefix) {
if (str == null || prefix == null) return false;
return String(str).startsWith(prefix);
}
function endsWith(str, suffix) {
if (str == null || suffix == null) return false;
return String(str).endsWith(suffix);
}
function toUpper(str) {
return str ? String(str).toUpperCase() : "";
}
function toLower(str) {
return str ? String(str).toLowerCase() : "";
}
function capitalize(str) {
if (!str) return "";
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
function defaultIfBlank(str, defaultStr) {
return isBlank(str) ? defaultStr : str;
}
function defaultIfEmpty(str, defaultStr) {
return isEmpty(str) ? defaultStr : str;
}
function substringBefore(str, separator) {
if (isEmpty(str) || separator == null) return str;
const idx = str.indexOf(separator);
return idx === -1 ? str : str.substring(0, idx);
}
function substringAfter(str, separator) {
if (isEmpty(str) || separator == null) return "";
const idx = str.indexOf(separator);
return idx === -1 ? "" : str.substring(idx + separator.length);
}
module.exports = {
isBlank,
isNotBlank,
isEmpty,
isNotEmpty,
equals,
equalsIgnoreCase,
contains,
startsWith,
endsWith,
toUpper,
toLower,
capitalize,
defaultIfBlank,
defaultIfEmpty,
substringBefore,
substringAfter
};