Skip to content

Commit

Permalink
added cos(), sin() and tan() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Nov 5, 2017
1 parent 48e6b95 commit c128ce5
Show file tree
Hide file tree
Showing 9 changed files with 707 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

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

* added `cos()` method
* added `log()` method
* added `pow()` method
* added `shuffle()` method
* added `sin()` method
* added `tan()` method

## 3.4.1 (November 4th, 2017; methods)

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,11 @@ Enumerable.create(-1, 22.57, 444, true, -333.85, false);
Enumerable.create(-1, 22.47, 444, null, -333.85, false);
.ceil();

// cos()
// 981.63, 927.18, 838.67
Enumerable.create(11, 22, 33)
.cos();

// floor()
// -1, 23, 444, NaN, -334, NaN
Enumerable.create(-1, 22.47, 444.0, undefined, -333.85, true);
Expand All @@ -641,10 +646,20 @@ Enumerable.create(1, 2, 3, 4)
Enumerable.create(-1, 22.47, 444.0, undefined, -333.85, 1.5, true);
.round();

// sin()
// 17.45, 34.89, 52.33
Enumerable.create(1, 2, 3)
.sin();

// sum()
// 10
Enumerable.create(1, 2, 3, 4)
.sum();

// tan()
// -2.61, 900.40, -509.52
Enumerable.create(111, 222, 333)
.tan();
```

### More [[↑](#examples-)]
Expand Down
24 changes: 18 additions & 6 deletions demo/js/enumerable.js

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

33 changes: 33 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,15 @@ declare namespace Enumerable {
* @returns {boolean} Sequence contains item or not.
*/
contains<U>(item: U, comparer?: EqualityComparer<T, U> | true): boolean;
/**
* Handles current items as base numbers and calculates the cosine for each item.
*
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
cos(handleAsInt?: boolean): IEnumerable<number>;
/**
* Counts the elements of that sequence.
*
Expand Down Expand Up @@ -788,6 +797,15 @@ declare namespace Enumerable {
* Alias for rand()
*/
shuffle(sortValueProvider?: () => any): IOrderedEnumerable<T>;
/**
* Handles current items as base numbers and calculates the sine for each item.
*
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
sin(handleAsInt?: boolean): IEnumerable<number>;
/**
* Returns the one and only element of that sequence.
*
Expand Down Expand Up @@ -856,6 +874,15 @@ declare namespace Enumerable {
* @return {IEnumerable<T>} The new sequence.
*/
takeWhile(predicate: Predicate<T>): IEnumerable<T>;
/**
* Handles current items as base numbers and calculates the tangent for each item.
*
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
tan(handleAsInt?: boolean): IEnumerable<number>;
/**
* Creates a new array from the items of that sequence.
*
Expand Down Expand Up @@ -1032,6 +1059,8 @@ declare namespace Enumerable {
/** @inheritdoc */
contains<U>(item: U, comparer?: EqualityComparer<T, U> | true): boolean;
/** @inheritdoc */
cos(handleAsInt?: boolean): IEnumerable<number>;
/** @inheritdoc */
count(predicate?: Predicate<T>): number;
/** @inheritdoc */
readonly current: IteratorResult<T>;
Expand Down Expand Up @@ -1178,6 +1207,8 @@ declare namespace Enumerable {
/** @inheritdoc */
shuffle(sortValueProvider?: () => any): any;
/** @inheritdoc */
sin(handleAsInt?: boolean): IEnumerable<number>;
/** @inheritdoc */
single(predicate?: Predicate<T>): T;
/** @inheritdoc */
singleOrDefault<U = symbol>(predicateOrDefaultValue?: Predicate<T> | T, defaultValue?: U): T | U;
Expand Down Expand Up @@ -1206,6 +1237,8 @@ declare namespace Enumerable {
*/
protected takeWhileInner(predicate: Predicate<T>): IterableIterator<T>;
/** @inheritdoc */
tan(handleAsInt?: boolean): IEnumerable<number>;
/** @inheritdoc */
toArray(): Array<T>;
/** @inheritdoc */
toLookup<TKey extends PropertyKey, U = any>(keySelector: Selector<T, TKey>, keyEqualityComparer?: EqualityComparer<TKey>): U;
Expand Down
63 changes: 56 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,15 @@ namespace Enumerable {
*/
contains<U>(item: U,
comparer?: EqualityComparer<T, U> | true): boolean;
/**
* Handles current items as base numbers and calculates the cosine for each item.
*
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
cos(handleAsInt?: boolean): IEnumerable<number>;
/**
* Counts the elements of that sequence.
*
Expand Down Expand Up @@ -857,6 +866,15 @@ namespace Enumerable {
* Alias for rand()
*/
shuffle(sortValueProvider?: () => any): IOrderedEnumerable<T>;
/**
* Handles current items as base numbers and calculates the sine for each item.
*
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
sin(handleAsInt?: boolean): IEnumerable<number>;
/**
* Returns the one and only element of that sequence.
*
Expand Down Expand Up @@ -926,6 +944,15 @@ namespace Enumerable {
* @return {IEnumerable<T>} The new sequence.
*/
takeWhile(predicate: Predicate<T>): IEnumerable<T>;
/**
* Handles current items as base numbers and calculates the tangent for each item.
*
* @param {boolean} [handleAsInt] Handle as integer values (true) or floats (false).
* Default: (false)
*
* @return {IEnumerable<number>} The new sequence.
*/
tan(handleAsInt?: boolean): IEnumerable<number>;
/**
* Creates a new array from the items of that sequence.
*
Expand Down Expand Up @@ -1078,7 +1105,7 @@ namespace Enumerable {
/** @inheritdoc */
public abs(handleAsInt?: boolean): IEnumerable<number> {
return this.select((x: any) => {
return invokeForValidNumber(x, x => Math.abs(x),
return invokeForValidNumber(x, y => Math.abs(y),
handleAsInt);
});
}
Expand Down Expand Up @@ -1325,7 +1352,8 @@ namespace Enumerable {
/** @inheritdoc */
public ceil(): IEnumerable<number> {
return this.select((x: any) => {
return invokeForValidNumber(x, x => Math.ceil(x));
return invokeForValidNumber(x,
y => Math.ceil(y));
});
}
/** @inheritdoc */
Expand Down Expand Up @@ -1415,6 +1443,12 @@ namespace Enumerable {
return this.indexOf<U>(item, comparer) > -1;
}
/** @inheritdoc */
public cos(handleAsInt?: boolean): IEnumerable<number> {
return this.select(x => invokeForValidNumber(x,
y => Math.cos(y),
handleAsInt));
}
/** @inheritdoc */
public count(predicate?: Predicate<T>): number {
predicate = toPredicateSafe(predicate);

Expand Down Expand Up @@ -1617,7 +1651,8 @@ namespace Enumerable {
/** @inheritdoc */
public floor(): IEnumerable<number> {
return this.select((x: any) => {
return invokeForValidNumber(x, x => Math.floor(x));
return invokeForValidNumber(x,
y => Math.floor(y));
});
}
/** @inheritdoc */
Expand Down Expand Up @@ -1986,7 +2021,7 @@ namespace Enumerable {

return this.select(x => {
return invokeForValidNumber(x,
x => logFunc(x),
y => logFunc(y),
handleAsInt);
});
}
Expand Down Expand Up @@ -2142,7 +2177,8 @@ namespace Enumerable {
}

return this.select((x: any) => {
return invokeForValidNumber(x, x => Math.pow(x, exponent),
return invokeForValidNumber(x,
y => Math.pow(y, exponent),
handleAsInt);
});
}
Expand Down Expand Up @@ -2185,7 +2221,8 @@ namespace Enumerable {
/** @inheritdoc */
public round(): IEnumerable<number> {
return this.select((x: any) => {
return invokeForValidNumber(x, x => Math.round(x));
return invokeForValidNumber(x,
y => Math.round(y));
});
}
/** @inheritdoc */
Expand Down Expand Up @@ -2253,6 +2290,12 @@ namespace Enumerable {
.apply(this, arguments);
}
/** @inheritdoc */
public sin(handleAsInt?: boolean): IEnumerable<number> {
return this.select(x => invokeForValidNumber(x,
y => Math.sin(y),
handleAsInt));
}
/** @inheritdoc */
public single(predicate?: Predicate<T>): T {
predicate = toPredicateSafe(predicate);

Expand Down Expand Up @@ -2395,6 +2438,12 @@ namespace Enumerable {
}
}
/** @inheritdoc */
public tan(handleAsInt?: boolean): IEnumerable<number> {
return this.select(x => invokeForValidNumber(x,
y => Math.tan(y),
handleAsInt));
}
/** @inheritdoc */
public toArray(): Array<T> {
const ARR: Array<T> = [];

Expand Down Expand Up @@ -3023,7 +3072,7 @@ namespace Enumerable {
} // fromString()

function invokeForValidNumber(x: any, action: (n: number) => any,
handleAsInt = false) {
handleAsInt = false) {
if ('number' !== typeof x) {
if (!handleAsInt) {
x = parseFloat( toStringSafe(x).trim() );
Expand Down
Loading

0 comments on commit c128ce5

Please sign in to comment.