From 5cc8d78b12256fa49a38927199cd6b11c35557bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Hillerstr=C3=B6m?= Date: Tue, 3 Sep 2024 17:40:46 +0300 Subject: [PATCH 1/2] deps: Update rambdax to latest version 11.2.0 --- package.json | 2 +- pnpm-lock.yaml | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 45054a9..74f8499 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,6 @@ "string-algorithm" ], "dependencies": { - "rambdax": "^7.4.1" + "rambdax": "^11.2.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index dc2ba15..a017f9a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: rambdax: - specifier: ^7.4.1 - version: 7.4.1 + specifier: ^11.2.0 + version: 11.2.0 devDependencies: '@eslint/js': specifier: ^9.9.1 @@ -1621,8 +1621,8 @@ packages: queue-microtask@1.2.3: resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==} - rambdax@7.4.1: - resolution: {integrity: sha512-BMeJ2Bzbc0Du0Pml3JQ3weJwh2s30rBhtXMJEXezQ2gLAEN6fusBUHqF9jGQxlViznTKFZkPr44lCwOUcDSe3A==} + rambdax@11.2.0: + resolution: {integrity: sha512-AzAHeTQUZMbbQbl5Crv8kgUXVzzjjnncPKw84b4lvflh1WTCFSOnEnW6UOmPRHVtZXG1IkZL5P03OqZUKpwJDQ==} react-dom@18.3.1: resolution: {integrity: sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw==} @@ -3689,7 +3689,7 @@ snapshots: queue-microtask@1.2.3: {} - rambdax@7.4.1: {} + rambdax@11.2.0: {} react-dom@18.3.1(react@18.3.1): dependencies: 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 2/2] 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.