Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
fix: more accurate types
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Sep 7, 2021
1 parent 16da318 commit 81459fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/odometer/Odometer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Replacement } from '../multireplacer';
import { Intermediate } from '../utils';

import { getEditingDistance } from './utils/getEditingDistance';
import { OdometerStats } from './OdometerStats';

export class Odometer {
export class Odometer<Context = unknown> {
public getDifference(
aSet: Intermediate[],
bSet: Intermediate[],
): OdometerStats {
aSet: Intermediate<Context, Replacement<Context>>[],
bSet: Intermediate<Context, Replacement<Context>>[],
): OdometerStats<Context> {
let minimalDistance = Number.POSITIVE_INFINITY;
let aBest: Intermediate | null = null;
let bBest: Intermediate | null = null;
let aBest: Intermediate<Context, Replacement<Context>> | null = null;
let bBest: Intermediate<Context, Replacement<Context>> | null = null;

for (const aRaw of aSet) {
const a = aRaw.value.toLowerCase();
Expand Down
7 changes: 4 additions & 3 deletions src/odometer/OdometerStats.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Replacement } from '../multireplacer';
import { Intermediate } from '../utils';

export type OdometerStats = {
a: Intermediate | null;
b: Intermediate | null;
export type OdometerStats<T> = {
a: Intermediate<T, Replacement<T>> | null;
b: Intermediate<T, Replacement<T>> | null;
distance: number;
};
21 changes: 21 additions & 0 deletions src/odometer/__tests__/Odometer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Odometer } from '../Odometer';
import { Intermediate } from '../../utils';
import { Replacement } from '../../multireplacer';

describe('Odometer', () => {
test('integration', () => {
const odometer = new Odometer();

const [a1, a2, b1, b2] = ['morje', 'måre', 'mare', 'miare'].map(
(s) => new Intermediate<unknown, Replacement<unknown>>(s, null),
);

const result = odometer.getDifference([a1, a2], [b1, b2]);

expect(result).toEqual({
a: a2,
b: b1,
distance: 0.125,
});
});
});

0 comments on commit 81459fa

Please sign in to comment.