<commit_before>import * as filters from "./filters";

export type Exports = Record<string, any>;

export interface Module {
    id: number;
    loaded: boolean;
    exports: Exports;
}

export type ModuleFunction = (this: Exports, module: Module, exports: Exports, require: Require) => void;

export interface Require {
    (id: number): any;

    /** module register */
    m: Record<string, ModuleFunction>;

    /** module cache */
    c: Record<string, Module>;
}

const getWebpackRequire = (): Require => {
    const moduleId = "discordium";

    let webpackRequire: Require;
    global.webpackJsonp.push([[], {
        [moduleId]: (_module: Module, _exports: Exports, require: Require) => {
            webpackRequire = require;
        }
    }, [[moduleId]]]);

    // cleanup
    delete webpackRequire.m[moduleId];
    delete webpackRequire.c[moduleId];

    return webpackRequire;
};

export type Filter = (exports: Exports, module: Module) => boolean;

// TODO: option to check filter on props?
const joinFilters = (filters: Filter[]) => {
    return (module: Module) => {
        const {exports} = module;
        return (
            filters.every((filter) => filter(exports, module))
            || exports?.__esModule && "default" in exports && filters.every((filter) => filter(exports.default, module))
        );
    };
};

export interface FilterOptions {
    filter?: Filter | Filter[];
    name?: string;
    props?: string[];
    protos?: string[];
    source?: string[];
}

const genFilters = ({filter, name, props, protos, source}: FilterOptions): Filter[] => [
    ...[filter].flat(),
    typeof name === "string" ? filters.byName(name) : null,
    props instanceof Array ? filters.byProps(props) : null,
    protos instanceof Array ? filters.byProtos(protos) : null,
    source instanceof Array ? filters.bySource(source) : null
].filter((entry) => entry instanceof Function);

export interface ResolveOptions {
    filter?: (target: any) => boolean;
    export?: string;
    name?: string;
}

export const require = getWebpackRequire();

/** @pure */
export const getAll = () => Object.values(require.c);

/** @pure */
export const getSources = () => Object.values(require.m);

/** @pure */
export const getSource = (id: number | string) => require.m[id] ?? null;

/** @pure */
export const find = (...filters: Filter[]) => getAll().find(joinFilters(filters)) ?? null;

/** @pure */
export const query = (options: FilterOptions) => find(...genFilters(options));

/** @pure */
export const byId = (id: number | string) => require.c[id] ?? null;

/** @pure */
export const byExports = (exported: Exports) => find(filters.byExports(exported));

/** @pure */
export const byName = (name: string) => find(filters.byName(name));

/** @pure */
export const byProps = (...props: string[]) => find(filters.byProps(props));

/** @pure */
export const byProtos = (...protos: string[]) => find(filters.byProtos(protos));
<commit_msg>Fix require name<commit_after>import * as filters from "./filters";

export type Exports = Record<string, any>;

export interface Module {
    id: number;
    loaded: boolean;
    exports: Exports;
}

export type ModuleFunction = (this: Exports, module: Module, exports: Exports, require: Require) => void;

export interface Require {
    (id: number): any;

    /** module register */
    m: Record<string, ModuleFunction>;

    /** module cache */
    c: Record<string, Module>;
}

const getWebpackRequire = (): Require => {
    const moduleId = "discordium";

    let webpackRequire: Require;
    global.webpackJsonp.push([[], {
        [moduleId]: (_module: Module, _exports: Exports, require: Require) => {
            webpackRequire = require;
        }
    }, [[moduleId]]]);

    // cleanup
    delete webpackRequire.m[moduleId];
    delete webpackRequire.c[moduleId];

    return webpackRequire;
};

export type Filter = (exports: Exports, module: Module) => boolean;

// TODO: option to check filter on props?
const joinFilters = (filters: Filter[]) => {
    return (module: Module) => {
        const {exports} = module;
        return (
            filters.every((filter) => filter(exports, module))
            || exports?.__esModule && "default" in exports && filters.every((filter) => filter(exports.default, module))
        );
    };
};

export interface FilterOptions {
    filter?: Filter | Filter[];
    name?: string;
    props?: string[];
    protos?: string[];
    source?: string[];
}

const genFilters = ({filter, name, props, protos, source}: FilterOptions): Filter[] => [
    ...[filter].flat(),
    typeof name === "string" ? filters.byName(name) : null,
    props instanceof Array ? filters.byProps(props) : null,
    protos instanceof Array ? filters.byProtos(protos) : null,
    source instanceof Array ? filters.bySource(source) : null
].filter((entry) => entry instanceof Function);

export interface ResolveOptions {
    filter?: (target: any) => boolean;
    export?: string;
    name?: string;
}

export const webpackRequire = getWebpackRequire();

/** @pure */
export const getAll = () => Object.values(webpackRequire.c);

/** @pure */
export const getSources = () => Object.values(webpackRequire.m);

/** @pure */
export const getSource = (id: number | string) => webpackRequire.m[id] ?? null;

/** @pure */
export const find = (...filters: Filter[]) => getAll().find(joinFilters(filters)) ?? null;

/** @pure */
export const query = (options: FilterOptions) => find(...genFilters(options));

/** @pure */
export const byId = (id: number | string) => webpackRequire.c[id] ?? null;

/** @pure */
export const byExports = (exported: Exports) => find(filters.byExports(exported));

/** @pure */
export const byName = (name: string) => find(filters.byName(name));

/** @pure */
export const byProps = (...props: string[]) => find(filters.byProps(props));

/** @pure */
export const byProtos = (...protos: string[]) => find(filters.byProtos(protos));

{'fraction_non_alphanumeric': 0.16795952782462056, 'fraction_numerical': 0.0, 'mean_word_length': 3.5518035303146585, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3716696', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1740, 'n_words': 490}