Skip to content

Commit

Permalink
Added: More pipe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dhershman1 committed Oct 13, 2024
1 parent 214e3d0 commit 0910abe
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/function/pipe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import add from '../../src/number/add.js'
import multiply from '../../src/number/multiply.js'
import pipe from '../../src/function/pipe.js'
import when from '../../src/function/when.js'
import eq from '../../src/function/eq.js'
import compose from '../../src/function/compose.js'
import reduced from '../../src/function/reduced.js'
import test from 'tape'

test('pipe -- Returns value after running pipe', t => {
Expand All @@ -26,3 +30,44 @@ test('pipe -- Handles customized functions', t => {
t.same([10, 11, 12].map(custom), [22, 24, 26])
t.end()
})

test('pipe -- Does not repeat calls', t => {
let count = 0
const custom = pipe([x => {
count++
return x + 1
}, x => {
count++
return x * 2
}])

custom(1)

t.same(count, 2)
t.end()
})

test('pipe -- Supports short circuiting', t => {
const done = compose(reduced)
const list = ['a', 'b', 'c', 'd']
const counts = {}
const piper = pipe([
when(eq('a'), done(x => {
counts.a = (counts.a || 0) + 1
return x + 1
})),
when(eq('b'), done(x => {
counts.b = (counts.b || 0) + 1
return x + 1
})),
when(eq('c'), done(x => {
counts.c = (counts.c || 0) + 1
return x + 1
}))
])
const results = list.map(piper)

t.same(results, ['a1', 'b1', 'c1', 'd'])
t.same(counts, { a: 1, b: 1, c: 1 })
t.end()
})

0 comments on commit 0910abe

Please sign in to comment.