From e9f4d76dfba5d04fa26bab8c564248af620d4a59 Mon Sep 17 00:00:00 2001 From: Martin Krause Date: Mon, 22 Nov 2021 13:42:14 +0200 Subject: [PATCH] feat(array/average, array/sortAsc, array/sortDesc): new features --- src/array/nth-items.js | 9 +++++ src/array/sort-asc.js | 10 +++++ src/array/sort-desc.js | 10 +++++ test/array/nth-items.spec.js | 76 ++++++++++++++++++++++++++++++++++++ test/array/sort-asc.spec.js | 17 ++++++++ test/array/sort-desc.spec.js | 17 ++++++++ 6 files changed, 139 insertions(+) create mode 100644 src/array/nth-items.js create mode 100644 src/array/sort-asc.js create mode 100644 src/array/sort-desc.js create mode 100644 test/array/nth-items.spec.js create mode 100644 test/array/sort-asc.spec.js create mode 100644 test/array/sort-desc.spec.js diff --git a/src/array/nth-items.js b/src/array/nth-items.js new file mode 100644 index 0000000..0bf41aa --- /dev/null +++ b/src/array/nth-items.js @@ -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; diff --git a/src/array/sort-asc.js b/src/array/sort-asc.js new file mode 100644 index 0000000..9cb103a --- /dev/null +++ b/src/array/sort-asc.js @@ -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; diff --git a/src/array/sort-desc.js b/src/array/sort-desc.js new file mode 100644 index 0000000..d45516a --- /dev/null +++ b/src/array/sort-desc.js @@ -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; diff --git a/test/array/nth-items.spec.js b/test/array/nth-items.spec.js new file mode 100644 index 0000000..a2795a1 --- /dev/null +++ b/test/array/nth-items.spec.js @@ -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); +}); + + + + + diff --git a/test/array/sort-asc.spec.js b/test/array/sort-asc.spec.js new file mode 100644 index 0000000..1c09373 --- /dev/null +++ b/test/array/sort-asc.spec.js @@ -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); +}); diff --git a/test/array/sort-desc.spec.js b/test/array/sort-desc.spec.js new file mode 100644 index 0000000..bfc5761 --- /dev/null +++ b/test/array/sort-desc.spec.js @@ -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); +});