Skip to content

Commit

Permalink
chore: modernize project
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Node.js 4 and 6 are no longer supported.
  • Loading branch information
targos committed Jul 13, 2019
1 parent f3d803f commit cac9c7e
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 1,287 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
4 changes: 3 additions & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
extends: 'eslint-config-cheminfo'
extends: cheminfo
parserOptions:
sourceType: module
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ logs
*.log
npm-debug.log*
coverage
distance-matrix.js
distance-matrix.d.ts
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
node_js:
- node
- lts/boron # v6
- lts/argon # v4
script: "npm run test-travis "
after_script: "bash <(curl -s https://codecov.io/bash)"
- 8
- 10
- 12
after_success:
- bash <(curl -s https://codecov.io/bash)
8 changes: 0 additions & 8 deletions .yo-rc.json

This file was deleted.

22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# distance-matrix

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]

Compute a distance/similarity matrix
Compute a distance/similarity matrix.

## Installation

`$ npm install ml-distance-matrix`
`npm i ml-distance-matrix`

## [API Documentation](https://mljs.github.io/distance-matrix/)

Expand All @@ -21,14 +20,9 @@ const distanceMatrix = require('ml-distance-matrix');
const euclidean = require('ml-distance-euclidean');

// Computes euclidan distance for each pair of possible vectors
const matrix = distanceMatrix([
[8, -1, 2],
[-4, 0, 3],
[-3, -1, 6]
], euclidean);
const matrix = distanceMatrix([[8, -1, 2], [-4, 0, 3], [-3, -1, 6]], euclidean);
```


## License

[MIT](./LICENSE)
Expand All @@ -39,7 +33,5 @@ const matrix = distanceMatrix([
[travis-url]: https://travis-ci.org/mljs/distance-matrix
[codecov-image]: https://img.shields.io/codecov/c/github/mljs/distance-matrix.svg?style=flat-square
[codecov-url]: https://codecov.io/gh/mljs/distance-matrix
[david-image]: https://img.shields.io/david/mljs/distance-matrix.svg?style=flat-square
[david-url]: https://david-dm.org/mljs/distance-matrix
[download-image]: https://img.shields.io/npm/dm/ml-distance-matrix.svg?style=flat-square
[download-url]: https://npmjs.org/package/ml-distance-matrix
38 changes: 23 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
"name": "ml-distance-matrix",
"version": "1.0.0",
"description": "Compute a distance/similarity matrix",
"main": "./src/index.js",
"main": "distance-matrix.js",
"module": "src/index.js",
"types": "distance-matrix.d.ts",
"sideEffects": false,
"files": [
"distance-matrix.js",
"distance-matrix.d.ts",
"src"
],
"scripts": {
"eslint": "eslint src test",
"eslint-fix": "npm run eslint -- --fix",
"test": "npm run test-mocha && npm run eslint",
"test-cov": "istanbul cover node_modules/.bin/_mocha -- --require should --reporter dot --recursive",
"test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- --require should --reporter dot --recursive",
"test-mocha": "mocha --require should --reporter mocha-better-spec-reporter"
"compile": "rollup -c",
"eslint": "eslint src",
"eslint-fix": "eslint src --fix",
"prepublishOnly": "npm run compile",
"test": "npm run test-coverage && npm run eslint",
"test-only": "jest",
"test-coverage": "jest --coverage"
},
"repository": {
"type": "git",
Expand All @@ -23,7 +29,9 @@
"learning",
"data",
"mining",
"datamining"
"datamining",
"distance",
"matrix"
],
"author": "Daniel Kostro <kostro.d@gmail.com> (https://github.com/Daniel Kostro)",
"license": "MIT",
Expand All @@ -32,12 +40,12 @@
},
"homepage": "https://github.com/mljs/distance-matrix#readme",
"devDependencies": {
"eslint": "^3.12.2",
"eslint-config-cheminfo": "^1.6.0",
"eslint-plugin-no-only-tests": "^1.1.0",
"istanbul": "^0.4.5",
"mocha": "^3.2.0",
"mocha-better-spec-reporter": "^3.1.0",
"should": "^11.1.2"
"@babel/plugin-transform-modules-commonjs": "^7.5.0",
"eslint": "^6.0.1",
"eslint-config-cheminfo": "^1.20.1",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-jest": "^22.7.2",
"jest": "^24.8.0",
"rollup": "^1.16.7"
}
}
8 changes: 8 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
input: 'src/index.js',
output: {
file: 'distance-matrix.js',
format: 'cjs',
},
external: [],
};
2 changes: 2 additions & 0 deletions src/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__tests__
.npmignore
19 changes: 19 additions & 0 deletions src/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import distanceMatrix from '..';

describe('distance-matrix test', () => {
it('identity distance (0 if the same, 1 if not', () => {
// [1, 0, 1, 1]
// =>
// [0, 1, 0, 0]
// [1, 0, 1, 1]
// [0, 1, 0, 0]
// [0, 1, 0, 0]
const result = distanceMatrix([1, 0, 1, 1], (a, b) => Number(a !== b));
expect(result).toStrictEqual([
[0, 1, 0, 0],
[1, 0, 1, 1],
[0, 1, 0, 0],
[0, 1, 0, 0],
]);
});
});
40 changes: 20 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
'use strict';


/**
* Computes a distance/similarity matrix given an array of data and a distance/similarity function.
* @param {Array} data An array of data
* @param {function} distanceFn A function that accepts two arguments and computes a distance/similarity between them
* @return {Array<Array>} The distance/similarity matrix. The matrix is square and has a size equal to the length of
* the data array
*/
function distanceMatrix(data, distanceFn) {
const length = data.length;
let result = Array.from({length}).map(() => Array.from({length}));

// Compute upper distance matrix
for (let i = 0; i < length; i++) {
for (let j = 0; j <= i; j++) {
result[i][j] = distanceFn(data[i], data[j]);
}
}
export default function distanceMatrix(data, distanceFn) {
const result = getMatrix(data.length);

// Copy to lower distance matrix
for (let i = 0; i < length; i++) {
for (let j = i + 1; j < length; j++) {
result[i][j] = result[j][i];
}
// Compute upper distance matrix
for (let i = 0; i < data.length; i++) {
for (let j = 0; j <= i; j++) {
result[i][j] = distanceFn(data[i], data[j]);
result[j][i] = result[i][j];
}
}

return result;
return result;
}

module.exports = distanceMatrix;
function getMatrix(size) {
const matrix = [];
for (let i = 0; i < size; i++) {
const row = [];
matrix.push(row);
for (let j = 0; j < size; j++) {
row.push(0);
}
}
return matrix;
}
2 changes: 0 additions & 2 deletions test/.eslintrc.yml

This file was deleted.

16 changes: 0 additions & 16 deletions test/test.js

This file was deleted.

Loading

0 comments on commit cac9c7e

Please sign in to comment.