Skip to content

Commit

Permalink
Extract out new types
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyalIcing committed Nov 27, 2023
1 parent 68eb074 commit 8005766
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 130 deletions.
133 changes: 3 additions & 130 deletions src/media-query.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
// https://www.w3.org/TR/mediaqueries-5/
import { afterEach, beforeEach, describe, expect, it } from './test-deps.ts';
import {
mustEnd,
optional,
parse,
ParsedType,
ParseGenerator,
ParseResult,
ParseYieldable,
} from './index.ts';
import { mustEnd, optional, parse, ParseResult } from './index.ts';
import { ParserGenerator, YieldedValue } from './types.ts';

const optionalWhitespace = /^\s*/;
const requiredWhitespace = /^\s+/;
Expand Down Expand Up @@ -202,6 +195,7 @@ class ParsedPointer {
yield 'pointer:';
yield optionalWhitespace;
const [hover] = yield ['none', 'coarse', 'fine'];
// const [hover] = yield* oneOf('none', 'coarse', 'fine');
yield optionalWhitespace;
yield ')';
return new ParsedPointer(
Expand Down Expand Up @@ -314,127 +308,6 @@ class ParsedMediaCondition {
}
}

type GetYield<T, Result> = T extends {
next(...args: [unknown]): IteratorResult<infer A, Result>;
}
? A
: never;

const internal = Symbol('internal');
class YieldedValue<T = never, S extends string = string> {
constructor(stringValue: string) {
this[internal] = stringValue;
}

get value(): T {
return this[internal];
}

get index(): number {
return 0;
}

*[Symbol.iterator](): IterableIterator<S> {
const a: Array<S> = this[internal];
yield* a;
}
}

type PrimitiveYield<T, S extends string> =
| S
| RegExp
// | (() => Omit<Generator<unknown, T, unknown>, "next" | "return" | "throw">)
// | (() => Omit<Generator<unknown, boolean, unknown>, "next" | "return" | "throw">)
| (() => {
[Symbol.iterator](): {
next: {
(result: unknown): IteratorResult<unknown, unknown | boolean>;
// (result: unknown): IteratorResult<unknown, boolean>
};
};
})
| Array<PrimitiveYield<T, S>>;

type Next<T extends object | number | boolean, Result> = {
// next: {
// (s: string): IteratorResult<string, Result>;
// (matches: [string]): IteratorResult<RegExp, Result>;
// };
next: {
// <S extends string = string>(result: YieldedValue<T, S>): IteratorResult<
// PrimitiveYield<T, S> | (() => Generator<unknown, boolean, unknown>),
// Result
// >;
(result: YieldedValue<T, string>): IteratorResult<
typeof result extends YieldedValue<T, infer Z>
? Z extends string
? PrimitiveYield<T, Z>
: PrimitiveYield<T, string>
: PrimitiveYield<T, string>,
Result
>;
// (result: YieldedValue<T>): IteratorResult<PrimitiveYield<T>, Result>;
// <A extends string | [string]>(result: A): A extends string
// ? IteratorResult<string, Result>
// : A extends Iterable<string>
// ? IteratorResult<RegExp, Result>
// : never;
};
};
// | {
// next(
// ...args: [boolean]
// ): IteratorResult<() => Generator<unknown, boolean>, Result>;
// }
// | {
// next(...args: [T]): IteratorResult<() => Generator<unknown, T>, Result>;
// }
// & {
// next(s: string): IteratorResult<string, Result>;
// }
// & {
// next(matches: [string]): IteratorResult<RegExp, Result>;
// }
// & {
// next(): IteratorResult<unknown, Result>;
// };
// type Next<T, Result> = GetYield<T, Result> extends RegExp ? {
// next(
// ...args: [[string] & ReadonlyArray<string>]
// ): IteratorResult<RegExp, Result>;
// } : GetYield<T, Result> extends string ? {
// next(
// ...args: [string]
// ): IteratorResult<string, Result>;
// } : never;

// type Next<T> = {
// next(
// ...args: [[string] & ReadonlyArray<string>]
// ): IteratorResult<RegExp, ParsedMediaAnds>;
// next(
// ...args: [string]
// ): IteratorResult<string, ParsedMediaAnds>;
// };
// type Next<T> = T extends RegExp ? {
// next(
// ...args: [[string] & ReadonlyArray<string>]
// ): IteratorResult<RegExp, ParsedMediaAnds>;
// }
// : T extends string ? {
// next(
// ...args: [string]
// ): IteratorResult<string, ParsedMediaAnds>;
// }
// : never;

type ParserGenerator<
Result,
NextValue extends object | number | boolean = never
> = {
[Symbol.iterator](): Next<NextValue, Result>;
};

class ParsedMediaAnds {
constructor(public readonly list: ReadonlyArray<ParsedMediaInParens>) {}

Expand Down
127 changes: 127 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
type GetYield<T, Result> = T extends {
next(...args: [unknown]): IteratorResult<infer A, Result>;
}
? A
: never;

const internal = Symbol('internal');
export class YieldedValue<T = never, S extends string = string> {
constructor(stringValue: S) {
this[internal] = stringValue;
}

get value(): T {
return this[internal];
}

get index(): number {
return 0;
}

*[Symbol.iterator](): IterableIterator<S> {
const a: Array<S> = this[internal];
yield* a;
}
}

export type PrimitiveYield<T, S extends string> =
| S
| RegExp
// | (() => Omit<Generator<unknown, T, unknown>, "next" | "return" | "throw">)
// | (() => Omit<Generator<unknown, boolean, unknown>, "next" | "return" | "throw">)
| (() => {
[Symbol.iterator](): {
next: {
(result: unknown): IteratorResult<unknown, unknown | boolean>;
// (result: unknown): IteratorResult<unknown, boolean>
};
};
})
| ReadonlyArray<PrimitiveYield<T, S>>;

type Next<T extends object | number | boolean, Result> = {
// next: {
// (s: string): IteratorResult<string, Result>;
// (matches: [string]): IteratorResult<RegExp, Result>;
// };
next: {
// <S extends string = string>(result: YieldedValue<T, S>): IteratorResult<
// PrimitiveYield<T, S> | (() => Generator<unknown, boolean, unknown>),
// Result
// >;
<S2 extends string>(result: YieldedValue<T, S2 | string>): IteratorResult<
typeof result extends YieldedValue<T, infer Z>
? S2 extends Z
? PrimitiveYield<T, string>
: PrimitiveYield<T, string>
: PrimitiveYield<T, string>,
Result
>;
// (result: YieldedValue<T, string>): IteratorResult<
// typeof result extends YieldedValue<T, infer Z>
// ? PrimitiveYield<T, Z>
// : PrimitiveYield<T, string>,
// Result
// >;
// (result: 42): IteratorResult<42, Result>;
// (result: YieldedValue<T>): IteratorResult<PrimitiveYield<T>, Result>;
// <A extends string | [string]>(result: A): A extends string
// ? IteratorResult<string, Result>
// : A extends Iterable<string>
// ? IteratorResult<RegExp, Result>
// : never;
};
};
// | {
// next(
// ...args: [boolean]
// ): IteratorResult<() => Generator<unknown, boolean>, Result>;
// }
// | {
// next(...args: [T]): IteratorResult<() => Generator<unknown, T>, Result>;
// }
// & {
// next(s: string): IteratorResult<string, Result>;
// }
// & {
// next(matches: [string]): IteratorResult<RegExp, Result>;
// }
// & {
// next(): IteratorResult<unknown, Result>;
// };
// type Next<T, Result> = GetYield<T, Result> extends RegExp ? {
// next(
// ...args: [[string] & ReadonlyArray<string>]
// ): IteratorResult<RegExp, Result>;
// } : GetYield<T, Result> extends string ? {
// next(
// ...args: [string]
// ): IteratorResult<string, Result>;
// } : never;

// type Next<T> = {
// next(
// ...args: [[string] & ReadonlyArray<string>]
// ): IteratorResult<RegExp, ParsedMediaAnds>;
// next(
// ...args: [string]
// ): IteratorResult<string, ParsedMediaAnds>;
// };
// type Next<T> = T extends RegExp ? {
// next(
// ...args: [[string] & ReadonlyArray<string>]
// ): IteratorResult<RegExp, ParsedMediaAnds>;
// }
// : T extends string ? {
// next(
// ...args: [string]
// ): IteratorResult<string, ParsedMediaAnds>;
// }
// : never;

export type ParserGenerator<
Result,
NextValue extends object | number | boolean = never
> = {
[Symbol.iterator](): Next<NextValue, Result>;
};

0 comments on commit 8005766

Please sign in to comment.