Skip to content

Commit

Permalink
feat(array/average, array/sortAsc, array/sortDesc): new features
Browse files Browse the repository at this point in the history
  • Loading branch information
martinkr committed Nov 22, 2021
1 parent 6e91400 commit e9f4d76
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/array/nth-items.js
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;
10 changes: 10 additions & 0 deletions src/array/sort-asc.js
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;
10 changes: 10 additions & 0 deletions src/array/sort-desc.js
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;
76 changes: 76 additions & 0 deletions test/array/nth-items.spec.js
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);
});





17 changes: 17 additions & 0 deletions test/array/sort-asc.spec.js
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);
});
17 changes: 17 additions & 0 deletions test/array/sort-desc.spec.js
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);
});

0 comments on commit e9f4d76

Please sign in to comment.