Skip to content

Commit

Permalink
added root() method
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Nov 5, 2017
1 parent beb0f63 commit 2a26e6c
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 3.6.0 (November 5th, 2017; methods)

* added `defaultArrayIfEmpty()` method
* added `root()` method

## 3.5.2 (November 5th, 2017; methods)

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,11 @@ Enumerable.create(1, 2, 3, 4)
Enumerable.create(1, 2, 3, 4)
.product();

// root()
// 1, 2, 3, 4
Enumerable.create(1, 8, 27, 128)
.root(3);

// round()
// -1, 23, 444, NaN, -334, 2, NaN
Enumerable.create(-1, 22.47, 444.0, undefined, -333.85, 1.5, true)
Expand Down
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,16 @@ declare namespace Enumerable {
* @return {IOrderedEnumerable<T>} The new sequence.
*/
reverse(): IOrderedEnumerable<T>;
/**
* Handles current items as numbers and calculates the n-th root for each item.
*
* @param {number} [power] The power. Default: 2
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
root(power?: number, handleAsInt?: boolean): IEnumerable<number>;
/**
* Handles current items as float values and return their nearest values.
*
Expand Down Expand Up @@ -1303,6 +1313,8 @@ declare namespace Enumerable {
/** @inheritdoc */
reverse(): IOrderedEnumerable<T>;
/** @inheritdoc */
root(power?: number, handleAsInt?: boolean): IEnumerable<number>;
/** @inheritdoc */
round(): IEnumerable<number>;
/** @inheritdoc */
select<U>(selector: Selector<T, U>): IEnumerable<U>;
Expand Down
23 changes: 23 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,16 @@ namespace Enumerable {
* @return {IOrderedEnumerable<T>} The new sequence.
*/
reverse(): IOrderedEnumerable<T>;
/**
* Handles current items as numbers and calculates the n-th root for each item.
*
* @param {number} [power] The power. Default: 2
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
root(power?: number, handleAsInt?: boolean): IEnumerable<number>;
/**
* Handles current items as float values and return their nearest values.
*
Expand Down Expand Up @@ -2360,6 +2370,19 @@ namespace Enumerable {
});
}
/** @inheritdoc */
public root(power?: number, handleAsInt?: boolean): IEnumerable<number> {
power = parseFloat( toStringSafe(power).trim() );
if (isNaN(power)) {
power = 2;
}

return this.select(x => {
return invokeForValidNumber(x,
y => Math.pow(y, 1 / power),
handleAsInt);
});
}
/** @inheritdoc */
public round(): IEnumerable<number> {
return this.select((x: any) => {
return invokeForValidNumber(x,
Expand Down
188 changes: 188 additions & 0 deletions test/IEnumerable/root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// The MIT License (MIT)
//
// node-enumerable (https://github.com/mkloubert/node-enumerable)
// Copyright (c) Marcel Joachim Kloubert <marcel.kloubert@gmx.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

import Assert = require('assert');
import Enumerable = require('../../');
import Helpers = require('../helpers');

const MAX_ARRAY_SIZE = 100;

Helpers.execute(
'Testing numbers (floats)...',
(ctx) => {
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
const ARR: number[] = [];
const EXPECTED: number[] = [];
for (let j = 0; j < i; j++) {
ARR.push( j );
EXPECTED.push( Math.pow(j, 1 / 3) );
}

const SEQ = Enumerable.from(ARR)
.root(3);

const ACTUAL: number[] = [];
for (let item of SEQ) {
ACTUAL.push(item);
}

Assert.equal( ACTUAL.length, EXPECTED.length );
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );

for (let k = 0; k < EXPECTED.length; k++) {
const A = ACTUAL[k];
const E = EXPECTED[k];

Assert.equal( A, E );
Assert.strictEqual( A, E );
Assert.equal( '' + A, E );
Assert.equal( A, '' + E );
Assert.equal( '' + A, '' + E );
Assert.strictEqual( '' + A, '' + E );
}
}
});

Helpers.execute(
'Testing strings (floats)...',
(ctx) => {
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
const ARR: string[] = [];
const EXPECTED: number[] = [];
for (let j = 0; j < i; j++) {
ARR.push( '' + j );
EXPECTED.push( Math.pow(j, 1 / 3) );
}

const SEQ = Enumerable.from(ARR)
.root(3);

const ACTUAL: number[] = [];
for (let item of SEQ) {
ACTUAL.push(item);
}

Assert.equal( ACTUAL.length, EXPECTED.length );
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );

for (let k = 0; k < EXPECTED.length; k++) {
const A = ACTUAL[k];
const E = EXPECTED[k];

Assert.equal( A, E );
Assert.strictEqual( A, E );
Assert.equal( '' + A, E );
Assert.equal( A, '' + E );
Assert.equal( '' + A, '' + E );
Assert.strictEqual( '' + A, '' + E );
}
}
});

Helpers.execute(
'Testing numbers (ints)...',
(ctx) => {
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
const ARR: number[] = [];
const EXPECTED: number[] = [];
for (let j = 0; j < i; j++) {
ARR.push( j );
EXPECTED.push( Math.pow(j, 1 / 3) );
}

const SEQ = Enumerable.from(ARR)
.root(3, true);

const ACTUAL: number[] = [];
for (let item of SEQ) {
ACTUAL.push(item);
}

Assert.equal( ACTUAL.length, EXPECTED.length );
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );

for (let k = 0; k < EXPECTED.length; k++) {
const A = ACTUAL[k];
const E = EXPECTED[k];

Assert.equal( A, E );
Assert.strictEqual( A, E );
Assert.equal( '' + A, E );
Assert.equal( A, '' + E );
Assert.equal( '' + A, '' + E );
Assert.strictEqual( '' + A, '' + E );
}
}
});

Helpers.execute(
'Testing strings (ints)...',
(ctx) => {
for (let i = 0; i <= MAX_ARRAY_SIZE; i++) {
const ARR: string[] = [];
const EXPECTED: number[] = [];
for (let j = 0; j < i; j++) {
ARR.push( '' + j );
EXPECTED.push( Math.pow(j, 1 / 3) );
}

const SEQ = Enumerable.from(ARR)
.root(3, true);

const ACTUAL: number[] = [];
for (let item of SEQ) {
ACTUAL.push(item);
}

Assert.equal( ACTUAL.length, EXPECTED.length );
Assert.strictEqual( ACTUAL.length, EXPECTED.length );
Assert.equal( '' + ACTUAL.length, EXPECTED.length );
Assert.equal( ACTUAL.length, '' + EXPECTED.length );
Assert.equal( '' + ACTUAL.length, '' + EXPECTED.length );
Assert.strictEqual( '' + ACTUAL.length, '' + EXPECTED.length );

for (let k = 0; k < EXPECTED.length; k++) {
const A = ACTUAL[k];
const E = EXPECTED[k];

Assert.equal( A, E );
Assert.strictEqual( A, E );
Assert.equal( '' + A, E );
Assert.equal( A, '' + E );
Assert.equal( '' + A, '' + E );
Assert.strictEqual( '' + A, '' + E );
}
}
});

0 comments on commit 2a26e6c

Please sign in to comment.