Skip to content

Commit

Permalink
feat: add allTrue and allFalse
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-lowrie committed Jun 2, 2023
1 parent 0ac5965 commit 5f65f47
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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) => {
Expand All @@ -211,3 +220,4 @@ exports.forEach = (a, f) => {
}
return exports.forEach(exports.pairs(a), f)
}
exports._forEach = (...args) => exports.make(exports.forEach, ...args)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
18 changes: 18 additions & 0 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 5f65f47

Please sign in to comment.