Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 02fedc8

Browse files
jzabalaKent C. Dodds
authored and
Kent C. Dodds
committed
feat(tail): add tail function (#177)
Add tail function that returns all but the first element of array. * Return [] for null and undefined Closes #176
1 parent e4f1b7f commit 02fedc8

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Diff for: src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import getQueryStringValue from './get-query-string-value'
6363
import isLeapYear from './leap-year'
6464
import removeElementFromArray from './removeElementFromArray'
6565
import generatePassword from './generate-password'
66+
import tail from './tail'
6667

6768
export {
6869
reverseArrayInPlace,
@@ -130,4 +131,5 @@ export {
130131
isLeapYear,
131132
removeElementFromArray,
132133
generatePassword,
134+
tail,
133135
}

Diff for: src/tail.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default tail
2+
3+
/**
4+
* Original Source: https://stackoverflow.com/a/35361274/5801753
5+
*
6+
* This function gets all but the first element of array
7+
*
8+
* @param {Array} array - The array to get the tail from
9+
* @return {Array} - The array without the first element
10+
*/
11+
function tail(array) {
12+
return Boolean(array) ? array.slice(1) : []
13+
}

Diff for: test/tail.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import test from 'ava'
2+
import {tail} from '../src'
3+
4+
test('returns empty array for null', t => {
5+
const expected = []
6+
const actual = tail(null)
7+
t.deepEqual(actual, expected)
8+
})
9+
10+
test('returns empty array for undefined', t => {
11+
const expected = []
12+
const actual = tail(undefined)
13+
t.deepEqual(actual, expected)
14+
})
15+
16+
test('returns empty array for empty array', t => {
17+
const expected = []
18+
const actual = tail([])
19+
t.deepEqual(actual, expected)
20+
})
21+
22+
test('returns the tail of array', t => {
23+
const original = [1, 2, 3, 4, 5]
24+
const expected = [2, 3, 4, 5]
25+
const actual = tail(original)
26+
t.deepEqual(actual, expected)
27+
})

0 commit comments

Comments
 (0)