mirror of
https://github.com/spacebarchat/server.git
synced 2026-05-05 02:55:32 +00:00
remove unused forEach
This commit is contained in:
@@ -5,19 +5,6 @@ import { describe, it } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
describe("Object extensions", () => {
|
||||
it("forEach", async () => {
|
||||
const obj: { [index:string]: number } = { a: 1, b: 2, c: 3 };
|
||||
const keys: string[] = [];
|
||||
const values: number[] = [];
|
||||
obj.forEach<number>((value, key, _) => {
|
||||
keys.push(key);
|
||||
values.push(value);
|
||||
});
|
||||
console.log(keys, values);
|
||||
assert.deepEqual(keys, ["a", "b", "c"]);
|
||||
assert.deepEqual(values, [1, 2, 3]);
|
||||
});
|
||||
|
||||
it("map", async () => {
|
||||
const obj = { a: 1, b: 2, c: 3 };
|
||||
const result = obj.map((value, key) => `${key}:${value}`);
|
||||
|
||||
@@ -1,16 +1,9 @@
|
||||
declare global {
|
||||
interface Object {
|
||||
forEach<T>(callback: (value: T, key: string, object: { [index: string]: T }) => void): void;
|
||||
map<SV, TV>(callback: (value: SV, key: string, object: { [index: string]: SV }) => TV): { [index: string]: TV };
|
||||
}
|
||||
}
|
||||
|
||||
export function objectForEach<T>(obj: { [index: string]: T }, callback: (value: T, key: string, object: { [index: string]: T }) => void): void {
|
||||
Object.keys(obj).forEach((key) => {
|
||||
callback(obj[key], key, obj);
|
||||
});
|
||||
}
|
||||
|
||||
export function objectMap<SV, TV>(srcObj: { [index: string]: SV }, callback: (value: SV, key: string, object: { [index: string]: SV }) => TV): { [index: string]: TV } {
|
||||
if (typeof callback !== "function") throw new TypeError(`${callback} is not a function`);
|
||||
const obj: { [index: string]: TV } = {};
|
||||
@@ -20,17 +13,6 @@ export function objectMap<SV, TV>(srcObj: { [index: string]: SV }, callback: (va
|
||||
return obj;
|
||||
}
|
||||
|
||||
if (!Object.prototype.forEach)
|
||||
Object.defineProperty(Object.prototype, "forEach", {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-expect-error
|
||||
value: function (cb) {
|
||||
return objectForEach(this, cb);
|
||||
},
|
||||
enumerable: false,
|
||||
writable: true,
|
||||
});
|
||||
|
||||
if (!Object.prototype.map)
|
||||
Object.defineProperty(Object.prototype, "map", {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
|
||||
Reference in New Issue
Block a user