From 0a564a1a13c7ed2d7585dee67de7ff30df7f8274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Hillerstr=C3=B6m?= Date: Tue, 3 Sep 2024 19:27:00 +0300 Subject: [PATCH] Fix typescript problems with length method --- src/commonTypes.ts | 3 ++- src/search.ts | 11 ++++------- src/utils/helpers.test.ts | 8 ++++++++ src/utils/helpers.ts | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/commonTypes.ts b/src/commonTypes.ts index 6524b5f..4a9f929 100644 --- a/src/commonTypes.ts +++ b/src/commonTypes.ts @@ -1,5 +1,6 @@ export interface Dictionary {[index: string]: T} +export type Length = number export type Ngram = string export type Position = number export type Term = string @@ -9,7 +10,7 @@ export type NormaliseFunction = (term: Term) => Ngram export type Indexable = string | number | symbol export type Description = Record -export type Positions = Dictionary +export type Lengths = Dictionary export type Locations = Dictionary export type StringDescription = Record export type Terms = Term[] | Record diff --git a/src/search.ts b/src/search.ts index 27ca7ce..f5ab561 100644 --- a/src/search.ts +++ b/src/search.ts @@ -1,5 +1,4 @@ import { - add, filter, filterObject, flatten, @@ -13,19 +12,18 @@ import { mergeAll, not, pick, - pipe, reduce, splitAt, values, } from 'rambdax' -import { empty, ids, match, nonEmpty } from './utils/helpers' +import { empty, ids, lengthFromPositions, match, nonEmpty } from './utils/helpers' import { ngram } from './ngram' import type { Description, Indexable, - Positions, + Lengths, Locations, Ngram, NgramIndex, @@ -202,10 +200,9 @@ export class Index { /** * Lengths of all the terms in the index */ - lengths (): Positions { + lengths (): Lengths { const descriptions: Description[] = this._ends() - const lengthFromPositions = pipe(last, add(1)) // get length from last position - const lengths: Positions = map( + const lengths: Lengths = map( lengthFromPositions, mergeAll(descriptions), ) diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index 49c6c0f..867a1f0 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -2,6 +2,7 @@ import tap from 'tap' import { ids, + lengthFromPositions, match, nonEmpty, } from './helpers' @@ -12,6 +13,13 @@ tap.test('utils ids', assert => { assert.end() }) +tap.test('utils lengthFromPositions', assert => { + assert.same(lengthFromPositions([]), 0) + assert.same(lengthFromPositions([0]), 1) + assert.same(lengthFromPositions([0, 1]), 2) + assert.end() +}) + tap.test('utils nonEmpty', assert => { assert.ok(nonEmpty({a: []})) assert.notOk(nonEmpty({})) diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index d86f628..7662a90 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -12,6 +12,7 @@ import { import type { Description, Indexable, + Length, Position, StringDescription, } from '../commonTypes' @@ -22,6 +23,19 @@ export const nil: Position[] = [] export const ids = (obj: object): Indexable[] => keys(obj) +/** + * Get length from last position + */ +export function lengthFromPositions ( + positions: Position[] +): Length { + if (isEmpty(positions)) return 0 + + const length: Length = positions[positions.length - 1] + 1 + + return length +} + /** * Rambda’s {@link https://selfrefactor.github.io/rambdax/#/?id=isempty|isEmpty} complemented (negated). * @returns true for non-empty things.