Skip to content

Commit

Permalink
Fix typescript problems with length method
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhil committed Sep 3, 2024
1 parent 5cc8d78 commit 0a564a1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/commonTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Dictionary<T> {[index: string]: T}

export type Length = number
export type Ngram = string
export type Position = number
export type Term = string
Expand All @@ -9,7 +10,7 @@ export type NormaliseFunction = (term: Term) => Ngram
export type Indexable = string | number | symbol

export type Description = Record<Indexable, Position[]>
export type Positions = Dictionary<Position>
export type Lengths = Dictionary<Length>
export type Locations = Dictionary<Position[]>
export type StringDescription = Record<string, Position[]>
export type Terms = Term[] | Record<Indexable, Term>
Expand Down
11 changes: 4 additions & 7 deletions src/search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
add,
filter,
filterObject,
flatten,
Expand All @@ -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,
Expand Down Expand Up @@ -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),
)
Expand Down
8 changes: 8 additions & 0 deletions src/utils/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import tap from 'tap'

import {
ids,
lengthFromPositions,
match,
nonEmpty,
} from './helpers'
Expand All @@ -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({}))
Expand Down
14 changes: 14 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import type {
Description,
Indexable,
Length,
Position,
StringDescription,
} from '../commonTypes'
Expand All @@ -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.
Expand Down

0 comments on commit 0a564a1

Please sign in to comment.