From 5f65f47b5f548c3028a771e8aa933f9456dc5817 Mon Sep 17 00:00:00 2001 From: Andres Lowrie Date: Thu, 1 Jun 2023 21:50:06 -0500 Subject: [PATCH] feat: add allTrue and allFalse --- index.js | 10 ++++++++++ package.json | 2 +- tests/main.js | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 10424aa..e8028ae 100644 --- a/index.js +++ b/index.js @@ -190,6 +190,12 @@ exports._isEmpty = a => exports.make(exports.isEmpty, a) exports.isNotEmpty = a => !exports.isEmpty(a) exports._isNotEmpty = a => exports.make(exports.isNotEmpty, a) +exports.allTrue = arr => arr.every(exports.isTrue) +exports._allTrue = (...args) => exports.make(exports.allTrue, ...args) + +exports.allFalse = arr => arr.every(exports.isFalse) +exports._allFalse = (...args) => exports.make(exports.allFalse, ...args) + // Lazy things exports.any = (pred, arr) => { for (let i = 0; i < arr.length; i++) { @@ -200,9 +206,12 @@ exports.any = (pred, arr) => { } return false } +exports._any = (...args) => exports.make(exports.any, ...args) exports.anyTrue = arr => exports.any(exports.isTrue, arr) +exports._anyTrue = (...args) => exports.make(exports.anyTrue, ...args) exports.anyFalse = arr => exports.any(exports.isFalse, arr) +exports._anyFalse = (...args) => exports.make(exports.anyFalse, ...args) // Loops/Collections exports.forEach = (a, f) => { @@ -211,3 +220,4 @@ exports.forEach = (a, f) => { } return exports.forEach(exports.pairs(a), f) } +exports._forEach = (...args) => exports.make(exports.forEach, ...args) diff --git a/package.json b/package.json index 14c4025..3af1989 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@andres-lowrie/declarative", - "version": "1.0.2", + "version": "1.0.3", "description": "Set of functions for more declarative programming", "main": "index.js", "directories": { diff --git a/tests/main.js b/tests/main.js index d3e468d..1dd9780 100644 --- a/tests/main.js +++ b/tests/main.js @@ -572,6 +572,24 @@ const cases = [ assert.strictEqual(true, mod._isNotEmpty([1])()) assert.strictEqual(true, mod._isNotEmpty({ a: 1 })()) }), + test('allTrue: It should return true only if all items are true', () => { + assert.strictEqual(true, mod.allTrue([true, 1 === 1])) + assert.strictEqual(false, mod.allTrue([false, 1 === 0])) + }), + test('_allTrue', () => { + assert.strictEqual(true, mod._allTrue([true, 1 === 1])()) + assert.strictEqual(false, mod._allTrue([false, 1 === 0])()) + }), + test('allFalse: It should return true only if all items are false', () => { + assert.strictEqual(false, mod.allFalse([true, 1 === 1])) + assert.strictEqual(true, mod.allFalse([false, 1 === 0])) + assert.strictEqual(false, mod.allFalse([false, 1 === 1])) + }), + test('_allFalse', () => { + assert.strictEqual(false, mod._allFalse([true, 1 === 1])()) + assert.strictEqual(true, mod._allFalse([false, 1 === 0])()) + assert.strictEqual(false, mod._allFalse([false, 1 === 1])()) + }), // Lazy things test('any: It should return first function in array that passes the predicate', () => {