-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(array/average, array/sortAsc, array/sortDesc): new features
- Loading branch information
Showing
6 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Returns a new array which contains every n-th item of the original array. | ||
* @param {Array} arr | ||
* @param {Number} pos, position to look for | ||
* @returns {Array} | ||
*/ | ||
const fn = (arr, pos) => arr.filter((arr, index) => index % pos === pos - 1); | ||
|
||
export default fn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Returns a new array sorted by ascending order (Numbers). | ||
* Beware of JavaScript's Automatic Type Conversion if your `Array` contains something else than `Numbers`. | ||
* @param {Array} arr | ||
* @returns {Array} | ||
*/ | ||
|
||
const fn = arr => [...arr].sort((a, b) => a - b); | ||
|
||
export default fn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Returns a new array sorted by descending order (Numbers). | ||
* Beware of JavaScript's Automatic Type Conversion if your `Array` contains something else than `Numbers`. | ||
* @param {Array} arr | ||
* @returns {Array} | ||
*/ | ||
|
||
const fn = arr => [...arr].sort((a, b) => a - b).reverse(); | ||
|
||
export default fn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { nthItems } from '../../src/array'; | ||
const test = require('ava'); | ||
|
||
let input; | ||
test.beforeEach(t => { | ||
input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; | ||
}) | ||
|
||
test('returns every item at n-th = 1', t => { | ||
let position = 1; | ||
let expectation = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
test('returns every item at n-th = 2', t => { | ||
let position = 2; | ||
let expectation = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
test('returns every item at n-th = 3', t => { | ||
let position = 3; | ||
let expectation = [3, 6, 9, 12, 15, 18]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
test('returns every item at n-th = 4', t => { | ||
let position = 4; | ||
let expectation = [4, 8, 12, 16, 20]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
test('returns every item at n-th = 5', t => { | ||
let position = 5; | ||
let expectation = [5, 10, 15, 20]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
test('returns every item at n-th = 6', t => { | ||
let position = 6; | ||
let expectation = [6, 12, 18]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
test('returns every item at n-th = 7', t => { | ||
let position = 7; | ||
let expectation = [7, 14]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
|
||
test('returns every item at n-th = 8', t => { | ||
let position = 8; | ||
let expectation = [8, 16]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
|
||
|
||
test('returns every item at n-th = 9', t => { | ||
let position = 9; | ||
let expectation = [9, 18]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
|
||
test('returns every item at n-th = 10', t => { | ||
let position = 10; | ||
let expectation = [10, 20]; | ||
t.deepEqual(nthItems(input, position), expectation); | ||
}); | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { sortAsc } from '../../src/array'; | ||
const test = require('ava'); | ||
|
||
let input; | ||
test.beforeEach(t => { | ||
input = [4, 2, 1, 0, 3]; | ||
}) | ||
|
||
test('returns a new array array', t => { | ||
let arr = [1, 2, 3, 4]; | ||
t.not(sortAsc(arr), arr); | ||
}); | ||
|
||
test('returns a new array array sorted by ascending order', t => { | ||
let expectation = [0, 1, 2, 3, 4]; | ||
t.deepEqual(sortAsc(input), expectation); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { sortDesc } from '../../src/array'; | ||
const test = require('ava'); | ||
|
||
let input; | ||
test.beforeEach(t => { | ||
input = [4, 2, 1, 0, 3]; | ||
}) | ||
|
||
test('returns a new array array', t => { | ||
let arr = [4, 3, 2, 1, 0]; | ||
t.not(sortDesc(arr), arr); | ||
}); | ||
|
||
test('returns a new array array sorted by ascending order', t => { | ||
let expectation = [4, 3, 2, 1, 0]; | ||
t.deepEqual(sortDesc(input), expectation); | ||
}); |