Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type definition improvements #102

Open
DamianoNaraku opened this issue Jul 22, 2023 · 0 comments
Open

type definition improvements #102

DamianoNaraku opened this issue Jul 22, 2023 · 0 comments

Comments

@DamianoNaraku
Copy link

DamianoNaraku commented Jul 22, 2023

i'm not knowledgeable enough to submit a push request, so i'm publishing my proposed changes here.
use any git-based editor to check the differences.


// Project: https://github.com/joaonuno/tree-model-js
// Definitions by: Abhas Bhattacharya <https://github.com/bendtherules>
// TypeScript Version: 2.2

export = TreeModel;

declare class TreeModel<T extends Object = Object> {
    constructor(config?: TreeModel.Config<T>);

    private config: TreeModel.Config<T>;

    safe_parse<T>(model: TreeModel.Model<T>): TreeModel.Node<T>;
    parse<T>(model: TreeModel.Model<T>): TreeModel.Node<T>;
}

declare namespace TreeModel {
    class Node<T> {
        constructor(config: Config<T>, model: Model<T>);

        isRoot(): boolean;
        hasChildren(): boolean;
        addChild(child: Node<T>): Node<T>;
        addChildAtIndex(child: Node<T>, index: number): Node<T>;
        setIndex(index: number): Node<T>;
        getPath(): Array<Node<T>>;
        getIndex(): number;

        walk(options: Options, fn: NodeVisitorFunction<T>, ctx?: object): void; 
        walk(fn: NodeVisitorFunction<T>, ctx?: object): void;

        all(options: Options, fn: NodeVisitorFunction<T>, ctx?: object): Array<Node<T>>;
        all(fn: NodeVisitorFunction<T>, ctx?: object): Array<Node<T>>;

        first(options: Options, fn: NodeVisitorFunction<T>, ctx?: object): Node<T> | undefined;
        first(fn: NodeVisitorFunction<T>, ctx?: object): Node<T> | undefined;

        drop(): Node<T>;
        children: Node<T>[];
        config: Config<T>;
        model: Model<T>;
        [propName: string]: any; // is this really necessary? can it have dynamic properties that cannot be listed and typed individually?
    }

    interface Config<T> {
        /**
         * The name for the children array property. Default is "children".
         */
        childrenPropertyName?: keyof T;
        modelComparatorFn?: ComparatorFunction<T>;
        [propName: string]: any;
    }

    interface Options {
        strategy: StrategyName;
    }

    type StrategyName = "pre" | "post" | "breadth";

    type ComparatorFunction<T> = (node1: Node<T>, node2: Node<T>) => number; // this was typed as bool, but from js source it looks to be a number. results are confronted with <= 0
    type NodeVisitorFunction<T> = (visitingNode: Node<T>) => boolean;

    type Model<T> = T;
    // this was "T & { children?: Array<Model<T>> }"
    // it was partially wrong because the children key is not always "children", but a custom key in config.
    // since it re-typed it as "keyof T", it is already guaranteed to be a key of T without adding the  & { customkeyname?: Array<Model<T>> }
    // actually that whole type definition is redundant.
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant