jsadvent/lib/_pollute/array.js
2020-12-12 00:59:44 -06:00

56 lines
1.1 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);
},
});
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));
};