ptrcports/node-print/index.js

198 lines
4.8 KiB
JavaScript
Raw Normal View History

2023-04-28 03:11:30 +00:00
const fs = require('fs')
const child_process = require('child_process')
2022-08-27 11:01:43 +00:00
global.stdin = () => {
const out = fs.readFileSync(0).toString()
2022-10-11 11:16:01 +00:00
return out.tryParseJSON()
2022-08-27 11:01:43 +00:00
}
Object.prototype.map = function(cb) {
return cb(this)
}
2023-04-28 03:11:30 +00:00
Object.prototype.apply = Object.prototype.map
2022-08-27 11:01:43 +00:00
String.prototype.lines = function() {
let res = this.split("\n")
return res.at(-1) === '' ? res.slice(0, -1) : res
}
2022-08-27 12:08:44 +00:00
String.prototype.reverse = function() {
return this.split('').reverse().join('')
}
2022-10-11 11:16:01 +00:00
2022-12-29 11:31:42 +00:00
String.prototype.includesAny = function(strings) {
if (!(strings instanceof Array)) throw new TypeError('strings must be an Array')
return strings.some(x => this.includes(x))
}
2022-10-11 11:16:01 +00:00
String.prototype.tryParseJSON = function() {
const str = this.trim()
if (
(str.at(0) === '{' && str.at(-1) === '}') ||
(str.at(0) === '[' && str.at(-1) === ']')
) {
try {
return JSON.parse(str)
} catch (err) {
return this.toString()
}
}
return this.toString()
}
Buffer.prototype.tryParseJSON = function() {
const out = this.toString().tryParseJSON()
if (typeof out === 'string') {
return this
} else {
return out
}
}
global.exec = (command, args, options) => {
const res = child_process.spawnSync(command, args, options)
res.stdout = res.stdout?.tryParseJSON()
res.stderr = res.stderr?.tryParseJSON()
2023-04-28 03:11:30 +00:00
res.orFail = function() {
if (res.status !== 0) {
const cmd = [
command,
...((args && Array.isArray(args)) ? args : [])
].join(' ')
throw new Error(`command '${cmd}' exited with code ${res.status}`)
}
}
2022-10-11 11:16:01 +00:00
return res
}
2023-03-04 19:16:14 +00:00
global.execnc = (command, args, options) => {
if (!options) options = {}
if (!options.stdio) options.stdio = 'inherit'
return exec(command, args, options)
}
/* wow it's almost like it's my own package and i can include whatever garbage i want :) */
/* not writing a test for that tho */
global.apkindex = input => {
if (!input) input = stdin()
return input
.split("\n\n")
.filter(str => str.length)
.map(x => Object.fromEntries(x.lines().map(e => [e.at(0), e.slice(2)])))
.map(pkg => ({
...pkg,
S: Number(pkg.S),
I: Number(pkg.I),
t: new Date(Number(pkg.t) * 1000),
i: pkg.i?.split(' '),
D: pkg.D?.split(' '),
p: pkg.p?.split(' ')
}))
.map(Object.prune)
}
2022-12-02 21:51:51 +00:00
Array.prototype.sum = function(def = 0) {
return this.reduce((a, b) => a + b, def)
}
2022-12-11 18:42:24 +00:00
Array.prototype.product = function(def = 1) {
return this.reduce((a, b) => a * b, def)
}
2023-04-28 03:11:30 +00:00
Array.prototype.sortNum = function(getter = (x => x)) {
return this.sort((a, b) => getter(a) - getter(b))
2022-12-02 21:51:51 +00:00
}
Array.prototype.partition = function(compareFn) {
const a = []
const b = []
this.forEach((elem, i, arr) => {
if (compareFn(elem, i, arr)) {
a.push(elem)
} else {
b.push(elem)
}
})
return [a, b]
}
Array.prototype.chunks = function(chunkSize) {
// inspired by https://stackoverflow.com/questions/8495687/split-array-into-chunks#comment84212474_8495740
return new Array(Math.ceil(this.length / chunkSize))
.fill()
.map((_, i) => this.slice(i * chunkSize, (i + 1) * chunkSize))
}
2022-12-29 11:31:42 +00:00
Array.prototype.transpose = function() {
return new Array(this[0].length).fill().map((_, i) => this.map(x => x[i]))
}
Array.prototype.shuffle = function() {
// borrowed from https://stackoverflow.com/a/12646864
for (let i = this.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this[i], this[j]] = [this[j], this[i]];
}
}
2023-10-20 01:43:06 +00:00
Array.prototype.uniq = function() {
return new Array(...(new Set(this)))
}
Array.prototype.lines = function() {
return this.join('\n')
}
2022-12-02 21:51:51 +00:00
Set.prototype.union = function(other) {
if (!other || !(other instanceof Set)) throw new TypeError('other must be a Set')
return new Set([...this, ...other])
}
Set.prototype.intersection = function(other) {
if (!other || !(other instanceof Set)) throw new TypeError('other must be a Set')
return new Set([...this].filter(el => other.has(el)))
}
2022-12-11 18:42:24 +00:00
Set.prototype.difference = function(other) {
if (!other || !(other instanceof Set)) throw new TypeError('other must be a Set')
return new Set([...this].filter(el => !other.has(el)))
}
2022-12-11 19:33:11 +00:00
Set.prototype.at = function(index) {
return [...this].at(index)
}
2022-12-11 18:42:24 +00:00
global.matrix = function(x, y, elem) {
return new Array(y).fill().map(() => new Array(x).fill().map((_, i) => {
if (typeof elem === 'function') {
return elem(i)
} else {
return elem
}
}))
}
2022-12-11 19:33:11 +00:00
Number.prototype.toXY = function(width) {
if (!(width && typeof width === 'number')) throw new TypeError('width must be a Number')
return [ this % width, Math.floor(this / width) ]
}
2022-12-29 11:31:42 +00:00
global.sed = function(filename, cb) {
let content = fs.readFileSync(filename, 'utf-8')
content = cb(content)
fs.writeFileSync(filename, content, 'utf-8')
}
Object.prune = function(obj) {
if (typeof obj !== 'object') throw new TypeError('obj must be an object')
for (let key of Object.keys(obj)) {
if (obj[key] === undefined) {
delete obj[key]
}
}
return obj
}