Skip to content

Commit

Permalink
feat: Extend Array/String prototypes
Browse files Browse the repository at this point in the history
  • Loading branch information
PartMan7 committed Jan 28, 2024
1 parent fccbe2d commit b91e7ce
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
116 changes: 116 additions & 0 deletions src/globals/prototypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
export = {};

declare global {
interface Array<T> {
random (): T;
random (amount: number): T[];
remove (...toRemove: T[]): T[];
shuffle (): T[];
filterMap<X> (cb: (element: T, index: number, thisArray: T[]) => X | undefined): X | undefined;
}

interface String {
lazySplit (match: string | RegExp, cases: number): string[];
gsub (match: RegExp, replace: string | ((arg: string, ...captures: string[]) => string)): string;
}
}

Object.defineProperties(Array.prototype, {
filterMap: {
enumerable: false,
writable: false,
configurable: false,
value: function<T, X> (callback: (element: T, index: number, thisArray: T[]) => X | undefined): X | undefined {
for (let i = 0; i < this.length; i++) {
const result = callback(this[i], i, this);
if (result === undefined) continue;
return result;
}
}
},
remove: {
enumerable: false,
writable: false,
configurable: false,
value: function (...terms) {
let out = true;
terms.forEach(term => {
if (this.indexOf(term) >= 0) this.splice(this.indexOf(term), 1);
else out = false;
});
return out;
}
},
random: {
enumerable: false,
writable: false,
configurable: false,
value: function (amount: number) {
if (amount === undefined) return this[Math.floor(Math.random() * this.length)];
const sample = Array.from(this), out = [];
let i = 0;
while (sample.length && i++ < amount) {
const term = sample[Math.floor(Math.random() * sample.length)];
out.push(term);
sample.remove(term);
}
return out;
}
},
shuffle: {
enumerable: false,
writable: false,
configurable: false,
value: function () {
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]];
}
return Array.from(this);
}
}
});

Object.defineProperties(String.prototype, {
lazySplit: {
enumerable: false,
writable: false,
configurable: false,
value: function (delim: string | RegExp, amount?: number): string[] {
if (typeof amount !== 'number') amount = 1;
const out: string[] = [];
let input = this.toString();
if (delim instanceof RegExp) delim = new RegExp(delim, delim.flags.replace('g', ''));
for (let i = 0; i < amount; i++) {
if (delim instanceof RegExp) {
const match = input.match(delim);
if (!match) return [...out, input];
const m = match[0];
out.push(input.slice(0, match.index));
input = input.slice(match.index + m.length);
for (let j = 1; j < match.length; j++) out.push(match[j]);
} else {
const match = input.indexOf(delim);
if (match < 0) return [...out, input];
out.push(input.slice(0, match));
input = input.slice(match + delim.length);
}
}
out.push(input);
return out;
}
},
gsub: {
enumerable: false,
writable: false,
configurable: false,
value: function (match: RegExp, replace: string | ((arg: string) => string)): string {

Check warning on line 107 in src/globals/prototypes.ts

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

'match' is defined but never used

Check warning on line 107 in src/globals/prototypes.ts

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

'replace' is defined but never used

Check warning on line 107 in src/globals/prototypes.ts

View workflow job for this annotation

GitHub Actions / test (16.x, macos-latest)

'match' is defined but never used

Check warning on line 107 in src/globals/prototypes.ts

View workflow job for this annotation

GitHub Actions / test (16.x, macos-latest)

'replace' is defined but never used

Check warning on line 107 in src/globals/prototypes.ts

View workflow job for this annotation

GitHub Actions / test (16.x, windows-latest)

'match' is defined but never used

Check warning on line 107 in src/globals/prototypes.ts

View workflow job for this annotation

GitHub Actions / test (16.x, windows-latest)

'replace' is defined but never used
// let latestMatch: RegExpExecArray;
// do {
// latestMatch = match.exec()
// } while (latestMatch);
// TODO
return 'FIXME';
}
},
});
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as dotenv from 'dotenv';
dotenv.config();

import 'globals/prototypes';
import 'globals';

import PS from 'ps';
Expand Down

0 comments on commit b91e7ce

Please sign in to comment.