# core/resolvers/layer.resolver.ts

> 27 lines of code and 6 definitions.

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

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

## Definitions

- `layerOf` (lexical_declaration, line 26, exported)
- `asLayerOptions` (lexical_declaration, line 7, exported)
- `isLayerOptions` (lexical_declaration, line 3)
- `matchSegment` (lexical_declaration, line 11)
- `tokensLayer` (lexical_declaration, line 20)
- `normalized` (lexical_declaration, line 30, exported)

## Contained in

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

## Used by

- [core/stylelint/custom-property-order.stylelint.rule.ts](https://banes-lab.com/source/quality/core/stylelint/custom-property-order.stylelint.rule.ts.md)

## Linked from

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

## Source

```typescript
import type { LayerOptions, LayerSegment } from "#types/layer.types";

const isLayerOptions = function isLayerOptions(value: unknown): value is LayerOptions {
    return typeof value === "object" && value !== null;
};

export const asLayerOptions = function asLayerOptions(value?: unknown): LayerOptions {
    return isLayerOptions(value) ? value : {};
};

const matchSegment = function matchSegment(normalized: string, segments: LayerSegment[]): string | null {
    for (const { layer, needle } of segments) {
        if (normalized.includes(needle)) {
            return layer;
        }
    }
    return null;
};

const tokensLayer = function tokensLayer(normalized: string, tokensFile: string | undefined): string {
    return typeof tokensFile === "string" && tokensFile.length > 0 && normalized.endsWith(tokensFile)
        ? "tokens"
        : "unknown";
};

export const layerOf = function layerOf(filepath: string | undefined, opts?: LayerOptions): string {
    if (typeof filepath !== "string" || filepath.length === 0) {
        return "unknown";
    }
    const normalized = filepath.split("\\").join("/");
    return matchSegment(normalized, opts?.segments ?? []) ?? tokensLayer(normalized, opts?.tokensFile);
};
```
