jsadvent/lib/_pollute/array.js

56 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-12-10 08:33:00 +00:00
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;
};
2020-12-12 06:59:44 +00:00
Array.prototype.sortInts = function sortInts() {
return this.safeSort((a, b) => (a < b ? -1 : 1));
};