Objects.AllPaths leaves only + ignore selected types #90
Unanswered
marek-hanzal
asked this question in
Q&A
Replies: 1 comment
-
Im new to hotscript so this can definately be written in a better way import { Fn, Objects, Pipe, Tuples, Call, Unions, ComposeLeft, _, PartialApply } from 'hotscript';
import { unset } from 'hotscript/dist/internals/core/Core';
import { Primitive } from 'hotscript/dist/internals/helpers';
export interface IsLeaf extends Fn {
return: this['arg0'] extends Primitive ? (true) : (keyof this['arg0'] extends never ? true : false)
}
export type PathToEntry<path extends string | number | _ | unset = unset, obj = unset> = PartialApply<PathToEntryFn, [path, obj]>
export interface PathToEntryFn extends Fn {
return: [
this['arg0'],
Call<
Objects.Get<this['arg0']>,
this['arg1']
>
]
}
export interface EntriesDeep extends Fn {
return: Pipe<
this['arg0'],
[
Objects.AllPaths,
Unions.ToTuple,
Tuples.Map<PathToEntry<_, this['arg0']>>,
]
>
}
export type Leaves = ComposeLeft<[
EntriesDeep,
Tuples.Filter<
ComposeLeft<[
Tuples.At<1>,
IsLeaf
]>
>,
]>
const testObject = {
a: 5,
b: {
c: 6
},
z: 'hello world',
w: {
a: {
b: {
c: 'nice',
doSomething() { }
}
}
}
} as const;
type test = Call<Leaves, typeof testObject>;
// [["a", 5], ["z", "hello world"], ["b.c", 6], ["w.a.b.c", "nice"], ["w.a.b.doSomething", () => void]] its a little long but the type names should be semantic enough I hope 😅 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
Maybe it's possible, but I just want to ask, if it's possible to get only leaves of an object, eventually filter out (ignore) specified types of an object (for example DateTime or so)?
Related to question
Thank you :)
Beta Was this translation helpful? Give feedback.
All reactions