MCPcopy Create free account
hub / github.com/arktypeio/arktype / normalizeIndex

Function normalizeIndex

ark/schema/structure/structure.ts:1921–1971  ·  view source on GitHub ↗
(
	signature: BaseRoot,
	value: BaseRoot,
	$: BaseScope
)

Source from the content-addressed store, hash-verified

1919
1920/** extract enumerable named props from an index signature */
1921export const normalizeIndex = (
1922 signature: BaseRoot,
1923 value: BaseRoot,
1924 $: BaseScope
1925): NormalizedIndex => {
1926 const [enumerableBranches, nonEnumerableBranches] = spliterate(
1927 signature.branches,
1928 k => k.is("unit")
1929 )
1930
1931 for (const n of enumerableBranches) {
1932 if (typeof n.unit !== "string" && typeof n.unit !== "symbol")
1933 throwParseError(writeInvalidPropertyKeyMessage(signature.expression))
1934 }
1935
1936 if (
1937 nonEnumerableBranches.length &&
1938 !signature.equals($ark.kw.string) &&
1939 !signature.equals($ark.kw.symbol) &&
1940 !signature.equals($ark.kw.key) &&
1941 !signature.extends($ark.kw.key)
1942 )
1943 throwParseError(writeInvalidPropertyKeyMessage(signature.expression))
1944
1945 if (!enumerableBranches.length) {
1946 return {
1947 index: $.node("index", { signature: signature as Index.KeyNode, value })
1948 }
1949 }
1950
1951 const normalized: NormalizedIndex = {}
1952
1953 for (const n of enumerableBranches) {
1954 // since required can be reduced to optional if it has a default or
1955 // optional meta on its value, we have to assign it depending on the
1956 // compiled kind
1957 const prop = $.node("required", { key: n.unit as Key, value })
1958 normalized[prop.kind] = append(normalized[prop.kind], prop as never)
1959 }
1960
1961 if (nonEnumerableBranches.length) {
1962 normalized.index = $.node("index", {
1963 signature: $.node("union", {
1964 branches: nonEnumerableBranches
1965 }) as Index.KeyNode,
1966 value
1967 })
1968 }
1969
1970 return normalized
1971}
1972
1973export const typeKeyToString = (k: KeyOrKeyNode): string =>
1974 hasArkKind(k, "root") ? k.expression : printable(k)

Callers 3

ts.tsFile · 0.90
parseObjectLiteralFunction · 0.90
narrowToKeysOfFunction · 0.90

Calls 8

spliterateFunction · 0.90
throwParseErrorFunction · 0.90
appendFunction · 0.90
isMethod · 0.80
extendsMethod · 0.80
nodeMethod · 0.80
equalsMethod · 0.65

Tested by

no test coverage detected