rays-javascript-extension/javascript-extensions/string.js

83 lines
1.6 KiB
JavaScript

String.isNullOrUndefined = function(value) {
if (typeof (value) == "undefined") {
return true;
}
if (value == null) {
return true;
}
return false;
};
String.isNullOrWhitespace = function(value) {
const a = this;
if (String.isNullOrUndefined(value)) {
return true;
}
if (typeof(value) == "string") {
return (value.trim().length <= 0);
} else {
return (value.toString().trim().length <= 0);
}
};
String.joinIfNotNullOrWhitespace = function (separator, ...values) {
const a = this;
let result = "";
for (let i = 0; i < values.length; i++) {
if (String.isNullOrWhitespace(values[i])) {
continue;
}
if (!String.isNullOrWhitespace(result)) {
result += separator;
}
result += values[i];
};
return result;
};
String.prototype.contains = function(...args) {
for (let arg of args) {
if (this == arg) {
return true;
}
}
return false;
};
String.prototype.containsCI = function(...args) {
for (let arg of args) {
if (this.toLowerCase() == arg.toLowerCase()) {
return true;
}
}
return false;
};
String.prototype.encodeHtmlLinks = function() {
return value.replace(/(http[s]{0,1}:\/\/[^\s]+)/g, "<a href='$1'>$1</a>");
};
String.prototype.toTitleCase = function () {
let result = this;
result = result.replace(/([A-Z]{1})/g, " $1");
result = result.trim();
result = result.charAt(0).toUpperCase() + result.substr(1);
return result;
};
String.prototype.getFilename = function () {
return this.substring(this.lastIndexOf('/') + 1);
};