Skip to content

Commit

Permalink
Added Min (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
CooRay authored and jreina committed Oct 7, 2018
1 parent 108e4c5 commit 87818de
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface Array<T> {
equalityComparer: IEqualityComparer<T>
);
Last<T>(): T;
Min<number>(): number;
Min<T>(xform: (x: T) => number): T;
Max<number>(): number;
Max<T>(xform: (x: T) => number): T;
Prepend<T>(element: T): Array<T>;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const Empty = require('./src/Empty')
const First = require('./src/First');
const GroupJoin = require('./src/GroupJoin');
const Last = require('./src/Last');
const Min = require('./src/Min');
const Max = require('./src/Max');
const Prepend = require('./src/Prepend');
const Reverse = require('./src/Reverse');
Expand All @@ -28,6 +29,7 @@ const bindAll = function() {
Array.prototype.First = First;
Array.prototype.GroupJoin = GroupJoin;
Array.prototype.Last = Last;
Array.prototype.Min = Min;
Array.prototype.Max = Max;
Array.prototype.Prepend = Prepend;
Array.prototype.Reverse = Reverse;
Expand Down
9 changes: 9 additions & 0 deletions src/Min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @template T
* @param {((x: T) => number)} xform
* @returns {T}
*/
function Min(xform = x => x) {
return this.reduce((a, b) => (xform(a) < xform(b) ? a : b));
}
module.exports = Min;
20 changes: 20 additions & 0 deletions test/Min.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const expect = require('chai').expect;
require('..')();
describe('Array#prototype#Min', function() {
const numInput = [1, 2, 3, 4, 5, 6];
it('Should return the min value in number array', function() {
const expected = 1;
const actual = numInput.Min();
expect(actual).to.eql(expected);
});
const objInput = [
{ name: 'Bill', age: 32 },
{ name: 'Jim', age: 13 },
{ name: 'Bart', age: 25 }
];
it('Should return the object with the min property value in object array', function() {
const expected = { name: 'Jim', age: 13 };
const actual = objInput.Min(x => x.age);
expect(actual).to.eql(expected);
});
});

0 comments on commit 87818de

Please sign in to comment.