Skip to content

Commit

Permalink
fix: Make slice compatible with all types of collections
Browse files Browse the repository at this point in the history
  • Loading branch information
WaldoJeffers committed Jul 20, 2019
1 parent 636e632 commit 2e4b892
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
106 changes: 103 additions & 3 deletions __tests__/slice.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,109 @@
const slice = require('../src/slice')

const object = {
'hello': 'Hello',
'bonsoir': 'Bonsoir',
'elliott': 'Elliot',
'world': 'World',
}
const map = new Map(Object.entries(object))
const array = Object.values(object)
const set = new Set(array)
const newObject = { bonsoir: 'Bonsoir', elliott: 'Elliot' }
const newMap = new Map(Object.entries(newObject))
const newArray = Object.values(newObject)
const newSet = new Set(newArray)


describe('slice', () => {
it('should retrieve a part of the input array', () => {
const words = ['Hello', 'Bonsoir', 'Elliot', 'World']
expect(slice(1, 3, words)).toEqual(['Bonsoir', 'Elliot'])
describe('it should retrieve a slice of the input collection', () => {
it('should retrieve a slice of the input array', () => {
expect(slice(1, 3, array)).toEqual(newArray)
})

it('should retrieve a slice of the input set', () => {
expect(slice(1, 3, set)).toEqual(newSet)
})

it('should retrieve a slice of the input map', () => {
expect(slice(1, 3, map)).toEqual(newMap)
})

it('should retrieve a slice of the input object', () => {
expect(slice(1, 3, object)).toEqual(newObject)
})
})

describe('it should start from the end if the first parameter is negative', () => {
it('should retrieve a slice of the input array', () => {
expect(slice(-3, 3, array)).toEqual(newArray)
})

it('should retrieve a slice of the input set', () => {
expect(slice(-3, 3, set)).toEqual(newSet)
})

it('should retrieve a slice of the input map', () => {
expect(slice(-3, 3, map)).toEqual(newMap)
})

it('should retrieve a slice of the input object', () => {
expect(slice(-3, 3, object)).toEqual(newObject)
})
})

describe('it should start from the end if the second parameter is negative', () => {
it('should retrieve a slice of the input array', () => {
expect(slice(1, -1, array)).toEqual(newArray)
})

it('should retrieve a slice of the input set', () => {
expect(slice(1, -1, set)).toEqual(newSet)
})

it('should retrieve a slice of the input map', () => {
expect(slice(1, -1, map)).toEqual(newMap)
})

it('should retrieve a slice of the input object', () => {
expect(slice(1, -1, object)).toEqual(newObject)
})
})

describe('it should work if both start and end are negative', () => {
it('should retrieve a slice of the input array', () => {
expect(slice(-3, -1, array)).toEqual(newArray)
})

it('should retrieve a slice of the input set', () => {
expect(slice(-3, -1, set)).toEqual(newSet)
})

it('should retrieve a slice of the input map', () => {
expect(slice(-3, -1, map)).toEqual(newMap)
})

it('should retrieve a slice of the input object', () => {
expect(slice(-3, -1, object)).toEqual(newObject)
})
})

describe('it should return an empty collection if no results are found', () => {
it('should retrieve a slice of the input array', () => {
expect(slice(-3, 1, array)).toEqual([])
})

it('should retrieve a slice of the input set', () => {
expect(slice(-3, 1, set)).toEqual(new Set())
})

it('should retrieve a slice of the input map', () => {
expect(slice(-3, 1, map)).toEqual(new Map([]))
})

it('should retrieve a slice of the input object', () => {
expect(slice(-3, 1, object)).toEqual({})
})
})

it('should be a pure function', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/slice.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const curry = require('./curry')
const dump = require('./dump')
const length = require('./length')

const slice = (start, end, array) => array.slice(start, end)
const sliceTransformer = (start, end) => reducer => (acc, item, key, collection, index) => index >= start && index < end ? reducer(acc, item, key, collection, index) : acc

const slice = (start, end, collection) => typeof collection.__proto__.slice === 'function' ? collection.slice(start, end) : dump(sliceTransformer(start < 0 ? start + length(collection) : start, end < 0 ? end + length(collection) : end), collection)

module.exports = curry(slice)

0 comments on commit 2e4b892

Please sign in to comment.