Skip to content

Commit

Permalink
fix recursive collectable id inference
Browse files Browse the repository at this point in the history
  • Loading branch information
rsek committed Jan 17, 2024
1 parent cc4070b commit be55555
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 50 deletions.
2 changes: 2 additions & 0 deletions pkg/nodejs/@datasworn/core/dist/Id/Utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@ export type ExtractAncestorCollectionPathElements<T extends Strings.RecursiveCol
...infer U extends Strings.CollectionAncestorKeys,
string
] ? U : never;
export type Last<T extends unknown[]> = T extends [infer U] ? U : T extends [...T[number][], infer U] ? U : never;
export type DropLast<T extends unknown[]> = T extends [T[number]] ? [] : T extends [...infer U extends T[number][], T[number]] ? U : never;
export type { AnyCollectionType as AnyCollection, AnyCollectableType as AnyCollectable, NonCollectableType as AnyNonCollectable } from './TypeMaps.js';
30 changes: 17 additions & 13 deletions pkg/nodejs/@datasworn/core/dist/Id/test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const IdParser_js_1 = require("../IdParser.js");
const recursiveCollectionId = 'sundered_isles/collections/oracles/core';
const recursiveCollectableId = 'sundered_isles/oracles/core/action';
const nonRecursiveCollectionId = 'starforged/collections/moves/combat';
const nonRecursiveCollectableId = 'starforged/moves/combat/strike';
const nonCollectableId = 'delve/site_themes/hallowed';
const testParse = [
IdParser_js_1.IdParser.from(recursiveCollectableId),
IdParser_js_1.IdParser.from(nonRecursiveCollectionId),
IdParser_js_1.IdParser.from(recursiveCollectionId),
IdParser_js_1.IdParser.from(nonRecursiveCollectableId),
IdParser_js_1.IdParser.from(nonCollectableId)
];
for (const id of testParse)
const recursiveCollectionString = 'sundered_isles/collections/oracles/core';
const recursiveCollectableString = 'sundered_isles/oracles/core/action';
const nonRecursiveCollectionString = 'starforged/collections/moves/combat';
const nonRecursiveCollectableString = 'starforged/moves/combat/strike';
const nonCollectableString = 'delve/site_themes/hallowed';
const recursiveCollectableId = IdParser_js_1.IdParser.from(recursiveCollectableString);
const nonRecursiveCollectionId = IdParser_js_1.IdParser.from(nonRecursiveCollectionString);
const recursiveCollectionId = IdParser_js_1.IdParser.from(recursiveCollectionString);
const nonRecursiveCollectableId = IdParser_js_1.IdParser.from(nonRecursiveCollectableString);
const nonCollectableId = IdParser_js_1.IdParser.from(nonCollectableString);
for (const id of [
recursiveCollectableId,
recursiveCollectionId,
nonRecursiveCollectableId,
nonRecursiveCollectionId,
nonCollectableId
])
console.log(id.toString(), id.toPath().join('.'));
8 changes: 3 additions & 5 deletions pkg/nodejs/@datasworn/core/dist/IdParser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ObjectGlobber from './ObjectGlobPath/ObjectGlobber.js';
import type * as Strings from './Id/Strings.js';
import { type CollectableType, type CollectionSubtype, type TypeForTypeComposite } from './Id/TypeMaps.js';
import type * as Utils from './Id/Utils.js';
type DataswornTree = Record<string, Datasworn.RulesPackage> | Map<string, Datasworn.RulesPackage>;
export type DataswornTree = Record<string, Datasworn.RulesPackage> | Map<string, Datasworn.RulesPackage>;
/**
* Creates, parses, and locates Datasworn IDs in the Datasworn tree. Id utilities are collected here as static methods.
* @see
Expand Down Expand Up @@ -159,8 +159,6 @@ declare namespace IdParser {
};
type ToStringArray<RulesPackage extends string, TypeKeys extends Utils.AnyTypeKeys, PathKeys extends Utils.AnyPathKeys> = [RulesPackage, ...TypeKeys, ...PathKeys];
type ToString<RulesPackage extends string, TypeKeys extends Utils.AnyTypeKeys, PathKeys extends Utils.AnyPathKeys> = Utils.Join<ToStringArray<RulesPackage, TypeKeys, PathKeys>, CONST.Sep>;
type Last<T extends unknown[]> = T extends [infer U] ? U : T extends [...T[number][], infer U] ? U : never;
type DropLast<T extends unknown[]> = T extends [T[number]] ? [] : T extends [...infer U extends T[number][], T[number]] ? U : never;
type AnyParsedId = IdParser.Parsed<Strings.AnyCollectableId> | IdParser.Parsed<Strings.AnyCollectionId> | IdParser.Parsed<Strings.NonCollectableId>;
}
declare abstract class CollectionId<RulesPackage extends string = string, Subtype extends TypeElements.Collectable.Any = TypeElements.Collectable.Any, AncestorKeys extends Strings.DictKey[] | [] = Strings.DictKey[], Key extends Strings.DictKey = Strings.DictKey> extends IdParser<RulesPackage, [
Expand Down Expand Up @@ -325,7 +323,7 @@ interface RecursiveCollectableId<RulesPackage extends string = string, Type exte
get ancestorCollectionKeys(): AncestorKeys;
}
declare namespace RecursiveCollectableId {
type FromString<T extends Strings.RecursiveCollectableId> = T extends `${infer RulesPackage}${CONST.Sep}${infer Type extends TypeElements.Collectable.Recursive}${infer AncestorPath extends `${CONST.Sep}${string}`}${CONST.Sep}${infer Key}` ? RecursiveCollectableId<RulesPackage, Type, Utils.Split<AncestorPath>, Key> & {
type FromString<T extends Strings.RecursiveCollectableId> = T extends `${infer RulesPackage}${CONST.Sep}${infer Type extends TypeElements.Collectable.Recursive}${CONST.Sep}${string}` ? RecursiveCollectableId<RulesPackage, Type, Utils.ExtractAncestorPathElements<T>, Utils.ExtractKey<T>> & {
id: T;
} : never;
}
Expand All @@ -350,7 +348,7 @@ declare class RecursiveCollectionId<RulesPackage extends string = string, Subtyp
/**
* Returns a new {@link RecursiveCollectionId} instance for the ID of this object's parent RecursiveCollection, if one exists.
*/
getParentCollectionId(): AncestorKeys extends [] ? never : RecursiveCollectionId<RulesPackage, Subtype, IdParser.DropLast<AncestorKeys>, IdParser.Last<AncestorKeys>>;
getParentCollectionId(): AncestorKeys extends [] ? never : RecursiveCollectionId<RulesPackage, Subtype, Utils.DropLast<AncestorKeys>, Utils.Last<AncestorKeys>>;
get canHaveCollectionChild(): AncestorKeys extends string[] & {
length: 1 | 2;
} ? true : false;
Expand Down
13 changes: 13 additions & 0 deletions src/pkg-core/Id/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,19 @@ export type ExtractAncestorCollectionPathElements<
? U
: never

export type Last<T extends unknown[]> = T extends [infer U]
? U
: T extends [...T[number][], infer U]
? U
: never

export type DropLast<T extends unknown[]> = T extends [T[number]]
? []
: T extends [...infer U extends T[number][], T[number]]
? U
: never


type fff = ExtractAncestorPathElements<'sundered_isles/oracles/core/action'>

export type {
Expand Down
33 changes: 20 additions & 13 deletions src/pkg-core/Id/test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import type * as Strings from './Strings.js'
import { IdParser } from '../IdParser.js'

const recursiveCollectionId =
const recursiveCollectionString =
'sundered_isles/collections/oracles/core' satisfies Strings.RecursiveCollectionId
const recursiveCollectableId =
const recursiveCollectableString =
'sundered_isles/oracles/core/action' satisfies Strings.RecursiveCollectableId
const nonRecursiveCollectionId =
const nonRecursiveCollectionString =
'starforged/collections/moves/combat' satisfies Strings.NonRecursiveCollectionId
const nonRecursiveCollectableId =
const nonRecursiveCollectableString =
'starforged/moves/combat/strike' satisfies Strings.NonRecursiveCollectableId
const nonCollectableId =
const nonCollectableString =
'delve/site_themes/hallowed' satisfies Strings.NonCollectableId
const testParse = [
IdParser.from(recursiveCollectableId),
IdParser.from(nonRecursiveCollectionId),
IdParser.from(recursiveCollectionId),
IdParser.from(nonRecursiveCollectableId),
IdParser.from(nonCollectableId)
]

for (const id of testParse) console.log(id.toString(), id.toPath().join('.'))
const recursiveCollectableId = IdParser.from(recursiveCollectableString)

const nonRecursiveCollectionId = IdParser.from(nonRecursiveCollectionString)
const recursiveCollectionId = IdParser.from(recursiveCollectionString)

const nonRecursiveCollectableId = IdParser.from(nonRecursiveCollectableString)
const nonCollectableId = IdParser.from(nonCollectableString)

for (const id of [
recursiveCollectableId,
recursiveCollectionId,
nonRecursiveCollectableId,
nonRecursiveCollectionId,
nonCollectableId
])
console.log(id.toString(), id.toPath().join('.'))
25 changes: 6 additions & 19 deletions src/pkg-core/IdParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from './Id/TypeMaps.js'
import type * as Utils from './Id/Utils.js'

type DataswornTree =
export type DataswornTree =
| Record<string, Datasworn.RulesPackage>
| Map<string, Datasworn.RulesPackage>

Expand Down Expand Up @@ -720,18 +720,6 @@ namespace IdParser {
PathKeys extends Utils.AnyPathKeys
> = Utils.Join<ToStringArray<RulesPackage, TypeKeys, PathKeys>, CONST.Sep>

export type Last<T extends unknown[]> = T extends [infer U]
? U
: T extends [...T[number][], infer U]
? U
: never

export type DropLast<T extends unknown[]> = T extends [T[number]]
? []
: T extends [...infer U extends T[number][], T[number]]
? U
: never

export type AnyParsedId =
| IdParser.Parsed<Strings.AnyCollectableId>
| IdParser.Parsed<Strings.AnyCollectionId>
Expand Down Expand Up @@ -1052,13 +1040,12 @@ interface RecursiveCollectableId<
namespace RecursiveCollectableId {
export type FromString<T extends Strings.RecursiveCollectableId> =
T extends `${infer RulesPackage}${CONST.Sep}${infer Type extends
TypeElements.Collectable.Recursive}${infer AncestorPath extends
`${CONST.Sep}${string}`}${CONST.Sep}${infer Key}`
TypeElements.Collectable.Recursive}${CONST.Sep}${string}`
? RecursiveCollectableId<
RulesPackage,
Type,
Utils.Split<AncestorPath>,
Key
Utils.ExtractAncestorPathElements<T>,
Utils.ExtractKey<T>
> & {
id: T
}
Expand Down Expand Up @@ -1135,8 +1122,8 @@ class RecursiveCollectionId<
const result = new RecursiveCollectionId<
RulesPackage,
Subtype,
IdParser.DropLast<AncestorKeys>,
IdParser.Last<AncestorKeys>
Utils.DropLast<AncestorKeys>,
Utils.Last<AncestorKeys>
>(this.rulesPackage, this.subtype, ...(this.ancestorCollectionKeys as any))

return result as AncestorKeys extends [] ? never : typeof result
Expand Down

0 comments on commit be55555

Please sign in to comment.