Skip to content

Commit

Permalink
Fix issue #466
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmorand committed Dec 16, 2019
1 parent d696e8e commit 52d793d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
11 changes: 3 additions & 8 deletions src/lib/extension/core/functions/max.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import {iteratorToArray} from "../../../helpers/iterator-to-array";
import {max as phpMax} from "locutus/php/math";

export function max(...values: any[]): Promise<any> {
export function max(...values: Array<any>): Promise<any> {
if (values.length === 1) {
values = values[0];
}

let array = iteratorToArray(values);

array.sort((a: any, b: any) => {
return a < b ? 1 : -1;
});

return Promise.resolve(array[0]);
return Promise.resolve(phpMax(iteratorToArray(values)));
}
7 changes: 2 additions & 5 deletions src/lib/extension/core/functions/min.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import {iteratorToArray} from "../../../helpers/iterator-to-array";
import {min as phpMin} from "locutus/php/math";

export function min(...values: Array<any>): Promise<any> {
if (values.length === 1) {
values = values[0];
}

let array = iteratorToArray(values);

array.sort();

return Promise.resolve(array[0]);
return Promise.resolve(phpMin(iteratorToArray(values)));
}
6 changes: 6 additions & 0 deletions test/tests/unit/lib/extension/core/functions/max/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ tape('max', async (test) => {
test.same(await max(1, 3, 2), 3);
test.same(await max('foo', 'bar'), 'foo');
test.same(await max({foo: 'foo', bar: 'bar'}), 'foo');
test.same(await max([1, 3, 2]), 3);
test.same(await max(0, 'a', 2), 2);
test.same(await max('b', 'a'), 'b');
test.same(await max('b', 'a', 2), 2);
test.same(await max('b', 'a', -2), 'b');
test.same(await max(-5, 5, 10, -10, 20, -20), 20);

test.end();
});
14 changes: 8 additions & 6 deletions test/tests/unit/lib/extension/core/functions/min/test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as tape from 'tape';
import {min} from "../../../../../../../../src/lib/extension/core/functions/min";

tape('min', (test) => {
test.test('supports multiple parameters', async (test) => {
test.same(await min(1, 3, 2), 1);

test.end();
});
tape('min', async (test) => {
test.same(await min(1, 3, 2), 1);
test.same(await min([1, 3, 2]), 1);
test.same(await min(0, 'a', 2), 0);
test.same(await min('b', 'a'), 'a');
test.same(await min('b', 'a', 2), 'a');
test.same(await min('b', 'a', -2), -2);
test.same(await min(-5, 5, 10, -10, 20, -20), -20);

test.end();
});

0 comments on commit 52d793d

Please sign in to comment.