# tools/core/predicates/secret.predicate.ts

> 40 lines of code and 13 definitions.

Tree: Coordination tree
Language: typescript
Layer: infrastructure
Canonical: https://banes-lab.com/anatomy/coordination#file-coordination-tools-core-predicates-secret-predicate-ts
Source text: https://banes-lab.com/assets/sources/source.b2c8fe6e01eb436ca8a7250f77845dc596ae679874faf6e992e35c8807e0422b.generated.txt

## Definitions

- `bearsSecret` (lexical_declaration, line 44, exported)
- `tokensOf` (lexical_declaration, line 26, exported)
- `matchesShape` (lexical_declaration, line 36)
- `isPlaceholder` (lexical_declaration, line 30, exported)
- `isTokenChar` (lexical_declaration, line 22)
- `SecretShape` (interface_declaration, line 3, exported)
- `SHAPE_SEPARATOR` (lexical_declaration, line 8)
- `shapeOf` (lexical_declaration, line 10)
- `cut` (lexical_declaration, line 11)
- `minimumLength` (lexical_declaration, line 12)
- `parseShapes` (lexical_declaration, line 18, exported)
- `core` (lexical_declaration, line 31, exported)
- `first` (lexical_declaration, line 32, exported)

## Uses

- [tools/core/predicates/token.predicate.ts](https://banes-lab.com/source/coordination/tools/core/predicates/token.predicate.ts.md)

## Used by

- [tools/rules/secret.rule.ts](https://banes-lab.com/source/coordination/tools/rules/secret.rule.ts.md)

## Source

```typescript
import { accumulate, isWordCharacter } from "./token.predicate.ts";

export interface SecretShape {
    readonly prefix: string;
    readonly minimumLength: number;
}

const SHAPE_SEPARATOR = ":";

const shapeOf = function shapeOf(entry: string): SecretShape[] {
    const cut = entry.lastIndexOf(SHAPE_SEPARATOR);
    const minimumLength = Number(entry.slice(cut + 1));
    return cut > 0 && Number.isFinite(minimumLength) && minimumLength > 0
        ? [{ minimumLength, prefix: entry.slice(0, cut) }]
        : [];
};

export const parseShapes = function parseShapes(declared: readonly string[]): SecretShape[] {
    return declared.flatMap(shapeOf);
};

const isTokenChar = function isTokenChar(char: string): boolean {
    return isWordCharacter(char) || char === "-";
};

export const tokensOf = function tokensOf(source: string): string[] {
    return accumulate(source, isTokenChar);
};

export const isPlaceholder = function isPlaceholder(body: string): boolean {
    const core = body.replaceAll("_", "").replaceAll("-", "");
    const first = core.codePointAt(0);
    return first !== undefined && core.replaceAll(String.fromCodePoint(first), "").length === 0;
};

const matchesShape = function matchesShape(token: string, shape: SecretShape): boolean {
    return (
        token.startsWith(shape.prefix) &&
        token.length >= shape.minimumLength &&
        !isPlaceholder(token.slice(shape.prefix.length))
    );
};

export const bearsSecret = function bearsSecret(source: string, shapes: readonly SecretShape[]): boolean {
    if (!shapes.some((shape) => source.includes(shape.prefix))) {
        return false;
    }
    return tokensOf(source).some((token) => shapes.some((shape) => matchesShape(token, shape)));
};
```
