Skip to content

Commit beb0f63

Browse files
committed
added defaultArrayIfEmpty() method
1 parent 48df102 commit beb0f63

File tree

7 files changed

+109
-3
lines changed

7 files changed

+109
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log (node-enumerable)
22

3+
## 3.6.0 (November 5th, 2017; methods)
4+
5+
* added `defaultArrayIfEmpty()` method
6+
37
## 3.5.2 (November 5th, 2017; methods)
48

59
* added `cos()` and `arcCos()` methods

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* [chunk](#chunk-)
3737
* [clone](#clone-)
3838
* [concat / concatArray](#concat--concatarray-)
39-
* [defaultIfEmpty / defaultSequenceIfEmpty](#defaultifempty--defaultsequenceifempty-)
39+
* [defaultIfEmpty / defaultArrayIfEmpty / defaultSequenceIfEmpty](#defaultifempty--defaultarrayifempty--defaultsequenceifempty-)
4040
* [intersperse / intersperseArray](#intersperse--interspersearray-)
4141
* [popFrom / shiftFrom](#popfrom--shiftfrom-)
4242
* [pushTo](#pushto-)
@@ -684,7 +684,7 @@ Enumerable.create(0, 0.5, -1)
684684

685685
### More [[↑](#examples-)]
686686

687-
#### Chunk [[↑](#more-)]
687+
#### chunk [[↑](#more-)]
688688

689689
```javascript
690690
let seq = Enumerable.range(0, 10);
@@ -721,7 +721,7 @@ Enumerable.create(0, 111, 222)
721721
.concatArray([ [ 'pz', 'tm' ], [ 'mk' ] ]);
722722
```
723723

724-
#### defaultIfEmpty / defaultSequenceIfEmpty [[↑](#more-)]
724+
#### defaultIfEmpty / defaultArrayIfEmpty / defaultSequenceIfEmpty [[↑](#more-)]
725725

726726
```javascript
727727
// 0, 1, 2
@@ -735,6 +735,7 @@ Enumerable.create()
735735
// 0, 11, 22
736736
Enumerable.create(0, 11, 22)
737737
.defaultSequenceIfEmpty(['pz', 'tm', 'mk']);
738+
// alt: defaultArrayIfEmpty()
738739

739740
// 'pz', 'tm', 'mk'
740741
Enumerable.create()

demo/js/enumerable.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ declare namespace Enumerable {
436436
* Gets the current iterator result.
437437
*/
438438
readonly current: IteratorResult<T>;
439+
/**
440+
* Alias for defaultSequenceIfEmpty()
441+
*/
442+
defaultArrayIfEmpty(defaultSequence: Sequence<T>): IEnumerable<T>;
439443
/**
440444
* Returns the items of that sequence or a default item list
441445
* if that sequence is empty.
@@ -1169,6 +1173,8 @@ declare namespace Enumerable {
11691173
/** @inheritdoc */
11701174
readonly current: IteratorResult<T>;
11711175
/** @inheritdoc */
1176+
defaultArrayIfEmpty(defaultSequence: Sequence<T>): IEnumerable<T>;
1177+
/** @inheritdoc */
11721178
defaultIfEmpty(...defaultItems: Array<T>): IEnumerable<T>;
11731179
/**
11741180
* @see defaultIfEmpty()

index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ namespace Enumerable {
479479
* Gets the current iterator result.
480480
*/
481481
readonly current: IteratorResult<T>;
482+
/**
483+
* Alias for defaultSequenceIfEmpty()
484+
*/
485+
defaultArrayIfEmpty(defaultSequence: Sequence<T>): IEnumerable<T>;
482486
/**
483487
* Returns the items of that sequence or a default item list
484488
* if that sequence is empty.
@@ -1598,6 +1602,11 @@ namespace Enumerable {
15981602
return this._current;
15991603
}
16001604
/** @inheritdoc */
1605+
public defaultArrayIfEmpty(defaultSequence: Sequence<T>): IEnumerable<T> {
1606+
return this.defaultSequenceIfEmpty
1607+
.apply(this, arguments);
1608+
}
1609+
/** @inheritdoc */
16011610
public defaultIfEmpty(...defaultItems: Array<T>): IEnumerable<T> {
16021611
return from(this.defaultIfEmptyInner(defaultItems));
16031612
}

js/enumerable.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// The MIT License (MIT)
2+
//
3+
// node-enumerable (https://github.com/mkloubert/node-enumerable)
4+
// Copyright (c) Marcel Joachim Kloubert <marcel.kloubert@gmx.net>
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to
8+
// deal in the Software without restriction, including without limitation the
9+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10+
// sell copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22+
// DEALINGS IN THE SOFTWARE.
23+
24+
import Assert = require('assert');
25+
import Enumerable = require('../../');
26+
import Helpers = require('../helpers');
27+
28+
const MAX_ARRAY_SIZE = 100;
29+
30+
Helpers.execute(
31+
'Testing numbers...',
32+
(ctx) => {
33+
for (let i = 0; i < MAX_ARRAY_SIZE; i++) {
34+
if (0 === i % 10) {
35+
ctx.log(`Testing with ${i} elements...`);
36+
}
37+
38+
// fill test array
39+
let arr: any[] = [];
40+
for (let j = 0; j < i; j++) {
41+
arr.push(j);
42+
}
43+
44+
let e = Enumerable.from(arr)
45+
.defaultArrayIfEmpty([ 'MK', 'TM', 'JS', 'YS' ]);
46+
47+
let testArr: any[] = [];
48+
while (!e.next().done) {
49+
testArr.push(e.current.value);
50+
}
51+
52+
let expected: any[];
53+
if (arr.length > 0) {
54+
expected = arr;
55+
}
56+
else {
57+
expected = ['MK', 'TM', 'JS', 'YS'];
58+
}
59+
60+
Assert.strictEqual(testArr.length, expected.length);
61+
Assert.equal(testArr.length, expected.length);
62+
Assert.equal('' + testArr.length, expected.length);
63+
Assert.equal(testArr.length, '' + expected.length);
64+
Assert.equal('' + testArr.length, '' + expected.length);
65+
Assert.strictEqual('' + testArr.length, '' + expected.length);
66+
67+
for (let j = 0; j < testArr.length; j++) {
68+
Assert.strictEqual(testArr[j], expected[j]);
69+
Assert.equal(testArr[j], expected[j]);
70+
Assert.equal('' + testArr[j], expected[j]);
71+
Assert.equal(testArr[j], '' + expected[j]);
72+
Assert.equal('' + testArr[j], '' + expected[j]);
73+
Assert.strictEqual('' + testArr[j], '' + expected[j]);
74+
}
75+
}
76+
});

0 commit comments

Comments
 (0)