Skip to content

Commit

Permalink
Merge pull request #6 from peterhil/develop
Browse files Browse the repository at this point in the history
Reduce esm size and fix documentation
  • Loading branch information
peterhil authored Sep 3, 2024
2 parents 747a4e3 + e659943 commit 066f4af
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"typings": "dist/index.d.ts",
"scripts": {
"analyze": "size-limit --why",
"doc": "typedoc src",
"doc": "typedoc",
"size": "size-limit",
"prepare": "tsc -noEmit && ./build.js",
"lint": "eslint --config eslint.config.mjs src *.js *.mjs",
Expand Down Expand Up @@ -74,6 +74,6 @@
"string-algorithm"
],
"dependencies": {
"rambdax": "^7.4.1"
"rambdax": "^11.2.0"
}
}
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 066f4af

Please sign in to comment.