.
This commit is contained in:
438
proto.js
438
proto.js
@@ -1,114 +1,4 @@
|
||||
let warned = false
|
||||
|
||||
Object.defineProperties(Object.prototype, {
|
||||
copyDeep: {
|
||||
value: function() {
|
||||
return JSON.parse(JSON.stringify(this))
|
||||
}
|
||||
},
|
||||
num: {
|
||||
value: function() {
|
||||
if (this.map) {
|
||||
return this.map((e) => +e)
|
||||
} else if (this.mapMut) {
|
||||
return this.mapMut((e) => +e)
|
||||
} else {
|
||||
console.error("Object.prototype.num: No suitable map method found")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperties(Array.prototype, {
|
||||
dll: {
|
||||
value: function() {
|
||||
return new DLL(this)
|
||||
}
|
||||
},
|
||||
copy: {
|
||||
value: function() {
|
||||
return this.slice()
|
||||
}
|
||||
},
|
||||
copyDeep: {
|
||||
value: function() {
|
||||
return JSON.parse(JSON.stringify(this))
|
||||
}
|
||||
},
|
||||
sum: {
|
||||
value: function(val) {
|
||||
return this.reduce((a, b) => a + b, val)
|
||||
}
|
||||
},
|
||||
flatDeep: {
|
||||
value: function() {
|
||||
return this.flat(Infinity)
|
||||
}
|
||||
},
|
||||
transpose: {
|
||||
value: function() {
|
||||
return Array(this[0].length).fill().map((_, i) => this.map(e => e[i]))
|
||||
}
|
||||
},
|
||||
findLast: {
|
||||
value: function(func) {
|
||||
for (let i = this.length - 1; i >= 0; i--) {
|
||||
if (func(this[i], i, this)) {
|
||||
return this[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
findLastIndex: {
|
||||
value: function(func) {
|
||||
for (let i = this.length - 1; i >= 0; i--) {
|
||||
if (func(this[i], i, this)) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
},
|
||||
interleave: {
|
||||
value: function(that) {
|
||||
return [this, that].transpose().flat()
|
||||
}
|
||||
},
|
||||
rotate: {
|
||||
value: function(n) {
|
||||
let k = (this.length + n) % this.length
|
||||
return [...this.slice(k), ...this.slice(0, k)]
|
||||
}
|
||||
},
|
||||
sub: {
|
||||
value: function(that) {
|
||||
return this.filter(e => !that.includes(e))
|
||||
}
|
||||
},
|
||||
int: {
|
||||
value: function(that) {
|
||||
return this.filter(e => that.includes(e))
|
||||
}
|
||||
},
|
||||
uniq: {
|
||||
value: function() {
|
||||
return this.filter((e, i) => this.indexOf(e) == i)
|
||||
}
|
||||
},
|
||||
pushUniq: {
|
||||
value: function(...vals) {
|
||||
if (!warned) {
|
||||
console.warn("You should probably use a Set")
|
||||
warned = true
|
||||
}
|
||||
|
||||
return this.push(...vals.uniq().sub(this))
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
class PointArray extends Array {
|
||||
PtArray = PointArray = class PointArray extends Array {
|
||||
static convert(arr) {
|
||||
if (!(arr instanceof PointArray)) {
|
||||
arr.__proto__ = PointArray.prototype
|
||||
@@ -118,54 +8,284 @@ class PointArray extends Array {
|
||||
}
|
||||
}
|
||||
|
||||
PtArray = PointArray
|
||||
let warned = false
|
||||
|
||||
Object.defineProperty(Array.prototype, "pt", {
|
||||
get: function() {
|
||||
return PointArray.convert(this)
|
||||
}
|
||||
})
|
||||
load = function load() {
|
||||
Object.defineProperties(Object.prototype, {
|
||||
copyDeep: {
|
||||
value: function() {
|
||||
return JSON.parse(JSON.stringify(this))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
num: {
|
||||
value: function() {
|
||||
if (this.map) {
|
||||
return this.map((e) => +e)
|
||||
} else if (this.mapMut) {
|
||||
return this.mapMut((e) => +e)
|
||||
} else {
|
||||
console.error("Object.prototype.num: No suitable map method found")
|
||||
}
|
||||
},
|
||||
configurable: true
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperties(PointArray.prototype, {
|
||||
sort: {
|
||||
value: function(func = (a, b) => a.readingOrderCompare(b)) {
|
||||
return Array.prototype.sort.apply(this, func)
|
||||
}
|
||||
},
|
||||
includes: {
|
||||
value: function(pt) {
|
||||
return pt.isIn(this)
|
||||
}
|
||||
},
|
||||
indexOf: {
|
||||
value: function(pt) {
|
||||
return pt.indexIn(this)
|
||||
}
|
||||
},
|
||||
lastIndexOf: {
|
||||
value: function(pt) {
|
||||
return pt.lastIndexIn(this)
|
||||
}
|
||||
},
|
||||
sub: {
|
||||
value: function(that) {
|
||||
return this.filter(e => !that.pt.includes(e))
|
||||
}
|
||||
},
|
||||
int: {
|
||||
value: function(that) {
|
||||
return this.filter(e => that.pt.includes(e))
|
||||
}
|
||||
},
|
||||
uniq: {
|
||||
value: function() {
|
||||
return this.filter((e, i) => this.pt.indexOf(e) == i)
|
||||
}
|
||||
},
|
||||
pushUniq: {
|
||||
value: function(...vals) {
|
||||
return this.push(...vals.pt.uniq().pt.sub(this))
|
||||
}
|
||||
}
|
||||
})
|
||||
Object.defineProperties(Array.prototype, {
|
||||
pt: {
|
||||
get: function() {
|
||||
return PointArray.convert(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
last: {
|
||||
get: function() {
|
||||
return this[this.length - 1]
|
||||
},
|
||||
set: function(val) {
|
||||
this[this.length - 1] = val
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
dll: {
|
||||
value: function() {
|
||||
return new DLL(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
copy: {
|
||||
value: function() {
|
||||
return this.slice()
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
copyDeep: {
|
||||
value: function() {
|
||||
return JSON.parse(JSON.stringify(this))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
sum: {
|
||||
value: function(val) {
|
||||
return this.reduce((a, b) => a + b, val)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
flatDeep: {
|
||||
value: function() {
|
||||
return this.flat(Infinity)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
transpose: {
|
||||
value: function() {
|
||||
return Array(this[0].length).fill().map((_, i) => this.map(e => e[i]))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
findLast: {
|
||||
value: function(func) {
|
||||
for (let i = this.length - 1; i >= 0; i--) {
|
||||
if (func(this[i], i, this)) {
|
||||
return this[i]
|
||||
}
|
||||
}
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
findLastIndex: {
|
||||
value: function(func) {
|
||||
for (let i = this.length - 1; i >= 0; i--) {
|
||||
if (func(this[i], i, this)) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
|
||||
return -1
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
interleave: {
|
||||
value: function(that) {
|
||||
return [this, that].transpose().flat()
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
rotate: {
|
||||
value: function(n) {
|
||||
let k = (this.length + n) % this.length
|
||||
return [...this.slice(k), ...this.slice(0, k)]
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
sub: {
|
||||
value: function(that) {
|
||||
return this.filter(e => !that.includes(e))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
int: {
|
||||
value: function(that) {
|
||||
return this.filter(e => that.includes(e))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
uniq: {
|
||||
value: function() {
|
||||
return this.filter((e, i) => this.indexOf(e) == i)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
pushUniq: {
|
||||
value: function(...vals) {
|
||||
if (!warned) {
|
||||
console.warn("You should probably use a Set")
|
||||
warned = true
|
||||
}
|
||||
|
||||
return this.push(...vals.uniq().sub(this))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
count: {
|
||||
value: function(fn) {
|
||||
return this.filter(typeof fn == "function" ? fn : (e) => e == fn).length
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
minIndex: {
|
||||
value: function(fn = (e) => e, tiebreak) {
|
||||
let min = Infinity
|
||||
let idx
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
let val = fn(this[i])
|
||||
|
||||
if (min > val ||
|
||||
(min == val && tiebreak && tiebreak(min, val, idx, i) > 0)) {
|
||||
min = val
|
||||
idx = i
|
||||
}
|
||||
}
|
||||
|
||||
return idx
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
min: {
|
||||
value: function(fn, tiebreak) {
|
||||
return this[this.minIndex(fn, tiebreak)]
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
maxIndex: {
|
||||
value: function(fn = (e) => e, tiebreak) {
|
||||
let max = -Infinity
|
||||
let idx
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
let val = fn(this[i])
|
||||
|
||||
if (max < val ||
|
||||
(max == val && tiebreak && tiebreak(max, val, idx, i) > 0)) {
|
||||
max = val
|
||||
idx = i
|
||||
}
|
||||
}
|
||||
|
||||
return idx
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
max: {
|
||||
value: function(fn, tiebreak) {
|
||||
return this[this.maxIndex(fn, tiebreak)]
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
mean: {
|
||||
value: function() {
|
||||
return this.sum() / this.length
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
freqs: {
|
||||
value: function() {
|
||||
return this.uniq().map((e) => [e, this.count(e)])
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
mode: {
|
||||
value: function(tiebreak) {
|
||||
return this.freqs().max((e) => e[1], (a, b, ai, bi) => tiebreak(a[0], b[0]))[0]
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
antimode: {
|
||||
value: function(tiebreak) {
|
||||
return this.freqs().min((e) => e[1], (a, b, ai, bi) => tiebreak(a[0], b[0]))[0]
|
||||
},
|
||||
configurable: true
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperties(PointArray.prototype, {
|
||||
sort: {
|
||||
value: function(func = (a, b) => a.readingOrderCompare(b)) {
|
||||
return Array.prototype.sort.apply(this, func)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
includes: {
|
||||
value: function(pt) {
|
||||
return pt.isIn(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
indexOf: {
|
||||
value: function(pt) {
|
||||
return pt.indexIn(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
lastIndexOf: {
|
||||
value: function(pt) {
|
||||
return pt.lastIndexIn(this)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
sub: {
|
||||
value: function(that) {
|
||||
return this.filter(e => !that.pt.includes(e))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
int: {
|
||||
value: function(that) {
|
||||
return this.filter(e => that.pt.includes(e))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
uniq: {
|
||||
value: function() {
|
||||
return this.filter((e, i) => this.pt.indexOf(e) == i)
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
pushUniq: {
|
||||
value: function(...vals) {
|
||||
return this.push(...vals.pt.uniq().pt.sub(this))
|
||||
},
|
||||
configurable: true
|
||||
},
|
||||
count: {
|
||||
value: function(fn) {
|
||||
return this.filter(typeof fn == "function" ? fn : (e) => e.equals(fn)).length
|
||||
},
|
||||
configurable: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
load()
|
||||
|
||||
Reference in New Issue
Block a user