Skip to content

Commit

Permalink
added append*() and prepend*() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Nov 7, 2017
1 parent 6175eb0 commit ae8c8d9
Show file tree
Hide file tree
Showing 10 changed files with 574 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log (node-enumerable)

## 3.8.0 (November 8th, 2017; methods)

* added `append()` and `appendArray()` methods
* added `prepend()` and `prependArray()` methods

## 3.7.1 (November 7th, 2017; methods)

* added `assert()` and `assertAll()` methods
Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* [forAll](#forall-)
* [intersperse / intersperseArray](#intersperse--interspersearray-)
* [popFrom / shiftFrom](#popfrom--shiftfrom-)
* [prepend / prependArray](#prepend--prependarray-)
* [pushTo](#pushto-)
* [random](#random-)
* [reset](#reset-)
Expand Down Expand Up @@ -754,11 +755,11 @@ for (let child of father.clone(3)) {
```javascript
// 0, 1, 2, 'PZ', 'TM', 'MK'
Enumerable.create(0, 1, 2)
.concat(['PZ'], ['TM', 'MK']);
.concat(['PZ'], ['TM', 'MK']); // alt: append()

// 0, 111, 222, 'pz', 'tm', 'mk'
Enumerable.create(0, 111, 222)
.concatArray([ [ 'pz', 'tm' ], [ 'mk' ] ]);
.concatArray([ [ 'pz', 'tm' ], [ 'mk' ] ]); // alt: appendArray()
```

#### consume [[↑](#more-)]
Expand Down Expand Up @@ -870,6 +871,18 @@ for (let item of Enumerable.shiftFrom(arr2)) {
// arr2 is empty now
```

#### prepend / prependArray [[↑](#more-)]

```javascript
// 'PZ', 'TM', 'MK', 0, 1, 2
Enumerable.create(0, 1, 2)
.prepend(['PZ'], ['TM', 'MK']);

// 'pz', 'tm', 'mk', 0, 111, 222
Enumerable.create(0, 111, 222)
.prependArray([ [ 'pz', 'tm' ], [ 'mk' ] ]);
```

#### pushTo [[↑](#more-)]

```javascript
Expand Down
34 changes: 34 additions & 0 deletions demo/js/enumerable.js

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

54 changes: 45 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ declare namespace Enumerable {
* @returns {boolean} One element satisfies the condition or not.
*/
any(predicate?: Predicate<T>): boolean;
/**
* Alias for concat()
*/
append<U = T>(...args: Sequence<U>[]): IEnumerable<T | U>;
/**
* Alias for concatArray()
*/
appendArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U>;
/**
* Handles current items as numbers and calculates the arc cosine for each item.
*
Expand Down Expand Up @@ -408,20 +416,20 @@ declare namespace Enumerable {
* Concats the items of that sequences with other ones
* to a new sequence.
*
* @param {...Sequence<T>[]} args The other sequences.
* @param {...Sequence<U>[]} args The other sequences.
*
* @memberof IEnumerable<T> The concated sequence.
* @memberof IEnumerable<T|U> The concated sequence.
*/
concat(...args: Sequence<T>[]): IEnumerable<T>;
concat<U = T>(...args: Sequence<U>[]): IEnumerable<T | U>;
/**
* Concats the items of that sequences with other ones
* to a new sequence.
*
* @param {...Sequence<T>[]} sequences The other sequences.
* @param {...Sequence<U>[]} sequences The other sequences.
*
* @memberof IEnumerable<T> The concated sequence.
* @memberof IEnumerable<T|U> The concated sequence.
*/
concatArray(sequences: ArrayLike<Sequence<T>>): IEnumerable<T>;
concatArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U>;
/**
* Completely consumes the given sequence. This method uses immediate execution,
* and doesn't store any data during execution.
Expand Down Expand Up @@ -851,6 +859,22 @@ declare namespace Enumerable {
* @return {IEnumerable<number>} The new sequence.
*/
pow(exponent?: number, handleAsInt?: boolean): IEnumerable<number>;
/**
* Prepends the itms of other sequences to the ones of that sequence.
*
* @param {...Sequence<T>[]} args The other sequences.
*
* @memberof IEnumerable<T|U> The concated sequence.
*/
prepend<U = T>(...args: Sequence<T | U>[]): IEnumerable<T | U>;
/**
* Prepends the itms of other sequences to the ones of that sequence.
*
* @param {...Sequence<T>[]} sequences The other sequences.
*
* @memberof IEnumerable<T|U> The concated sequence.
*/
prependArray<U = T>(sequences: ArrayLike<Sequence<T | U>>): IEnumerable<T | U>;
/**
* Calculates the product of that sequence.
*
Expand Down Expand Up @@ -1266,6 +1290,10 @@ declare namespace Enumerable {
/** @inheritdoc */
any(predicate?: Predicate<T>): boolean;
/** @inheritdoc */
append<U = T>(...args: Sequence<U>[]): IEnumerable<T | U>;
/** @inheritdoc */
appendArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U>;
/** @inheritdoc */
arcCos(handleAsInt?: boolean): IEnumerable<number>;
/** @inheritdoc */
arcCosH(handleAsInt?: boolean): IEnumerable<number>;
Expand Down Expand Up @@ -1304,13 +1332,13 @@ declare namespace Enumerable {
*/
protected cloneInner<U>(count: number, itemSelector: Selector<T, U>): IterableIterator<any>;
/** @inheritdoc */
concat(...args: Sequence<T>[]): IEnumerable<T>;
concat<U = T>(...args: Sequence<U>[]): IEnumerable<T | U>;
/** @inheritdoc */
concatArray(sequences: ArrayLike<Sequence<T>>): IEnumerable<T>;
concatArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U>;
/**
* @see concatArray()
*/
protected concatArrayInner(sequences: ArrayLike<Sequence<T>>): IterableIterator<T>;
protected concatArrayInner<U>(sequences: ArrayLike<Sequence<U>>): IterableIterator<T | U>;
/** @inheritdoc */
consume(): this;
/** @inheritdoc */
Expand Down Expand Up @@ -1452,6 +1480,14 @@ declare namespace Enumerable {
/** @inheritdoc */
pow(exponent?: number, handleAsInt?: boolean): IEnumerable<number>;
/** @inheritdoc */
prepend<U = T>(...args: Sequence<U>[]): IEnumerable<T | U>;
/** @inheritdoc */
prependArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U>;
/**
* @see concatArray()
*/
protected prependArrayInner<U>(sequences: ArrayLike<Sequence<U>>): IterableIterator<T | U>;
/** @inheritdoc */
product(): T | symbol;
/** @inheritdoc */
pushTo(stack: Stack<T>): this;
Expand Down
78 changes: 69 additions & 9 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ namespace Enumerable {
* @returns {boolean} One element satisfies the condition or not.
*/
any(predicate?: Predicate<T>): boolean;
/**
* Alias for concat()
*/
append<U = T>(...args: Sequence<U>[]): IEnumerable<T | U>;
/**
* Alias for concatArray()
*/
appendArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U>;
/**
* Handles current items as numbers and calculates the arc cosine for each item.
*
Expand Down Expand Up @@ -453,20 +461,20 @@ namespace Enumerable {
* Concats the items of that sequences with other ones
* to a new sequence.
*
* @param {...Sequence<T>[]} args The other sequences.
* @param {...Sequence<U>[]} args The other sequences.
*
* @memberof IEnumerable<T> The concated sequence.
* @memberof IEnumerable<T|U> The concated sequence.
*/
concat(...args: Sequence<T>[]): IEnumerable<T>;
concat<U = T>(...args: Sequence<U>[]): IEnumerable<T | U>;
/**
* Concats the items of that sequences with other ones
* to a new sequence.
*
* @param {...Sequence<T>[]} sequences The other sequences.
* @param {...Sequence<U>[]} sequences The other sequences.
*
* @memberof IEnumerable<T> The concated sequence.
* @memberof IEnumerable<T|U> The concated sequence.
*/
concatArray(sequences: ArrayLike<Sequence<T>>): IEnumerable<T>;
concatArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U>;
/**
* Completely consumes the given sequence. This method uses immediate execution,
* and doesn't store any data during execution.
Expand Down Expand Up @@ -922,6 +930,22 @@ namespace Enumerable {
* @return {IEnumerable<number>} The new sequence.
*/
pow(exponent?: number, handleAsInt?: boolean): IEnumerable<number>;
/**
* Prepends the itms of other sequences to the ones of that sequence.
*
* @param {...Sequence<T>[]} args The other sequences.
*
* @memberof IEnumerable<T|U> The concated sequence.
*/
prepend<U = T>(...args: Sequence<T | U>[]): IEnumerable<T | U>;
/**
* Prepends the itms of other sequences to the ones of that sequence.
*
* @param {...Sequence<T>[]} sequences The other sequences.
*
* @memberof IEnumerable<T|U> The concated sequence.
*/
prependArray<U = T>(sequences: ArrayLike<Sequence<T | U>>): IEnumerable<T | U>;
/**
* Calculates the product of that sequence.
*
Expand Down Expand Up @@ -1458,6 +1482,16 @@ namespace Enumerable {
return false;
}
/** @inheritdoc */
public append<U = T>(...args: Sequence<U>[]): IEnumerable<T | U> {
return this.concat
.apply(this, arguments);
}
/** @inheritdoc */
public appendArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U> {
return this.concatArray
.apply(this, arguments);
}
/** @inheritdoc */
public arcCos(handleAsInt?: boolean): IEnumerable<number> {
return this.select(x => invokeForValidNumber(x,
y => Math.acos(y),
Expand Down Expand Up @@ -1795,17 +1829,17 @@ namespace Enumerable {
}
}
/** @inheritdoc */
public concat(...args: Sequence<T>[]): IEnumerable<T> {
public concat<U = T>(...args: Sequence<U>[]): IEnumerable<T | U> {
return this.concatArray(args);
}
/** @inheritdoc */
public concatArray(sequences: ArrayLike<Sequence<T>>): IEnumerable<T> {
public concatArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U> {
return from(this.concatArrayInner(sequences));
}
/**
* @see concatArray()
*/
protected *concatArrayInner(sequences: ArrayLike<Sequence<T>>): IterableIterator<T> {
protected *concatArrayInner<U>(sequences: ArrayLike<Sequence<U>>): IterableIterator<T | U> {
for (let item of this) {
yield item;
}
Expand Down Expand Up @@ -2625,6 +2659,32 @@ namespace Enumerable {
});
}
/** @inheritdoc */
public prepend<U = T>(...args: Sequence<U>[]): IEnumerable<T | U> {
return this.prependArray(args);
}
/** @inheritdoc */
public prependArray<U = T>(sequences: ArrayLike<Sequence<U>>): IEnumerable<T | U> {
return from(this.prependArrayInner(sequences));
}
/**
* @see concatArray()
*/
protected *prependArrayInner<U>(sequences: ArrayLike<Sequence<U>>): IterableIterator<T | U> {
if (sequences) {
for (let i = 0; i < sequences.length; i++) {
const SEQ = sequences[i];

for (let item of from(SEQ)) {
yield item;
}
}
}

for (let item of this) {
yield item;
}
}
/** @inheritdoc */
public product(): T | symbol {
return this.aggregate((acc, x) => IS_EMPTY !== acc ? (acc * <any>x) : x,
<any>IS_EMPTY);
Expand Down
34 changes: 34 additions & 0 deletions js/enumerable.js

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

Loading

0 comments on commit ae8c8d9

Please sign in to comment.