Skip to content

Commit

Permalink
Merge pull request #116 from jsr-core/pred-rest
Browse files Browse the repository at this point in the history
fix[isTupleOf,isParametersOf]: rename predElse to predRest
  • Loading branch information
lambdalisue authored Aug 11, 2024
2 parents 255ee41 + 6c0a688 commit e3c9627
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 48 deletions.
8 changes: 4 additions & 4 deletions is/__snapshots__/parameters_of_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ snapshot[`isParametersOf<T> > returns properly named predicate function 4`] = `
])"
`;
snapshot[`isParametersOf<T, E> > returns properly named predicate function 1`] = `
snapshot[`isParametersOf<T, R> > returns properly named predicate function 1`] = `
"isParametersOf([
isNumber,
isString,
asOptional(isBoolean)
], isArray)"
`;
snapshot[`isParametersOf<T, E> > returns properly named predicate function 2`] = `"isParametersOf([(anonymous)], isArrayOf(isString))"`;
snapshot[`isParametersOf<T, R> > returns properly named predicate function 2`] = `"isParametersOf([(anonymous)], isArrayOf(isString))"`;
snapshot[`isParametersOf<T, E> > returns properly named predicate function 3`] = `"isParametersOf([], isArrayOf(isString))"`;
snapshot[`isParametersOf<T, R> > returns properly named predicate function 3`] = `"isParametersOf([], isArrayOf(isString))"`;
snapshot[`isParametersOf<T, E> > returns properly named predicate function 4`] = `
snapshot[`isParametersOf<T, R> > returns properly named predicate function 4`] = `
"isParametersOf([
isParametersOf([
isParametersOf([
Expand Down
6 changes: 3 additions & 3 deletions is/__snapshots__/tuple_of_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ snapshot[`isTupleOf<T> > returns properly named predicate function 3`] = `
])"
`;
snapshot[`isTupleOf<T, E> > returns properly named predicate function 1`] = `
snapshot[`isTupleOf<T, R> > returns properly named predicate function 1`] = `
"isTupleOf([
isNumber,
isString,
isBoolean
], isArray)"
`;
snapshot[`isTupleOf<T, E> > returns properly named predicate function 2`] = `"isTupleOf([(anonymous)], isArrayOf(isString))"`;
snapshot[`isTupleOf<T, R> > returns properly named predicate function 2`] = `"isTupleOf([(anonymous)], isArrayOf(isString))"`;
snapshot[`isTupleOf<T, E> > returns properly named predicate function 3`] = `
snapshot[`isTupleOf<T, R> > returns properly named predicate function 3`] = `
"isTupleOf([
isTupleOf([
isTupleOf([
Expand Down
12 changes: 6 additions & 6 deletions is/parameters_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { isArray } from "./array.ts";
* }
* ```
*
* With `predElse`:
* With `predRest` to represent rest parameters:
*
* ```ts
* import { as, is } from "@core/unknownutil";
Expand Down Expand Up @@ -71,18 +71,18 @@ export function isParametersOf<
E extends Predicate<unknown[]>,
>(
predTup: T,
predElse: E,
predRest: E,
): Predicate<[...ParametersOf<T>, ...PredicateType<E>]>;
export function isParametersOf<
T extends readonly [...Predicate<unknown>[]],
E extends Predicate<unknown[]>,
>(
predTup: T,
predElse?: E,
predRest?: E,
): Predicate<ParametersOf<T> | [...ParametersOf<T>, ...PredicateType<E>]> {
const requiresLength = 1 +
predTup.findLastIndex((pred) => !hasOptional(pred));
if (!predElse) {
if (!predRest) {
return rewriteName(
(x: unknown): x is ParametersOf<T> => {
if (
Expand All @@ -103,11 +103,11 @@ export function isParametersOf<
}
const head = x.slice(0, predTup.length);
const tail = x.slice(predTup.length);
return predTup.every((pred, i) => pred(head[i])) && predElse(tail);
return predTup.every((pred, i) => pred(head[i])) && predRest(tail);
},
"isParametersOf",
predTup,
predElse,
predRest,
);
}
}
Expand Down
26 changes: 13 additions & 13 deletions is/parameters_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Deno.test("isParametersOf<T>", async (t) => {
});
});

Deno.test("isParametersOf<T, E>", async (t) => {
Deno.test("isParametersOf<T, R>", async (t) => {
await t.step("returns properly named predicate function", async (t) => {
assertEquals(typeof isParametersOf([], is.Array), "function");
await assertSnapshot(
Expand Down Expand Up @@ -92,32 +92,32 @@ Deno.test("isParametersOf<T, E>", async (t) => {

await t.step("returns true on T tuple", () => {
const predTup = [is.Number, is.String, as.Optional(is.Boolean)] as const;
const predElse = is.ArrayOf(is.Number);
const predRest = is.ArrayOf(is.Number);
assertEquals(
isParametersOf(predTup, predElse)([0, "a", true, 0, 1, 2]),
isParametersOf(predTup, predRest)([0, "a", true, 0, 1, 2]),
true,
);
assertEquals(
isParametersOf(predTup, predElse)([0, "a", undefined, 0, 1, 2]),
isParametersOf(predTup, predRest)([0, "a", undefined, 0, 1, 2]),
true,
);
assertEquals(isParametersOf(predTup, predElse)([0, "a"]), true);
assertEquals(isParametersOf(predTup, predRest)([0, "a"]), true);
});

await t.step("returns false on non T tuple", () => {
const predTup = [is.Number, is.String, as.Optional(is.Boolean)] as const;
const predElse = is.ArrayOf(is.String);
assertEquals(isParametersOf(predTup, predElse)([0, 1, 2, 0, 1, 2]), false);
assertEquals(isParametersOf(predTup, predElse)([0, "a", 0, 1, 2]), false);
const predRest = is.ArrayOf(is.String);
assertEquals(isParametersOf(predTup, predRest)([0, 1, 2, 0, 1, 2]), false);
assertEquals(isParametersOf(predTup, predRest)([0, "a", 0, 1, 2]), false);
assertEquals(
isParametersOf(predTup, predElse)([0, "a", true, 0, 1, 2]),
isParametersOf(predTup, predRest)([0, "a", true, 0, 1, 2]),
false,
);
assertEquals(
isParametersOf(predTup, predElse)([0, "a", undefined, 0, 1, 2]),
isParametersOf(predTup, predRest)([0, "a", undefined, 0, 1, 2]),
false,
);
assertEquals(isParametersOf(predTup, predElse)([0, "a", "b"]), false);
assertEquals(isParametersOf(predTup, predRest)([0, "a", "b"]), false);
});

await t.step("predicated type is correct", () => {
Expand All @@ -127,9 +127,9 @@ Deno.test("isParametersOf<T, E>", async (t) => {
as.Optional(is.String),
as.Optional(is.Boolean),
] as const;
const predElse = is.ArrayOf(is.Number);
const predRest = is.ArrayOf(is.Number);
const a: unknown = [0, "a"];
if (isParametersOf(predTup, predElse)(a)) {
if (isParametersOf(predTup, predRest)(a)) {
assertType<
Equal<
typeof a,
Expand Down
24 changes: 12 additions & 12 deletions is/tuple_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Predicate, PredicateType } from "../type.ts";
import { isArray } from "./array.ts";

/**
* Return a type predicate function that returns `true` if the type of `x` is `TupleOf<T>` or `TupleOf<T, E>`.
* Return a type predicate function that returns `true` if the type of `x` is `TupleOf<T>` or `TupleOf<T, R>`.
*
* Use {@linkcode isUniformTupleOf} to check if the type of `x` is a tuple of uniform types.
*
Expand All @@ -19,7 +19,7 @@ import { isArray } from "./array.ts";
* }
* ```
*
* With `predElse`:
* With `predRest` to represent rest elements:
*
* ```ts
* import { is } from "@core/unknownutil";
Expand Down Expand Up @@ -56,20 +56,20 @@ export function isTupleOf<

export function isTupleOf<
T extends readonly [Predicate<unknown>, ...Predicate<unknown>[]],
E extends Predicate<unknown[]>,
R extends Predicate<unknown[]>,
>(
predTup: T,
predElse: E,
): Predicate<[...TupleOf<T>, ...PredicateType<E>]>;
predRest: R,
): Predicate<[...TupleOf<T>, ...PredicateType<R>]>;

export function isTupleOf<
T extends readonly [Predicate<unknown>, ...Predicate<unknown>[]],
E extends Predicate<unknown[]>,
R extends Predicate<unknown[]>,
>(
predTup: T,
predElse?: E,
): Predicate<TupleOf<T> | [...TupleOf<T>, ...PredicateType<E>]> {
if (!predElse) {
predRest?: R,
): Predicate<TupleOf<T> | [...TupleOf<T>, ...PredicateType<R>]> {
if (!predRest) {
return rewriteName(
(x: unknown): x is TupleOf<T> => {
if (!isArray(x) || x.length !== predTup.length) {
Expand All @@ -82,17 +82,17 @@ export function isTupleOf<
);
} else {
return rewriteName(
(x: unknown): x is [...TupleOf<T>, ...PredicateType<E>] => {
(x: unknown): x is [...TupleOf<T>, ...PredicateType<R>] => {
if (!isArray(x) || x.length < predTup.length) {
return false;
}
const head = x.slice(0, predTup.length);
const tail = x.slice(predTup.length);
return predTup.every((pred, i) => pred(head[i])) && predElse(tail);
return predTup.every((pred, i) => pred(head[i])) && predRest(tail);
},
"isTupleOf",
predTup,
predElse,
predRest,
);
}
}
Expand Down
20 changes: 10 additions & 10 deletions is/tuple_of_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Deno.test("isTupleOf<T>", async (t) => {
});
});

Deno.test("isTupleOf<T, E>", async (t) => {
Deno.test("isTupleOf<T, R>", async (t) => {
await t.step("returns properly named predicate function", async (t) => {
await assertSnapshot(
t,
Expand All @@ -67,27 +67,27 @@ Deno.test("isTupleOf<T, E>", async (t) => {

await t.step("returns true on T tuple", () => {
const predTup = [is.Number, is.String, is.Boolean] as const;
const predElse = is.ArrayOf(is.Number);
assertEquals(isTupleOf(predTup, predElse)([0, "a", true, 0, 1, 2]), true);
const predRest = is.ArrayOf(is.Number);
assertEquals(isTupleOf(predTup, predRest)([0, "a", true, 0, 1, 2]), true);
});

await t.step("returns false on non T tuple", () => {
const predTup = [is.Number, is.String, is.Boolean] as const;
const predElse = is.ArrayOf(is.String);
assertEquals(isTupleOf(predTup, predElse)([0, 1, 2, 0, 1, 2]), false);
assertEquals(isTupleOf(predTup, predElse)([0, "a", 0, 1, 2]), false);
const predRest = is.ArrayOf(is.String);
assertEquals(isTupleOf(predTup, predRest)([0, 1, 2, 0, 1, 2]), false);
assertEquals(isTupleOf(predTup, predRest)([0, "a", 0, 1, 2]), false);
assertEquals(
isTupleOf(predTup, predElse)([0, "a", true, 0, 0, 1, 2]),
isTupleOf(predTup, predRest)([0, "a", true, 0, 0, 1, 2]),
false,
);
assertEquals(isTupleOf(predTup, predElse)([0, "a", true, 0, 1, 2]), false);
assertEquals(isTupleOf(predTup, predRest)([0, "a", true, 0, 1, 2]), false);
});

await t.step("predicated type is correct", () => {
const predTup = [is.Number, is.String, is.Boolean] as const;
const predElse = is.ArrayOf(is.Number);
const predRest = is.ArrayOf(is.Number);
const a: unknown = [0, "a", true, 0, 1, 2];
if (isTupleOf(predTup, predElse)(a)) {
if (isTupleOf(predTup, predRest)(a)) {
assertType<Equal<typeof a, [number, string, boolean, ...number[]]>>(
true,
);
Expand Down

0 comments on commit e3c9627

Please sign in to comment.