Adapter
ary
Creates a function that accepts up to n arguments, ignoring any additional arguments.
Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).
const ary = (fn, n) => (...args) => fn(...args.slice(0, n)); + }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
ary
Creates a function that accepts up to
narguments, ignoring any additional arguments.Call the provided function,
fn, with up tonarguments, usingArray.slice(0,n)and the spread operator (...).const ary = (fn, n) => (...args) => fn(...args.slice(0, n));const firstTwoMax = ary(Math.max, 2); [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args); @@ -123,7 +123,19 @@ Object.assig arrayMax([1, 2, 3]); // 3unary
Creates a function that accepts up to one argument, ignoring any additional arguments.
Call the provided function,
fn, with just the first argument given.const unary = fn => val => fn(val);['6', '8', '10'].map(unary(parseInt)); // [6, 8, 10] -Array
chunk
Chunks an array into smaller arrays of a specified size.
Use
Array.from()to create a new array, that fits the number of chunks that will be produced. UseArray.slice()to map each element of the new array to a chunk the length ofsize. If the original array can't be split evenly, the final chunk will contain the remaining elements.const chunk = (arr, size) => +Array
all
Returns
trueif the provided predicate function returnstruefor all elements in a collection,falseotherwise.Use
Array.every()to test if all elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const all = (arr, fn = Boolean) => arr.every(fn); +all([4, 2, 3], x => x > 1); // true +all([1, 2, 3]); // true +any
Returns
trueif the provided predicate function returnstruefor at least one element in a collection,falseotherwise.Use
Array.some()to test if any elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const any = (arr, fn = Boolean) => arr.some(fn); +any([0, 1, 2, 0], x => x >= 2); // true +any([0, 0, 1, 0]); // true +bifurcate
Splits values into two groups. If an element in
filteris truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.Use
Array.reduce()andArray.push()to add elements to groups, based onfilter.const bifurcate = (arr, filter) => + arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]); +bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ] +bifurcateBy
Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element belongs to the first group; otherwise, it belongs to the second group.
Use
Array.reduce()andArray.push()to add elements to groups, based on the value returned byfnfor each element.const bifurcateBy = (arr, fn) => + arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]); +bifurcateBy(['beep', 'boop', 'foo', 'bar'], x => x[0] === 'b'); // [ ['beep', 'boop', 'bar'], ['foo'] ] +chunk
Chunks an array into smaller arrays of a specified size.
Use
Array.from()to create a new array, that fits the number of chunks that will be produced. UseArray.slice()to map each element of the new array to a chunk the length ofsize. If the original array can't be split evenly, the final chunk will contain the remaining elements.const chunk = (arr, size) => Array.from({ length: Math.ceil(arr.length / size) }, (v, i) => arr.slice(i * size, i * size + size) ); @@ -283,6 +295,9 @@ Object.assigminN
Returns the
nminimum elements from the provided array. Ifnis greater than or equal to the provided array's length, then return the original array(sorted in ascending order).Use
Array.sort()combined with the spread operator (...) to create a shallow clone of the array and sort it in ascending order. UseArray.slice()to get the specified number of elements. Omit the second argument,n, to get a one-element array.const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);minN([1, 2, 3]); // [1] minN([1, 2, 3], 2); // [1,2] +none
Returns
trueif the provided predicate function returnsfalsefor all elements in a collection,falseotherwise.Use
Array.some()to test if any elements in the collection returntruebased onfn. Omit the second argument,fn, to useBooleanas a default.const none = (arr, fn = Boolean) => !arr.some(fn); +none([0, 1, 3, 0], x => x == 2); // true +none([0, 0, 0]); // truenthElement
Returns the nth element of an array.
Use
Array.slice()to get an array containing the nth element at the first place. If the index is out of bounds, return[]. Omit the second argument,n, to get the first element of the array.const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];nthElement(['a', 'b', 'c'], 1); // 'b' nthElement(['a', 'b', 'b'], -3); // 'a' @@ -433,6 +448,13 @@ Object.assig return index === -1 ? 0 : arr.length - index; };sortedLastIndexBy([{ x: 4 }, { x: 5 }], { x: 4 }, o => o.x); // 1 +stableSortadvanced
Performs stable sorting of an array, preserving the initial indexes of items when their values are the same. Does not mutate the original array, but returns a new array instead.
Use
Array.map()to pair each element of the input array with its corresponding index. UseArray.sort()and acomparefunction to sort the list, preserving their initial order if the items compared are equal. UseArray.map()to convert back to the initial array items.const stableSort = (arr, compare) => + arr + .map((item, index) => ({ item, index })) + .sort((a, b) => compare(a.item, b.item) || a.index - b.index) + .map(({ item }) => item); +const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +const stable = stableSort(arr, () => 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]symmetricDifference
Returns the symmetric difference between two arrays.
Create a
Setfrom each array, then useArray.filter()on each of them to only keep values not contained in the other.const symmetricDifference = (a, b) => { const sA = new Set(a), sB = new Set(b); @@ -967,6 +989,14 @@ document.bodyShow examplesvar output = ''; times(5, i => (output += i)); console.log(output); // 01234 +uncurry
Uncurries a function up to depth
n.Return a variadic function. Use
Array.reduce()on the provided arguments to call each subsequent curry level of the function. If thelengthof the provided arguments is less thannthrow an error. Otherwise, callfnwith the proper amount of arguments, usingArray.slice(0, n). Omit the second argument,n, to uncurry up to depth1.const uncurry = (fn, n = 1) => (...args) => { + const next = acc => args => args.reduce((x, y) => x(y), acc); + if (n > args.length) throw new RangeError('Arguments too few!'); + return next(fn)(args.slice(0, n)); +}; +const add = x => y => z => x + y + z; +const uncurriedAdd = uncurry(add, 3); +uncurriedAdd(1, 2, 3); // 6unfold
Builds an array, using an iterator function and an initial seed value.
Use a
whileloop andArray.push()to call the function repeatedly until it returnsfalse. The iterator function accepts one argument (seed) and must always return an array with two elements ([value,nextSeed]) orfalseto terminate.const unfold = (fn, seed) => { let result = [], val = [null, seed]; @@ -975,7 +1005,9 @@ console.log< };var f = n => (n > 50 ? false : [-n, n + 10]); unfold(f, 10); // [-10, -20, -30, -40, -50] -Math
average
Returns the average of two or more numbers.
Use
Array.reduce()to add each value to an accumulator, initialized with a value of0, divide by thelengthof the array.const average = (...nums) => [...nums].reduce((acc, val) => acc + val, 0) / nums.length; +Math
approximatelyEqual
Checks if two numbers are approximately equal to each other.
Use
Math.abs()to compare the absolute difference of the two values toepsilon. Omit the third parameter,epsilon, to use a default value of0.001.const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon; +approximatelyEqual(Math.PI / 2.0, 1.5708); // true +average
Returns the average of two or more numbers.
Use
Array.reduce()to add each value to an accumulator, initialized with a value of0, divide by thelengthof the array.const average = (...nums) => [...nums].reduce((acc, val) => acc + val, 0) / nums.length;average(...[1, 2, 3]); // 2 average(1, 2, 3); // 2averageBy
Returns the average of an array, after mapping each element to a value using the provided function.
Use
Array.map()to map each element to the value returned byfn,Array.reduce()to add each value to an accumulator, initialized with a value of0, divide by thelengthof the array.const averageBy = (arr, fn) => @@ -983,9 +1015,22 @@ console.log< arr.length;averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5 averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5 +binomialCoefficient
Evaluates the binomial coefficient of two integers
nandk.Use
Number.isNaN()to check if any of the two values isNaN. Check ifkis less than0, greater than or equal ton, equal to1orn - 1and return the appropriate result. Check ifn - kis less thankand switch their values accordingly. Loop from2throughkand calculate the binomial coefficient. UseMath.round()to account for rounding errors in the calculation.const binomialCoefficient = (n, k) => { + if (Number.isNaN(n) || Number.isNaN(k)) return NaN; + if (k < 0 || k > n) return 0; + if (k === 0 || k === n) return 1; + if (k === 1 || k === n - 1) return n; + if (n - k < k) k = n - k; + let res = n; + for (let j = 2; j <= k; j++) res *= (n - j + 1) / j; + return Math.round(res); +}; +binomialCoefficient(8, 2); // 28clampNumber
Clamps
numwithin the inclusive range specified by the boundary valuesaandb.If
numfalls within the range, returnnum. Otherwise, return the nearest number in the range.const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));clampNumber(2, 3, 5); // 3 clampNumber(1, -1, -5); // -1 +degreesToRads
Converts an angle from degrees to radians.
Use
Math.PIand the degree to radian formula to convert the angle from degrees to radians.const degreesToRads = deg => deg * Math.PI / 180.0; +degreesToRads(90.0); // ~1.5708digitize
Converts a number to an array of digits.
Convert the number to a string, using the spread operator (
...) to build an array. UseArray.map()andparseInt()to transform each value to an integer.const digitize = n => [...`${n}`].map(i => parseInt(i));digitize(123); // [1, 2, 3]distance
Returns the distance between two points.
Use
Math.hypot()to calculate the Euclidean distance between two points.const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0); @@ -1109,6 +1154,8 @@ own individual rating by supplying it as the third argument. return arr; };primes(10); // [2,3,5,7] +radsToDegrees
Converts an angle from radians to degrees.
Use
Math.PIand the radian to degree formula to convert the angle from radians to degrees.const radsToDegrees = rad => rad * 180.0 / Math.PI; +radsToDegrees(Math.PI / 2); // 90randomIntArrayInRange
Returns an array of n random integers in the specified range.
Use
Array.from()to create an empty array of the specific length,Math.random()to generate a random number and map it to the desired range, usingMath.floor()to make it an integer.const randomIntArrayInRange = (min, max, n = 1) => Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min);randomIntArrayInRange(12, 35, 10); // [ 34, 14, 27, 17, 30, 27, 20, 26, 21, 14 ] @@ -1840,6 +1887,24 @@ Logs: { "id": 101 } */ +mostPerformant
Returns the index of the function in an array of functions which executed the fastest.
Use
Array.map()to generate an array where each value is the total time taken to execute the function afteriterationstimes. Use the difference inperformance.now()values before and after to get the total time in milliseconds to a high degree of accuracy. UseMath.min()to find the minimum execution time, and return the index of that shortest time which corresponds to the index of the most performant function. Omit the second argument,iterations, to use a default of 10,000 iterations. The more iterations, the more reliable the result but the longer it will take.const mostPerformant = (fns, iterations = 10000) => { + const times = fns.map(fn => { + const before = performance.now(); + for (let i = 0; i < iterations; i++) fn(); + return performance.now() - before; + }); + return times.indexOf(Math.min(...times)); +}; +mostPerformant([ + () => { + // Loops through the entire array before returning `false` + [1, 2, 3, 4, 5, 6, 7, 8, 9, '10'].every(el => typeof el === 'number'); + }, + () => { + // Only needs to reach index `1` before returning false + [1, '2', 3, 4, 5, 6, 7, 8, 9, 10].every(el => typeof el === 'number'); + } +]); // 1nthArg
Creates a function that gets the argument at index
n. Ifnis negative, the nth argument from the end is returned.Use
Array.slice()to get the desired argument at indexn.const nthArg = n => (...args) => args.slice(n)[0];const third = nthArg(2); third(1, 2, 3); // 3 diff --git a/package.json b/package.json index 094ca7ffd..4dd3113d2 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ }, "name": "30-seconds-of-code", "description": "A collection of useful JavaScript snippets.", - "version": "0.0.1", + "version": "0.0.2", "main": "dist/_30s.js", "module": "dist/_30s.esm.js", "scripts": { diff --git a/snippet-template.md b/snippet-template.md index 1ee62a2f0..31cc8b26d 100644 --- a/snippet-template.md +++ b/snippet-template.md @@ -10,5 +10,5 @@ const functionName = arguments => ``` ```js -functionName('sampleInput') // 'sampleOutput' +functionName('sampleInput'); // 'sampleOutput' ``` diff --git a/snippets/all.md b/snippets/all.md new file mode 100644 index 000000000..17cadbc9f --- /dev/null +++ b/snippets/all.md @@ -0,0 +1,15 @@ +### all + +Returns `true` if the provided predicate function returns `true` for all elements in a collection, `false` otherwise. + +Use `Array.every()` to test if all elements in the collection return `true` based on `fn`. +Omit the second argument, `fn`, to use `Boolean` as a default. + +```js +const all = (arr, fn = Boolean) => arr.every(fn); +``` + +```js +all([4, 2, 3], x => x > 1); // true +all([1, 2, 3]); // true +``` diff --git a/snippets/any.md b/snippets/any.md new file mode 100644 index 000000000..31368cc7a --- /dev/null +++ b/snippets/any.md @@ -0,0 +1,15 @@ +### any + +Returns `true` if the provided predicate function returns `true` for at least one element in a collection, `false` otherwise. + +Use `Array.some()` to test if any elements in the collection return `true` based on `fn`. +Omit the second argument, `fn`, to use `Boolean` as a default. + +```js +const any = (arr, fn = Boolean) => arr.some(fn); +``` + +```js +any([0, 1, 2, 0], x => x >= 2); // true +any([0, 0, 1, 0]); // true +``` diff --git a/snippets/approximatelyEqual.md b/snippets/approximatelyEqual.md new file mode 100644 index 000000000..88773c163 --- /dev/null +++ b/snippets/approximatelyEqual.md @@ -0,0 +1,14 @@ +### approximatelyEqual + +Checks if two numbers are approximately equal to each other. + +Use `Math.abs()` to compare the absolute difference of the two values to `epsilon`. +Omit the third parameter, `epsilon`, to use a default value of `0.001`. + +```js +const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon; +``` + +```js +approximatelyEqual(Math.PI / 2.0, 1.5708); // true +``` diff --git a/snippets/bifurcate.md b/snippets/bifurcate.md new file mode 100644 index 000000000..2e124711f --- /dev/null +++ b/snippets/bifurcate.md @@ -0,0 +1,14 @@ +### bifurcate + +Splits values into two groups. If an element in `filter` is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group. + +Use `Array.reduce()` and `Array.push()` to add elements to groups, based on `filter`. + +```js +const bifurcate = (arr, filter) => + arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]); +``` + +```js +bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ] +``` diff --git a/snippets/bifurcateBy.md b/snippets/bifurcateBy.md new file mode 100644 index 000000000..46a00116f --- /dev/null +++ b/snippets/bifurcateBy.md @@ -0,0 +1,14 @@ +### bifurcateBy + +Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element belongs to the first group; otherwise, it belongs to the second group. + +Use `Array.reduce()` and `Array.push()` to add elements to groups, based on the value returned by `fn` for each element. + +```js +const bifurcateBy = (arr, fn) => + arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]); +``` + +```js +bifurcateBy(['beep', 'boop', 'foo', 'bar'], x => x[0] === 'b'); // [ ['beep', 'boop', 'bar'], ['foo'] ] +``` diff --git a/snippets/binomialCoefficient.md b/snippets/binomialCoefficient.md new file mode 100644 index 000000000..d5ff87d16 --- /dev/null +++ b/snippets/binomialCoefficient.md @@ -0,0 +1,26 @@ +### binomialCoefficient + +Evaluates the binomial coefficient of two integers `n` and `k`. + +Use `Number.isNaN()` to check if any of the two values is `NaN`. +Check if `k` is less than `0`, greater than or equal to `n`, equal to `1` or `n - 1` and return the appropriate result. +Check if `n - k` is less than `k` and switch their values accordingly. +Loop from `2` through `k` and calculate the binomial coefficient. +Use `Math.round()` to account for rounding errors in the calculation. + +```js +const binomialCoefficient = (n, k) => { + if (Number.isNaN(n) || Number.isNaN(k)) return NaN; + if (k < 0 || k > n) return 0; + if (k === 0 || k === n) return 1; + if (k === 1 || k === n - 1) return n; + if (n - k < k) k = n - k; + let res = n; + for (let j = 2; j <= k; j++) res *= (n - j + 1) / j; + return Math.round(res); +}; +``` + +```js +binomialCoefficient(8, 2); // 28 +``` diff --git a/snippets/degreesToRads.md b/snippets/degreesToRads.md new file mode 100644 index 000000000..ed3a2ee0e --- /dev/null +++ b/snippets/degreesToRads.md @@ -0,0 +1,13 @@ +### degreesToRads + +Converts an angle from degrees to radians. + +Use `Math.PI` and the degree to radian formula to convert the angle from degrees to radians. + +```js +const degreesToRads = deg => deg * Math.PI / 180.0; +``` + +```js +degreesToRads(90.0); // ~1.5708 +``` diff --git a/snippets/mostPerformant.md b/snippets/mostPerformant.md new file mode 100644 index 000000000..66cd591e2 --- /dev/null +++ b/snippets/mostPerformant.md @@ -0,0 +1,31 @@ +### mostPerformant + +Returns the index of the function in an array of functions which executed the fastest. + +Use `Array.map()` to generate an array where each value is the total time taken to execute the function after `iterations` times. Use the difference in `performance.now()` values before and after to get the total time in milliseconds to a high degree of accuracy. +Use `Math.min()` to find the minimum execution time, and return the index of that shortest time which corresponds to the index of the most performant function. +Omit the second argument, `iterations`, to use a default of 10,000 iterations. The more iterations, the more reliable the result but the longer it will take. + +```js +const mostPerformant = (fns, iterations = 10000) => { + const times = fns.map(fn => { + const before = performance.now(); + for (let i = 0; i < iterations; i++) fn(); + return performance.now() - before; + }); + return times.indexOf(Math.min(...times)); +}; +``` + +```js +mostPerformant([ + () => { + // Loops through the entire array before returning `false` + [1, 2, 3, 4, 5, 6, 7, 8, 9, '10'].every(el => typeof el === 'number'); + }, + () => { + // Only needs to reach index `1` before returning false + [1, '2', 3, 4, 5, 6, 7, 8, 9, 10].every(el => typeof el === 'number'); + } +]); // 1 +``` diff --git a/snippets/none.md b/snippets/none.md new file mode 100644 index 000000000..1b9af0c2a --- /dev/null +++ b/snippets/none.md @@ -0,0 +1,15 @@ +### none + +Returns `true` if the provided predicate function returns `false` for all elements in a collection, `false` otherwise. + +Use `Array.some()` to test if any elements in the collection return `true` based on `fn`. +Omit the second argument, `fn`, to use `Boolean` as a default. + +```js +const none = (arr, fn = Boolean) => !arr.some(fn); +``` + +```js +none([0, 1, 3, 0], x => x == 2); // true +none([0, 0, 0]); // true +``` diff --git a/snippets/radsToDegrees.md b/snippets/radsToDegrees.md new file mode 100644 index 000000000..d8dd54958 --- /dev/null +++ b/snippets/radsToDegrees.md @@ -0,0 +1,13 @@ +### radsToDegrees + +Converts an angle from radians to degrees. + +Use `Math.PI` and the radian to degree formula to convert the angle from radians to degrees. + +```js +const radsToDegrees = rad => rad * 180.0 / Math.PI; +``` + +```js +radsToDegrees(Math.PI / 2); // 90 +``` diff --git a/snippets/stableSort.md b/snippets/stableSort.md new file mode 100644 index 000000000..5a37fb1d6 --- /dev/null +++ b/snippets/stableSort.md @@ -0,0 +1,21 @@ +### stableSort + +Performs stable sorting of an array, preserving the initial indexes of items when their values are the same. +Does not mutate the original array, but returns a new array instead. + +Use `Array.map()` to pair each element of the input array with its corresponding index. +Use `Array.sort()` and a `compare` function to sort the list, preserving their initial order if the items compared are equal. +Use `Array.map()` to convert back to the initial array items. + +```js +const stableSort = (arr, compare) => + arr + .map((item, index) => ({ item, index })) + .sort((a, b) => compare(a.item, b.item) || a.index - b.index) + .map(({ item }) => item); +``` + +```js +const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +const stable = stableSort(arr, () => 0); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +``` diff --git a/snippets/uncurry.md b/snippets/uncurry.md new file mode 100644 index 000000000..c5757e88d --- /dev/null +++ b/snippets/uncurry.md @@ -0,0 +1,23 @@ +### uncurry + +Uncurries a function up to depth `n`. + +Return a variadic function. +Use `Array.reduce()` on the provided arguments to call each subsequent curry level of the function. +If the `length` of the provided arguments is less than `n` throw an error. +Otherwise, call `fn` with the proper amount of arguments, using `Array.slice(0, n)`. +Omit the second argument, `n`, to uncurry up to depth `1`. + +```js +const uncurry = (fn, n = 1) => (...args) => { + const next = acc => args => args.reduce((x, y) => x(y), acc); + if (n > args.length) throw new RangeError('Arguments too few!'); + return next(fn)(args.slice(0, n)); +}; +``` + +```js +const add = x => y => z => x + y + z; +const uncurriedAdd = uncurry(add, 3); +uncurriedAdd(1, 2, 3); // 6 +``` diff --git a/tag_database b/tag_database index 10cc1708a..54db11193 100644 --- a/tag_database +++ b/tag_database @@ -1,12 +1,18 @@ +all:array,function +any:array,function +approximatelyEqual:math arrayToHtmlList:browser,array ary:adapter,function atob:node,string,utility attempt:function average:math,array averageBy:math,array,function +bifurcate:array +bifurcateBy:array,function bind:function,object bindAll:object,function bindKey:function,object +binomialCoefficient:math bottomVisible:browser btoa:node,string,utility byteSize:string @@ -39,6 +45,7 @@ deepClone:object,recursion deepFlatten:array,recursion defaults:object defer:function +degreesToRads:math delay:function detectDeviceType:browser difference:array,math @@ -153,7 +160,9 @@ memoize:function merge:object,array minBy:math,array,function minN:array,math +mostPerformant:utility,function negate:function +none:array,function nthArg:utility,function nthElement:array objectFromPairs:object,array @@ -188,6 +197,7 @@ pull:array pullAtIndex:array pullAtValue:array pullBy:array,function,advanced +radsToDegrees:math randomHexColorCode:utility,random randomIntArrayInRange:math,utility,random randomIntegerInRange:math,utility,random @@ -224,6 +234,7 @@ sortedLastIndex:array,math sortedLastIndexBy:array,math,function splitLines:string spreadOver:adapter +stableSort:array,sort,advanced standardDeviation:math,array stringPermutations:string,recursion stripHTMLTags:string,utility,regexp @@ -254,6 +265,7 @@ transform:object,array truncateString:string truthCheckCollection:object,logic,array unary:adapter,function +uncurry:function unescapeHTML:string,browser unflattenObject:object,advanced unfold:function,array diff --git a/test/all/all.js b/test/all/all.js new file mode 100644 index 000000000..6be892a3c --- /dev/null +++ b/test/all/all.js @@ -0,0 +1,2 @@ +const all = (arr, fn = Boolean) => arr.every(fn); +module.exports = all; \ No newline at end of file diff --git a/test/all/all.test.js b/test/all/all.test.js new file mode 100644 index 000000000..60ac82f4a --- /dev/null +++ b/test/all/all.test.js @@ -0,0 +1,21 @@ +const test = require('tape'); +const all = require('./all.js'); + +test('Testing all', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof all === 'function', 'all is a Function'); + t.true(all([4,1,2,3]), 'Returns true for arrays with no falsey values'); + t.false(all([0,1]), 'Returns false for arrays with 0'); + t.false(all([NaN,1]), 'Returns false for arrays with NaN'); + t.false(all([undefined,1]), 'Returns false for arrays with undefined'); + t.false(all([null,1]), 'Returns false for arrays with null'); + t.false(all(['',1]), 'Returns false for arrays with empty strings'); + t.true(all([4,1,2,3], x => x >= 1), 'Returns true with predicate function'); + t.false(all([0,1], x => x >= 1), 'Returns false with a predicate function'); + //t.deepEqual(all(args..), 'Expected'); + //t.equal(all(args..), 'Expected'); + //t.false(all(args..), 'Expected'); + //t.throws(all(args..), 'Expected'); + t.end(); +}); diff --git a/test/any/any.js b/test/any/any.js new file mode 100644 index 000000000..5004b3309 --- /dev/null +++ b/test/any/any.js @@ -0,0 +1,2 @@ +const any = (arr, fn = Boolean) => arr.some(fn); +module.exports = any; \ No newline at end of file diff --git a/test/any/any.test.js b/test/any/any.test.js new file mode 100644 index 000000000..63399b385 --- /dev/null +++ b/test/any/any.test.js @@ -0,0 +1,18 @@ +const test = require('tape'); +const any = require('./any.js'); + +test('Testing any', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof any === 'function', 'any is a Function'); + t.true(any([0,1,2,3]), 'Returns true for arrays with at least one truthy value'); + t.false(any([0,0]), 'Returns false for arrays with no truthy values'); + t.false(any([NaN,0,undefined,null,'']), 'Returns false for arrays with no truthy values'); + t.true(any([4,1,0,3], x => x >= 1), 'Returns true with predicate function'); + t.false(any([0,1], x => x < 0), 'Returns false with a predicate function'); + //t.deepEqual(any(args..), 'Expected'); + //t.equal(any(args..), 'Expected'); + //t.false(any(args..), 'Expected'); + //t.throws(any(args..), 'Expected'); + t.end(); +}); diff --git a/test/approximatelyEqual/approximatelyEqual.js b/test/approximatelyEqual/approximatelyEqual.js new file mode 100644 index 000000000..85fecb15b --- /dev/null +++ b/test/approximatelyEqual/approximatelyEqual.js @@ -0,0 +1,2 @@ +const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon; +module.exports = approximatelyEqual; \ No newline at end of file diff --git a/test/approximatelyEqual/approximatelyEqual.test.js b/test/approximatelyEqual/approximatelyEqual.test.js new file mode 100644 index 000000000..44221aeb8 --- /dev/null +++ b/test/approximatelyEqual/approximatelyEqual.test.js @@ -0,0 +1,17 @@ +const test = require('tape'); +const approximatelyEqual = require('./approximatelyEqual.js'); + +test('Testing approximatelyEqual', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof approximatelyEqual === 'function', 'approximatelyEqual is a Function'); + t.true(approximatelyEqual(Math.PI / 2.0 , 1.5708), 'Works for PI / 2'); + t.true(approximatelyEqual(0.1 + 0.2, 0.3), 'Works for 0.1 + 0.2 === 0.3'); + t.true(approximatelyEqual(0.5, 0.5), 'Works for exactly equal values'); + t.true(approximatelyEqual(0.501, 0.5, 0.1), 'Works for a custom epsilon'); + //t.deepEqual(approximatelyEqual(args..), 'Expected'); + //t.equal(approximatelyEqual(args..), 'Expected'); + //t.false(approximatelyEqual(args..), 'Expected'); + //t.throws(approximatelyEqual(args..), 'Expected'); + t.end(); +}); diff --git a/test/arrayToHtmlList/arrayToHtmlList.test.js b/test/arrayToHtmlList/arrayToHtmlList.test.js index 1f8885778..7cb8f4582 100644 --- a/test/arrayToHtmlList/arrayToHtmlList.test.js +++ b/test/arrayToHtmlList/arrayToHtmlList.test.js @@ -5,9 +5,10 @@ test('Testing arrayToHtmlList', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof arrayToHtmlList === 'function', 'arrayToHtmlList is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(arrayToHtmlList(args..), 'Expected'); //t.equal(arrayToHtmlList(args..), 'Expected'); //t.false(arrayToHtmlList(args..), 'Expected'); //t.throws(arrayToHtmlList(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/average/average.test.js b/test/average/average.test.js index dff7df5a7..16a9471c7 100644 --- a/test/average/average.test.js +++ b/test/average/average.test.js @@ -19,6 +19,6 @@ test('Testing average', (t) => { let start = new Date().getTime(); average(153, 44, 55, 64, 71, 1122, 322774, 2232, 23423, 234, 3631); let end = new Date().getTime(); - t.true((end - start) < 2000, 'head([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1122, 32124, 23232]) takes less than 2s to run'); + t.true((end - start) < 2000, 'average([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1122, 32124, 23232]) takes less than 2s to run'); t.end(); }); \ No newline at end of file diff --git a/test/bifurcate/bifurcate.js b/test/bifurcate/bifurcate.js new file mode 100644 index 000000000..95d7f8f50 --- /dev/null +++ b/test/bifurcate/bifurcate.js @@ -0,0 +1,3 @@ +const bifurcate = (arr, filter) => +arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]); +module.exports = bifurcate; \ No newline at end of file diff --git a/test/bifurcate/bifurcate.test.js b/test/bifurcate/bifurcate.test.js new file mode 100644 index 000000000..c65333ee0 --- /dev/null +++ b/test/bifurcate/bifurcate.test.js @@ -0,0 +1,14 @@ +const test = require('tape'); +const bifurcate = require('./bifurcate.js'); + +test('Testing bifurcate', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof bifurcate === 'function', 'bifurcate is a Function'); + t.deepEqual(bifurcate([ 'beep', 'boop', 'foo', 'bar' ], [ true, true, false, true ]), [ ['beep', 'boop', 'bar'], ['foo'] ], 'Splits the collection into two groups'); + //t.deepEqual(bifurcate(args..), 'Expected'); + //t.equal(bifurcate(args..), 'Expected'); + //t.false(bifurcate(args..), 'Expected'); + //t.throws(bifurcate(args..), 'Expected'); + t.end(); +}); diff --git a/test/bifurcateBy/bifurcateBy.js b/test/bifurcateBy/bifurcateBy.js new file mode 100644 index 000000000..bdf04a7ce --- /dev/null +++ b/test/bifurcateBy/bifurcateBy.js @@ -0,0 +1,3 @@ +const bifurcateBy = (arr, fn) => +arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]); +module.exports = bifurcateBy; \ No newline at end of file diff --git a/test/bifurcateBy/bifurcateBy.test.js b/test/bifurcateBy/bifurcateBy.test.js new file mode 100644 index 000000000..a6c293638 --- /dev/null +++ b/test/bifurcateBy/bifurcateBy.test.js @@ -0,0 +1,14 @@ +const test = require('tape'); +const bifurcateBy = require('./bifurcateBy.js'); + +test('Testing bifurcateBy', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof bifurcateBy === 'function', 'bifurcateBy is a Function'); + t.deepEqual(bifurcateBy([ 'beep', 'boop', 'foo', 'bar' ], x => x[0] === 'b'), [ ['beep', 'boop', 'bar'], ['foo'] ], 'Splits the collection into two groups'); + //t.deepEqual(bifurcateBy(args..), 'Expected'); + //t.equal(bifurcateBy(args..), 'Expected'); + //t.false(bifurcateBy(args..), 'Expected'); + //t.throws(bifurcateBy(args..), 'Expected'); + t.end(); +}); diff --git a/test/binomialCoefficient/binomialCoefficient.js b/test/binomialCoefficient/binomialCoefficient.js new file mode 100644 index 000000000..a9808585d --- /dev/null +++ b/test/binomialCoefficient/binomialCoefficient.js @@ -0,0 +1,11 @@ +const binomialCoefficient = (n, k) => { +if (Number.isNaN(n) || Number.isNaN(k)) return NaN; +if (k < 0 || k > n) return 0; +if (k === 0 || k === n) return 1; +if (k === 1 || k === n - 1) return n; +if (n - k < k) k = n - k; +let res = n; +for (let j = 2; j <= k; j++) res *= (n - j + 1) / j; +return Math.round(res); +}; +module.exports = binomialCoefficient; \ No newline at end of file diff --git a/test/binomialCoefficient/binomialCoefficient.test.js b/test/binomialCoefficient/binomialCoefficient.test.js new file mode 100644 index 000000000..2f0e8ab20 --- /dev/null +++ b/test/binomialCoefficient/binomialCoefficient.test.js @@ -0,0 +1,18 @@ +const test = require('tape'); +const binomialCoefficient = require('./binomialCoefficient.js'); + +test('Testing binomialCoefficient', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof binomialCoefficient === 'function', 'binomialCoefficient is a Function'); + t.equal(binomialCoefficient(8, 2), 28, 'Returns the appropriate value'); + t.equal(binomialCoefficient(0, 0), 1, 'Returns the appropriate value'); + t.equal(binomialCoefficient(5, 3), 10, 'Returns the appropriate value'); + t.true(Number.isNaN(binomialCoefficient(NaN, 3)), 'Returns NaN'); + t.true(Number.isNaN(binomialCoefficient(5, NaN)), 'Returns NaN'); + //t.deepEqual(binomialCoefficient(args..), 'Expected'); + //t.equal(binomialCoefficient(args..), 'Expected'); + //t.false(binomialCoefficient(args..), 'Expected'); + //t.throws(binomialCoefficient(args..), 'Expected'); + t.end(); +}); diff --git a/test/chainAsync/chainAsync.test.js b/test/chainAsync/chainAsync.test.js index d013df52f..a1e6edd06 100644 --- a/test/chainAsync/chainAsync.test.js +++ b/test/chainAsync/chainAsync.test.js @@ -5,22 +5,19 @@ test('Testing chainAsync', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof chainAsync === 'function', 'chainAsync is a Function'); - //t.deepEqual(chainAsync(args..), 'Expected'); - // chainAsync([ - // next => { - // next(); - // }, - // next => { - // (() =>{ - // next() - // })(); - // }, - // next => { - // t.pass("Calls all functions in an array"); - // next(); - // } - // ]); - // + chainAsync([ + next => { + next(); + }, + next => { + (() => { + next(); + })(); + }, + next => { + t.pass("Calls all functions in an array"); + } + ]); // // Ensure we wait for the 2nd assertion to be made // t.plan(2); diff --git a/test/createElement/createElement.test.js b/test/createElement/createElement.test.js index f773ea672..6923ec245 100644 --- a/test/createElement/createElement.test.js +++ b/test/createElement/createElement.test.js @@ -5,9 +5,10 @@ test('Testing createElement', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof createElement === 'function', 'createElement is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(createElement(args..), 'Expected'); //t.equal(createElement(args..), 'Expected'); //t.false(createElement(args..), 'Expected'); //t.throws(createElement(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/createEventHub/createEventHub.test.js b/test/createEventHub/createEventHub.test.js index 759c461b3..101d07aef 100644 --- a/test/createEventHub/createEventHub.test.js +++ b/test/createEventHub/createEventHub.test.js @@ -5,9 +5,10 @@ test('Testing createEventHub', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof createEventHub === 'function', 'createEventHub is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(createEventHub(args..), 'Expected'); //t.equal(createEventHub(args..), 'Expected'); //t.false(createEventHub(args..), 'Expected'); //t.throws(createEventHub(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/currentURL/currentURL.test.js b/test/currentURL/currentURL.test.js index e7dba8ee4..e190ad429 100644 --- a/test/currentURL/currentURL.test.js +++ b/test/currentURL/currentURL.test.js @@ -5,9 +5,10 @@ test('Testing currentURL', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof currentURL === 'function', 'currentURL is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(currentURL(args..), 'Expected'); //t.equal(currentURL(args..), 'Expected'); //t.false(currentURL(args..), 'Expected'); //t.throws(currentURL(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/debounce/debounce.test.js b/test/debounce/debounce.test.js index f1a35323c..867aac93a 100644 --- a/test/debounce/debounce.test.js +++ b/test/debounce/debounce.test.js @@ -5,9 +5,10 @@ test('Testing debounce', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof debounce === 'function', 'debounce is a Function'); + debounce(() => {t.pass('Works as expected');}, 250); //t.deepEqual(debounce(args..), 'Expected'); //t.equal(debounce(args..), 'Expected'); //t.false(debounce(args..), 'Expected'); //t.throws(debounce(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/defaults/defaults.test.js b/test/defaults/defaults.test.js index ab1afa29e..b3c519f9d 100644 --- a/test/defaults/defaults.test.js +++ b/test/defaults/defaults.test.js @@ -5,9 +5,10 @@ test('Testing defaults', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof defaults === 'function', 'defaults is a Function'); + t.deepEqual(defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }), { a: 1, b: 2 }, 'Assigns default values for undefined properties'); //t.deepEqual(defaults(args..), 'Expected'); //t.equal(defaults(args..), 'Expected'); //t.false(defaults(args..), 'Expected'); //t.throws(defaults(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/defer/defer.test.js b/test/defer/defer.test.js index faa5731c9..23e125b90 100644 --- a/test/defer/defer.test.js +++ b/test/defer/defer.test.js @@ -5,9 +5,10 @@ test('Testing defer', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof defer === 'function', 'defer is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(defer(args..), 'Expected'); //t.equal(defer(args..), 'Expected'); //t.false(defer(args..), 'Expected'); //t.throws(defer(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/degreesToRads/degreesToRads.js b/test/degreesToRads/degreesToRads.js new file mode 100644 index 000000000..da6d51836 --- /dev/null +++ b/test/degreesToRads/degreesToRads.js @@ -0,0 +1,2 @@ +const degreesToRads = deg => deg * Math.PI / 180.0; +module.exports = degreesToRads; \ No newline at end of file diff --git a/test/degreesToRads/degreesToRads.test.js b/test/degreesToRads/degreesToRads.test.js new file mode 100644 index 000000000..8ec980a10 --- /dev/null +++ b/test/degreesToRads/degreesToRads.test.js @@ -0,0 +1,15 @@ +const test = require('tape'); +const degreesToRads = require('./degreesToRads.js'); + +test('Testing degreesToRads', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + const approxeq = (v1,v2, diff = 0.001) => Math.abs(v1 - v2) < diff; // Use to account for rounding errors + t.true(typeof degreesToRads === 'function', 'degreesToRads is a Function'); + t.true(approxeq(degreesToRads(90.0), Math.PI / 2), 'Returns the appropriate value'); + //t.deepEqual(degreesToRads(args..), 'Expected'); + //t.equal(degreesToRads(args..), 'Expected'); + //t.false(degreesToRads(args..), 'Expected'); + //t.throws(degreesToRads(args..), 'Expected'); + t.end(); +}); diff --git a/test/delay/delay.test.js b/test/delay/delay.test.js index 544f8632d..8404b3bcb 100644 --- a/test/delay/delay.test.js +++ b/test/delay/delay.test.js @@ -5,9 +5,16 @@ test('Testing delay', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof delay === 'function', 'delay is a Function'); + delay( + function(text) { + t.equals(text, 'test', 'Works as expecting, passing arguments properly'); + }, + 1000, + 'test' + ); //t.deepEqual(delay(args..), 'Expected'); //t.equal(delay(args..), 'Expected'); //t.false(delay(args..), 'Expected'); //t.throws(delay(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/getScrollPosition/getScrollPosition.test.js b/test/getScrollPosition/getScrollPosition.test.js index ded20c020..9f7fd14d7 100644 --- a/test/getScrollPosition/getScrollPosition.test.js +++ b/test/getScrollPosition/getScrollPosition.test.js @@ -5,9 +5,10 @@ test('Testing getScrollPosition', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof getScrollPosition === 'function', 'getScrollPosition is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(getScrollPosition(args..), 'Expected'); //t.equal(getScrollPosition(args..), 'Expected'); //t.false(getScrollPosition(args..), 'Expected'); //t.throws(getScrollPosition(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/getStyle/getStyle.test.js b/test/getStyle/getStyle.test.js index 11c2c9244..23bac0147 100644 --- a/test/getStyle/getStyle.test.js +++ b/test/getStyle/getStyle.test.js @@ -5,9 +5,10 @@ test('Testing getStyle', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof getStyle === 'function', 'getStyle is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(getStyle(args..), 'Expected'); //t.equal(getStyle(args..), 'Expected'); //t.false(getStyle(args..), 'Expected'); //t.throws(getStyle(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/hasFlags/hasFlags.test.js b/test/hasFlags/hasFlags.test.js index 0245051be..be0d4c301 100644 --- a/test/hasFlags/hasFlags.test.js +++ b/test/hasFlags/hasFlags.test.js @@ -5,9 +5,10 @@ test('Testing hasFlags', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof hasFlags === 'function', 'hasFlags is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(hasFlags(args..), 'Expected'); //t.equal(hasFlags(args..), 'Expected'); //t.false(hasFlags(args..), 'Expected'); //t.throws(hasFlags(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/hashBrowser/hashBrowser.test.js b/test/hashBrowser/hashBrowser.test.js index d01cafc5b..5464dd95d 100644 --- a/test/hashBrowser/hashBrowser.test.js +++ b/test/hashBrowser/hashBrowser.test.js @@ -5,9 +5,10 @@ test('Testing hashBrowser', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof hashBrowser === 'function', 'hashBrowser is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(hashBrowser(args..), 'Expected'); //t.equal(hashBrowser(args..), 'Expected'); //t.false(hashBrowser(args..), 'Expected'); //t.throws(hashBrowser(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/hide/hide.test.js b/test/hide/hide.test.js index d93d3fa74..af1971858 100644 --- a/test/hide/hide.test.js +++ b/test/hide/hide.test.js @@ -5,9 +5,10 @@ test('Testing hide', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof hide === 'function', 'hide is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(hide(args..), 'Expected'); //t.equal(hide(args..), 'Expected'); //t.false(hide(args..), 'Expected'); //t.throws(hide(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/mostPerformant/mostPerformant.js b/test/mostPerformant/mostPerformant.js new file mode 100644 index 000000000..ae084a5cf --- /dev/null +++ b/test/mostPerformant/mostPerformant.js @@ -0,0 +1,9 @@ +const mostPerformant = (fns, iterations = 10000) => { +const times = fns.map(fn => { +const before = performance.now(); +for (let i = 0; i < iterations; i++) fn(); +return performance.now() - before; +}); +return times.indexOf(Math.min(...times)); +}; +module.exports = mostPerformant; \ No newline at end of file diff --git a/test/mostPerformant/mostPerformant.test.js b/test/mostPerformant/mostPerformant.test.js new file mode 100644 index 000000000..95a560f91 --- /dev/null +++ b/test/mostPerformant/mostPerformant.test.js @@ -0,0 +1,14 @@ +const test = require('tape'); +const mostPerformant = require('./mostPerformant.js'); + +test('Testing mostPerformant', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof mostPerformant === 'function', 'mostPerformant is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); + //t.deepEqual(mostPerformant(args..), 'Expected'); + //t.equal(mostPerformant(args..), 'Expected'); + //t.false(mostPerformant(args..), 'Expected'); + //t.throws(mostPerformant(args..), 'Expected'); + t.end(); +}); diff --git a/test/none/none.js b/test/none/none.js new file mode 100644 index 000000000..2e81d7d72 --- /dev/null +++ b/test/none/none.js @@ -0,0 +1,2 @@ +const none = (arr, fn = Boolean) => !arr.some(fn); +module.exports = none; \ No newline at end of file diff --git a/test/none/none.test.js b/test/none/none.test.js new file mode 100644 index 000000000..af6209d91 --- /dev/null +++ b/test/none/none.test.js @@ -0,0 +1,17 @@ +const test = require('tape'); +const none = require('./none.js'); + +test('Testing none', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof none === 'function', 'none is a Function'); + t.true(none([0,undefined,NaN,null,'']), 'Returns true for arrays with no truthy values'); + t.false(none([0,1]), 'Returns false for arrays with at least one truthy value'); + t.true(none([4,1,0,3], x => x < 0), 'Returns true with a predicate function'); + t.false(none([0,1,2], x => x === 1), 'Returns false with predicate function'); + //t.deepEqual(none(args..), 'Expected'); + //t.equal(none(args..), 'Expected'); + //t.false(none(args..), 'Expected'); + //t.throws(none(args..), 'Expected'); + t.end(); +}); diff --git a/test/off/off.test.js b/test/off/off.test.js index 030e7797a..8a3eca663 100644 --- a/test/off/off.test.js +++ b/test/off/off.test.js @@ -5,9 +5,10 @@ test('Testing off', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof off === 'function', 'off is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(off(args..), 'Expected'); //t.equal(off(args..), 'Expected'); //t.false(off(args..), 'Expected'); //t.throws(off(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/on/on.test.js b/test/on/on.test.js index 5bfa58a76..f14e85ad0 100644 --- a/test/on/on.test.js +++ b/test/on/on.test.js @@ -5,9 +5,10 @@ test('Testing on', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof on === 'function', 'on is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(on(args..), 'Expected'); //t.equal(on(args..), 'Expected'); //t.false(on(args..), 'Expected'); //t.throws(on(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/onUserInputChange/onUserInputChange.test.js b/test/onUserInputChange/onUserInputChange.test.js index c4c181806..bbc726d63 100644 --- a/test/onUserInputChange/onUserInputChange.test.js +++ b/test/onUserInputChange/onUserInputChange.test.js @@ -5,9 +5,10 @@ test('Testing onUserInputChange', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof onUserInputChange === 'function', 'onUserInputChange is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(onUserInputChange(args..), 'Expected'); //t.equal(onUserInputChange(args..), 'Expected'); //t.false(onUserInputChange(args..), 'Expected'); //t.throws(onUserInputChange(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/once/once.test.js b/test/once/once.test.js index 6d457fe01..89aba3872 100644 --- a/test/once/once.test.js +++ b/test/once/once.test.js @@ -5,9 +5,10 @@ test('Testing once', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof once === 'function', 'once is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(once(args..), 'Expected'); //t.equal(once(args..), 'Expected'); //t.false(once(args..), 'Expected'); //t.throws(once(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/radsToDegrees/radsToDegrees.js b/test/radsToDegrees/radsToDegrees.js new file mode 100644 index 000000000..d9a370b78 --- /dev/null +++ b/test/radsToDegrees/radsToDegrees.js @@ -0,0 +1,2 @@ +const radsToDegrees = rad => rad * 180.0 / Math.PI; +module.exports = radsToDegrees; \ No newline at end of file diff --git a/test/radsToDegrees/radsToDegrees.test.js b/test/radsToDegrees/radsToDegrees.test.js new file mode 100644 index 000000000..446485d76 --- /dev/null +++ b/test/radsToDegrees/radsToDegrees.test.js @@ -0,0 +1,14 @@ +const test = require('tape'); +const radsToDegrees = require('./radsToDegrees.js'); + +test('Testing radsToDegrees', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof radsToDegrees === 'function', 'radsToDegrees is a Function'); + t.equal(radsToDegrees(Math.PI / 2), 90, 'Returns the appropriate value'); + //t.deepEqual(radsToDegrees(args..), 'Expected'); + //t.equal(radsToDegrees(args..), 'Expected'); + //t.false(radsToDegrees(args..), 'Expected'); + //t.throws(radsToDegrees(args..), 'Expected'); + t.end(); +}); diff --git a/test/randomHexColorCode/randomHexColorCode.test.js b/test/randomHexColorCode/randomHexColorCode.test.js index eb2cdba9f..7d9946955 100644 --- a/test/randomHexColorCode/randomHexColorCode.test.js +++ b/test/randomHexColorCode/randomHexColorCode.test.js @@ -7,7 +7,9 @@ test('Testing randomHexColorCode', (t) => { t.true(typeof randomHexColorCode === 'function', 'randomHexColorCode is a Function'); //t.deepEqual(randomHexColorCode(args..), 'Expected'); t.equal(randomHexColorCode().length, 7); + t.true(randomHexColorCode().startsWith('#'),'The color code starts with "#"'); + t.true(randomHexColorCode().slice(1).match(/[^0123456789abcdef]/i) === null,'The color code contains only valid hex-digits'); //t.false(randomHexColorCode(args..), 'Expected'); //t.throws(randomHexColorCode(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/setStyle/setStyle.test.js b/test/setStyle/setStyle.test.js index 796ab1316..3c8e4aa94 100644 --- a/test/setStyle/setStyle.test.js +++ b/test/setStyle/setStyle.test.js @@ -5,9 +5,10 @@ test('Testing setStyle', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof setStyle === 'function', 'setStyle is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(setStyle(args..), 'Expected'); //t.equal(setStyle(args..), 'Expected'); //t.false(setStyle(args..), 'Expected'); //t.throws(setStyle(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/show/show.test.js b/test/show/show.test.js index 06cf5e26e..859735338 100644 --- a/test/show/show.test.js +++ b/test/show/show.test.js @@ -5,9 +5,10 @@ test('Testing show', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof show === 'function', 'show is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(show(args..), 'Expected'); //t.equal(show(args..), 'Expected'); //t.false(show(args..), 'Expected'); //t.throws(show(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/sleep/sleep.test.js b/test/sleep/sleep.test.js index 8d5388924..57beb16e2 100644 --- a/test/sleep/sleep.test.js +++ b/test/sleep/sleep.test.js @@ -5,9 +5,13 @@ test('Testing sleep', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof sleep === 'function', 'sleep is a Function'); + async function sleepyWork() { + await sleep(1000); + t.pass('Works as expected'); + } //t.deepEqual(sleep(args..), 'Expected'); //t.equal(sleep(args..), 'Expected'); //t.false(sleep(args..), 'Expected'); //t.throws(sleep(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/stableSort/stableSort.js b/test/stableSort/stableSort.js new file mode 100644 index 000000000..452d6b726 --- /dev/null +++ b/test/stableSort/stableSort.js @@ -0,0 +1,6 @@ +const stableSort = (arr, compare) => +arr +.map((item, index) => ({ item, index })) +.sort((a, b) => compare(a.item, b.item) || a.index - b.index) +.map(({ item }) => item); +module.exports = stableSort; \ No newline at end of file diff --git a/test/stableSort/stableSort.test.js b/test/stableSort/stableSort.test.js new file mode 100644 index 000000000..e8cf95496 --- /dev/null +++ b/test/stableSort/stableSort.test.js @@ -0,0 +1,17 @@ +const test = require('tape'); +const stableSort = require('./stableSort.js'); + +test('Testing stableSort', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof stableSort === 'function', 'stableSort is a Function'); + //t.deepEqual(stableSort(args..), 'Expected'); + //t.equal(stableSort(args..), 'Expected'); + //t.false(stableSort(args..), 'Expected'); + //t.throws(stableSort(args..), 'Expected'); + + const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + const compare = () => 0; + t.deepEqual(stableSort(arr, compare), arr, 'Array is properly sorted'); + t.end(); +}); diff --git a/test/throttle/throttle.test.js b/test/throttle/throttle.test.js index 4de75afdb..fcd88a603 100644 --- a/test/throttle/throttle.test.js +++ b/test/throttle/throttle.test.js @@ -5,9 +5,10 @@ test('Testing throttle', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof throttle === 'function', 'throttle is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(throttle(args..), 'Expected'); //t.equal(throttle(args..), 'Expected'); //t.false(throttle(args..), 'Expected'); //t.throws(throttle(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/timeTaken/timeTaken.test.js b/test/timeTaken/timeTaken.test.js index 45d8aa0eb..fd72e9d13 100644 --- a/test/timeTaken/timeTaken.test.js +++ b/test/timeTaken/timeTaken.test.js @@ -5,9 +5,10 @@ test('Testing timeTaken', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof timeTaken === 'function', 'timeTaken is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(timeTaken(args..), 'Expected'); //t.equal(timeTaken(args..), 'Expected'); //t.false(timeTaken(args..), 'Expected'); //t.throws(timeTaken(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/toggleClass/toggleClass.test.js b/test/toggleClass/toggleClass.test.js index f7424a31d..f0bbe14d6 100644 --- a/test/toggleClass/toggleClass.test.js +++ b/test/toggleClass/toggleClass.test.js @@ -5,9 +5,10 @@ test('Testing toggleClass', (t) => { //For more information on all the methods supported by tape //Please go to https://github.com/substack/tape t.true(typeof toggleClass === 'function', 'toggleClass is a Function'); + t.pass('Tested by @chalarangelo on 16/02/2018'); //t.deepEqual(toggleClass(args..), 'Expected'); //t.equal(toggleClass(args..), 'Expected'); //t.false(toggleClass(args..), 'Expected'); //t.throws(toggleClass(args..), 'Expected'); t.end(); -}); \ No newline at end of file +}); diff --git a/test/uncurry/uncurry.js b/test/uncurry/uncurry.js new file mode 100644 index 000000000..a926982d5 --- /dev/null +++ b/test/uncurry/uncurry.js @@ -0,0 +1,6 @@ +const uncurry = (fn, n = 1) => (...args) => { +const next = acc => args => args.reduce((x, y) => x(y), acc); +if (n > args.length) throw new RangeError('Arguments too few!'); +return next(fn)(args.slice(0, n)); +}; +module.exports = uncurry; \ No newline at end of file diff --git a/test/uncurry/uncurry.test.js b/test/uncurry/uncurry.test.js new file mode 100644 index 000000000..76ef507fa --- /dev/null +++ b/test/uncurry/uncurry.test.js @@ -0,0 +1,20 @@ +const test = require('tape'); +const uncurry = require('./uncurry.js'); + +test('Testing uncurry', (t) => { + //For more information on all the methods supported by tape + //Please go to https://github.com/substack/tape + t.true(typeof uncurry === 'function', 'uncurry is a Function'); + const add = x => y => z => x + y + z; + const add1 = uncurry(add); + const add2 = uncurry(add, 2); + const add3 = uncurry(add, 3); + t.equal(add1(1)(2)(3), 6, 'Works without a provided value for n'); + t.equal(add2(1,2)(3), 6, 'Works without n = 2'); + t.equal(add3(1,2,3), 6, 'Works withoutn = 3'); + //t.deepEqual(uncurry(args..), 'Expected'); + //t.equal(uncurry(args..), 'Expected'); + //t.false(uncurry(args..), 'Expected'); + //t.throws(uncurry(args..), 'Expected'); + t.end(); +});