missing-native-js-functions
Version:
mnJSf that should be the base library for every JS project
118 lines • 3.65 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Util_1 = require("./Util");
(0, Util_1.define)(String.prototype, {
capitalize: function () {
return this.slice(0, 1).toUpperCase() + this.slice(1);
},
title: function () {
return this.split(" ")
.map(function (element) {
return element.capitalize();
})
.join(" ");
},
replaceAll: function (find, replace) {
return this.replace(new RegExp(escapeRegExp(find), "g"), replace);
},
similarity: function (s2) {
var s1 = this.toLowerCase();
s2 = s2.toLowerCase();
if (s1.length < s2.length) {
s1 = s2;
s2 = this;
}
var longerLength = s1.length;
if (longerLength == 0) {
return 1.0;
}
var costs = new Array();
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0)
costs[j] = j;
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length] = lastValue;
}
return (longerLength - costs[s2.length]) / parseFloat(longerLength);
},
join: function (iterate) {
if (typeof iterate === "string")
return iterate;
return iterate.join(this);
},
partition: function (seperator) {
if (!this.includes(seperator)) {
return [this];
}
var returnArray = [];
var splitarray = this.split(seperator);
for (var i = 0; i < splitarray.length; i++) {
returnArray.push(splitarray[i]);
if (i !== splitarray.length - 1)
returnArray.push(seperator);
}
return returnArray;
},
toNumber: function () {
return Number(this);
},
toBigInt: function () {
try {
return BigInt(this);
}
catch (error) {
return NaN;
}
},
equalsIgnoreCase: function (compareString) {
return this.toLowerCase() === compareString.toLowerCase();
},
count: function (countString) {
return this.split(countString).length - 1;
},
swapcase: function () {
return this.split("")
.map(function (char) {
if (char === char.toUpperCase())
return char.toLowerCase();
return char.toUpperCase();
})
.join("");
},
toObject: function () {
return JSON.parse(this);
},
toBoolean: function () {
switch (this.toLowerCase().trim()) {
case "true":
case "yes":
case "1":
return true;
case "false":
case "no":
case "0":
case null:
return false;
default:
return Boolean(this);
}
},
});
// copied from https://github.com/aceakash/string-similarity/blob/master/src/index.js
// MIT License Copyright (c) 2018 Akash Kurdekar
function escapeRegExp(str) {
return str.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}
//# sourceMappingURL=String.js.map