jsadvent/lib/_pollute/array.js
2020-12-13 00:52:00 -06:00

64 lines
1.3 KiB
JavaScript

const Glib = require('../glib');
Object.defineProperty(Array.prototype, 'glib', {
enumerable: false,
configurable: false,
get() {
return Glib.fromIterable(this);
},
});
Object.defineProperty(Array.prototype, 'set', {
enumerable: false,
configurable: false,
get() {
return new Set(this);
},
});
Object.defineProperty(Array.prototype, 'lengthN', {
enumerable: false,
configurable: false,
get() {
return BigInt(this.length);
},
});
Array.prototype.safeSplice = function safeSplice(...args) {
const spliced = this.slice();
spliced.splice(...args);
return spliced;
};
Array.prototype.chainSplice = function chainSplice(...args) {
this.splice(...args);
return this;
};
Array.prototype.chainPush = function chainPush(...args) {
this.push(...args);
return this;
};
Array.prototype.safePush = function safePush(...args) {
const clone = this.slice();
clone.push(...args);
return clone;
};
Array.prototype.safeReverse = function safeReverse(...args) {
const clone = this.slice();
clone.reverse(...args);
return clone;
};
Array.prototype.safeSort = function safeSort(...args) {
const clone = this.slice();
clone.sort(...args);
return clone;
};
Array.prototype.sortInts = function sortInts() {
return this.safeSort((a, b) => (a < b ? -1 : 1));
};