Skip to content

Instantly share code, notes, and snippets.

@meglio
Last active August 29, 2015 13:56
Show Gist options
  • Save meglio/8823817 to your computer and use it in GitHub Desktop.
Save meglio/8823817 to your computer and use it in GitHub Desktop.
Denis' generic utils for JS
RegExp.escape = function (text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
if (!Math.sqr) {
Math.sqr = function (x) {
return x * x;
}
}
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
if (!String.prototype.ltrim) {
String.prototype.ltrim = function () {
return this.replace(/^\s+/, '');
};
}
if (!String.prototype.rtrim) {
String.prototype.rtrim = function () {
return this.replace(/\s+$/, '');
};
}
if (!String.prototype.fulltrim) {
String.prototype.fulltrim = function () {
return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g, '').replace(/\s+/g, ' ');
};
}
if (!String.prototype.repeat) {
String.prototype.repeat = function (count) {
return Array(count + 1).join(this);
};
}
if (!Number.prototype.multipleBy) {
Number.prototype.multipleBy = function (step) {
return Math.round(Math.ceil(this / step) * step);
};
}
if (!Number.prototype.paddingTo) {
Number.prototype.paddingTo = String.prototype.paddingTo = function (minlength, prefix) {
if (!prefix) {
prefix = '0';
}
var t = String(this);
if (t.length >= minlength) {
return t;
}
return String(prefix).repeat(minlength - t.length) + t;
}
}
if (!Number.prototype.d) {
Number.prototype.d = function (digits) {
if (!digits) {
digits = 2;
}
return this.paddingTo(digits)
}
}
if (!Number.prototype.deg) {
Number.prototype.deg = function () {
return this * 180 / Math.PI;
}
}
if (!String.prototype.deg) {
String.prototype.deg = function () {
return parseFloat(this).deg();
}
}
if (!Number.prototype.rad) {
Number.prototype.rad = function () {
return this / 180 * Math.PI;
}
}
if (!String.prototype.rad) {
String.prototype.rad = function () {
return parseFloat(this).rad();
}
}
if (!String.prototype.isin) {
String.prototype.isin = function (list) {
return list.contains(this)
}
String.prototype.in=String.prototype.isin;
}
if (!Array.prototype.each) {
Array.prototype.each = function (fn, thisObj) {
for (var i = 0, l = this.length; i < l; i++) {
if (this[i]) {
fn.call(thisObj, this[i], i, this);
}
}
};
}
if (!Array.prototype.binaryIndexOf){
Array.prototype.binaryIndexOf = function (searchElement) {
var minIndex = 0;
var maxIndex = this.length - 1;
var currentIndex;
var currentElement;
while(minIndex <= maxIndex){
currentIndex = (minIndex + maxIndex) / 2 | 0;
currentElement = this[currentIndex];
if (currentElement < searchElement){
minIndex = currentIndex + 1;
}else if (currentElement > searchElement){
maxIndex = currentIndex - 1;
}else{
return currentIndex;
}
}
return -1;
}
}
if (!Array.prototype.unique){
Array.prototype.unique = function() {
this.sort();
return this.reduce(function(p, c) {
if (p.binaryIndexOf(c) < 0) p.push(c);
return p;
}, []);
};
}
if (!Array.prototype.allStartsWith){
Array.prototype.allStartsWith = function (searchElement) {
var found = [];
var minIndex = 0;
var maxIndex = this.length - 1;
var currentIndex;
var currentElement;
while(minIndex <= maxIndex){
currentIndex = (minIndex + maxIndex) / 2 | 0;
currentElement = this[currentIndex];
if (currentElement < searchElement){
minIndex = currentIndex + 1;
}else if (currentElement > searchElement){
maxIndex = currentIndex - 1;
}else{
break;
}
}
while (currentIndex>=0 && this[currentIndex].indexOf(searchElement)!=-1){
currentIndex--;
}
currentIndex++;
while (currentIndex<this.length && this[currentIndex].indexOf(searchElement)==0){
found.push(this[currentIndex]);
currentIndex++;
}
return found;
}
}
if (!Array.prototype.contains) {
Array.prototype.contains = function (p_val) {
for (var i = 0, l = this.length; i < l; i++) {
if (this[i] == p_val) {
return true;
}
}
return false;
}
}
// Array Remove - By John Resig (MIT Licensed)
if (!Array.prototype.remove) {
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment