-
Notifications
You must be signed in to change notification settings - Fork 0
/
toEntries.ts
31 lines (25 loc) · 933 Bytes
/
toEntries.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import type { Entries, GetKeys, GetValues, Split, UnknownArray, UnknownMap, UnknownObject, UnknownSet } from '@13onthecode/types'
import { isArray } from '../guard/isArray'
import { isMap } from '../guard/isMap'
import { isSet } from '../guard/isSet'
export function toEntries<T extends string>(source: T): (
T extends `${infer S}`
? [GetKeys<Split<S>>, GetValues<Split<S>>][]
: T extends string
? [string, string][]
: never
)
export function toEntries<T extends UnknownArray | UnknownMap | UnknownSet>(source: T): (
Entries<T> extends (infer R)[]
? IterableIterator<R>
: never
)
export function toEntries<T extends UnknownObject>(source: T): Entries<T>
export function toEntries<T>(source: T): []
export function toEntries<T>(source: T) {
return (
isArray(source) || isMap(source) || isSet(source)
? source.entries()
: Object.entries(source as UnknownObject)
) as unknown
}