Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ kmeans.clusterize(vectors, {k: 4}, (err,res) => {
if (err) console.error(err);
else console.log('%o',res);
});

// Example with promise
await const res = kmeans.clusterize(vectors, {k: 4});
console.log('%o',res);
```
## Inputs
- **vectors** is a nXm array (n [lines] : number of points, m [columns] : number of dimensions)
- **options** object:
- **k** : number of clusters
- **distance** (optional) : custom distance function returning the distance between two points `(a,b) => number`, *default* Euclidian Distance
- **callback** node-style callback taking error and result argument
- **callback** (optional) node-style callback taking error and result argument. If not supplied, the function returns a promise with the result.

## Outputs
An array of objects (one for each cluster) with the following properties:
Expand Down
15 changes: 15 additions & 0 deletions lib/kmeans.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,21 @@ class Clusterize {
}

exports.clusterize = (vector, options, callback) => {
if (!options || !vector) {
throw new Error('Provide 2 arguments: vector, options');
}

if (vector && options && !callback) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't if (!callback) sufficient thanks to your first check?

return new Promise((resolve, reject) => {
/* eslint-disable no-new */
new Clusterize(vector, options, (err, data) => {
if (err !== null) {
return reject(err);
}
return resolve(data);
});
});
}
return new Clusterize(vector, options, callback);
};

Expand Down
97 changes: 48 additions & 49 deletions package-lock.json

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

28 changes: 23 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,30 @@ const data3D = [
* Tests
*/
describe('kmeans', () => {
describe('#clusterize()', () => {
it('should return a promise if no callback function', () => {
/* eslint-disable new-cap */
kmeans.clusterize([], {}).should.be.Promise();
});
});

describe('#clusterize() errors', () => {
it('should throw an error if there aren\'t 3 arguments', () => {
it('should throw an error if there aren\'t 2 arguments', () => {
(() => {
kmeans.clusterize();
}).should.throw('Provide 3 arguments: vector, options, callback');
}).should.throw('Provide 2 arguments: vector, options');
(() => {
kmeans.clusterize({});
}).should.throw('Provide 3 arguments: vector, options, callback');
}).should.throw('Provide 2 arguments: vector, options');
});

it('should not throw an error if there are 2 arguments', () => {
(() => {
kmeans.clusterize({}, {});
}).should.throw('Provide 3 arguments: vector, options, callback');
}).should.not.throw();
});

it('should throw an error if no callback function', () => {
it('should throw an error if callback is no function', () => {
(() => {
kmeans.clusterize([], {}, {});
}).should.throw('Provide a callback function');
Expand All @@ -57,6 +67,10 @@ describe('kmeans', () => {
});
});

it('should reject promise if no \'k\' option', () => {
kmeans.clusterize({}, { k: 3 }).should.be.rejected();
});

it('should return an error if the data vector is not an array', done => {
kmeans.clusterize({}, { k: 3 }, (err, res) => {
should.not.exist(res);
Expand Down Expand Up @@ -85,6 +99,10 @@ describe('kmeans', () => {
});
});

it('should return a result (array) with promise', () => {
kmeans.clusterize(data3D, { k: 3 }).should.be.eventually.have.length(3);
});

it('should return 2 groups with the 2 vectors', done => {
kmeans.clusterize([[1, 1], [2, 2]], { k: 2 }, (err, res) => {
should.not.exist(err);
Expand Down