Comparing version 0.19.0 to 0.19.2
{ | ||
"name": "absurdum", | ||
"version": "0.19.0", | ||
"version": "0.19.2", | ||
"description": "Reducio Ad Absurdum - The Riduculous Application of Reduce", | ||
@@ -18,5 +18,6 @@ "keywords": [ | ||
"scripts": { | ||
"docs": "node --experimental-modules docs.config.js", | ||
"test": "find . -wholename '**/*.spec.js' -not -path './node_modules/*' | xargs -n1 node --experimental-modules", | ||
"test:watch": "npx chokidar-cli -i 'node_modules' '**/*.js' -c 'npm test'", | ||
"lint": "npx semistandard", | ||
"lint": "semistandard", | ||
"package": "npx rimraf package && npm pack | tail -n 1 | xargs tar -xf", | ||
@@ -28,2 +29,5 @@ "preversion": "npm test && npm run lint", | ||
"devDependencies": { | ||
"docdown": "github:evanplaice/docdown", | ||
"glob": "^7.1.4", | ||
"semistandard": "^14.2.0", | ||
"tape": "^4.8.0" | ||
@@ -36,5 +40,6 @@ }, | ||
"ignore": [ | ||
"index.js" | ||
"index.js", | ||
"docs.config.js" | ||
] | ||
} | ||
} |
@@ -5,2 +5,6 @@ [![npm](https://img.shields.io/npm/v/absurdum.svg)](https://www.npmjs.com/package/absurdum) | ||
> "if all you have is a hammer, everything looks like a nail" - Maslow's Hammer | ||
In this package, [Array.prototype.reduce][] is the hammer | ||
## Why the Funky Name? | ||
@@ -12,13 +16,18 @@ | ||
It is often said that of the big trio **Map**, **Filter**, and **Reduce**, that reduce is by far the most flexible. In fact, it's trivial to implement both map and filter using reduce. | ||
It is often said that of the big trio `map`, `filter`, and `reduce`, the first two are redundant. In fact, it's trivial to implement both `map` and `filter` using `reduce`. | ||
This library exists to take that concept to an absurd extreme by experimenting with leveraging reduce to produce as many different types functional transforms as possible. | ||
This library exists to take that concept to an absurd extreme by using `reduce` to implement as many different types of functional transforms as possible. | ||
## What's the Point? | ||
Who cares, it's fun. It's good practice. It's a challenging approach to practice transforming data in a purely function manner. At some point, Javscript will include the pipeline operator and these may actually become useful. If you're up for the challenge give it a try. | ||
- It's a challenge | ||
- It's good practice | ||
- It forces you to code/think in a functional manner | ||
- Who cares, it's fun | ||
...and just maybe, new patterns/practice will emerge from pushing `reduce` to its limits. | ||
## Operator(s) Documentation | ||
### [Arrays][arrays] | ||
### Arrays | ||
@@ -38,22 +47,19 @@ - [chunk][arrays.chunk] | ||
[arrays]: ./docs/arrays.md | ||
[arrays.chunk]: ./docs/arrays.md#arrayschunkarray-size1 | ||
[arrays.compact]: ./docs/arrays.md#arrayscompactarray | ||
[arrays.concat]: ./docs/arrays.md#arraysconcatarrays | ||
[arrays.difference]: ./docs/arrays.md#arraysdifferencearray-values | ||
[arrays.drop]: ./docs/arrays.md#arraysdroparray-n | ||
[arrays.dropRight]: ./docs/arrays.md#arraysdroprightarray-n--1 | ||
[arrays.fill]: ./docs/arrays.md#arraysfilllarray-value-start--0-end--arraylength-1 | ||
[arrays.filter]: ./docs/arrays.md#arraysfilterarray-predicate | ||
[arrays.map]: ./docs/arrays.md#arraysmaparray-func | ||
[arrays.reduceRight]: ./docs/arrays.md#arraysreducerightarray-reducer----array-initial-- | ||
[arrays.reverse]: ./docs/arrays.md#arraysreversearray | ||
[arrays.tap]: ./docs/arrays.md#arraystaparray-func | ||
[arrays.chunk]: ./docs/arrays/chunk.md | ||
[arrays.compact]: ./docs/arrays/compact.md | ||
[arrays.concat]: ./docs/arrays/concat.md | ||
[arrays.difference]: ./docs/arrays/difference.md | ||
[arrays.drop]: ./docs/arrays/drop.md | ||
[arrays.dropRight]: ./docs/arrays/dropRight.md | ||
[arrays.fill]: ./docs/arrays/fill.md | ||
[arrays.filter]: ./docs/arrays/filter.md | ||
[arrays.map]: ./docs/arrays/map.md | ||
[arrays.reduceRight]: ./docs/arrays/reduceRight.md | ||
[arrays.reverse]: ./docs/arrays/reverse.md | ||
[arrays.tap]: ./docs/arrays/tap.md | ||
### [Objects][objects] | ||
### Objects | ||
[objects]: ./docs/objects.md | ||
### Strings | ||
### [Strings][strings] | ||
- [endsWith][strings.endswith] | ||
@@ -65,13 +71,11 @@ - [padEnd][strings.padEnd] | ||
[strings]: ./docs/strings.md | ||
[strings.endswith]: ./docs/strings.md#stringsendswithstring-substr | ||
[strings.padEnd]: ./docs/strings.md#stringspadendstring-length-substr | ||
[strings.padStart]: ./docs/strings.md#stringspadstartstring-length-substr | ||
[strings.reverse]: ./docs/strings.md#stringsreversestring | ||
[strings.startswith]: ./docs/strings.md#stringsstartswithstring-substr | ||
[strings.endswith]: ./docs/strings/endsWith.md | ||
[strings.padEnd]: ./docs/strings/padEnd.md | ||
[strings.padStart]: ./docs/strings/padStart.md | ||
[strings.reverse]: ./docs/strings/reverse.md | ||
[strings.startswith]: ./docs/strings/startsWith.md | ||
### [HTML][html] | ||
### HTML | ||
[html]: ./docs/html.md | ||
[Array.prototype.reduce]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | ||
[wikipedia]: https://en.wikipedia.org/wiki/Reductio_ad_absurdum | ||
@@ -78,0 +82,0 @@ [operator]: https://github.com/evanplaice/absurdum/issues/new?title=Operator([operator])&template=OPERATOR_TEMPLATE.md&labels=enhancement,operator |
@@ -1,2 +0,14 @@ | ||
export const chunk = (array, size = 1) => { | ||
/** | ||
* Splits an array up into an array of equal size chunks. | ||
* | ||
* @param {Array} array The input array | ||
* @param {number} [size=1] The size of each chunk | ||
* @returns {Array} An array of chunk arrays | ||
* | ||
* @example | ||
* const result = arrays.chunk([1, 2, 3, 4, 5], 2); | ||
* console.log(result); | ||
* // > [[1, 2], [3, 4], [5]] | ||
*/ | ||
function chunk (array, size = 1) { | ||
let chunk = []; | ||
@@ -14,2 +26,4 @@ return array.reduce((acc, curr, idx, arr) => { | ||
}, []); | ||
}; | ||
} | ||
export { chunk }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.chunk(array) - should return a chunk for each item in the array`, t => { | ||
test('arrays.chunk(array) - should return a chunk for each item in the array', t => { | ||
const expect = [[1], [2], [3], [4]]; | ||
@@ -15,3 +15,3 @@ const result = arrays.chunk([1, 2, 3, 4]); | ||
test(`arrays.chunk(array, size) - should return an array of chunks of the specified size`, t => { | ||
test('arrays.chunk(array, size) - should return an array of chunks of the specified size', t => { | ||
const expect = [[1, 2], [3, 4], [5]]; | ||
@@ -18,0 +18,0 @@ const result = arrays.chunk([1, 2, 3, 4, 5], 2); |
@@ -1,2 +0,13 @@ | ||
export const compact = (array) => { | ||
/** | ||
* Returns an array with all falsy `[false, null, 0, "", undefined, NaN]` values removed. | ||
* | ||
* @param {Array} array | ||
* @returns {Array} the compacted array | ||
* | ||
* @example | ||
* const result = arrays.compact([1, false, 2, null, 3, 0, 4, "", 5, undefined, 6, NaN]); | ||
* console.log(result); | ||
* > [1, 2, 3, 4, 5, 6] | ||
*/ | ||
function compact (array) { | ||
return array.reduce((acc, curr) => { | ||
@@ -17,2 +28,4 @@ if (isNaN(curr)) { return acc; } | ||
}, []); | ||
}; | ||
} | ||
export { compact }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.compact(array) - should return the array with false removed`, t => { | ||
test('arrays.compact(array) - should return the array with false removed', t => { | ||
const expect = [1, 3, 4]; | ||
@@ -15,3 +15,3 @@ const result = arrays.compact([1, false, 3, 4]); | ||
test(`arrays.compact(array) - should return the array with null removed`, t => { | ||
test('arrays.compact(array) - should return the array with null removed', t => { | ||
const expect = [1, 3, 4]; | ||
@@ -27,3 +27,3 @@ const result = arrays.compact([1, null, 3, 4]); | ||
test(`arrays.compact(array) - should return the array with 0 removed`, t => { | ||
test('arrays.compact(array) - should return the array with 0 removed', t => { | ||
const expect = [1, 3, 4]; | ||
@@ -39,3 +39,3 @@ const result = arrays.compact([1, 0, 3, 4]); | ||
test(`arrays.compact(array) - should return the array with "" removed`, t => { | ||
test('arrays.compact(array) - should return the array with "" removed', t => { | ||
const expect = [1, 3, 4]; | ||
@@ -51,3 +51,3 @@ const result = arrays.compact([1, 0, 3, 4]); | ||
test(`arrays.compact(array) - should return the array with undefined removed`, t => { | ||
test('arrays.compact(array) - should return the array with undefined removed', t => { | ||
const expect = [1, 3, 4]; | ||
@@ -63,3 +63,3 @@ const result = arrays.compact([1, undefined, 3, 4]); | ||
test(`arrays.compact(array) - should return the array with NaN removed`, t => { | ||
test('arrays.compact(array) - should return the array with NaN removed', t => { | ||
const expect = [1, 3, 4]; | ||
@@ -66,0 +66,0 @@ const result = arrays.compact([1, NaN, 3, 4]); |
@@ -1,4 +0,15 @@ | ||
export const concat = (...arrays) => { | ||
/** | ||
* Concat takes any number or arrays or values as input. The arrays can be any level of depth. The output will be a single, one-dimensional array containing all the values. | ||
* | ||
* @param {Array} arrays | ||
* @returns {Array} The concatenated array | ||
* | ||
* @example | ||
* const result = arrays.concat([1], 2, [3], [[4]]); | ||
* console.log(result); | ||
* > [1, 2, 3, 4] | ||
*/ | ||
function concat (...arrays) { | ||
return flatten(arrays, []); | ||
}; | ||
} | ||
@@ -15,1 +26,3 @@ function flatten (array, initial = []) { | ||
} | ||
export { concat }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.concat(...arrays) - should return an array with all inputs flattened and concatenated`, t => { | ||
test('arrays.concat(...arrays) - should return an array with all inputs flattened and concatenated', t => { | ||
const expect = [1, 2, 3, 4]; | ||
@@ -6,0 +6,0 @@ const result = arrays.concat([1], 2, [3], [[4]]); |
@@ -1,2 +0,14 @@ | ||
export const difference = (array, values) => { | ||
/** | ||
* Returns an array containing the difference of the input array vs the specified values. | ||
* | ||
* @param {Array} array | ||
* @param {Array} values | ||
* @returns {Array} an array containing the difference | ||
* | ||
* @example | ||
* const result = arrays.difference([2, 1], [2, 3]); | ||
* console.log(result); | ||
* > [1] | ||
*/ | ||
function difference (array, values) { | ||
values = new Set(values); | ||
@@ -10,2 +22,4 @@ | ||
}, []); | ||
}; | ||
} | ||
export { difference }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.difference(array, values) - should return the difference between the input array and included values`, t => { | ||
test('arrays.difference(array, values) - should return the difference between the input array and included values', t => { | ||
const expect = [1]; | ||
@@ -6,0 +6,0 @@ const result = arrays.difference([2, 1], [2, 3]); |
@@ -1,2 +0,14 @@ | ||
export const drop = (array, n = 1) => { | ||
/** | ||
* Returns an array with n items dropped from the beginning. | ||
* | ||
* @param {Array} array | ||
* @param {number} [n=1] | ||
* @returns {Array} The input array sans the dropped items | ||
* | ||
* @example | ||
* const result = arrays.drop([1, 2, 3], 2); | ||
* console.log(result); | ||
* > [3] | ||
*/ | ||
function drop (array, n = 1) { | ||
return array.reduce((acc, curr) => { | ||
@@ -10,2 +22,4 @@ if (n > 0) { | ||
}, []); | ||
}; | ||
} | ||
export { drop }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.drop(array) - should return an array w/ the first item dropped when n isn't specified`, t => { | ||
test('arrays.drop(array) - should return an array w/ the first item dropped when n is not specified', t => { | ||
const expect = [2, 3]; | ||
@@ -15,3 +15,3 @@ const result = arrays.drop([1, 2, 3]); | ||
test(`arrays.drop(array, n) - should return an array with n items dropped from the beginning`, t => { | ||
test('arrays.drop(array, n) - should return an array with n items dropped from the beginning', t => { | ||
const expect = [3]; | ||
@@ -27,3 +27,3 @@ const result = arrays.drop([1, 2, 3], 2); | ||
test(`arrays.drop(array, n) - should return an empty array when n is larger than array.length`, t => { | ||
test('arrays.drop(array, n) - should return an empty array when n is larger than array.length', t => { | ||
const expect = []; | ||
@@ -39,3 +39,3 @@ const result = arrays.drop([1, 2, 3], 5); | ||
test(`arrays.drop(array, n) - should return the input array when n is 0`, t => { | ||
test('arrays.drop(array, n) - should return the input array when n is 0', t => { | ||
const expect = [1, 2, 3]; | ||
@@ -42,0 +42,0 @@ const result = arrays.drop([1, 2, 3], 0); |
@@ -1,2 +0,14 @@ | ||
export const dropRight = (array, n = 1) => { | ||
/** | ||
* Returns an array with n items dropped from the end. | ||
* | ||
* @param {Array} array | ||
* @param {number} [n=1] | ||
* @returns {Array} The input array sans the dropped items | ||
* | ||
* @example | ||
* const result = arrays.drop([1, 2, 3], 2); | ||
* console.log(result); | ||
* > [1] | ||
*/ | ||
function dropRight (array, n = 1) { | ||
return array.reduce((acc, curr, idx, arr) => { | ||
@@ -10,2 +22,4 @@ if (n > 0) { | ||
}, []); | ||
}; | ||
} | ||
export { dropRight }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.dropRight(array) - should return an array w/ the last item dropped when n isn't specified`, t => { | ||
test('arrays.dropRight(array) - should return an array w/ the last item dropped when n is not specified', t => { | ||
const expect = [1, 2]; | ||
@@ -15,3 +15,3 @@ const result = arrays.dropRight([1, 2, 3]); | ||
test(`arrays.dropRight(array, n) - should return an array with n items dropped from the end`, t => { | ||
test('arrays.dropRight(array, n) - should return an array with n items dropped from the end', t => { | ||
const expect = [1]; | ||
@@ -27,3 +27,3 @@ const result = arrays.dropRight([1, 2, 3], 2); | ||
test(`arrays.dropRight(array, n) - should return an empty array when n is larger than array.length`, t => { | ||
test('arrays.dropRight(array, n) - should return an empty array when n is larger than array.length', t => { | ||
const expect = []; | ||
@@ -39,3 +39,3 @@ const result = arrays.dropRight([1, 2, 3], 5); | ||
test(`arrays.dropRight(array, n) - should return the input array when n is 0`, t => { | ||
test('arrays.dropRight(array, n) - should return the input array when n is 0', t => { | ||
const expect = [1, 2, 3]; | ||
@@ -42,0 +42,0 @@ const result = arrays.dropRight([1, 2, 3], 0); |
@@ -1,2 +0,16 @@ | ||
export const fill = (array, value, start = 0, end) => { | ||
/** | ||
* Fills the array items with the value. Can optionally start and/or end from a specific index | ||
* | ||
* @param {Array} array | ||
* @param {*} value | ||
* @param {number} [start=0] | ||
* @param {number} end | ||
* @returns {Array} The filled array | ||
* | ||
* @example | ||
* const result = arrays.fill([1, 2, 3, 4], 'a', 1, 2); | ||
* console.log(result) | ||
* > [1, 'a', 'a', 4] | ||
*/ | ||
function fill (array, value, start = 0, end) { | ||
if (!end) { | ||
@@ -13,2 +27,4 @@ end = array.length - 1; | ||
}, []); | ||
}; | ||
} | ||
export { fill }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.fill(array, value) - should fill the array with the value`, t => { | ||
test('arrays.fill(array, value) - should fill the array with the value', t => { | ||
const expect = ['a', 'a', 'a', 'a']; | ||
@@ -15,3 +15,3 @@ const result = arrays.fill([1, 2, 3, 4], 'a'); | ||
test(`arrays.fill(array, value, start) - should fill the array with the value from the start index`, t => { | ||
test('arrays.fill(array, value, start) - should fill the array with the value from the start index', t => { | ||
const expect = [1, 2, 'b', 'b']; | ||
@@ -27,3 +27,3 @@ const result = arrays.fill([1, 2, 3, 4], 'b', 2); | ||
test(`arrays.fill(array, value, , end) - should fill the array up to the end index`, t => { | ||
test('arrays.fill(array, value, , end) - should fill the array up to the end index', t => { | ||
const expect = ['c', 'c', 3, 4]; | ||
@@ -30,0 +30,0 @@ const result = arrays.fill([1, 2, 3, 4], 'c', null, 1); |
@@ -1,2 +0,14 @@ | ||
export const filter = (array, predicate) => { | ||
/** | ||
* Filter iterates over an array of values and only outputs values where `predicate = true`. | ||
* | ||
* @param {Array} array | ||
* @param {Function} predicate | ||
* @returns {Array} A filtered array | ||
* | ||
* @example | ||
* const result = arrays.filter([1, 2, 3, 4], (x) => x % 2 === 0); | ||
* console.log(result) | ||
* > [ 2, 4 ] | ||
*/ | ||
function filter (array, predicate) { | ||
return array.reduce((acc, curr) => { | ||
@@ -8,2 +20,4 @@ if (predicate(curr)) { | ||
}, []); | ||
}; | ||
} | ||
export { filter }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.filter(array, predicate) - should filter out values based on the result of the predicate`, t => { | ||
test('arrays.filter(array, predicate) - should filter out values based on the result of the predicate', t => { | ||
const expect = [2, 4]; | ||
@@ -6,0 +6,0 @@ const result = arrays.filter([1, 2, 3, 4], (x) => x % 2 === 0); |
@@ -1,2 +0,14 @@ | ||
export const map = (array, func) => { | ||
/** | ||
* Map iterates over an array of values and applies a function to each value | ||
* | ||
* @param {Array} array | ||
* @param {Function} func | ||
* @returns {Array} The array of mutated values | ||
* | ||
* @example | ||
* const result = arrays.map([1, 2, 3, 4], (x) => x + 2); | ||
* console.log(result) | ||
* > [ 3, 4, 5, 6 ] | ||
*/ | ||
function map (array, func) { | ||
return array.reduce((acc, curr) => { | ||
@@ -6,2 +18,4 @@ acc.push(func(curr)); | ||
}, []); | ||
}; | ||
} | ||
export { map }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.map(array, func) - should map over and apply the function to each value`, t => { | ||
test('arrays.map(array, func) - should map over and apply the function to each value', t => { | ||
const expect = [3, 4, 5, 6]; | ||
@@ -6,0 +6,0 @@ const result = arrays.map([1, 2, 3, 4], (x) => x + 2); |
@@ -1,7 +0,25 @@ | ||
export const reduceRight = (arr, func = () => arr, initial = []) => { | ||
return arr.reduce((accB, currB, idxB) => { | ||
const idxA = arr.length - idxB - 1; | ||
const currA = arr[idxA]; | ||
return func(accB, currA, idxA, arr); | ||
/** | ||
* Reduce right performs reduce in reverse order (ie last->first). The reducer parameter follows the standard API (ie reducer(accumulator, current, index, array)). The initial parameter can be used to set the starting value for the accumulator. | ||
* | ||
* @param {Array} array | ||
* @param {Function} reducer | ||
* @param {*} [initial = []] | ||
* @returns {*} The reduced value | ||
* | ||
* @example | ||
* const result = arrays.reduceRight(['a', 'b', 'c', 'd'], (acc, curr, idx, arr) => { | ||
* acc.push(curr); | ||
* return acc; | ||
* }); | ||
* console.log(result); | ||
* > ['d', 'c', 'b', 'a']; | ||
*/ | ||
function reduceRight (array, reducer = x => array, initial = []) { | ||
return array.reduce((accB, currB, idxB) => { | ||
const idxA = array.length - idxB - 1; | ||
const currA = array[idxA]; | ||
return reducer(accB, currA, idxA, array); | ||
}, initial); | ||
}; | ||
} | ||
export { reduceRight }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.reduceRight(array, func, initial) - should return the input array when no func is specified`, t => { | ||
test('arrays.reduceRight(array, func, initial) - should return the input array when no func is specified', t => { | ||
const expect = [1, 2, 3, 4]; | ||
const result = arrays.reduceRight([ 1, 2, 3, 4 ]); | ||
const result = arrays.reduceRight([1, 2, 3, 4]); | ||
@@ -15,3 +15,3 @@ t.equal(Object.prototype.toString.call(result), '[object Array]', 'return type'); | ||
test(`arrays.reduceRight(array, func) - should reduce the array in reverse order`, t => { | ||
test('arrays.reduceRight(array, func) - should reduce the array in reverse order', t => { | ||
const expect = ['d', 'c', 'b', 'a']; | ||
@@ -30,3 +30,3 @@ const result = arrays.reduceRight(['a', 'b', 'c', 'd'], (acc, curr, idx, arr) => { | ||
test(`arrays.reduceRight(array, func) - should return an array of indexes in reverse order`, t => { | ||
test('arrays.reduceRight(array, func) - should return an array of indexes in reverse order', t => { | ||
const expect = [3, 2, 1, 0]; | ||
@@ -45,3 +45,3 @@ const result = arrays.reduceRight(['a', 'b', 'c', 'd'], (acc, curr, idx, arr) => { | ||
test(`arrays.reduceRight(array, func, initial) - should be able to reduce to a non-array output type`, t => { | ||
test('arrays.reduceRight(array, func, initial) - should be able to reduce to a non-array output type', t => { | ||
const expect = 11; | ||
@@ -48,0 +48,0 @@ const result = arrays.reduceRight([1, 2, 3, 5], (acc, curr) => { |
@@ -1,2 +0,12 @@ | ||
export const reverse = (array) => { | ||
/** | ||
* Returns an array in reverse order | ||
* | ||
* @param {Array} array | ||
* @returns {Array} The array in reversed order | ||
* | ||
* @example | ||
* const result = arrays.reverse([1, 2, 3, 4]); | ||
* > [4, 3, 2, 1] | ||
*/ | ||
function reverse (array) { | ||
return array.reduce((acc, curr, idx, arr) => { | ||
@@ -6,2 +16,4 @@ acc.push(arr[arr.length - idx - 1]); | ||
}, []); | ||
}; | ||
} | ||
export { reverse }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.reverse(array) - should return the input array in reverse order`, t => { | ||
test('arrays.reverse(array) - should return the input array in reverse order', t => { | ||
const expect = [4, 3, 2, 1]; | ||
@@ -6,0 +6,0 @@ const result = arrays.reverse([1, 2, 3, 4]); |
@@ -1,4 +0,22 @@ | ||
export const tap = (array, func) => { | ||
/** | ||
* Applies a function to each element in the array without mutating it | ||
* | ||
* @param {Array} array | ||
* @param {Function} func | ||
* @returns {Array} The source array | ||
* | ||
* @example | ||
* const result = arrays.tap([1, 2, 3, 4], console.log); | ||
* > 1 | ||
* > 2 | ||
* > 3 | ||
* > 4 | ||
* console.log(result); | ||
* > [1, 2, 3, 4] | ||
*/ | ||
function tap (array, func) { | ||
array.forEach(x => func(x)); | ||
return array; | ||
}; | ||
} | ||
export { tap }; |
import test from 'tape'; | ||
import { arrays } from '../../index.js'; | ||
test(`arrays.tap(array, func) - should apply a function to each value in the input array without mutating it`, t => { | ||
test('arrays.tap(array, func) - should apply a function to each value in the input array without mutating it', t => { | ||
const expect = [1, 2, 3, 4]; | ||
@@ -6,0 +6,0 @@ const result = arrays.tap([1, 2, 3, 4], console.log); |
@@ -1,2 +0,19 @@ | ||
export const endsWith = (string, substr = '') => { | ||
/** | ||
* EndsWith tests a string to see if it ends with a substring | ||
* | ||
* @param {string} string | ||
* @param {string} [substr=''] | ||
* @returns {boolean} | ||
* | ||
* @example | ||
* const result = strings.endsWith('This sentence ends with', 'with'); | ||
* console.log(result); | ||
* > true | ||
* | ||
* @example | ||
* const result = strings.endsWith('This sentence does not end with', 'nope'); | ||
* console.log(result); | ||
* > false | ||
*/ | ||
function endsWith (string, substr = '') { | ||
return string.split('').reduce((acc, curr, idx, arr) => { | ||
@@ -15,2 +32,4 @@ // exit early on mismatch | ||
}, null); | ||
}; | ||
} | ||
export { endsWith }; |
import test from 'tape'; | ||
import { strings } from '../../index.js'; | ||
test(`strings.endsWith(string, substr) - returns truthy if the string ends with the substring`, t => { | ||
test('strings.endsWith(string, substr) - returns truthy if the string ends with the substring', t => { | ||
const expect = true; | ||
@@ -14,3 +14,3 @@ const result = strings.endsWith('This sentence ends with', 'with'); | ||
test(`strings.endsWith(string, substr) - returns falsy when the string doesn't end with the substr`, t => { | ||
test('strings.endsWith(string, substr) - returns falsy when the string does not end with the substr', t => { | ||
const expect = false; | ||
@@ -25,3 +25,3 @@ const result = strings.endsWith('This sentence does not end with', 'nope'); | ||
test(`strings.endsWith(string, substr) - returns falsy when the string doesn't end with the substr`, t => { | ||
test('strings.endsWith(string, substr) - returns falsy when the string does not end with the substr', t => { | ||
const expect = false; | ||
@@ -36,3 +36,3 @@ const result = strings.endsWith('abc', 'f'); | ||
test(`strings.endsWith(string, substr) - should not mutate the input`, t => { | ||
test('strings.endsWith(string, substr) - should not mutate the input', t => { | ||
const input = 'abc'; | ||
@@ -39,0 +39,0 @@ const expect = 'abc'; |
@@ -1,4 +0,30 @@ | ||
export const padEnd = (string, length, substr = ' ') => { | ||
/** | ||
* PadEnd pads the end of a string. | ||
* | ||
* @param {string} string | ||
* @param {number} length | ||
* @param {string} [substr=' '] | ||
* @returns {string} | ||
* | ||
* @example | ||
* // if no `substr` is provided, it pads the string w/ spaces | ||
* const result = strings.padEnd('abcabc', 9); | ||
* console.log(result); | ||
* > abcabc | ||
* | ||
* @example | ||
* // if `length` is shorter than `string` it doesn't add any padding | ||
* const result = strings.padEnd('abcabc', 4); | ||
* console.log(result); | ||
* > abcabc | ||
* | ||
* @example | ||
* // if `substr` is defined, it uses that for padding | ||
* const result = strings.padEnd('abcabc', 16, 'fun'); | ||
* console.log(result); | ||
* > 'abcabcfunfunfunf' | ||
*/ | ||
function padEnd (string, length, substr = ' ') { | ||
let strLen = string.length; | ||
let padLen = (length - string.length) > 0 ? length - string.length : 0; | ||
const padLen = (length - string.length) > 0 ? length - string.length : 0; | ||
let substrIdx = 0; | ||
@@ -17,2 +43,4 @@ string = [...string, ...Array(padLen)]; | ||
}, []).join(''); | ||
}; | ||
} | ||
export { padEnd }; |
import test from 'tape'; | ||
import { strings } from '../../index.js'; | ||
test(`strings.padEnd(string, length) - returns a string padded w/ spaces`, t => { | ||
test('strings.padEnd(string, length) - returns a string padded w/ spaces', t => { | ||
const expect = 'abcabc '; | ||
@@ -15,3 +15,3 @@ const result = strings.padEnd('abcabc', 9); | ||
test(`strings.padEnd(string, length) - returns the string unchanged when the length is < string.length`, t => { | ||
test('strings.padEnd(string, length) - returns the string unchanged when the length is < string.length', t => { | ||
const expect = 'abcabc'; | ||
@@ -27,3 +27,3 @@ const result = strings.padEnd('abcabc', 4); | ||
test(`strings.padEnd(string, length, substr) - returns a string padded w/ substr`, t => { | ||
test('strings.padEnd(string, length, substr) - returns a string padded w/ substr', t => { | ||
const expect = 'abcabcfunfunfunf'; | ||
@@ -30,0 +30,0 @@ const result = strings.padEnd('abcabc', 16, 'fun'); |
@@ -1,2 +0,28 @@ | ||
export const padStart = (string, length, substr = ' ') => { | ||
/** | ||
* PadStart pads the start of of a string. | ||
* | ||
* @param {string} string | ||
* @param {number} length | ||
* @param {string} [substr=' '] | ||
* @returns {string} | ||
* | ||
* @example | ||
* // if no `substr` is provided, it pads the string w/ spaces | ||
* const result = strings.padStart('abcabc', 9); | ||
* console.log(result); | ||
* > abcabc | ||
* | ||
* @example | ||
* // if `length` is shorter than `string` it doesn't add any padding | ||
* const result = strings.padStart('abcabc', 4); | ||
* console.log(result); | ||
* > abcabc | ||
* | ||
* @example | ||
* // if `substr` is defined, it uses that for padding | ||
* const result = strings.padStart('abcabc', 16, 'fun'); | ||
* console.log(result); | ||
* > 'funfunfunfabcabc' | ||
*/ | ||
function padStart (string, length, substr = ' ') { | ||
let padLen = (length - string.length) > 0 ? length - string.length : 0; | ||
@@ -16,2 +42,4 @@ let substrIdx = 0; | ||
}, []).join(''); | ||
}; | ||
} | ||
export { padStart }; |
import test from 'tape'; | ||
import { strings } from '../../index.js'; | ||
test(`strings.padStart(string, length) - returns a string padded w/ spaces`, t => { | ||
test('strings.padStart(string, length) - returns a string padded w/ spaces', t => { | ||
const expect = ' abcabc'; | ||
@@ -15,3 +15,3 @@ const result = strings.padStart('abcabc', 9); | ||
test(`strings.padStart(string, length) - returns the string unchanged when the length is < string.length`, t => { | ||
test('strings.padStart(string, length) - returns the string unchanged when the length is < string.length', t => { | ||
const expect = 'abcabc'; | ||
@@ -27,3 +27,3 @@ const result = strings.padStart('abcabc', 4); | ||
test(`strings.padStart(string, length, substr) - returns a string padded w/ substr`, t => { | ||
test('strings.padStart(string, length, substr) - returns a string padded w/ substr', t => { | ||
const expect = 'funfunfunfabcabc'; | ||
@@ -30,0 +30,0 @@ const result = strings.padStart('abcabc', 16, 'fun'); |
@@ -1,5 +0,18 @@ | ||
export const reverse = string => { | ||
/** | ||
* Reverse takes an returns a reversed version of the string input. | ||
* | ||
* @param {string} string | ||
* @returns {string} | ||
* | ||
* @example | ||
* const result = strings.reverse('This string will be reversed'); | ||
* console.log(result); | ||
* > desrever eb lliw gnirts sihT | ||
*/ | ||
function reverse (string) { | ||
return string.split('').reduce((acc, curr) => { | ||
return curr + acc; | ||
}, ''); | ||
}; | ||
} | ||
export { reverse }; |
import test from 'tape'; | ||
import { strings } from '../../index.js'; | ||
test(`strings.reverse(string) - returns the input string reversed`, t => { | ||
test('strings.reverse(string) - returns the input string reversed', t => { | ||
const expect = 'gnirts tupni na si siht'; | ||
@@ -6,0 +6,0 @@ const result = strings.reverse('this is an input string'); |
@@ -1,2 +0,19 @@ | ||
export const startsWith = (string, substr) => { | ||
/** | ||
* StartsWith tests a string to see if it starts with a substring | ||
* | ||
* @param {string} string | ||
* @param {string} substr | ||
* @returns {string} | ||
* | ||
* @example | ||
* const result = strings.startsWith('This sentence starts with', 'This'); | ||
* console.log(result); | ||
* > true | ||
* | ||
* @example | ||
* const result = strings.startsWith('This sentence does not start with', 'Nope'); | ||
* console.log(result); | ||
* > false | ||
*/ | ||
function startsWith (string, substr) { | ||
let chars = string.split(''); | ||
@@ -16,2 +33,4 @@ return chars.reduce((acc, curr, idx, arr) => { | ||
}, null); | ||
}; | ||
} | ||
export { startsWith }; |
import test from 'tape'; | ||
import { strings } from '../../index.js'; | ||
test(`strings.startsWith(string, substr) - returns truthy when the string starts with the substring`, t => { | ||
test('strings.startsWith(string, substr) - returns truthy when the string starts with the substring', t => { | ||
const expect = true; | ||
@@ -14,3 +14,3 @@ const result = strings.startsWith('This sentence starts with', 'This'); | ||
test(`strings.startsWith(string, substr) - returns falsy when the string doesn't start with the substring`, t => { | ||
test('strings.startsWith(string, substr) - returns falsy when the string does not start with the substring', t => { | ||
const expect = false; | ||
@@ -25,3 +25,3 @@ const result = strings.startsWith('This sentence does not start with', 'Nope'); | ||
test(`strings.startsWith(string, substr) - returns falsy when the string doesn't start with the substr`, t => { | ||
test('strings.startsWith(string, substr) - returns falsy when the string does not start with the substr', t => { | ||
const expect = false; | ||
@@ -36,3 +36,3 @@ const result = strings.startsWith('abc', 'f'); | ||
test(`strings.startsWith(string, substr) - should not mutate the input`, t => { | ||
test('strings.startsWith(string, substr) - should not mutate the input', t => { | ||
const input = 'abc'; | ||
@@ -39,0 +39,0 @@ const expect = 'abc'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39355
50
912
81
4
2