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

Add typings #24

Merged
merged 4 commits into from
Aug 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Type definitions for parsel-js 1.0.2
// Project: https://github.com/LeaVerou/parsel
// Definitions by:
// - Matt Oxley <https://github.com/mattflux>
// - Naveen DA <https://github.com/NaveenDA>
// Definitions: https://github.com/LeaVerou/parsel/blob/master/index.d.ts

interface Tokens {
type:
| "class"
| "attribute"
| "id"
| "type"
| "pseudo-element"
| "pseudo-class"
| "comma"
| "combinator";
content: string;
name: string;
namespace?: string;
value?: string;
pos: [number, number];
operator?: string;
argument?: string;
subtree?: AST;
caseSensitive?: "i";
}

interface Complex {
type: "complex";
combinator: string;
right: AST;
left: AST;
}

interface Compound {
type: "compound";
list: Tokens[];
}

interface List {
type: "list";
list: AST[];
}

interface ParserOptions {
recursive?: boolean;
list?: boolean;
}

interface SpecificityOptions {
format?: string;
}

type AST = Complex | Compound | List | Tokens;

/**
* Get AST:
*/
export function parse(selector: string, options?: ParserOptions): AST;

/**
* Get list of tokens as a flat array:
*/
export function tokenize(selector: string): Tokens[];

/**
* Traverse all tokens of a (sub)tree:
*/
export function walk(node: AST, cb: (node: AST, parentNode: AST) => {}): void;

/**
* Calculate specificity (returns an array of 3 numbers):
*/
export function specificity(
selector: string | AST,
options?: SpecificityOptions
): number[];

/**
* To convert the specificity array to a number
*/
export function specificityToNumber(
specificity: number[],
base?: number
): number;