# core/resolvers/representation.resolver.ts

> 32 lines of code and 8 definitions.

Tree: GovLab Patterns
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/patterns#file-patterns-core-resolvers-representation-resolver-ts
Source text: https://banes-lab.com/source/patterns/core/resolvers/representation.resolver.ts.txt

Listed in [core/resolvers](https://banes-lab.com/api/source/patterns/core/resolvers.md), after [core/resolvers/package.resolver.ts](https://banes-lab.com/source/patterns/core/resolvers/package.resolver.ts.md) and before [core/resolvers/walk.resolver.ts](https://banes-lab.com/source/patterns/core/resolvers/walk.resolver.ts.md).

## Definitions

- `inferMapping` (lexical_declaration, line 28, exported)
- `inferField` (lexical_declaration, line 21)
- `inferKey` (lexical_declaration, line 17)
- `unrepresentable` (lexical_declaration, line 36, exported)
- `INFERENCE` (lexical_declaration, line 4)
- `COORDINATE_REPRESENTATIONS` (lexical_declaration, line 13)
- `NUMERIC_LIST_REPRESENTATIONS` (lexical_declaration, line 15)
- `mapped` (lexical_declaration, line 37, exported)

## Contained in

- [core/resolvers](https://banes-lab.com/anatomy/patterns/folder-patterns-core-resolvers.md)

## Uses

- [core/predicates/schema.predicate.ts](https://banes-lab.com/source/patterns/core/predicates/schema.predicate.ts.md)

## Used by

- [core/pipelines/snapshot.pipeline.ts](https://banes-lab.com/source/patterns/core/pipelines/snapshot.pipeline.ts.md)
- [runtime/entrypoints/pattern.entrypoint.ts](https://banes-lab.com/source/patterns/runtime/entrypoints/pattern.entrypoint.ts.md)

## Linked from

- [core/pipelines](https://banes-lab.com/anatomy/patterns/folder-patterns-core-pipelines.md)
- [core/predicates](https://banes-lab.com/anatomy/patterns/folder-patterns-core-predicates.md)
- [core/resolvers](https://banes-lab.com/anatomy/patterns/folder-patterns-core-resolvers.md)
- [runtime/entrypoints](https://banes-lab.com/anatomy/patterns/folder-patterns-runtime-entrypoints.md)

## Source

```typescript
import type { FieldSchema, Kind, Primitive } from "#types/schema.types";
import { isCoordinatePair } from "#core/predicates/schema.predicate";

const INFERENCE = new Map<string, readonly string[]>([
    ["scalar:integer", ["vector", "sequence"]],
    ["scalar:float", ["vector", "sequence"]],
    ["scalar:boolean", ["distribution", "sequence"]],
    ["scalar:string", ["distribution", "sequence"]],
    ["list:", ["graph"]],
    ["object:", ["tree"]],
]);

const COORDINATE_REPRESENTATIONS: readonly string[] = ["grid"];

const NUMERIC_LIST_REPRESENTATIONS: readonly string[] = ["graph", "vector"];

const inferKey = function inferKey(kind: Kind, primitive: Primitive | null): string {
    return `${kind}:${primitive ?? ""}`;
};

const inferField = function inferField(field: FieldSchema): readonly string[] {
    if (field.kind === "list" && field.elementNumeric) {
        return isCoordinatePair(field) ? COORDINATE_REPRESENTATIONS : NUMERIC_LIST_REPRESENTATIONS;
    }
    return INFERENCE.get(inferKey(field.kind, field.primitive)) ?? [];
};

export const inferMapping = function inferMapping(schema: readonly FieldSchema[]): Map<string, readonly string[]> {
    return new Map(
        schema
            .map((field): [string, readonly string[]] => [field.name, inferField(field)])
            .filter(([, representations]) => representations.length > 0),
    );
};

export const unrepresentable = function unrepresentable(schema: readonly FieldSchema[]): FieldSchema[] {
    const mapped = inferMapping(schema);
    return schema.filter((field) => !mapped.has(field.name));
};
```
