Skip to content

Commit 5f65f47

Browse files
committed
feat: add allTrue and allFalse
1 parent 0ac5965 commit 5f65f47

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ exports._isEmpty = a => exports.make(exports.isEmpty, a)
190190
exports.isNotEmpty = a => !exports.isEmpty(a)
191191
exports._isNotEmpty = a => exports.make(exports.isNotEmpty, a)
192192

193+
exports.allTrue = arr => arr.every(exports.isTrue)
194+
exports._allTrue = (...args) => exports.make(exports.allTrue, ...args)
195+
196+
exports.allFalse = arr => arr.every(exports.isFalse)
197+
exports._allFalse = (...args) => exports.make(exports.allFalse, ...args)
198+
193199
// Lazy things
194200
exports.any = (pred, arr) => {
195201
for (let i = 0; i < arr.length; i++) {
@@ -200,9 +206,12 @@ exports.any = (pred, arr) => {
200206
}
201207
return false
202208
}
209+
exports._any = (...args) => exports.make(exports.any, ...args)
203210

204211
exports.anyTrue = arr => exports.any(exports.isTrue, arr)
212+
exports._anyTrue = (...args) => exports.make(exports.anyTrue, ...args)
205213
exports.anyFalse = arr => exports.any(exports.isFalse, arr)
214+
exports._anyFalse = (...args) => exports.make(exports.anyFalse, ...args)
206215

207216
// Loops/Collections
208217
exports.forEach = (a, f) => {
@@ -211,3 +220,4 @@ exports.forEach = (a, f) => {
211220
}
212221
return exports.forEach(exports.pairs(a), f)
213222
}
223+
exports._forEach = (...args) => exports.make(exports.forEach, ...args)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@andres-lowrie/declarative",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Set of functions for more declarative programming",
55
"main": "index.js",
66
"directories": {

tests/main.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,24 @@ const cases = [
572572
assert.strictEqual(true, mod._isNotEmpty([1])())
573573
assert.strictEqual(true, mod._isNotEmpty({ a: 1 })())
574574
}),
575+
test('allTrue: It should return true only if all items are true', () => {
576+
assert.strictEqual(true, mod.allTrue([true, 1 === 1]))
577+
assert.strictEqual(false, mod.allTrue([false, 1 === 0]))
578+
}),
579+
test('_allTrue', () => {
580+
assert.strictEqual(true, mod._allTrue([true, 1 === 1])())
581+
assert.strictEqual(false, mod._allTrue([false, 1 === 0])())
582+
}),
583+
test('allFalse: It should return true only if all items are false', () => {
584+
assert.strictEqual(false, mod.allFalse([true, 1 === 1]))
585+
assert.strictEqual(true, mod.allFalse([false, 1 === 0]))
586+
assert.strictEqual(false, mod.allFalse([false, 1 === 1]))
587+
}),
588+
test('_allFalse', () => {
589+
assert.strictEqual(false, mod._allFalse([true, 1 === 1])())
590+
assert.strictEqual(true, mod._allFalse([false, 1 === 0])())
591+
assert.strictEqual(false, mod._allFalse([false, 1 === 1])())
592+
}),
575593

576594
// Lazy things
577595
test('any: It should return first function in array that passes the predicate', () => {

0 commit comments

Comments
 (0)