Skip to content

Commit

Permalink
added isEmpty() method and similar global functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Nov 7, 2017
1 parent ae8c8d9 commit 357c5c5
Show file tree
Hide file tree
Showing 10 changed files with 452 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## 3.8.0 (November 8th, 2017; methods)

* added `append()` and `appendArray()` methods
* added `isEmpty()` method
* added `isNullOrEmpty()`, `isUndefinedNullOrEmpty()` and `isUndefinedOrEmpty()` functions
* added `prepend()` and `prependArray()` methods

## 3.7.1 (November 7th, 2017; methods)
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,21 @@ Enumerable.create(11, 22, 33, 44)
// based sequence here
//
// a generator based sequence will behave as count()

// (false)
Enumerable.create(111, 222, 333)
.isEmpty();

// all are (false)
Enumerable.isNullOrEmpty(
Enumerable.create(1111, 2222, 3333)
);
Enumerable.isUndefinedNullOrEmpty(
Enumerable.create(11111, 22222, 33333)
);
Enumerable.isUndefinedNullOrEmpty(
Enumerable.create(0, true, false)
);
```

### Math [[↑](#math-)]
Expand Down
41 changes: 41 additions & 0 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 @@ -687,6 +687,13 @@ declare namespace Enumerable {
* @return {IEnumerable<T>} The new sequence.
*/
intersect(second: Sequence<T>, comparer?: EqualityComparer<T> | true): IEnumerable<T>;
/**
* Checks if that sequence is empty or not
* by using the length() method.
*
* @return {boolean} Is Empty or not.
*/
isEmpty(): boolean;
/**
* Correlates the elements of that sequence and another based on matching keys.
*
Expand Down Expand Up @@ -1436,6 +1443,8 @@ declare namespace Enumerable {
*/
protected intersectInner(second: Array<T>, comparer: EqualityComparer<T>): IterableIterator<T>;
/** @inheritdoc */
isEmpty(): boolean;
/** @inheritdoc */
join<TInner = T, TOuterKey = TInnerKey | T, TInnerKey = TOuterKey | TInner, TResult = JoinedItems<T, TInner>>(inner: Sequence<TInner>, outerKeySelector?: Selector<T, TOuterKey>, innerKeySelector?: Selector<TInner, TInnerKey>, resultSelector?: (outer: T, inner: TInner) => TResult, keyEqualityComparer?: EqualityComparer<TOuterKey, TInnerKey> | true): IEnumerable<TResult>;
/**
* @see join()
Expand Down Expand Up @@ -1791,6 +1800,14 @@ declare namespace Enumerable {
* @returns {boolean} Is enumerable (sequence) or not.
*/
function isEnumerable<T = any>(val: any): val is IEnumerable<T>;
/**
* Checks if a sequence is (null) or empty.
*
* @param {IEnumerable<T>} seq The sequence to check.
*
* @return {boolean} Is (null) or empty.
*/
function isNullOrEmpty<T>(seq: IEnumerable<T>): seq is (null | IEnumerable<T>);
/**
* Checks if a value can be used as enumerable (sequence).
*
Expand All @@ -1799,6 +1816,22 @@ declare namespace Enumerable {
* @return {boolean} Is sequence or not.
*/
function isSequence<T = any>(val: any): val is Sequence<T>;
/**
* Checks if a sequence is (undefined) / (null) or empty.
*
* @param {IEnumerable<T>} seq The sequence to check.
*
* @return {boolean} Is (undefined), (null) or empty.
*/
function isUndefinedNullOrEmpty<T>(seq: IEnumerable<T>): seq is (undefined | null | IEnumerable<T>);
/**
* Checks if a sequence is (undefined) or empty.
*
* @param {IEnumerable<T>} seq The sequence to check.
*
* @return {boolean} Is (undefined) or empty.
*/
function isUndefinedOrEmpty<T>(seq: IEnumerable<T>): seq is (undefined | IEnumerable<T>);
/**
* Checks if a value represents the NOT_FOUND symbol.
*
Expand Down
48 changes: 48 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,13 @@ namespace Enumerable {
*/
intersect(second: Sequence<T>,
comparer?: EqualityComparer<T> | true): IEnumerable<T>;
/**
* Checks if that sequence is empty or not
* by using the length() method.
*
* @return {boolean} Is Empty or not.
*/
isEmpty(): boolean;
/**
* Correlates the elements of that sequence and another based on matching keys.
*
Expand Down Expand Up @@ -2351,6 +2358,10 @@ namespace Enumerable {
}
}
/** @inheritdoc */
public isEmpty(): boolean {
return this.length() < 1;
}
/** @inheritdoc */
public join<TInner = T, TOuterKey = TInnerKey | T, TInnerKey = TOuterKey | TInner, TResult = JoinedItems<T, TInner>>(
inner: Sequence<TInner>,
outerKeySelector?: Selector<T, TOuterKey>,
Expand Down Expand Up @@ -3654,6 +3665,18 @@ namespace Enumerable {
return false;
} // isEnumerable()

/**
* Checks if a sequence is (null) or empty.
*
* @param {IEnumerable<T>} seq The sequence to check.
*
* @return {boolean} Is (null) or empty.
*/
export function isNullOrEmpty<T>(seq: IEnumerable<T>): seq is (null | IEnumerable<T>) {
return null === seq ||
('undefined' !== typeof seq && seq.isEmpty());
} // isNullOrEmpty<T>()

/**
* Checks if a value can be used as enumerable (sequence).
*
Expand All @@ -3680,6 +3703,31 @@ namespace Enumerable {
return false;
} // isSequence()

/**
* Checks if a sequence is (undefined) / (null) or empty.
*
* @param {IEnumerable<T>} seq The sequence to check.
*
* @return {boolean} Is (undefined), (null) or empty.
*/
export function isUndefinedNullOrEmpty<T>(seq: IEnumerable<T>): seq is (undefined | null | IEnumerable<T>) {
return 'undefined' === typeof seq ||
null === seq ||
seq.isEmpty();
} // isUndefinedNullOrEmpty<T>()

/**
* Checks if a sequence is (undefined) or empty.
*
* @param {IEnumerable<T>} seq The sequence to check.
*
* @return {boolean} Is (undefined) or empty.
*/
export function isUndefinedOrEmpty<T>(seq: IEnumerable<T>): seq is (undefined | IEnumerable<T>) {
return 'undefined' === typeof seq ||
(null !== seq && seq.isEmpty());
} // isUndefinedOrEmpty<T>()

/**
* Checks if a value represents the NOT_FOUND symbol.
*
Expand Down
41 changes: 41 additions & 0 deletions js/enumerable.js

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

53 changes: 53 additions & 0 deletions test/IEnumerable/isEmpty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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 (array)...',
(ctx) => {
for (let i = 0; i < MAX_ARRAY_SIZE; i++) {
if (0 === i % 10) {
ctx.log(`Testing with ${i} elements...`);
}

// fill test array
let arr: any[] = [];
for (let j = 0; j < i; j++) {
arr.push(j);
}

const ACTUAL = Enumerable.from(arr)
.isEmpty();
const EXPECTED = arr.length < 1;

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

0 comments on commit 357c5c5

Please sign in to comment.