Skip to content

Commit

Permalink
Add max (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
CooRay authored and jreina committed Oct 7, 2018
1 parent d6f9089 commit 108e4c5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
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;
Max<number>(): number;
Max<T>(xform: (x: T) => number): T;
Prepend<T>(element: T): Array<T>;
Reverse<T>(): Array<T>;
Select<T, U>(xform: (x: T) => U): Array<U>;
Expand Down
5 changes: 4 additions & 1 deletion 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 Max = require('./src/Max');
const Prepend = require('./src/Prepend');
const Reverse = require('./src/Reverse');
const Select = require('./src/Select');
Expand All @@ -27,6 +28,7 @@ const bindAll = function() {
Array.prototype.First = First;
Array.prototype.GroupJoin = GroupJoin;
Array.prototype.Last = Last;
Array.prototype.Max = Max;
Array.prototype.Prepend = Prepend;
Array.prototype.Reverse = Reverse;
Array.prototype.Select = Select;
Expand All @@ -36,6 +38,7 @@ const bindAll = function() {
Array.prototype.ToDictionary = ToDictionary;
Array.prototype.Union = Union;
Array.prototype.Where = Where;
Array.prototype.ElementAt = ElementAt;
};

module.exports = bindAll;
module.exports = bindAll;
10 changes: 10 additions & 0 deletions src/Max.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const _max = (a, b) => (a > b ? a : b);
/**
* @template T
* @param {((x: T) => number)} xform
* @returns {T}
*/
function Max(xform = x => x) {
return this.reduce((a, b) => (xform(a) > xform(b) ? a : b));
}
module.exports = Max;
21 changes: 21 additions & 0 deletions test/Max.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const expect = require('chai').expect;
require('..')();

describe('Array#prototype#Max', function() {
const numInput = [1, 2, 3, 4, 5, 6];
it('Should return the max value in number array', function() {
const expected = 6;
const actual = numInput.Max();
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 max property in object array', function() {
const expected = { name: 'Bill', age: 32 };
const actual = objInput.Max(x => x.age);
expect(actual).to.eql(expected);
});
});

0 comments on commit 108e4c5

Please sign in to comment.