Skip to content

Latest commit

 

History

History
80 lines (55 loc) · 1.72 KB

extending-lyra.md

File metadata and controls

80 lines (55 loc) · 1.72 KB

Extending Lyra

Extending Lyra has just become easier than ever. Starting from v0.3.0, Lyra exposes some of its internals:

import {
  formatNanoseconds,
  getNanosecondsTime,
  intersectTokenScores,
  includes,
  boundedLevenshtein,
  tokenize
 } from '@lyrasearch/lyra/internals' 

Every exposed method comes with its own type definition.

Let's break them down:

formatNanoseconds

Takes a BigInt as input and returns a human-readable string.

import { formatNanoseconds } from '@lyrasearch/lyra/internals'

formatNanoseconds(30000n) // "30μs"

getNanosecondsTime

Gets the current time with nanoseconds-precision. Returns a BigInt.

import { getNanosecondsTime } from '@lyrasearch/lyra/internals'

getNanosecondsTime() // 1363500821581208n

intersectTokenScores

Returns the intersection of N arrays.

import { intersectTokenScores } from '@lyrasearch/lyra/internals'

intersectTokenScores([
  [
    ["foo", 1], ["bar", 1], ["baz", 2]
  ],
  [
    ["foo", 4], ["quick", 10], ["brown", 3], ["bar", 2]
  ],
  [
    ["fox", 12], ["foo", 4], ["jumps", 3], ["bar", 6]
  ]
])

// Result: [["foo", 9], ["bar", 9]]

includes

faster alternative to Array.prototype.includes.

import { includes } from '@lyrasearch/lyra/internals'

includes([10,20,30], 10) // true

boundedLevenshtein

Computes the Levenshtein distance between two strings (a, b), returning early with -1 if the distance is greater than the given tolerance. It assumes that tolerance >= ||a| - |b|| >= 0.

import { boundedLevenshtein } from '@lyrasearch/lyra/internals'

boundedLevenshtein("moon", "lions", 3) // { isBounded: true, distance: 3 }