Skip to content

Commit 91cdbf6

Browse files
committed
feat!: migrate to type:module and do not expose default
1 parent 0467f72 commit 91cdbf6

File tree

15 files changed

+138
-136
lines changed

15 files changed

+138
-136
lines changed

.eslintrc.yml

Lines changed: 0 additions & 1 deletion
This file was deleted.

.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"arrowParens": "always",
3+
"semi": true,
4+
"singleQuote": true,
5+
"tabWidth": 2,
6+
"trailingComma": "all"
7+
}

eslint.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { defineConfig, globalIgnores } from 'eslint/config';
2+
import cheminfo from 'eslint-config-cheminfo-typescript';
3+
4+
export default defineConfig(globalIgnores(['coverage', 'lib']), cheminfo);

package.json

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,25 @@
22
"name": "ml-nearest-vector",
33
"version": "2.0.1",
44
"description": "Find the nearest point to a sample point",
5-
"main": "./lib/index.js",
6-
"module": "./lib-es6/index.js",
7-
"types": "./lib/index.d.ts",
5+
"type": "module",
6+
"exports": "./lib/index.js",
87
"files": [
9-
"lib",
10-
"lib-es6"
8+
"src",
9+
"lib"
1110
],
1211
"scripts": {
13-
"clean": "rimraf lib lib-es6",
14-
"prepublishOnly": "npm run tsc",
15-
"test": "npm run test-only && npm run tslint",
16-
"test-only": "jest",
17-
"tsc": "npm run clean && npm run tsc-es5 && npm run tsc-es6",
18-
"tsc-es5": "tsc",
19-
"tsc-es6": "tsc --project tsconfig.es6.json",
20-
"tslint": "tslint --project tsconfig.base.json",
21-
"tslint-fix": "npm run tslint -- --fix"
22-
},
23-
"jest": {
24-
"testEnvironment": "node",
25-
"transform": {
26-
"^.+\\.tsx?$": "ts-jest"
27-
},
28-
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
29-
"moduleFileExtensions": [
30-
"ts",
31-
"tsx",
32-
"js",
33-
"jsx",
34-
"json",
35-
"node"
36-
],
37-
"collectCoverage": true
12+
"check-types": "tsc --noEmit",
13+
"clean": "rimraf lib",
14+
"eslint": "eslint src",
15+
"eslint-fix": "npm run eslint -- --fix",
16+
"prepack": "npm run tsc",
17+
"prettier": "prettier --check src",
18+
"prettier-write": "prettier --write src",
19+
"test": "npm run test-only && npm run eslint && npm run prettier && npm run check-types && npm run verify-build",
20+
"test-only": "vitest run --coverage",
21+
"tsc": "npm run clean && npm run tsc-build",
22+
"tsc-build": "tsc --project tsconfig.build.json",
23+
"verify-build": "npm run tsc && node lib/index.js"
3824
},
3925
"repository": {
4026
"type": "git",
@@ -57,13 +43,14 @@
5743
},
5844
"homepage": "https://github.com/mljs/nearest-vector#readme",
5945
"devDependencies": {
60-
"@types/jest": "^23.3.1",
61-
"jest": "^23.5.0",
62-
"rimraf": "^2.6.2",
63-
"ts-jest": "^23.1.3",
64-
"tslint": "^5.11.0",
65-
"tslint-config-prettier": "^1.14.0",
66-
"typescript": "^3.0.1"
46+
"@types/node": "^24.10.1",
47+
"@vitest/coverage-v8": "^4.0.8",
48+
"@zakodium/tsconfig": "^1.0.2",
49+
"eslint-config-cheminfo-typescript": "^21.0.1",
50+
"prettier": "^3.6.2",
51+
"rimraf": "^6.1.0",
52+
"typescript": "^5.9.3",
53+
"vitest": "^4.0.8"
6754
},
6855
"dependencies": {
6956
"ml-distance-euclidean": "^2.0.0"

src/__tests__/index.test.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { describe, expect, it, test } from 'vitest';
2+
3+
import { findNearestVector, nearestVector } from '../index.js';
4+
5+
function cosineSimilarity(a: number[], b: number[]) {
6+
const ii = a.length;
7+
8+
let p = 0;
9+
10+
let p2 = 0;
11+
12+
let q2 = 0;
13+
for (let i = 0; i < ii; i++) {
14+
p += a[i] * b[i];
15+
p2 += a[i] * a[i];
16+
q2 += b[i] * b[i];
17+
}
18+
return p / (Math.sqrt(p2) * Math.sqrt(q2));
19+
}
20+
21+
test('Distance tests', () => {
22+
const centers = [
23+
[1, 2, 1],
24+
[-1, -1, -1],
25+
];
26+
const data = [
27+
[1, 1, 1],
28+
[1, 2, 1],
29+
[-1, -1, -1],
30+
[-1, -1, -1.5],
31+
];
32+
const clusterID = [0, 0, 1, 1];
33+
34+
for (let i = 0; i < clusterID.length; i++) {
35+
expect(nearestVector(centers, data[i])).toBe(clusterID[i]);
36+
}
37+
});
38+
39+
describe('Similarity tests', () => {
40+
const centers = [
41+
[1, 2, 1],
42+
[-1, -1, -1],
43+
];
44+
const data = [
45+
[1, 1, 1],
46+
[1, 2, 1],
47+
[-1, -1, -1],
48+
[-1, -1, -1.5],
49+
];
50+
const clusterID = [0, 0, 1, 1];
51+
52+
// cosine similarity
53+
const options = { similarityFunction: cosineSimilarity };
54+
55+
it('Find nearest vector with cosine similarity', () => {
56+
for (let i = 0; i < clusterID.length; i++) {
57+
expect(nearestVector(centers, data[i], options)).toBe(clusterID[i]);
58+
}
59+
});
60+
61+
it('Throws for non-function', () => {
62+
//@ts-expect-error testing invalid input
63+
expect(nearestVector.bind(null, [], [], { distanceFunction: 1 })).toThrow(
64+
"A similarity or distance function it's required",
65+
);
66+
});
67+
68+
it('find nearest vector', () => {
69+
const centers = [
70+
[1, 2, 1],
71+
[-1, -1, -1],
72+
];
73+
const data = [
74+
[1, 1, 1],
75+
[1, 2, 1],
76+
[-1, -1, -1],
77+
[-1, -1, -1.5],
78+
];
79+
const clusterID = [0, 0, 1, 1];
80+
81+
for (let i = 0; i < clusterID.length; i++) {
82+
expect(findNearestVector(centers, data[i])).toStrictEqual(
83+
centers[clusterID[i]],
84+
);
85+
}
86+
});
87+
});

src/__tests__/test.ts

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/index.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
2-
31
import { squaredEuclidean } from 'ml-distance-euclidean';
42

53
const defaultOptions: IOptions = {
6-
distanceFunction: squaredEuclidean
4+
distanceFunction: squaredEuclidean,
75
};
86

97
type Vector = number[];
@@ -14,10 +12,10 @@ interface IOptions {
1412
similarityFunction?: (vector1: Vector, vector2: Vector) => number;
1513
}
1614

17-
export default function nearestVector(
15+
export function nearestVector(
1816
listVectors: Matrix,
1917
vector: number[],
20-
options: IOptions = defaultOptions
18+
options: IOptions = defaultOptions,
2119
) {
2220
const distanceFunction =
2321
options.distanceFunction || defaultOptions.distanceFunction;
@@ -55,7 +53,7 @@ export default function nearestVector(
5553
export function findNearestVector(
5654
vectorList: Matrix,
5755
vector: Vector,
58-
options: IOptions = defaultOptions
56+
options: IOptions = defaultOptions,
5957
) {
6058
const index = nearestVector(vectorList, vector, options);
6159
return vectorList[index];

0 commit comments

Comments
 (0)