Skip to content
Merged
Show file tree
Hide file tree
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
56 changes: 18 additions & 38 deletions scripts/dataParser/parsers/record/findRecordRequiredKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { nilKind } from "../nil";

export function findRecordRequiredKeyOnTemplateLiteralPart(
templatePart: readonly TemplateLiteralParts[],
): string[] | null {
): readonly string[] {
return pipe(
templatePart,
DArray.map(
Expand All @@ -24,7 +24,7 @@ export function findRecordRequiredKeyOnTemplateLiteralPart(
(value) => stringKind.has(value)
|| numberKind.has(value)
|| bigIntKind.has(value),
justReturn(null),
justReturn([]),
),
DPattern.when(
or([
Expand All @@ -40,12 +40,20 @@ export function findRecordRequiredKeyOnTemplateLiteralPart(
isType("bigint"),
(value) => `${value}n`,
),
String,
DString.to,
),
),
DPattern.when(
literalKind.has,
(value) => findRecordRequiredKey(value),
(value) => pipe(
value.definition.value,
DArray.map(
(element) => findRecordRequiredKeyOnTemplateLiteralPart(
[element],
),
),
DArray.flat,
),
),
DPattern.when(
templateLiteralKind.has,
Expand All @@ -72,24 +80,16 @@ export function findRecordRequiredKeyOnTemplateLiteralPart(
[element],
),
),
DPattern.when(
DArray.notIncludes(null),
DArray.flat,
),
DPattern.otherwise(justReturn(null)),
DArray.flat,
),
),
DPattern.exhaustive,
),
),
DArray.reduce(
DArray.reduceFrom<string[]>([""]),
({ lastValue, element, exit, next }) => pipe(
({ lastValue, element, next }) => pipe(
element,
DPattern.when(
isType("null"),
justReturn(exit(null)),
),
DPattern.when(
isType("string"),
(element) => next(
Expand Down Expand Up @@ -117,43 +117,23 @@ export function findRecordRequiredKeyOnTemplateLiteralPart(
);
}

export function findRecordRequiredKey(keyParser: DataParserRecordKey): string[] | null {
export function findRecordRequiredKey(keyParser: DataParserRecordKey): readonly string[] {
return pipe(
keyParser,
DPattern.when(
(value) => stringKind.has(value) || numberKind.has(value),
justReturn(null),
justReturn([]),
),
DPattern.when(
literalKind.has,
(dataParser) => pipe(
dataParser.definition.value,
DArray.map(
innerPipe(
when(
isType("bigint"),
(value) => `${value}n`,
),
String,
),
),
),
(dataParser) => dataParser.definition.value,
),
DPattern.when(
unionKind.has,
(dataParser) => pipe(
dataParser.definition.options,
DArray.map(findRecordRequiredKey),
DPattern.when(
DArray.includes(null),
justReturn(null),
),
DPattern.otherwise(
innerPipe(
DArray.filter(isType("array")),
DArray.flat,
),
),
DArray.flat,
),
),
DPattern.when(
Expand Down
51 changes: 22 additions & 29 deletions scripts/dataParser/parsers/record/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import { type NeverCoalescing, type Kind, type FixDeepFunctionInfer, type Adaptor, createOverride } from "@scripts/common";
/* eslint-disable @typescript-eslint/prefer-for-of */
import { type NeverCoalescing, type Kind, type FixDeepFunctionInfer, createOverride } from "@scripts/common";
import { type DataParserDefinition, type DataParser, dataParserInit, type Output, type Input, SymbolDataParserError, type DataParserChecker } from "../../base";
import { type AddCheckersToDefinition, type MergeDefinition } from "@scripts/dataParser/types";
import { popErrorPath, setErrorPath, SymbolDataParserErrorIssue } from "@scripts/dataParser/error";
import { type DataParserString } from "../string";
import { type DataParserTemplateLiteral } from "../templateLiteral";
import { type DataParserLiteral } from "../literal";
import { type DataParserDefinitionLiteral, type DataParserLiteral } from "../literal";
import { type DataParserDefinitionNumber, type DataParserNumber } from "../number";
import { type DataParserDefinitionUnion, type DataParserUnion } from "../union";
import { createDataParserKind } from "../../kind";
import { type CheckerRefineImplementation } from "../refine";
import { findRecordRequiredKey } from "./findRecordRequiredKey";
import { type TemplateLiteralContainLargeType } from "@scripts/string";
import { type GetPropsWithValueExtends } from "@scripts/object";

export * from "./findRecordRequiredKey";

export type DataParserRecordKey = (
| DataParserString
| DataParserTemplateLiteral
| DataParserLiteral
| DataParserLiteral<
& DataParserDefinitionLiteral
& {
value: readonly string[];
}
>
| DataParserNumber<
& DataParserDefinitionNumber
& {
Expand Down Expand Up @@ -55,7 +60,7 @@ export interface DataParserDefinitionRecord extends DataParserDefinition<
> {
readonly key: DataParserRecordKey;
readonly value: DataParser;
readonly requireKey: string[] | null;
readonly requireKey: readonly string[];
}

export const recordKind = createDataParserKind("record");
Expand All @@ -73,13 +78,7 @@ export type DataParserRecordShapeOutput<
: never
>,
any
> extends infer InferredOutput extends Record<string, unknown>
? TemplateLiteralContainLargeType<
Adaptor<keyof InferredOutput, string>
> extends true
? Partial<InferredOutput>
: InferredOutput
: never;
>;

export type DataParserRecordShapeInput<
GenericDataParserKey extends DataParserRecordKey,
Expand All @@ -94,13 +93,7 @@ export type DataParserRecordShapeInput<
: never
>,
any
> extends infer InferredInput extends Record<string, unknown>
? TemplateLiteralContainLargeType<
Adaptor<keyof InferredInput, string>
> extends true
? Partial<InferredInput>
: InferredInput
: never;
>;

type _DataParserRecord<
GenericDefinition extends DataParserDefinitionRecord,
Expand Down Expand Up @@ -217,11 +210,11 @@ export function record<
return output;
}

if (
self.definition.requireKey
&& self.definition.requireKey.length !== Object.keys(output).length
) {
return SymbolDataParserErrorIssue;
for (let index = 0; index < self.definition.requireKey.length; index++) {
const requiredKey = self.definition.requireKey[index]!;
if (!(requiredKey in output)) {
return SymbolDataParserErrorIssue;
}
}

return output;
Expand Down Expand Up @@ -270,11 +263,11 @@ export function record<
return output;
}

if (
self.definition.requireKey
&& self.definition.requireKey.length !== Object.keys(output).length
) {
return SymbolDataParserErrorIssue;
for (let index = 0; index < self.definition.requireKey.length; index++) {
const requiredKey = self.definition.requireKey[index]!;
if (!(requiredKey in output)) {
return SymbolDataParserErrorIssue;
}
}

return output;
Expand Down
21 changes: 14 additions & 7 deletions scripts/string/types/pop.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import type { IsEqual } from "@scripts/common";
import type { TemplateLiteralContainLargeType } from "./templateLiteralContainLargeType";

type _Pop<
GenericValue extends string,
GenericCount extends never[] = [],
> = IsEqual<GenericCount["length"], 250> extends true
? string
: GenericValue extends `${infer InferredFirst}${infer InferredRest}`
? IsEqual<InferredRest, ""> extends true
? ""
: _Pop<InferredRest, [...GenericCount, never]> extends infer InferredResult extends string
? `${InferredFirst}${InferredResult}`
: never
: string;

export type Pop<
GenericValue extends string,
> = TemplateLiteralContainLargeType<GenericValue> extends true
? string
: IsEqual<GenericValue, ""> extends true
? ""
: GenericValue extends `${infer InferredFirst}${infer InferredRest}`
? IsEqual<InferredRest, ""> extends true
? ""
: Pop<InferredRest> extends infer InferredResult extends string
? `${InferredFirst}${InferredResult}`
: never
: string;
: _Pop<GenericValue>;
48 changes: 29 additions & 19 deletions scripts/string/types/split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,43 @@ import { type Or, type IsEqual } from "@scripts/common";
import { type Includes } from "./includes";
import { type TemplateLiteralContainLargeType } from "./templateLiteralContainLargeType";

export type Split<
type _Split<
GenericString extends string,
GenericSeparator extends string,
GenericLimit extends number = number,
GenericLastResult extends string[] = [],
> = GenericString extends `${infer InferredBefore}${GenericSeparator}${infer InferredAfter}`
? [...GenericLastResult, InferredBefore] extends infer InferredResult extends any[]
? IsEqual<InferredAfter, ""> extends true
? InferredResult
: IsEqual<InferredResult["length"], 250> extends true
? Includes<InferredAfter, GenericSeparator> extends true
? [...InferredResult, ...string[]]
: InferredResult
: IsEqual<InferredResult["length"], GenericLimit> extends true
? InferredResult
: _Split<
InferredAfter,
GenericSeparator,
GenericLimit,
InferredResult
>
: never
: [...GenericLastResult, GenericString];

export type Split<
GenericString extends string,
GenericSeparator extends string,
GenericLimit extends number = number,
> = IsEqual<GenericLimit, 0> extends true
? []
: Or<[
TemplateLiteralContainLargeType<GenericString>,
TemplateLiteralContainLargeType<GenericSeparator>,
]> extends true
? [string, ...string[]]
: GenericString extends `${infer InferredBefore}${GenericSeparator}${infer InferredAfter}`
? [...GenericLastResult, InferredBefore] extends infer InferredResult extends any[]
? IsEqual<InferredAfter, ""> extends true
? InferredResult
: IsEqual<InferredResult["length"], 250> extends true
? Includes<InferredAfter, GenericSeparator> extends true
? [...InferredResult, ...string[]]
: InferredResult
: IsEqual<InferredResult["length"], GenericLimit> extends true
? InferredResult
: Split<
InferredAfter,
GenericSeparator,
GenericLimit,
InferredResult
>
: never
: [...GenericLastResult, GenericString];
: _Split<
GenericString,
GenericSeparator,
GenericLimit
>;
18 changes: 5 additions & 13 deletions scripts/string/types/templateLiteralContainLargeType.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { type IsEqual, type Or } from "@scripts/common";

export type TemplateLiteralContainLargeType<
GenericValue extends string,
> = (
GenericValue extends `${infer InferredFirst}${infer InferredLast}`
? Or<[
IsEqual<InferredFirst, `${number}`>,
IsEqual<InferredFirst, `${bigint}`>,
IsEqual<InferredFirst, string>,
]> extends false
? TemplateLiteralContainLargeType<InferredLast>
: true
: GenericValue extends ""
? false
: true
GenericValue extends string
? {} extends Record<GenericValue, never>
? true
: false
: never
) extends false
? false
: true;
2 changes: 1 addition & 1 deletion tests/dataParser/extended/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe("extended.record", () => {

type check = ExpectType<
typeof result,
DEither.Error<DDataParser.DataParserError> | DEither.Success<Partial<Record<string, string>>>,
DEither.Error<DDataParser.DataParserError> | DEither.Success<Record<string, string>>,
"strict"
>;
});
Expand Down
Loading
Loading