Skip to content

Commit

Permalink
fix: Fix filter implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
WaldoJeffers committed Jul 13, 2019
1 parent 821e1ea commit f064716
Show file tree
Hide file tree
Showing 4 changed files with 6,240 additions and 13 deletions.
51 changes: 51 additions & 0 deletions __tests__/del.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const del = require('../src/del')

const object = { three: 3, one: 1, four: 4 }
const newObject = { three: 3, four: 4 }

const array = Object.values(object)
const newArray = Object.values(newObject)

const set = new Set(array)
const newSet = new Set(newArray)

const map = new Map(Object.entries(object))
const newMap = new Map(Object.entries(newObject))

describe('del', () => {
describe('it should delete an existing item in a collection', () => {
it('should delete an item from an array by its key', () => {
expect(del(1, array)).toEqual(newArray)
})

it('should delete an item from a set by its key', () => {
expect(del(1, set)).toEqual(newSet)
})

it('should delete an item from an object by its key', () => {
expect(del('one', object)).toEqual(newObject)
})

it('should object an item from a map by its key', () => {
expect(del('one', map)).toEqual(newMap)
})
})

describe('it should not delete a non existing item in a collection', () => {
it('should not delete an item from an array by its key', () => {
expect(del(6, array)).toEqual(array)
})

it('should not delete an item from a set by its key', () => {
expect(del(6, set)).toEqual(set)
})

it('should not delete an item from an object by its key', () => {
expect(del('two', object)).toEqual(object)
})

it('should object an item from a map by its key', () => {
expect(del('two', map)).toEqual(map)
})
})
})
29 changes: 20 additions & 9 deletions __tests__/reject.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ const delay = require('../src/delay')
const random = require('../src/random')

const isEven = nb => nb % 2 === 0
const isKeyEqualToDrumsticks = (_, key) => key === 'drumsticks'
const isKeyEqualToDrumsticksAsync = (...args) =>
Promise.resolve(isKeyEqualToDrumsticks(...args))
const isKeyEqualToDrumsticksRandomSync = (_, key) =>
key === 'drumsticks' ? Promise.resolve(true) : false
const isKeyEven = (_, key) => isEven(key)
const isEvenAsync = nb => Promise.resolve(isEven(nb))
const isKeyEvenAsync = (_, key) => isEvenAsync(key)
const isEvenRandomSync = nb => (nb % 2 === 0 ? Promise.resolve(true) : false)
const isEvenRandomSync = nb => (isEven(nb) ? Promise.resolve(true) : false)
const isKeyEvenRandomSync = (_, key) => isEvenRandomSync(key)
const rejectWithRandomDelay = nb => delay(random(100, 1000))(nb).then(isEven)

Expand All @@ -29,9 +34,9 @@ describe('reject', () => {

it('should reject items in collection using a synchronous predicate function on the key', () => {
expect(reject(isKeyEven, array)).toEqual(newArray)
expect(reject(isKeyEven, object)).toEqual(newObject)
expect(reject(isKeyEven, map)).toEqual(newMap)
expect(reject(isKeyEven, set)).toEqual(newSet)
expect(reject(isKeyEqualToDrumsticks, object)).toEqual(newObject)
expect(reject(isKeyEqualToDrumsticks, map)).toEqual(newMap)
})

it('should reject values in a collection using an asynchronous predicate function', async () => {
Expand All @@ -43,9 +48,13 @@ describe('reject', () => {

it('should reject values in a collection using an asynchronous predicate function on the key', async () => {
await expect(reject(isKeyEvenAsync, array)).resolves.toEqual(newArray)
await expect(reject(isKeyEvenAsync, object)).resolves.toEqual(newObject)
await expect(reject(isKeyEvenAsync, set)).resolves.toEqual(newSet)
await expect(reject(isKeyEvenAsync, map)).resolves.toEqual(newMap)
await expect(reject(isKeyEqualToDrumsticksAsync, map)).resolves.toEqual(
newMap
)
await expect(reject(isKeyEqualToDrumsticksAsync, object)).resolves.toEqual(
newObject
)
})

it('should reject values in a collection using a predicate function which is sometimes synchronous and sometimes asynchronous', async () => {
Expand All @@ -57,11 +66,13 @@ describe('reject', () => {

it('should reject values in a collection using a predicate function on the key which is sometimes synchronous and sometimes asynchronous', async () => {
await expect(reject(isKeyEvenRandomSync, array)).resolves.toEqual(newArray)
await expect(reject(isKeyEvenRandomSync, object)).resolves.toEqual(
newObject
)
await expect(reject(isKeyEvenRandomSync, set)).resolves.toEqual(newSet)
await expect(reject(isKeyEvenRandomSync, map)).resolves.toEqual(newMap)
await expect(
reject(isKeyEqualToDrumsticksRandomSync, object)
).resolves.toEqual(newObject)
await expect(
reject(isKeyEqualToDrumsticksRandomSync, map)
).resolves.toEqual(newMap)
})

it('should reject values in a collection and keep the same order', async () => {
Expand Down
Loading

0 comments on commit f064716

Please sign in to comment.