From ed179f44c69b3d9b7a27597a20094bf0e0070a90 Mon Sep 17 00:00:00 2001 From: Guillaume ARM Date: Wed, 17 Jan 2018 16:43:48 +0100 Subject: [PATCH] feat: add isNonEmptyArray Ref #279 --- src/index.d.ts | 5 +++++ src/index.js | 1 + src/isNonEmptyArray.js | 29 +++++++++++++++++++++++++++++ test/isNonEmptyArray.js | 23 +++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/isNonEmptyArray.js create mode 100644 test/isNonEmptyArray.js diff --git a/src/index.d.ts b/src/index.d.ts index 38e9c2c43c..49cbf007f8 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -53,6 +53,11 @@ declare namespace RamdaAdjunct { */ isNotArray(val: any): boolean; + /** + * Checks if input value is a non empty `Array`. + */ + isNonEmptyArray(val: any): val is Array; + /** * Checks if input value is complement of `Boolean`. */ diff --git a/src/index.js b/src/index.js index 0daf9b6afd..b2b2ac2487 100644 --- a/src/index.js +++ b/src/index.js @@ -10,6 +10,7 @@ export { default as isNotNull } from './isNotNull'; export { default as isNotNil } from './isNotNil'; export { default as isArray } from './isArray'; export { default as isNotArray } from './isNotArray'; +export { default as isNonEmptyArray } from './isNonEmptyArray'; export { default as isBoolean } from './isBoolean'; export { default as isNotBoolean } from './isNotBoolean'; export { default as isNilOrEmpty } from './isNilOrEmpty'; diff --git a/src/isNonEmptyArray.js b/src/isNonEmptyArray.js new file mode 100644 index 0000000000..9143df8126 --- /dev/null +++ b/src/isNonEmptyArray.js @@ -0,0 +1,29 @@ +import { both } from 'ramda'; + +import isNotEmpty from './isNotEmpty'; +import isArray from './isArray'; + +/** + * Checks if input value is not an empty `Array`. + * + * @func isNonEmptyArray + * @memberOf RA + * @since {@link https://char0n.github.io/ramda-adjunct/2.4.0|v2.4.0} + * @category Type + * @sig * -> Boolean + * @param {*} val The value to test + * @return {Boolean} + * @see {@link RA.isEmptyArray|isEmptyArray} + * @example + * + * RA.isNonEmptyArray([42]); // => true + * RA.isNonEmptyArray([]); // => false + * RA.isNonEmptyArray({}); // => false + * RA.isNonEmptyArray(null); // => false + * RA.isNonEmptyArray(undefined); // => false + * RA.isNonEmptyArray(42); // => false + * RA.isNonEmptyArray('42'); // => false + */ +const isNonEmptyArray = both(isNotEmpty, isArray); + +export default isNonEmptyArray; diff --git a/test/isNonEmptyArray.js b/test/isNonEmptyArray.js new file mode 100644 index 0000000000..a106d9ee68 --- /dev/null +++ b/test/isNonEmptyArray.js @@ -0,0 +1,23 @@ +import * as RA from '../src/index'; +import eq from './shared/eq'; + +describe('isNonEmptyArray', function() { + it('tests a value to be a non empty `Array`', function() { + eq(RA.isNonEmptyArray([42]), true); + eq(RA.isNonEmptyArray(new Array('content')), true); + + eq(RA.isNonEmptyArray([]), false); + eq(RA.isNonEmptyArray(new Array()), false); + eq(RA.isNonEmptyArray(new Array(42)), false); + eq(RA.isNonEmptyArray(Array.prototype), false); + eq(RA.isNonEmptyArray(void 0), false); + eq(RA.isNonEmptyArray({}), false); + eq(RA.isNonEmptyArray(null), false); + eq(RA.isNonEmptyArray(undefined), false); + eq(RA.isNonEmptyArray(42), false); + eq(RA.isNonEmptyArray('Array'), false); + eq(RA.isNonEmptyArray(true), false); + eq(RA.isNonEmptyArray(false), false); + eq(RA.isNonEmptyArray({ __proto__: Array.prototype }), false); + }); +});