246 lines
5.1 KiB
Plaintext
246 lines
5.1 KiB
Plaintext
Object.mixin = function (target, source) {
|
|
if (typeof source != "object") return target;
|
|
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
return target;
|
|
};
|
|
|
|
Object.defineProperty(Object.prototype, "mixin", {
|
|
value: function mixin(obj) {
|
|
if (typeof obj == "string") obj = use(obj);
|
|
if (obj) Object.mixin(this, obj);
|
|
},
|
|
});
|
|
|
|
/* STRING DEFS */
|
|
Object.defineProperty(String.prototype, "rm", {
|
|
value: function (index, endidx = index + 1) {
|
|
return this.slice(0, index) + this.slice(endidx);
|
|
},
|
|
});
|
|
|
|
Object.defineProperty(String.prototype, "tolast", {
|
|
value: function (val) {
|
|
var idx = this.lastIndexOf(val);
|
|
if (idx == -1) return this.slice();
|
|
return this.slice(0, idx);
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "dir", {
|
|
value: function () {
|
|
if (!this.includes("/")) return "";
|
|
return this.tolast("/");
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "next", {
|
|
value: function (char, from) {
|
|
if (!Array.isArray(char)) char = [char];
|
|
if (from > this.length - 1) return -1;
|
|
else if (!from) from = 0;
|
|
|
|
var find = this.slice(from).search(char[0]);
|
|
if (find == -1) return -1;
|
|
else return from + find;
|
|
|
|
var i = 0;
|
|
var c = this.charAt(from + i);
|
|
while (!char.includes(c)) {
|
|
i++;
|
|
if (from + i > this.length - 1) return -1;
|
|
c = this.charAt(from + i);
|
|
}
|
|
return from + i;
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "prev", {
|
|
value: function (char, from, count = 0) {
|
|
if (from > this.length - 1) return -1;
|
|
else if (!from) from = this.length - 1;
|
|
|
|
var find = this.slice(0, from).lastIndexOf(char);
|
|
|
|
while (count > 1) {
|
|
find = this.slice(0, find).lastIndexOf(char);
|
|
count--;
|
|
}
|
|
|
|
if (find == -1) return 0;
|
|
else return find;
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "strip_ext", {
|
|
value: function () {
|
|
return this.tolast(".");
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "ext", {
|
|
value: function () {
|
|
return this.fromlast(".");
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "up_path", {
|
|
value: function () {
|
|
var base = this.base();
|
|
var dirs = this.dir().split("/");
|
|
dirs.pop();
|
|
return dirs.join("/") + base;
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "fromlast", {
|
|
value: function (val) {
|
|
var idx = this.lastIndexOf(val);
|
|
if (idx == -1) return "";
|
|
return this.slice(idx + 1);
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "tofirst", {
|
|
value: function (val) {
|
|
var idx = this.indexOf(val);
|
|
if (idx == -1) return this.slice();
|
|
return this.slice(0, idx);
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "fromfirst", {
|
|
value: function (val) {
|
|
var idx = this.indexOf(val);
|
|
if (idx == -1) return this;
|
|
return this.slice(idx + val.length);
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "name", {
|
|
value: function () {
|
|
var idx = this.indexOf("/");
|
|
if (idx == -1) return this.tolast(".");
|
|
return this.fromlast("/").tolast(".");
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "set_name", {
|
|
value: function (name) {
|
|
var dir = this.dir();
|
|
return this.dir() + "/" + name + "." + this.ext();
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "base", {
|
|
value: function () {
|
|
return this.fromlast("/");
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(String.prototype, "updir", {
|
|
value: function () {
|
|
if (this.lastIndexOf("/") == this.length - 1) return this.slice(0, this.length - 1);
|
|
var dir = (this + "/").dir();
|
|
return dir.dir();
|
|
},
|
|
});
|
|
|
|
|
|
/* ARRAY DEFS */
|
|
Object.defineProperty(Array.prototype, "filter!", {
|
|
value: function array_dofilter(fn) {
|
|
for (let i = 0; i < this.length; i++) {
|
|
if (!fn.call(this, this[i], i, this)) {
|
|
this.splice(i, 1);
|
|
i--;
|
|
}
|
|
}
|
|
return this;
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(Array.prototype, "delete", {
|
|
value: function(item) {
|
|
var idx = this.indexOf(item);
|
|
if (idx > -1) this.splice(idx,1);
|
|
return undefined;
|
|
}
|
|
});
|
|
|
|
|
|
Object.defineProperty(Array.prototype, "copy", {
|
|
value: function () {
|
|
var c = [];
|
|
this.forEach(function (x, i) {
|
|
c[i] = deep_copy(x);
|
|
});
|
|
return c;
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(Array.prototype, "equal", {
|
|
value: function equal(b) {
|
|
if (this.length != b.length) return false;
|
|
if (b == null) return false;
|
|
if (this == b) return true;
|
|
return JSON.stringify(this) == JSON.stringify(b);
|
|
|
|
for (var i = 0; i < this.length; i++) {
|
|
if (!this[i] == b[i]) return false;
|
|
}
|
|
return true;
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(Array.prototype, "last", {
|
|
value: function () {
|
|
return this[this.length - 1];
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(Array.prototype, "wrapped", {
|
|
value: function (x) {
|
|
var c = this.slice(0, this.length);
|
|
for (var i = 0; i < x; i++) c.push(this[i]);
|
|
return c;
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(Array.prototype, "wrap_idx", {
|
|
value: function (x) {
|
|
while (x >= this.length) {
|
|
x -= this.length;
|
|
}
|
|
return x;
|
|
},
|
|
});
|
|
|
|
|
|
Object.defineProperty(Array.prototype, "mirrored", {
|
|
value: function (x) {
|
|
var c = this.slice(0);
|
|
if (c.length <= 1) return c;
|
|
for (var i = c.length - 2; i >= 0; i--) c.push(c[i]);
|
|
return c;
|
|
},
|
|
});
|
|
|