API surface

August 2, 2026 ยท View on GitHub

Glyph Ribbon

API surface

Every public export from @koda.oss/glyph, grouped by layer.

Deep docs live under Core, Index, Query, Collections, Completions, and Spotlight.

Core

Functions

ExportSignatureDoc
Create(text, options?) => GlyphRecordCreate
Compare(a, b, options?) => GlyphComparisonResultCompare
CompareGlyphs(a, b, options?) => GlyphComparisonResultCompare
CreateGroup(glyphs: Glyph[] | string[]) => GlyphGroupGroups
CompareGroups(group1, group2, options?) => GlyphComparisonResultGroups
GroupResultAggregatorMaxGroupResultAggregatorGroups
GroupResultAggregatorSumGroupResultAggregatorGroups
Serialize(Glyph | GlyphSignature | GlyphRecord) => stringSerialize
Deserialize(string) => GlyphSerialize
CreateTokens(text, normalize?) => GlyphToken[]Tokenize
CreateUnigrams(text, normalize?) => GlyphUnigram[]Tokenize
CreateVGrams(text, vgramSize, normalize?) => GlyphVGram[]Tokenize
Tokenize(text, options?) => GlyphTokenizationResultTokenize
TextFilter(text) => stringText normalization
TextStrip(text) => stringText normalization
index.New(options?) => GlyphIndexInstanceIndex

Core types

type Glyph = Uint32Array & { readonly __glyph: true };
type GlyphGroup = Record<string, Glyph>;
type GlyphGroupInput = GlyphGroup | Glyph[];
type GlyphToken = string;
type GlyphUnigram = string;
type GlyphVGram = string;

interface GlyphSignature {
  version: number;
  glyph: Glyph;
}

interface GlyphRecord extends GlyphSignature {
  createdAt: number;
}

interface GlyphCreateOptions {
  size?: number;        // default 128
  vgramSize?: number;   // default 4
  normalize?: boolean;  // default true
}

interface GlyphTokenizationOptions {
  vgramSize?: number;   // default 2 in Tokenize()
  normalize?: boolean;  // default true
}

interface GlyphTokenizationResult {
  tokens: GlyphToken[];
  unigrams: GlyphUnigram[];
  vgrams: GlyphVGram[];
}

interface GlyphComparisonResult {
  similarity: number;
  distance: number;
  matches: number;
  size: number;
  matchedLeft?: string | number;
  matchedRight?: string | number;
}


interface GlyphComparisonOptions {
  aggregate?: GroupResultAggregator;
}

type GroupResultAggregatorContext = {
  scores: number[];
  left: GlyphGroup;
  right: GlyphGroup;
};

type GroupResultAggregator = (context: GroupResultAggregatorContext) => number;

type GlyphIndexMode = "bands" | "direct";

interface GlyphIndexOptions {
  mode?: GlyphIndexMode;   // default "bands"
  bands?: number;          // default 64 when size is 128
  rows?: number;           // default 2 when size is 128
  glyphSize?: number;
}

interface GlyphIndexInstance {
  readonly mode: GlyphIndexMode;
  Get(key: string): Glyph | GlyphGroup | undefined;
  Set(key: string, glyphs?: Glyph | GlyphGroupInput): void;
  Add(key: string, glyphs: Glyph | GlyphGroupInput): void;
  Remove(key: string): void;
  Has(key: string): boolean;
  Clear(): void;
  Size(): number;
  Keys(): IterableIterator<string>;
  Values(): IterableIterator<Glyph | GlyphGroup>;
  Entries(): IterableIterator<[string, Glyph | GlyphGroup]>;
  CandidateKeys(
    probe: Glyph | GlyphSignature | GlyphGroupInput,
  ): IterableIterator<string>;
}

See Glyph and Index for type notes.

Query

Functions

ExportSignatureDoc
query.New(index) => GlyphQueryInstanceQuery

Query types

interface GlyphQueryInstance {
  Search(
    probe: Glyph | GlyphSignature | GlyphGroupInput,
    options?: GlyphQueryOptions,
  ): GlyphQueryResult[];
}

interface GlyphQueryOptions {
  limit?: number;
  threshold?: number;      // default 0
  normalize?: boolean;     // default false
  aggregate?: GroupResultAggregator;
  compare?: GlyphComparisonOptions;
}

interface GlyphQueryResult {
  key: string;
  similarity: number;
  comparison: GlyphComparisonResult;
  matched?: string | number;
}

Result fields: Query results.

Collections

Functions

ExportSignatureDoc
collections.New(options?) => GlyphCollectionInstanceCollections
CollectionAggregatorMinCollectionAggregatorAggregators
CollectionAggregatorMaxCollectionAggregatorAggregators
CollectionAggregatorMeanCollectionAggregatorAggregators
CollectionAggregatorMidCollectionAggregatorAggregators
CollectionAggregatorSumCollectionAggregatorAggregators
CollectionAggregatorSoftmaxCollectionAggregatorAggregators

Collections types

type CollectionAggregatorContext = {
  collection: GlyphGroup;
  index: number;
};

type CollectionAggregator = (
  values: number[],
  context?: CollectionAggregatorContext,
) => number;

interface GlyphCollectionOptions {
  create?: GlyphCreateOptions;
  aggregator?: CollectionAggregator; // default CollectionAggregatorSoftmax
}

interface GlyphCollectionInstance {
  readonly glyph: Glyph;
  Add(key: string, example: string | Glyph): void;
  AddGroup(group: GlyphGroupInput): void;
  Remove(key: string): void;
  Clear(): void;
  Collection(): GlyphGroup;
  Has(key: string): boolean;
  Count(): number;
}

See Collections and Aggregators.

Completions

Functions

ExportSignatureDoc
completions.New(options?) => CompletionChainInstanceChain

Completions types

interface GlyphCompletionChainOptions {
  order?: number;               // default 3
  create?: GlyphCreateOptions;
}

interface GlyphCompletionOptions {
  limit?: number;    // default 5
  minCount?: number; // default 1
}

interface GlyphCompletionResult {
  token: string;
  score: number;
  count: number;
  comparison: GlyphComparisonResult;
  source: {
    key: string;
    glyph: Glyph;
  };
}

interface CompletionChainInstance {
  Ingest(key: string, text: string): void;
  Complete(prefix: string, options?: GlyphCompletionOptions): GlyphCompletionResult[];
  Clear(): void;
  Size(): number;
}

Result fields: Completion results.

Glyph Spotlight

Spotlight

Functions

ExportSignatureDoc
spotlight.New(content, options?) => GlyphSpotlightDocumentInstanceDocument

Spotlight types

type GlyphSpotlightChunk = string;
type GlyphSpotlightChunker = (text: string) => GlyphSpotlightChunk[];

type GlyphSpotlightCompiledChunk = {
  text: string;
  glyph: Glyph;
  length: number;
};

interface GlyphSpotlightOptions {
  normalize?: boolean;
  create?: GlyphCreateOptions;
  aggregate?: GroupResultAggregator;  // default GroupResultAggregatorSum for group probes
  chunker?: GlyphSpotlightChunker;
  textOutput?: boolean;        // default false
}

interface GlyphSpotlightQueryOptions extends GlyphSpotlightOptions {
  limit?: number;
  threshold?: number;          // default 0
}

interface GlyphSpotlightRankOptions extends GlyphSpotlightOptions {}

interface GlyphSpotlightResult extends GlyphSpotlightCompiledChunk {
  score: number;
  comparison: GlyphComparisonResult;
  matched?: string | number;
}

interface GlyphSpotlightDocumentInstance {
  Rank(probe, options?): GlyphSpotlightResult[] | string[];
  Query(probe, options?): GlyphSpotlightResult[] | string[];
  Chunks(): readonly GlyphSpotlightCompiledChunk[];
  Size(): number;
}

See Document, Rank, Query.

Errors

Named error classes exported from @koda.oss/glyph:

ExportWhen thrown
GlyphSizeMismatchErrorCompare or collection operations receive glyphs of different lengths
EmptyGroupErrorGroup compare runs on an empty group (default message: "Cannot compare empty glyph groups")
InvalidSerializedGlyphErrorDeserialize() receives malformed or unsupported serialized glyph data