diff --git a/CHANGELOG.MD b/CHANGELOG.MD new file mode 100644 index 0000000..e2616c7 --- /dev/null +++ b/CHANGELOG.MD @@ -0,0 +1,19 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +## [Unreleased] + +## [0.1.3] - 2021-15-8 + +### Added + +- /src/collection/sortByProperty.ts - sorts an array of objects by the value of + one of it's properties (provided as a string). + +### Changed + +- moved version information to VERSION.ts in the root of the project (was + previously in the function that wrote documentation) + +### Removed diff --git a/README.MD b/README.MD index 4e3bb7e..94d36a1 100644 --- a/README.MD +++ b/README.MD @@ -2,6 +2,38 @@ ## A Typescript-First utility library for Deno +## Getting Started + +### Taskfile + +To use Taskfile, it's recommended that you alias this to your shell: + +```bash +alias deno-task='deno run --allow-run $(git rev-parse --show-toplevel)/Taskfile.ts' +``` + +Or for Windows PowerShell: + +```powershell +Set-Alias -Name deno-task -Value deno run --allow-run ./Taskfile.ts +``` + +Then you will be able to execute the following from the command line: + +- `deno-task docs` -- generates documentation. +- `deno-task fmt` -- formats the files. +- `deno-task test` -- runs the test suite. + +Thanks to +[https://dev.to/vonheikemen/a-simple-way-to-replace-npm-scripts-in-deno-4j0g](https://dev.to/vonheikemen/a-simple-way-to-replace-npm-scripts-in-deno-4j0g) +for this idea. + +### Using in your library + +Just import from deno.land, we'll take care of the rest :) + +Current version is "https://deno.land/x/denodash@0.1.3/" + ### Why yet another utility library? I'll be honest, I _love_ Lodash in my node projects. Even though I could @@ -12,7 +44,7 @@ especially when going from Node to Deno. Lodash was designed in a EMCA 3 kind of world, where things such as "filter" and "slice" were not yet part of the official Javascript prototype. Moving forward, there are a lot of utilities that a utility library can omit. Do we really need -_.last when we can just write arr[arr.length - 1]? Or _.compact when we can just +_.last when we can just write arr[arr.length - 1]? Or_.compact when we can just write arr.filter(x => !!x)? Instead of importing Lodash into the new Deno ecosystem, I thought it was a good @@ -69,7 +101,7 @@ intersectionBy(Math.floor, [2.1, 1.2], [2.3, 3.2]); At one point I considered using currying, but as this is a utility library, the inefficiency of closures in V8 makes a simple function the way to go for now. -## Current High Priority Needs: +## Current High Priority Needs ### Debounce & Throttle @@ -77,9 +109,9 @@ It can be difficult to test Debounce and Throttle, and I'm not 100% sure I got the implementation right. I'd like another pair of eyes on it if it's possible, and some more tests. -## Methods: +## Methods -### Currently Supported: +### Currently Supported - array - cartesianProduct diff --git a/VERSION.txt b/VERSION.txt new file mode 100644 index 0000000..7693c96 --- /dev/null +++ b/VERSION.txt @@ -0,0 +1 @@ +0.1.3 \ No newline at end of file diff --git a/documentation/array/cartesianProduct.md b/documentation/array/cartesianProduct.md index 13a4b8d..ec78873 100644 --- a/documentation/array/cartesianProduct.md +++ b/documentation/array/cartesianProduct.md @@ -1,17 +1,19 @@ - ## cartesianProduct #### import + ```typescript -import cartesianProduct from "https://deno.land/x/denodash@0.1.2/src/array/cartesianProduct.ts" +import cartesianProduct from "https://deno.land/x/denodash@0.1.3/src/array/cartesianProduct.ts"; ``` #### signature + ```typescript cartesianProduct = (a: T[], b: U[]): [T, U][] ``` -Takes two arrays of type T and type U respectively, and creates an array of tuple type [T, U] for every combination of the elements of a and b. +Takes two arrays of type T and type U respectively, and creates an array of +tuple type [T, U] for every combination of the elements of a and b. #### Source: @@ -27,25 +29,22 @@ export const cartesianProduct = (a: T[], b: U[]): [T, U][] => { }; export default cartesianProduct; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("cartesianProduct()", () => { - Rhum.testCase( - "Should calculate the cartesian product of two arrays", - () => { - Rhum.asserts.assertEquals(cartesianProduct(["x", "y"], [1, 2]), [ - ["x", 1], - ["x", 2], - ["y", 1], - ["y", 2], - ]); - }, - ); - }); +Rhum.testSuite("cartesianProduct()", () => { + Rhum.testCase( + "Should calculate the cartesian product of two arrays", + () => { + Rhum.asserts.assertEquals(cartesianProduct(["x", "y"], [1, 2]), [ + ["x", 1], + ["x", 2], + ["y", 1], + ["y", 2], + ]); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/array/chunk.md b/documentation/array/chunk.md index e601fb2..c8e4e3b 100644 --- a/documentation/array/chunk.md +++ b/documentation/array/chunk.md @@ -1,12 +1,13 @@ - ## chunk #### import + ```typescript -import chunk from "https://deno.land/x/denodash@0.1.2/src/array/chunk.ts" +import chunk from "https://deno.land/x/denodash@0.1.3/src/array/chunk.ts"; ``` #### signature + ```typescript chunk = (arr: T[], size: number = 1): T[][] ``` @@ -28,10 +29,9 @@ export const chunk = (arr: T[], size = 1): T[][] => { }; export default chunk; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("chunk()", () => { @@ -70,5 +70,3 @@ Rhum.testSuite("chunk()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/chunkIntoParts.md b/documentation/array/chunkIntoParts.md index 70fd2ad..8f1cc05 100644 --- a/documentation/array/chunkIntoParts.md +++ b/documentation/array/chunkIntoParts.md @@ -1,17 +1,20 @@ - ## chunkIntoParts #### import + ```typescript -import chunkIntoParts from "https://deno.land/x/denodash@0.1.2/src/array/chunkIntoParts.ts" +import chunkIntoParts from "https://deno.land/x/denodash@0.1.3/src/array/chunkIntoParts.ts"; ``` #### signature + ```typescript chunkIntoParts = (arr: T[], parts = 1): T[][] ``` -Takes an array (arr) and splits it into multiple parts (parts) of equal size. For example: an array of length 10 split into 3 parts would be split into 4, 4, and 2 parts +Takes an array (arr) and splits it into multiple parts (parts) of equal size. +For example: an array of length 10 split into 3 parts would be split into 4, 4, +and 2 parts #### Source: @@ -24,10 +27,9 @@ export const chunkIntoParts = (arr: T[], parts = 1): T[][] => { }; export default chunkIntoParts; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("chunkIntoParts()", () => { @@ -89,5 +91,3 @@ Rhum.testSuite("chunkIntoParts()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/difference.md b/documentation/array/difference.md index 25e1c96..d03440a 100644 --- a/documentation/array/difference.md +++ b/documentation/array/difference.md @@ -1,17 +1,19 @@ - ## difference #### import + ```typescript -import difference from "https://deno.land/x/denodash@0.1.2/src/array/difference.ts" +import difference from "https://deno.land/x/denodash@0.1.3/src/array/difference.ts"; ``` #### signature + ```typescript difference = (a: T[], b: T[]): T[] ``` -Takes two arrays (a, b) and returns an array of elements in a that do not exist in b +Takes two arrays (a, b) and returns an array of elements in a that do not exist +in b #### Source: @@ -23,10 +25,9 @@ export const difference = (a: T[], b: T[]): T[] => differenceBy(identity, a, b); export default difference; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("difference()", () => { @@ -37,5 +38,3 @@ Rhum.testSuite("difference()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/differenceBy.md b/documentation/array/differenceBy.md index e8a3d4f..7d6aea8 100644 --- a/documentation/array/differenceBy.md +++ b/documentation/array/differenceBy.md @@ -1,17 +1,21 @@ - ## differenceBy #### import + ```typescript -import differenceBy from "https://deno.land/x/denodash@0.1.2/src/array/differenceBy.ts" +import differenceBy from "https://deno.land/x/denodash@0.1.3/src/array/differenceBy.ts"; ``` #### signature + ```typescript differenceBy = (iteratee: Iteratee, a: T[], b: T[]): T[] ``` -Takes two arrays (a, b) and an iteratee. It returns an array of elements in a where the return of iteratee(a) does not equal any return of the map of b over iteratee. In other words, after running the function on both, remove any elements where iteratee(a) matches any iteratee(b) +Takes two arrays (a, b) and an iteratee. It returns an array of elements in a +where the return of iteratee(a) does not equal any return of the map of b over +iteratee. In other words, after running the function on both, remove any +elements where iteratee(a) matches any iteratee(b) #### Source: @@ -34,10 +38,9 @@ export const differenceBy = ( }; export default differenceBy; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("differenceBy()", () => { @@ -48,5 +51,3 @@ Rhum.testSuite("differenceBy()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/differenceWith.md b/documentation/array/differenceWith.md index ee781f3..a4b1dff 100644 --- a/documentation/array/differenceWith.md +++ b/documentation/array/differenceWith.md @@ -1,12 +1,13 @@ - ## differenceWith #### import + ```typescript -import differenceWith from "https://deno.land/x/denodash@0.1.2/src/array/differenceWith.ts" +import differenceWith from "https://deno.land/x/denodash@0.1.3/src/array/differenceWith.ts"; ``` #### signature + ```typescript differenceWith = ( comparator: Comparator, @@ -15,7 +16,9 @@ differenceWith = ( ) ``` -Takes two arrays (a, b) and a comparator (which will return a boolean). It returns an array of elements in a where no element in b returns true for comparator(elemA, elemB) +Takes two arrays (a, b) and a comparator (which will return a boolean). It +returns an array of elements in a where no element in b returns true for +comparator(elemA, elemB) #### Source: @@ -37,10 +40,9 @@ export const differenceWith = ( }; export default differenceWith; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("differenceWith()", () => { @@ -69,5 +71,3 @@ Rhum.testSuite("differenceWith()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/dropWhile.md b/documentation/array/dropWhile.md index 80ce6e6..2fd8a6f 100644 --- a/documentation/array/dropWhile.md +++ b/documentation/array/dropWhile.md @@ -1,20 +1,24 @@ - ## dropWhile #### import + ```typescript -import dropWhile from "https://deno.land/x/denodash@0.1.2/src/array/dropWhile.ts" +import dropWhile from "https://deno.land/x/denodash@0.1.3/src/array/dropWhile.ts"; ``` #### signature + ```typescript dropWhile = ( arr: T[], predicate: Predicate, - ): T[] + ): T[] ``` -Takes an array (arr) and a Predicate (predicate) which will return boolean. It iterates over the array and will drop (or more accurately, not copy) all elements until it reaches an element where predicate(element) returns false. It returns a new array. +Takes an array (arr) and a Predicate (predicate) which will return boolean. It +iterates over the array and will drop (or more accurately, not copy) all +elements until it reaches an element where predicate(element) returns false. It +returns a new array. #### Source: @@ -34,10 +38,9 @@ export const dropWhile = ( }; export default dropWhile; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("dropWhile()", () => { @@ -53,5 +56,3 @@ Rhum.testSuite("dropWhile()", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/dropWhileRight.md b/documentation/array/dropWhileRight.md index 0823c3c..cdf58f0 100644 --- a/documentation/array/dropWhileRight.md +++ b/documentation/array/dropWhileRight.md @@ -1,12 +1,13 @@ - ## dropWhileRight #### import + ```typescript -import dropWhileRight from "https://deno.land/x/denodash@0.1.2/src/array/dropWhileRight.ts" +import dropWhileRight from "https://deno.land/x/denodash@0.1.3/src/array/dropWhileRight.ts"; ``` #### signature + ```typescript dropWhileRight = ( arr: T[], @@ -14,7 +15,11 @@ dropWhileRight = ( ): T[] ``` -Takes an array (arr) and a Predicate (predicate) which will return boolean. It iterates over the array starting from the last element in the array, towards the first element, and will drop (or more accurately, not copy) all elements until it reaches an element where predicate(element) returns false. It returns a new array. +Takes an array (arr) and a Predicate (predicate) which will return boolean. It +iterates over the array starting from the last element in the array, towards the +first element, and will drop (or more accurately, not copy) all elements until +it reaches an element where predicate(element) returns false. It returns a new +array. #### Source: @@ -34,10 +39,9 @@ export const dropWhileRight = ( }; export default dropWhileRight; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("dropWhileRight()", () => { @@ -53,5 +57,3 @@ Rhum.testSuite("dropWhileRight()", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/findLastIndex.md b/documentation/array/findLastIndex.md index b1089cc..7077ed4 100644 --- a/documentation/array/findLastIndex.md +++ b/documentation/array/findLastIndex.md @@ -1,12 +1,13 @@ - ## findLastIndex #### import + ```typescript -import findLastIndex from "https://deno.land/x/denodash@0.1.2/src/array/findLastIndex.ts" +import findLastIndex from "https://deno.land/x/denodash@0.1.3/src/array/findLastIndex.ts"; ``` #### signature + ```typescript findLastIndex = ( arr: T[], @@ -14,7 +15,8 @@ findLastIndex = ( ): number ``` -Takes an array (arr) and a Predicate (predicate) and finds the last index in the array where predicate(arr[index]) returns true +Takes an array (arr) and a Predicate (predicate) and finds the last index in the +array where predicate(arr[index]) returns true #### Source: @@ -34,10 +36,9 @@ export const findLastIndex = ( }; export default findLastIndex; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("findLastIndex()", () => { @@ -50,5 +51,3 @@ Rhum.testSuite("findLastIndex()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/flatten.md b/documentation/array/flatten.md index f5b71c6..6d8cfc8 100644 --- a/documentation/array/flatten.md +++ b/documentation/array/flatten.md @@ -1,12 +1,13 @@ - ## flatten #### import + ```typescript -import flatten from "https://deno.land/x/denodash@0.1.2/src/array/flatten.ts" +import flatten from "https://deno.land/x/denodash@0.1.3/src/array/flatten.ts"; ``` #### signature + ```typescript flatten = (arr: any[]): any[] ``` @@ -21,10 +22,9 @@ import flattenDepth from "./flattenDepth.ts"; export const flatten = (arr: any[]): any[] => flattenDepth(arr, 1); export default flatten; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("flatten()", () => { @@ -34,5 +34,3 @@ Rhum.testSuite("flatten()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/flattenDeep.md b/documentation/array/flattenDeep.md index eae6211..071df4f 100644 --- a/documentation/array/flattenDeep.md +++ b/documentation/array/flattenDeep.md @@ -1,12 +1,13 @@ - ## flattenDeep #### import + ```typescript -import flattenDeep from "https://deno.land/x/denodash@0.1.2/src/array/flattenDeep.ts" +import flattenDeep from "https://deno.land/x/denodash@0.1.3/src/array/flattenDeep.ts"; ``` #### signature + ```typescript flattenDeep = (arr: any[]): any[] ``` @@ -22,10 +23,9 @@ export const flattenDeep = (arr: any[]) => flattenDepth(arr, Number.MAX_SAFE_INTEGER); export default flattenDeep; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("flattenDeep()", () => { @@ -35,5 +35,3 @@ Rhum.testSuite("flattenDeep()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/flattenDepth.md b/documentation/array/flattenDepth.md index aec9daa..e892624 100644 --- a/documentation/array/flattenDepth.md +++ b/documentation/array/flattenDepth.md @@ -1,17 +1,19 @@ - ## flattenDepth #### import + ```typescript -import flattenDepth from "https://deno.land/x/denodash@0.1.2/src/array/flattenDepth.ts" +import flattenDepth from "https://deno.land/x/denodash@0.1.3/src/array/flattenDepth.ts"; ``` #### signature + ```typescript flattenDepth = (arr: any[], level: number = 1): any[] ``` -Takes an array (arr) and flattens it by (level) levels. There is a third parameter, currLevel, which is only used internally for recursion. +Takes an array (arr) and flattens it by (level) levels. There is a third +parameter, currLevel, which is only used internally for recursion. #### Source: @@ -34,10 +36,9 @@ export const flattenDepth = (arr: any[], level = 1, currLevel = 1): any[] => { }; export default flattenDepth; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("flattenDepth()", () => { @@ -49,5 +50,3 @@ Rhum.testSuite("flattenDepth()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/fromPairs.md b/documentation/array/fromPairs.md index f79d4fa..b7bf02b 100644 --- a/documentation/array/fromPairs.md +++ b/documentation/array/fromPairs.md @@ -1,19 +1,21 @@ - ## fromPairs #### import + ```typescript -import fromPairs from "https://deno.land/x/denodash@0.1.2/src/array/fromPairs.ts" +import fromPairs from "https://deno.land/x/denodash@0.1.3/src/array/fromPairs.ts"; ``` #### signature + ```typescript fromPairs = ( arr: Array<[K, T]>, ): Record ``` -Takes an array of tuples of [key: K, value: T] and returns an object where {[key: k]: value} +Takes an array of tuples of [key: K, value: T] and returns an object where +{[key: k]: value} #### Source: @@ -33,10 +35,9 @@ export const fromPairs = ( }; export default fromPairs; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("fromPairs()", () => { @@ -52,5 +53,3 @@ Rhum.testSuite("fromPairs()", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/intersection.md b/documentation/array/intersection.md index 740be0c..4ffbd77 100644 --- a/documentation/array/intersection.md +++ b/documentation/array/intersection.md @@ -1,17 +1,19 @@ - ## intersection #### import + ```typescript -import intersection from "https://deno.land/x/denodash@0.1.2/src/array/intersection.ts" +import intersection from "https://deno.land/x/denodash@0.1.3/src/array/intersection.ts"; ``` #### signature + ```typescript intersection = (...arrays: T[][]): T[] ``` -Takes any number of arrays and returns every element that occurs in each array. The order is determined by the first array passed in. +Takes any number of arrays and returns every element that occurs in each array. +The order is determined by the first array passed in. #### Source: @@ -23,10 +25,9 @@ export const intersection = (...arrays: T[][]): T[] => intersectionBy(identity, ...arrays); export default intersection; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("intersection()", () => { @@ -42,5 +43,3 @@ Rhum.testSuite("intersection()", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/intersectionBy.md b/documentation/array/intersectionBy.md index c5b3b41..f11bafc 100644 --- a/documentation/array/intersectionBy.md +++ b/documentation/array/intersectionBy.md @@ -1,17 +1,20 @@ - ## intersectionBy #### import + ```typescript -import intersectionBy from "https://deno.land/x/denodash@0.1.2/src/array/intersectionBy.ts" +import intersectionBy from "https://deno.land/x/denodash@0.1.3/src/array/intersectionBy.ts"; ``` #### signature + ```typescript intersectionBy = (fn: Iteratee, ...arrays: T[][]): T[] ``` -Takes any number of arrays and returns every element in the first array where iteratee(elementOfFirst) has the same value as iteratee(oneOfTheElementsOfTheOtherArray/s) +Takes any number of arrays and returns every element in the first array where +iteratee(elementOfFirst) has the same value as +iteratee(oneOfTheElementsOfTheOtherArray/s) #### Source: @@ -36,10 +39,9 @@ const intersectionBy = (fn: Iteratee, ...arrays: T[][]): T[] => { }; export default intersectionBy; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("intersectionBy()", () => { @@ -65,5 +67,3 @@ Rhum.testSuite("intersectionBy()", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/intersectionWith.md b/documentation/array/intersectionWith.md index 4c2373f..a02c1c9 100644 --- a/documentation/array/intersectionWith.md +++ b/documentation/array/intersectionWith.md @@ -1,12 +1,13 @@ - ## intersectionWith #### import + ```typescript -import intersectionWith from "https://deno.land/x/denodash@0.1.2/src/array/intersectionWith.ts" +import intersectionWith from "https://deno.land/x/denodash@0.1.3/src/array/intersectionWith.ts"; ``` #### signature + ```typescript intersectionWith = ( comparator: Comparator, @@ -14,7 +15,9 @@ intersectionWith = ( ): T[] ``` -Takes any number of arrays and returns every element in the first array where some element of each of the other arrays returns true when placed in the comparator with the element from the first array +Takes any number of arrays and returns every element in the first array where +some element of each of the other arrays returns true when placed in the +comparator with the element from the first array #### Source: @@ -41,10 +44,9 @@ const intersectionWith = ( }; export default intersectionWith; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("intersectionWith()", () => { @@ -71,5 +73,3 @@ Rhum.testSuite("intersectionWith()", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/lastIndexOf.md b/documentation/array/lastIndexOf.md index cabd695..f579988 100644 --- a/documentation/array/lastIndexOf.md +++ b/documentation/array/lastIndexOf.md @@ -1,17 +1,19 @@ - ## lastIndexOf #### import + ```typescript -import lastIndexOf from "https://deno.land/x/denodash@0.1.2/src/array/lastIndexOf.ts" +import lastIndexOf from "https://deno.land/x/denodash@0.1.3/src/array/lastIndexOf.ts"; ``` #### signature + ```typescript lastIndexOf = (arr: T[], target: T): number ``` -Finds the last index of array (arr) that is the target (target) and returns the index +Finds the last index of array (arr) that is the target (target) and returns the +index #### Source: @@ -22,10 +24,9 @@ export const lastIndexOf = (arr: T[], target: T): number => findLastIndex(arr, (elem: T): boolean => elem === target); export default lastIndexOf; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("lastIndexOf()", () => { @@ -35,5 +36,3 @@ Rhum.testSuite("lastIndexOf()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/partition.md b/documentation/array/partition.md index 788bc37..14e91b5 100644 --- a/documentation/array/partition.md +++ b/documentation/array/partition.md @@ -1,17 +1,20 @@ - ## partition #### import + ```typescript -import partition from "https://deno.land/x/denodash@0.1.2/src/array/partition.ts" +import partition from "https://deno.land/x/denodash@0.1.3/src/array/partition.ts"; ``` #### signature + ```typescript partition = (arr: T[], filterArray: boolean[]): [T[], T[]] ``` -Divides an array of elements into two seperate arrays. If the value in filterArray[index] is true, then arr[index] is placed in the first array of the returned tuple; otherwise it is placed in the second +Divides an array of elements into two seperate arrays. If the value in +filterArray[index] is true, then arr[index] is placed in the first array of the +returned tuple; otherwise it is placed in the second #### Source: @@ -22,23 +25,20 @@ export const partition = (arr: T[], filterArray: boolean[]): [T[], T[]] => partitionBy((_unused: T, i: number) => filterArray[i], arr); export default partition; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("partition()", () => { - Rhum.testCase( - "Should split values into two groups based on a given filter array", - () => { - Rhum.asserts.assertEquals( - partition(["beep", "boop", "foo", "bar"], [true, true, false, true]), - [["beep", "boop", "bar"], ["foo"]], - ); - }, - ); - }); +Rhum.testSuite("partition()", () => { + Rhum.testCase( + "Should split values into two groups based on a given filter array", + () => { + Rhum.asserts.assertEquals( + partition(["beep", "boop", "foo", "bar"], [true, true, false, true]), + [["beep", "boop", "bar"], ["foo"]], + ); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/array/partitionBy.md b/documentation/array/partitionBy.md index a42019b..5a61326 100644 --- a/documentation/array/partitionBy.md +++ b/documentation/array/partitionBy.md @@ -1,12 +1,13 @@ - ## partitionBy #### import + ```typescript -import partitionBy from "https://deno.land/x/denodash@0.1.2/src/array/partitionBy.ts" +import partitionBy from "https://deno.land/x/denodash@0.1.3/src/array/partitionBy.ts"; ``` #### signature + ```typescript partitionBy = ( fn: (val: T, i: number) => boolean, @@ -14,7 +15,9 @@ partitionBy = ( ): [T[], T[]] ``` -Divides an array of elements into two seperate arrays. If fn(arr[index]) returns true, then arr[index] is placed in the first array in the returned tuple, otherwise it is placed in the second. +Divides an array of elements into two seperate arrays. If fn(arr[index]) returns +true, then arr[index] is placed in the first array in the returned tuple, +otherwise it is placed in the second. #### Source: @@ -36,28 +39,25 @@ export const partitionBy = ( }; export default partitionBy; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("partitionBy()", () => { - Rhum.testCase( - "Should split values into two groups based on a given filter function", - () => { - Rhum.asserts.assertEquals( - partitionBy((x: string) => x.charAt(0) === "b", [ - "beep", - "boop", - "foo", - "bar", - ]), - [["beep", "boop", "bar"], ["foo"]], - ); - }, - ); - }); +Rhum.testSuite("partitionBy()", () => { + Rhum.testCase( + "Should split values into two groups based on a given filter function", + () => { + Rhum.asserts.assertEquals( + partitionBy((x: string) => x.charAt(0) === "b", [ + "beep", + "boop", + "foo", + "bar", + ]), + [["beep", "boop", "bar"], ["foo"]], + ); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/array/shank.md b/documentation/array/shank.md index ad678aa..083eba4 100644 --- a/documentation/array/shank.md +++ b/documentation/array/shank.md @@ -1,12 +1,13 @@ - ## shank #### import + ```typescript -import shank from "https://deno.land/x/denodash@0.1.2/src/array/shank.ts" +import shank from "https://deno.land/x/denodash@0.1.3/src/array/shank.ts"; ``` #### signature + ```typescript shank = ( arr: T[], @@ -16,8 +17,9 @@ shank = ( ): T[] ``` -Works exactly like [Array.prototype.splice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice), but returns a new array, - rather than mutating the original. +Works exactly like +[Array.prototype.splice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice), +but returns a new array, rather than mutating the original. #### Source: @@ -34,10 +36,9 @@ export const shank = ( .concat(arr.slice(index + delCount)); export default shank; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("shank", () => { @@ -59,5 +60,3 @@ Rhum.testSuite("shank", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/union.md b/documentation/array/union.md index a66821f..29eeafa 100644 --- a/documentation/array/union.md +++ b/documentation/array/union.md @@ -1,12 +1,13 @@ - ## union #### import + ```typescript -import union from "https://deno.land/x/denodash@0.1.2/src/array/union.ts" +import union from "https://deno.land/x/denodash@0.1.3/src/array/union.ts"; ``` #### signature + ```typescript union = (...arrays: T[][]): T[] ``` @@ -23,10 +24,9 @@ export const union = (...arrays: T[][]): T[] => unionBy(identity, ...arrays); export default union; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("union()", () => { @@ -38,5 +38,3 @@ Rhum.testSuite("union()", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/unionBy.md b/documentation/array/unionBy.md index c349a8b..813b5c3 100644 --- a/documentation/array/unionBy.md +++ b/documentation/array/unionBy.md @@ -1,17 +1,19 @@ - ## unionBy #### import + ```typescript -import unionBy from "https://deno.land/x/denodash@0.1.2/src/array/unionBy.ts" +import unionBy from "https://deno.land/x/denodash@0.1.3/src/array/unionBy.ts"; ``` #### signature + ```typescript unionBy = (fn: Iteratee, ...arrays: T[][]): T[] ``` -Creates an array of values that result in a unique value when passed through fn, in order +Creates an array of values that result in a unique value when passed through fn, +in order #### Source: @@ -32,10 +34,9 @@ export const unionBy = (fn: Iteratee, ...arrays: T[][]): T[] => { }; export default unionBy; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("unionBy()", () => { @@ -65,5 +66,3 @@ Rhum.testSuite("unionBy()", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/unionWith.md b/documentation/array/unionWith.md index 1dbd93e..580676b 100644 --- a/documentation/array/unionWith.md +++ b/documentation/array/unionWith.md @@ -1,17 +1,19 @@ - ## unionWith #### import + ```typescript -import unionWith from "https://deno.land/x/denodash@0.1.2/src/array/unionWith.ts" +import unionWith from "https://deno.land/x/denodash@0.1.3/src/array/unionWith.ts"; ``` #### signature + ```typescript unionWith = (comparator: Comparator, ...arrays: T[][]): T[] ``` -Creates an array of values where every value does equals false when passed in a comparator with every other value +Creates an array of values where every value does equals false when passed in a +comparator with every other value #### Source: @@ -30,10 +32,9 @@ const unionWith = (comparator: Comparator, ...arrays: T[][]): T[] => { }; export default unionWith; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("unionWith()", () => { @@ -64,5 +65,3 @@ Rhum.testSuite("unionWith()", () => { ); }); ``` - - \ No newline at end of file diff --git a/documentation/array/unzip.md b/documentation/array/unzip.md index 1254a55..2d4f192 100644 --- a/documentation/array/unzip.md +++ b/documentation/array/unzip.md @@ -1,17 +1,19 @@ - ## unzip #### import + ```typescript -import unzip from "https://deno.land/x/denodash@0.1.2/src/array/unzip.ts" +import unzip from "https://deno.land/x/denodash@0.1.3/src/array/unzip.ts"; ``` #### signature + ```typescript unzip = (arrays: any[][]): any[][] ``` -accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration. +accepts an array of grouped elements and creates an array regrouping the +elements to their pre-zip configuration. #### Source: @@ -21,10 +23,9 @@ import zip from "./zip.ts"; const unzip = (arrays: any[][]): any[][] => zip(...arrays); export default unzip; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("unzip()", () => { @@ -43,5 +44,3 @@ Rhum.testSuite("unzip()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/xor.md b/documentation/array/xor.md index f796a15..4d89eb2 100644 --- a/documentation/array/xor.md +++ b/documentation/array/xor.md @@ -1,17 +1,19 @@ - ## xor #### import + ```typescript -import xor from "https://deno.land/x/denodash@0.1.2/src/array/xor.ts" +import xor from "https://deno.land/x/denodash@0.1.3/src/array/xor.ts"; ``` #### signature + ```typescript xor = (...arrays: any[][]): any[] ``` -Creates an array of elements where the element appears in one and only one of the arrays +Creates an array of elements where the element appears in one and only one of +the arrays #### Source: @@ -26,10 +28,9 @@ export const xor = (...arrays: any[][]): any[] => { }; export default xor; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("xor()", () => { @@ -38,5 +39,3 @@ Rhum.testSuite("xor()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/array/zip.md b/documentation/array/zip.md index 877fdaf..f6b3900 100644 --- a/documentation/array/zip.md +++ b/documentation/array/zip.md @@ -1,17 +1,20 @@ - ## zip #### import + ```typescript -import zip from "https://deno.land/x/denodash@0.1.2/src/array/zip.ts" +import zip from "https://deno.land/x/denodash@0.1.3/src/array/zip.ts"; ``` #### signature + ```typescript zip = (...arrays: any[][]): any[] ``` -Creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. +Creates an array of grouped elements, the first of which contains the first +elements of the given arrays, the second of which contains the second elements +of the given arrays, and so on. #### Source: @@ -25,10 +28,9 @@ const zip = (...arrays: any[][]): any[] => { }; export default zip; - ``` -#### Test Examples: +#### Test Examples: ```typescript Rhum.testSuite("zip()", () => { @@ -40,5 +42,3 @@ Rhum.testSuite("zip()", () => { }); }); ``` - - \ No newline at end of file diff --git a/documentation/collection/count.md b/documentation/collection/count.md index f5669cc..4a0fd45 100644 --- a/documentation/collection/count.md +++ b/documentation/collection/count.md @@ -1,19 +1,21 @@ - ## count #### import + ```typescript -import count from "https://deno.land/x/denodash@0.1.2/src/collection/count.ts" +import count from "https://deno.land/x/denodash@0.1.3/src/collection/count.ts"; ``` #### signature + ```typescript count = ( arr: T[], ): Record ``` -Counts the number of occurances of each element and returns a Record count of elements +Counts the number of occurances of each element and returns a Record count of elements #### Source: @@ -26,31 +28,28 @@ export const count = ( ): Record => countBy(identity, arr); export default count; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("countBy()", () => { - Rhum.testCase( - "Should create an object composed of keys generated by running each element of collection through the iteratee", - () => { - Rhum.asserts.assertEquals(countBy(Math.floor, [6.1, 4.2, 6.3]), { - "4": 1, - "6": 2, - }); - Rhum.asserts.assertEquals( - countBy((elem: string) => elem.length, ["one", "two", "three"]), - { "3": 2, "5": 1 }, - ); - Rhum.asserts.assertEquals( - countBy(identity, ["foo", "bar", "foo", "bar", "foo", "baz"]), - { bar: 2, baz: 1, foo: 3 }, - ); - }, - ); - }); +Rhum.testSuite("countBy()", () => { + Rhum.testCase( + "Should create an object composed of keys generated by running each element of collection through the iteratee", + () => { + Rhum.asserts.assertEquals(countBy(Math.floor, [6.1, 4.2, 6.3]), { + "4": 1, + "6": 2, + }); + Rhum.asserts.assertEquals( + countBy((elem: string) => elem.length, ["one", "two", "three"]), + { "3": 2, "5": 1 }, + ); + Rhum.asserts.assertEquals( + countBy(identity, ["foo", "bar", "foo", "bar", "foo", "baz"]), + { bar: 2, baz: 1, foo: 3 }, + ); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/countBy.md b/documentation/collection/countBy.md index 58cfab8..3d0671d 100644 --- a/documentation/collection/countBy.md +++ b/documentation/collection/countBy.md @@ -1,12 +1,13 @@ - ## countBy #### import + ```typescript -import countBy from "https://deno.land/x/denodash@0.1.2/src/collection/countBy.ts" +import countBy from "https://deno.land/x/denodash@0.1.3/src/collection/countBy.ts"; ``` #### signature + ```typescript countBy = ( iteratee: Iteratee, @@ -14,7 +15,8 @@ countBy = ( ): Record ``` -Runs each element through a iteratee, and returns a count of how many times the result occurs. Returns Record count of results +Runs each element through a iteratee, and returns a count of how many times the +result occurs. Returns Record count of results #### Source: @@ -34,31 +36,28 @@ export const countBy = ( }; export default countBy; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("countBy()", () => { - Rhum.testCase( - "Should create an object composed of keys generated by running each element of collection through the iteratee", - () => { - Rhum.asserts.assertEquals(countBy(Math.floor, [6.1, 4.2, 6.3]), { - "4": 1, - "6": 2, - }); - Rhum.asserts.assertEquals( - countBy((elem: string) => elem.length, ["one", "two", "three"]), - { "3": 2, "5": 1 }, - ); - Rhum.asserts.assertEquals( - countBy(identity, ["foo", "bar", "foo", "bar", "foo", "baz"]), - { bar: 2, baz: 1, foo: 3 }, - ); - }, - ); - }); +Rhum.testSuite("countBy()", () => { + Rhum.testCase( + "Should create an object composed of keys generated by running each element of collection through the iteratee", + () => { + Rhum.asserts.assertEquals(countBy(Math.floor, [6.1, 4.2, 6.3]), { + "4": 1, + "6": 2, + }); + Rhum.asserts.assertEquals( + countBy((elem: string) => elem.length, ["one", "two", "three"]), + { "3": 2, "5": 1 }, + ); + Rhum.asserts.assertEquals( + countBy(identity, ["foo", "bar", "foo", "bar", "foo", "baz"]), + { bar: 2, baz: 1, foo: 3 }, + ); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/flatMapDeep.md b/documentation/collection/flatMapDeep.md index 721de96..8bd6297 100644 --- a/documentation/collection/flatMapDeep.md +++ b/documentation/collection/flatMapDeep.md @@ -1,17 +1,20 @@ - ## flatMapDeep #### import + ```typescript -import flatMapDeep from "https://deno.land/x/denodash@0.1.2/src/collection/flatMapDeep.ts" +import flatMapDeep from "https://deno.land/x/denodash@0.1.3/src/collection/flatMapDeep.ts"; ``` #### signature + ```typescript flatMapDeep = (iteratee: Iteratee, arr: T[]): U[] ``` -Runs each element through a iteratee, and returns a flatMap of the results after being run through iteratee. T is the parameter type of iteratee, U is the return type of the iteratee, which may or may not be the same type. +Runs each element through a iteratee, and returns a flatMap of the results after +being run through iteratee. T is the parameter type of iteratee, U is the return +type of the iteratee, which may or may not be the same type. #### Source: @@ -23,20 +26,17 @@ export const flatMapDeep = (iteratee: Iteratee, arr: T[]): U[] => flatMapDepth(iteratee, arr, Number.MAX_SAFE_INTEGER); export default flatMapDeep; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("flatMapDeep()", () => { - Rhum.testCase("should created a deeply flattened flatmap", () => { - Rhum.asserts.assertEquals( - flatMapDeep((x: number) => [[x * 2]], [1, 2, 3, 4]), - [2, 4, 6, 8], - ); - }); +Rhum.testSuite("flatMapDeep()", () => { + Rhum.testCase("should created a deeply flattened flatmap", () => { + Rhum.asserts.assertEquals( + flatMapDeep((x: number) => [[x * 2]], [1, 2, 3, 4]), + [2, 4, 6, 8], + ); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/flatMapDepth.md b/documentation/collection/flatMapDepth.md index 57f4d85..67f52cf 100644 --- a/documentation/collection/flatMapDepth.md +++ b/documentation/collection/flatMapDepth.md @@ -1,12 +1,13 @@ - ## flatMapDepth #### import + ```typescript -import flatMapDepth from "https://deno.land/x/denodash@0.1.2/src/collection/flatMapDepth.ts" +import flatMapDepth from "https://deno.land/x/denodash@0.1.3/src/collection/flatMapDepth.ts"; ``` #### signature + ```typescript flatMapDepth = ( iteratee: Iteratee, @@ -15,7 +16,9 @@ flatMapDepth = ( ): U[] ``` -Runs each element through a iteratee, and returns a flatMap of the results after being run through iteratee. T is the parameter type of iteratee, up to a number of levels specified. +Runs each element through a iteratee, and returns a flatMap of the results after +being run through iteratee. T is the parameter type of iteratee, up to a number +of levels specified. #### Source: @@ -31,26 +34,23 @@ export const flatMapDepth = ( ): U[] => flattenDepth(arr.map(iteratee), depth); export default flatMapDepth; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("flatMapDepth()", () => { - Rhum.testCase( - "should created a flattened flatmap only to a certain level", - () => { - Rhum.asserts.assertEquals( - flatMapDepth((n: any) => [[[n, n]]], [1, 2], 2), - [ - [1, 1], - [2, 2], - ], - ); - }, - ); - }); +Rhum.testSuite("flatMapDepth()", () => { + Rhum.testCase( + "should created a flattened flatmap only to a certain level", + () => { + Rhum.asserts.assertEquals( + flatMapDepth((n: any) => [[[n, n]]], [1, 2], 2), + [ + [1, 1], + [2, 2], + ], + ); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/groupBy.md b/documentation/collection/groupBy.md index e69482e..5022ff0 100644 --- a/documentation/collection/groupBy.md +++ b/documentation/collection/groupBy.md @@ -1,12 +1,13 @@ - ## groupBy #### import + ```typescript -import groupBy from "https://deno.land/x/denodash@0.1.2/src/collection/groupBy.ts" +import groupBy from "https://deno.land/x/denodash@0.1.3/src/collection/groupBy.ts"; ``` #### signature + ```typescript groupBy = ( iteratee: Iteratee, @@ -14,7 +15,8 @@ groupBy = ( ): Record ``` -Groups elements of an array of type T according to a criteria provided (iteratee) as a Record of arrays of type T +Groups elements of an array of type T according to a criteria provided +(iteratee) as a Record of arrays of type T #### Source: @@ -37,36 +39,30 @@ export const groupBy = ( }; export default groupBy; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("groupBy()", () => { - Rhum.testCase( - "create an object composed of keys generated from the collection run through an iteratee", - () => { - Rhum.asserts.assertEquals(groupBy(Math.floor, [6.1, 4.2, 6.3]), { - "4": [4.2], - "6": [6.1, 6.3], - }); - Rhum.asserts.assertEquals( - groupBy((n: number): string => (n % 2 === 0 ? "even" : "odd"), [ - 1, - 2, - 3, - 4, - 5, - ]), - { - even: [2, 4], - odd: [1, 3, 5], - }, - ); - }, - ); - }); +Rhum.testSuite("groupBy()", () => { + Rhum.testCase( + "create an object composed of keys generated from the collection run through an iteratee", + () => { + Rhum.asserts.assertEquals(groupBy(Math.floor, [6.1, 4.2, 6.3]), { + "4": [4.2], + "6": [6.1, 6.3], + }); + Rhum.asserts.assertEquals( + groupBy( + (n: number): string => (n % 2 === 0 ? "even" : "odd"), + [1, 2, 3, 4, 5], + ), + { + even: [2, 4], + odd: [1, 3, 5], + }, + ); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/keyBy.md b/documentation/collection/keyBy.md index 9ab128a..8ff68e8 100644 --- a/documentation/collection/keyBy.md +++ b/documentation/collection/keyBy.md @@ -1,12 +1,13 @@ - ## keyBy #### import + ```typescript -import keyBy from "https://deno.land/x/denodash@0.1.2/src/collection/keyBy.ts" +import keyBy from "https://deno.land/x/denodash@0.1.3/src/collection/keyBy.ts"; ``` #### signature + ```typescript keyBy = >( argument: Iteratee | string, @@ -14,7 +15,10 @@ keyBy = >( ): Record ``` -Creates an object composed of keys generated from the results of running each element of collection thru iteratee if a function is provide, or through (elem)=> elem[argument] if argument is a string. Input must be a Record (i.e., a key-value object with strings as keys) +Creates an object composed of keys generated from the results of running each +element of collection thru iteratee if a function is provide, or through +(elem)=> elem[argument] if argument is a string. Input must be a Record (i.e., a +key-value object with strings as keys) #### Source: @@ -37,38 +41,35 @@ export const keyBy = >( }; export default keyBy; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("keyBy()", () => { - Rhum.testCase( - "Creates an object composed of keys generated from the results of running each element of collection thru iteratee.", - () => { - interface Button { - dir: string; - code: number; - } - const testArray: Button[] = [ - { dir: "left", code: 97 }, - { dir: "right", code: 100 }, - ]; - Rhum.asserts.assertEquals( - keyBy((x: Button) => String.fromCharCode(x.code), testArray), - { - a: { dir: "left", code: 97 }, - d: { dir: "right", code: 100 }, - }, - ); - Rhum.asserts.assertEquals(keyBy("dir", testArray), { - left: { dir: "left", code: 97 }, - right: { dir: "right", code: 100 }, - }); - }, - ); - }); +Rhum.testSuite("keyBy()", () => { + Rhum.testCase( + "Creates an object composed of keys generated from the results of running each element of collection thru iteratee.", + () => { + interface Button { + dir: string; + code: number; + } + const testArray: Button[] = [ + { dir: "left", code: 97 }, + { dir: "right", code: 100 }, + ]; + Rhum.asserts.assertEquals( + keyBy((x: Button) => String.fromCharCode(x.code), testArray), + { + a: { dir: "left", code: 97 }, + d: { dir: "right", code: 100 }, + }, + ); + Rhum.asserts.assertEquals(keyBy("dir", testArray), { + left: { dir: "left", code: 97 }, + right: { dir: "right", code: 100 }, + }); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/sample.md b/documentation/collection/sample.md index 0c041c8..fa15f77 100644 --- a/documentation/collection/sample.md +++ b/documentation/collection/sample.md @@ -1,17 +1,19 @@ - ## sample #### import + ```typescript -import sample from "https://deno.land/x/denodash@0.1.2/src/collection/sample.ts" +import sample from "https://deno.land/x/denodash@0.1.3/src/collection/sample.ts"; ``` #### signature + ```typescript sample = (array: T[], sampleSize: number = 1): T[] ``` -Randomly picks n (sampleSize) elements from an array. The same element will not be picked twice. +Randomly picks n (sampleSize) elements from an array. The same element will not +be picked twice. #### Source: @@ -33,23 +35,20 @@ export const sample = (array: T[], sampleSize: number = 1): T[] => { }; export default sample; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("sample()", () => { - Rhum.testCase("gets N unique elements of an array", () => { - const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - const sampled1 = sample(arr, 3); - Rhum.asserts.assertArrayContains(arr, sampled1); - Rhum.asserts.assertStrictEquals(sampled1.length, 3); - const sampled2 = sample(arr, 5); - Rhum.asserts.assertArrayContains(arr, sampled2); - Rhum.asserts.assertStrictEquals(sampled2.length, 5); - }); +Rhum.testSuite("sample()", () => { + Rhum.testCase("gets N unique elements of an array", () => { + const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + const sampled1 = sample(arr, 3); + Rhum.asserts.assertArrayContains(arr, sampled1); + Rhum.asserts.assertStrictEquals(sampled1.length, 3); + const sampled2 = sample(arr, 5); + Rhum.asserts.assertArrayContains(arr, sampled2); + Rhum.asserts.assertStrictEquals(sampled2.length, 5); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/sampleOne.md b/documentation/collection/sampleOne.md index c4d94a8..e706715 100644 --- a/documentation/collection/sampleOne.md +++ b/documentation/collection/sampleOne.md @@ -1,12 +1,13 @@ - ## sampleOne #### import + ```typescript -import sampleOne from "https://deno.land/x/denodash@0.1.2/src/collection/sampleOne.ts" +import sampleOne from "https://deno.land/x/denodash@0.1.3/src/collection/sampleOne.ts"; ``` #### signature + ```typescript sampleOne = (array: T[]): T ``` @@ -21,20 +22,17 @@ import randomOf from "../utils/randomOf.ts"; export const sampleOne = (array: T[]): T => array[randomOf(array.length)]; export default sampleOne; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("sampleOne()", () => { - Rhum.testCase("returns one random element of an array", () => { - const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - const one = sampleOne(arr); - Rhum.asserts.assertArrayContains(arr, one); - Rhum.asserts.assertEquals(Array.isArray(one), false); - }); +Rhum.testSuite("sampleOne()", () => { + Rhum.testCase("returns one random element of an array", () => { + const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + const one = sampleOne(arr); + Rhum.asserts.assertArrayContains(arr, one); + Rhum.asserts.assertEquals(Array.isArray(one), false); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/shuffle.md b/documentation/collection/shuffle.md index 7eec634..72229b8 100644 --- a/documentation/collection/shuffle.md +++ b/documentation/collection/shuffle.md @@ -1,17 +1,19 @@ - ## shuffle #### import + ```typescript -import shuffle from "https://deno.land/x/denodash@0.1.2/src/collection/shuffle.ts" +import shuffle from "https://deno.land/x/denodash@0.1.3/src/collection/shuffle.ts"; ``` #### signature + ```typescript shuffle = (array: T[]): T[] ``` -Implements a Fischer-Yates shuffler. Takes an array and returns a new array with the same elements but randomly shuffled. +Implements a Fischer-Yates shuffler. Takes an array and returns a new array with +the same elements but randomly shuffled. #### Source: @@ -33,20 +35,17 @@ export const shuffle = (array: T[]): T[] => { }; export default shuffle; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("shuffle()", () => { - Rhum.testCase("shuffles an array", () => { - const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - const shuffled = shuffle(arr); - Rhum.asserts.assertArrayContains(shuffled, arr); - Rhum.asserts.assertNotEquals(shuffled, arr); // this test may fail, the chances of that however, are 10!:1 (3.6M/1); - }); +Rhum.testSuite("shuffle()", () => { + Rhum.testCase("shuffles an array", () => { + const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; + const shuffled = shuffle(arr); + Rhum.asserts.assertArrayContains(shuffled, arr); + Rhum.asserts.assertNotEquals(shuffled, arr); // this test may fail, the chances of that however, are 10!:1 (3.6M/1); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/sortBy.md b/documentation/collection/sortBy.md index fe5076d..21ede6c 100644 --- a/documentation/collection/sortBy.md +++ b/documentation/collection/sortBy.md @@ -1,12 +1,13 @@ - ## sortBy #### import + ```typescript -import sortBy from "https://deno.land/x/denodash@0.1.2/src/collection/sortBy.ts" +import sortBy from "https://deno.land/x/denodash@0.1.3/src/collection/sortBy.ts"; ``` #### signature + ```typescript sortBy = ( array: T[], @@ -14,7 +15,8 @@ sortBy = ( ): T[] ``` -Returns a copy of the array provided, sorted by the criteria provided (comparators). Comparators are prioritized from first to last. +Returns a copy of the array provided, sorted by the criteria provided +(comparators). Comparators are prioritized from first to last. #### Source: @@ -30,52 +32,49 @@ export const sortBy = ( return array.slice().sort((a, b) => chain(a, b)); }; export default sortBy; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("sortBy()", () => { - Rhum.testCase( - "sorts the order of the iteratees based on the criteria provided", - () => { - type TestType = { user: string; age: number }; - const testArray: TestType[] = [ - { user: "fred", age: 48 }, +Rhum.testSuite("sortBy()", () => { + Rhum.testCase( + "sorts the order of the iteratees based on the criteria provided", + () => { + type TestType = { user: string; age: number }; + const testArray: TestType[] = [ + { user: "fred", age: 48 }, + { user: "barney", age: 34 }, + { user: "fred", age: 40 }, + { user: "barney", age: 36 }, + ]; + Rhum.asserts.assertEquals( + sortBy( + testArray, + (a: TestType, b: TestType): number => a.user.localeCompare(b.user), + (a: TestType, b: TestType): number => a.age - b.age, + ), + [ { user: "barney", age: 34 }, + { user: "barney", age: 36 }, { user: "fred", age: 40 }, + { user: "fred", age: 48 }, + ], + ); + Rhum.asserts.assertEquals( + sortBy( + testArray, + (a: TestType, b: TestType): number => a.user.localeCompare(b.user), + (a: TestType, b: TestType): number => b.age - a.age, + ), + [ { user: "barney", age: 36 }, - ]; - Rhum.asserts.assertEquals( - sortBy( - testArray, - (a: TestType, b: TestType): number => a.user.localeCompare(b.user), - (a: TestType, b: TestType): number => a.age - b.age, - ), - [ - { user: "barney", age: 34 }, - { user: "barney", age: 36 }, - { user: "fred", age: 40 }, - { user: "fred", age: 48 }, - ], - ); - Rhum.asserts.assertEquals( - sortBy( - testArray, - (a: TestType, b: TestType): number => a.user.localeCompare(b.user), - (a: TestType, b: TestType): number => b.age - a.age, - ), - [ - { user: "barney", age: 36 }, - { user: "barney", age: 34 }, - { user: "fred", age: 48 }, - { user: "fred", age: 40 }, - ], - ); - }, - ); - }); + { user: "barney", age: 34 }, + { user: "fred", age: 48 }, + { user: "fred", age: 40 }, + ], + ); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/collection/sortByProperty.md b/documentation/collection/sortByProperty.md new file mode 100644 index 0000000..a6a2cb1 --- /dev/null +++ b/documentation/collection/sortByProperty.md @@ -0,0 +1,67 @@ +## sortByProperty + +#### import + +```typescript +import sortByProperty from "https://deno.land/x/denodash@0.1.3/src/collection/sortByProperty.ts"; +``` + +#### signature + +```typescript +sortByProperty = ( + array: T[], + property: string, + ): T[] | never +``` + +Returns a copy of the array provided, sorted by the value of a key provided + +#### Source: + +```typescript +export const sortByProperty = ( + array: T[], + property: string, +): T[] | never => + array.slice().sort((a, b) => { + if (typeof a[property] === "string") { + return a[property].localeCompare(b[property]); + } else { + return a[property] - b[property]; + } + }); + +export default sortByProperty; +``` + +#### Test Examples: + +```typescript +Rhum.testSuite("sortByProperty()", () => { + Rhum.testCase( + "sorts the order of the iteratees by a string provided", + () => { + type TestType = { user: string; age: number }; + const testArray: TestType[] = [ + { user: "fred", age: 48 }, + { user: "barney", age: 34 }, + { user: "fred", age: 40 }, + { user: "barney", age: 36 }, + ]; + Rhum.asserts.assertEquals(sortByProperty(testArray, "age"), [ + { user: "barney", age: 34 }, + { user: "barney", age: 36 }, + { user: "fred", age: 40 }, + { user: "fred", age: 48 }, + ]); + Rhum.asserts.assertEquals(sortByProperty(testArray, "user"), [ + { user: "barney", age: 34 }, + { user: "barney", age: 36 }, + { user: "fred", age: 48 }, + { user: "fred", age: 40 }, + ]); + }, + ); +}); +``` diff --git a/documentation/documentationObjects.ts b/documentation/documentationObjects.ts index 43996dd..a6993d1 100644 --- a/documentation/documentationObjects.ts +++ b/documentation/documentationObjects.ts @@ -283,6 +283,15 @@ const collectionDocObjects = [ description: `Returns a copy of the array provided, sorted by the criteria provided (comparators). Comparators are prioritized from first to last.`, }, + { + name: "sortByProperty", + signature: `( + array: T[], + property: string, + ): T[] | never`, + description: + `Returns a copy of the array provided, sorted by the value of a key provided`, + }, ].map((cdo) => ({ ...cdo, testFile: "src/collection.test.ts", diff --git a/documentation/function/after.md b/documentation/function/after.md index 202fdf7..162851c 100644 --- a/documentation/function/after.md +++ b/documentation/function/after.md @@ -1,12 +1,13 @@ - ## after #### import + ```typescript -import after from "https://deno.land/x/denodash@0.1.2/src/function/after.ts" +import after from "https://deno.land/x/denodash@0.1.3/src/function/after.ts"; ``` #### signature + ```typescript after = ( n: number, @@ -14,7 +15,9 @@ after = ( ): ((...args: any) => any | void) ``` -Invokes the provided function *only* after it has been called n times (inclusive). In other words, if n is 3, it will not be called on the first or second invocation, but will be called on the third. +Invokes the provided function _only_ after it has been called n times +(inclusive). In other words, if n is 3, it will not be called on the first or +second invocation, but will be called on the third. #### Source: @@ -33,28 +36,25 @@ export const after = ( }; export default after; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("after", async () => { - Rhum.testCase( - "should invoke provided function only after called at least N times", - () => { - const log: any[] = []; - const logger = (x: any) => { - log.push(x); - }; - const logAfterThree = after(3, logger); - for (let i = 0; i < 10; i++) { - logAfterThree(i); - } - Rhum.asserts.assertEquals(log, [2, 3, 4, 5, 6, 7, 8, 9]); - }, - ); - }); +Rhum.testSuite("after", async () => { + Rhum.testCase( + "should invoke provided function only after called at least N times", + () => { + const log: any[] = []; + const logger = (x: any) => { + log.push(x); + }; + const logAfterThree = after(3, logger); + for (let i = 0; i < 10; i++) { + logAfterThree(i); + } + Rhum.asserts.assertEquals(log, [2, 3, 4, 5, 6, 7, 8, 9]); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/function/before.md b/documentation/function/before.md index 65a7654..18c5621 100644 --- a/documentation/function/before.md +++ b/documentation/function/before.md @@ -1,12 +1,13 @@ - ## before #### import + ```typescript -import before from "https://deno.land/x/denodash@0.1.2/src/function/before.ts" +import before from "https://deno.land/x/denodash@0.1.3/src/function/before.ts"; ``` #### signature + ```typescript before = ( n: number, @@ -14,7 +15,8 @@ before = ( ): ((...args: any) => any | void) ``` -Creates a function that invokes fn up to only n times. Subsequent calls will return the value returned on the nth invocation. +Creates a function that invokes fn up to only n times. Subsequent calls will +return the value returned on the nth invocation. #### Source: @@ -32,32 +34,29 @@ export const before = (n: number, fn: Function) => { }; export default before; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("before()", async () => { - Rhum.testCase( - "should invoke provided function only when before N times", - () => { - let count = 0; - const incrementAndReturn = () => { - count += 1; - return count; - }; - - const beforeThree = before(3, incrementAndReturn); - - Rhum.asserts.assertStrictEquals(beforeThree(), 1); - Rhum.asserts.assertStrictEquals(beforeThree(), 2); - Rhum.asserts.assertStrictEquals(beforeThree(), 3); - Rhum.asserts.assertStrictEquals(beforeThree(), 3); - Rhum.asserts.assertStrictEquals(beforeThree(), 3); - }, - ); - }); +Rhum.testSuite("before()", async () => { + Rhum.testCase( + "should invoke provided function only when before N times", + () => { + let count = 0; + const incrementAndReturn = () => { + count += 1; + return count; + }; + + const beforeThree = before(3, incrementAndReturn); + + Rhum.asserts.assertStrictEquals(beforeThree(), 1); + Rhum.asserts.assertStrictEquals(beforeThree(), 2); + Rhum.asserts.assertStrictEquals(beforeThree(), 3); + Rhum.asserts.assertStrictEquals(beforeThree(), 3); + Rhum.asserts.assertStrictEquals(beforeThree(), 3); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/function/debounce.md b/documentation/function/debounce.md index 890b1f1..5e22183 100644 --- a/documentation/function/debounce.md +++ b/documentation/function/debounce.md @@ -1,12 +1,13 @@ - ## debounce #### import + ```typescript -import debounce from "https://deno.land/x/denodash@0.1.2/src/function/debounce.ts" +import debounce from "https://deno.land/x/denodash@0.1.3/src/function/debounce.ts"; ``` #### signature + ```typescript debounce = (func: Function, wait = 0, immediate = false): { (...args: any[]): any; @@ -14,8 +15,11 @@ debounce = (func: Function, wait = 0, immediate = false): { } ``` -Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last time the debounced function was invoked. The debounced function comes with a cancel method to cancel delayed func invocations. - +Creates a debounced function that delays invoking func until after wait +milliseconds have elapsed since the last time the debounced function was +invoked. The debounced function comes with a cancel method to cancel delayed +func invocations. + _This method is specifically requested for additional testing and code review._ #### Source: @@ -62,39 +66,36 @@ export const debounce = (func: Function, wait = 0, immediate = false): { }; export default debounce; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("debounce()", () => { - Rhum.testCase("debounce", async () => { - let counter = 0; - const incr = () => { - counter++; - }; - const debouncedIncr = debounce(incr, 32); - debouncedIncr(); - debouncedIncr(); - await delay(16); - debouncedIncr(); - await delay(96); - Rhum.asserts.assertStrictEquals(counter, 1); - }); - - Rhum.testCase("debounce cancel", async () => { - let counter = 0; - const incr = () => { - counter++; - }; - const debouncedIncr = debounce(incr, 32); - debouncedIncr(); - debouncedIncr.cancel(); - await delay(96); - Rhum.asserts.assertStrictEquals(counter, 0); - }); +Rhum.testSuite("debounce()", () => { + Rhum.testCase("debounce", async () => { + let counter = 0; + const incr = () => { + counter++; + }; + const debouncedIncr = debounce(incr, 32); + debouncedIncr(); + debouncedIncr(); + await delay(16); + debouncedIncr(); + await delay(96); + Rhum.asserts.assertStrictEquals(counter, 1); }); -``` - \ No newline at end of file + Rhum.testCase("debounce cancel", async () => { + let counter = 0; + const incr = () => { + counter++; + }; + const debouncedIncr = debounce(incr, 32); + debouncedIncr(); + debouncedIncr.cancel(); + await delay(96); + Rhum.asserts.assertStrictEquals(counter, 0); + }); +}); +``` diff --git a/documentation/function/defer.md b/documentation/function/defer.md index dd24aa0..9bea105 100644 --- a/documentation/function/defer.md +++ b/documentation/function/defer.md @@ -1,18 +1,20 @@ - ## defer #### import + ```typescript -import defer from "https://deno.land/x/denodash@0.1.2/src/function/defer.ts" +import defer from "https://deno.land/x/denodash@0.1.3/src/function/defer.ts"; ``` #### signature + ```typescript defer = (fn: Function, ...args: any[]): void ``` -Defers invoking the func until the current call stack has cleared. Any additional arguments are provided to func when it's invoked. - +Defers invoking the func until the current call stack has cleared. Any +additional arguments are provided to func when it's invoked. + _This method is specifically requested for additional testing and code review._ #### Source: @@ -23,25 +25,22 @@ export const defer = (fn: Function, ...args: any[]): void => { }; export default defer; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("defer()", () => { - Rhum.testCase( - "Should defer a function until the call stack has cleared", - async () => { - let deferred = false; - defer((bool: boolean): void => { - deferred = bool; - }, true); - await delay(50); - Rhum.asserts.assertStrictEquals(deferred, true); - }, - ); - }); +Rhum.testSuite("defer()", () => { + Rhum.testCase( + "Should defer a function until the call stack has cleared", + async () => { + let deferred = false; + defer((bool: boolean): void => { + deferred = bool; + }, true); + await delay(50); + Rhum.asserts.assertStrictEquals(deferred, true); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/function/memoize.md b/documentation/function/memoize.md index 4994994..1285712 100644 --- a/documentation/function/memoize.md +++ b/documentation/function/memoize.md @@ -1,12 +1,13 @@ - ## memoize #### import + ```typescript -import memoize from "https://deno.land/x/denodash@0.1.2/src/function/memoize.ts" +import memoize from "https://deno.land/x/denodash@0.1.3/src/function/memoize.ts"; ``` #### signature + ```typescript memoize = ( @@ -15,10 +16,11 @@ memoize = ): ((...args: any[]) => any) /* type HashingFunction = (...args: any[]) => string; */ - ``` -Creates a function that memoizes the result of func by the result of a hashing function. If no hashing function is provided, it uses normalHashingFunction, which is (...args) => JSON.stringify(args) +Creates a function that memoizes the result of func by the result of a hashing +function. If no hashing function is provided, it uses normalHashingFunction, +which is (...args) => JSON.stringify(args) #### Source: @@ -42,67 +44,64 @@ export const memoize = ( }; export default memoize; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("memoize()", () => { - Rhum.testCase("should memoize a function", async () => { - let timesInvoked = 0; - const testFn = (s: string) => { - timesInvoked += 1; - return s.split("").reverse().join(""); - }; - const memoTestFn = memoize(testFn); - - const t1 = memoTestFn("alpha"); - Rhum.asserts.assertStrictEquals(timesInvoked, 1); - Rhum.asserts.assertStrictEquals(t1, "ahpla"); - - const t2 = memoTestFn("beta"); - Rhum.asserts.assertStrictEquals(timesInvoked, 2); - Rhum.asserts.assertStrictEquals(t2, "ateb"); - - const t3 = memoTestFn("alpha"); - Rhum.asserts.assertStrictEquals(timesInvoked, 2); - Rhum.asserts.assertStrictEquals(t3, "ahpla"); - - const t4 = memoTestFn("beta"); - Rhum.asserts.assertStrictEquals(timesInvoked, 2); - Rhum.asserts.assertStrictEquals(t4, "ateb"); - }); - Rhum.testCase("should memoize a function with a custom hash", async () => { - let timesInvoked = 0; - const testFn = (s: string) => { - timesInvoked += 1; - return s.split("").reverse().join(""); - }; - const customHasher = (s: string): string => s.charAt(0); - const memoTestFn = memoize(testFn, customHasher); - - const t1 = memoTestFn("alpha"); - Rhum.asserts.assertStrictEquals(timesInvoked, 1); - Rhum.asserts.assertStrictEquals(t1, "ahpla"); - - const t2 = memoTestFn("beta"); - Rhum.asserts.assertStrictEquals(timesInvoked, 2); - Rhum.asserts.assertStrictEquals(t2, "ateb"); - - const t3 = memoTestFn("alpha"); - Rhum.asserts.assertStrictEquals(timesInvoked, 2); - Rhum.asserts.assertStrictEquals(t3, "ahpla"); - - const t4 = memoTestFn("beta"); - Rhum.asserts.assertStrictEquals(timesInvoked, 2); - Rhum.asserts.assertStrictEquals(t4, "ateb"); - - const t5 = memoTestFn("brian"); - Rhum.asserts.assertStrictEquals(timesInvoked, 2); - Rhum.asserts.assertStrictEquals(t5, "ateb"); - }); +Rhum.testSuite("memoize()", () => { + Rhum.testCase("should memoize a function", async () => { + let timesInvoked = 0; + const testFn = (s: string) => { + timesInvoked += 1; + return s.split("").reverse().join(""); + }; + const memoTestFn = memoize(testFn); + + const t1 = memoTestFn("alpha"); + Rhum.asserts.assertStrictEquals(timesInvoked, 1); + Rhum.asserts.assertStrictEquals(t1, "ahpla"); + + const t2 = memoTestFn("beta"); + Rhum.asserts.assertStrictEquals(timesInvoked, 2); + Rhum.asserts.assertStrictEquals(t2, "ateb"); + + const t3 = memoTestFn("alpha"); + Rhum.asserts.assertStrictEquals(timesInvoked, 2); + Rhum.asserts.assertStrictEquals(t3, "ahpla"); + + const t4 = memoTestFn("beta"); + Rhum.asserts.assertStrictEquals(timesInvoked, 2); + Rhum.asserts.assertStrictEquals(t4, "ateb"); + }); + Rhum.testCase("should memoize a function with a custom hash", async () => { + let timesInvoked = 0; + const testFn = (s: string) => { + timesInvoked += 1; + return s.split("").reverse().join(""); + }; + const customHasher = (s: string): string => s.charAt(0); + const memoTestFn = memoize(testFn, customHasher); + + const t1 = memoTestFn("alpha"); + Rhum.asserts.assertStrictEquals(timesInvoked, 1); + Rhum.asserts.assertStrictEquals(t1, "ahpla"); + + const t2 = memoTestFn("beta"); + Rhum.asserts.assertStrictEquals(timesInvoked, 2); + Rhum.asserts.assertStrictEquals(t2, "ateb"); + + const t3 = memoTestFn("alpha"); + Rhum.asserts.assertStrictEquals(timesInvoked, 2); + Rhum.asserts.assertStrictEquals(t3, "ahpla"); + + const t4 = memoTestFn("beta"); + Rhum.asserts.assertStrictEquals(timesInvoked, 2); + Rhum.asserts.assertStrictEquals(t4, "ateb"); + + const t5 = memoTestFn("brian"); + Rhum.asserts.assertStrictEquals(timesInvoked, 2); + Rhum.asserts.assertStrictEquals(t5, "ateb"); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/function/once.md b/documentation/function/once.md index 73e10c2..4da5f47 100644 --- a/documentation/function/once.md +++ b/documentation/function/once.md @@ -1,17 +1,19 @@ - ## once #### import + ```typescript -import once from "https://deno.land/x/denodash@0.1.2/src/function/once.ts" +import once from "https://deno.land/x/denodash@0.1.3/src/function/once.ts"; ``` #### signature + ```typescript once = (fn: Function): ((...args: any) => any | void) ``` -Creates a function that only runs once. Further invocations will return the same value as originally resolved. +Creates a function that only runs once. Further invocations will return the same +value as originally resolved. #### Source: @@ -22,32 +24,29 @@ export const once = (fn: Function): ((...args: any) => any | void) => before(1, fn); export default once; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("once()", () => { - Rhum.testCase("should only invoke provided function once", () => { - let counter = 0; - let expected: number; - const incr = (n: number) => { - counter += 1; - return n; - }; - const onceIncr = once(incr); - expected = onceIncr(4); - Rhum.asserts.assertStrictEquals(expected, 4); - Rhum.asserts.assertStrictEquals(counter, 1); - expected = onceIncr(70); - Rhum.asserts.assertStrictEquals(expected, 4); - Rhum.asserts.assertStrictEquals(counter, 1); - expected = onceIncr(52830); - Rhum.asserts.assertStrictEquals(expected, 4); - Rhum.asserts.assertStrictEquals(counter, 1); - }); +Rhum.testSuite("once()", () => { + Rhum.testCase("should only invoke provided function once", () => { + let counter = 0; + let expected: number; + const incr = (n: number) => { + counter += 1; + return n; + }; + const onceIncr = once(incr); + expected = onceIncr(4); + Rhum.asserts.assertStrictEquals(expected, 4); + Rhum.asserts.assertStrictEquals(counter, 1); + expected = onceIncr(70); + Rhum.asserts.assertStrictEquals(expected, 4); + Rhum.asserts.assertStrictEquals(counter, 1); + expected = onceIncr(52830); + Rhum.asserts.assertStrictEquals(expected, 4); + Rhum.asserts.assertStrictEquals(counter, 1); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/function/overArgs.md b/documentation/function/overArgs.md index 1a3017e..7acc8e9 100644 --- a/documentation/function/overArgs.md +++ b/documentation/function/overArgs.md @@ -1,12 +1,13 @@ - ## overArgs #### import + ```typescript -import overArgs from "https://deno.land/x/denodash@0.1.2/src/function/overArgs.ts" +import overArgs from "https://deno.land/x/denodash@0.1.3/src/function/overArgs.ts"; ``` #### signature + ```typescript overArgs = ( fn: Function, @@ -15,7 +16,8 @@ overArgs = ( ): any ``` -Maps the arguments (args) through the mapping functions (mapFuncs) and is then passed to fn and returns the result of fn with the mapped arguments +Maps the arguments (args) through the mapping functions (mapFuncs) and is then +passed to fn and returns the result of fn with the mapped arguments #### Source: @@ -32,27 +34,24 @@ export const overArgs = ( ); export default overArgs; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("overArgs()", () => { - Rhum.testCase("invokes func with its arguments transformed.", () => { - const double = (n: number): number => n * 2; - const square = (n: number): number => n * n; - - Rhum.asserts.assertEquals( - overArgs((x: number, y: number) => [x, y], [square, double], 9, 3), - [81, 6], - ); - Rhum.asserts.assertEquals( - overArgs((x: number, y: number) => [x, y], [square, double], 10, 5), - [100, 10], - ); - }); +Rhum.testSuite("overArgs()", () => { + Rhum.testCase("invokes func with its arguments transformed.", () => { + const double = (n: number): number => n * 2; + const square = (n: number): number => n * n; + + Rhum.asserts.assertEquals( + overArgs((x: number, y: number) => [x, y], [square, double], 9, 3), + [81, 6], + ); + Rhum.asserts.assertEquals( + overArgs((x: number, y: number) => [x, y], [square, double], 10, 5), + [100, 10], + ); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/function/throttle.md b/documentation/function/throttle.md index 1d8c50f..705f0e3 100644 --- a/documentation/function/throttle.md +++ b/documentation/function/throttle.md @@ -1,12 +1,13 @@ - ## throttle #### import + ```typescript -import throttle from "https://deno.land/x/denodash@0.1.2/src/function/throttle.ts" +import throttle from "https://deno.land/x/denodash@0.1.3/src/function/throttle.ts"; ``` #### signature + ```typescript throttle = ( func: Function, @@ -18,8 +19,9 @@ throttle = ( } ``` -Creates a throttled function that only invokes func at most once per every wait milliseconds. - +Creates a throttled function that only invokes func at most once per every wait +milliseconds. + _This method is specifically requested for additional testing and code review._ #### Source: @@ -86,211 +88,208 @@ export const throttle = ( }; export default throttle; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("throttle()", () => { - Rhum.testCase("basic throttle", async () => { +Rhum.testSuite("throttle()", () => { + Rhum.testCase("basic throttle", async () => { + let counter = 0; + const incr = () => { + counter++; + }; + const throttledIncr = throttle(incr, 32); + throttledIncr(); + throttledIncr(); + + Rhum.asserts.assertStrictEquals( + counter, + 1, + "incr was called immediately", + ); + await delay(64); + Rhum.asserts.assertStrictEquals(counter, 2); + }); + + Rhum.testCase("throttle arguments", async () => { + let value = 0; + const update = function (val: number) { + value = val; + }; + const throttledUpdate = throttle(update, 32); + throttledUpdate(1); + throttledUpdate(2); + Rhum.asserts.assertStrictEquals(value, 1); + await delay(64); + throttledUpdate(3); + await delay(104); + Rhum.asserts.assertStrictEquals(value, 3); + }); + + Rhum.testCase("throttle once", async () => { + let counter = 0; + const incr = () => { + return ++counter; + }; + const throttledIncr = throttle(incr, 32); + const result = throttledIncr(); + await delay(64); + Rhum.asserts.assertStrictEquals( + result, + 1, + "throttled functions return their value", + ); + Rhum.asserts.assertStrictEquals(counter, 1); + }); + + Rhum.testCase("more throttling", async () => { + let counter = 0; + const incr = () => { + counter++; + }; + const throttledIncr = throttle(incr, 30); + throttledIncr(); + throttledIncr(); + Rhum.asserts.assertStrictEquals(counter, 1); + await delay(85); + Rhum.asserts.assertStrictEquals(counter, 2); + throttledIncr(); + Rhum.asserts.assertStrictEquals(counter, 3); + }); + + Rhum.testCase("throttle repeatedly with results", async () => { + let counter = 0; + const incr = () => { + return ++counter; + }; + const throttledIncr = throttle(incr, 100); + let results: any = []; + const saveResult = () => { + results.push(throttledIncr()); + }; + saveResult(); // 0 + saveResult(); // 1 + await delay(50); + saveResult(); // 2 + await delay(200); + saveResult(); // 3 + await delay(10); + saveResult(); // 4 + await delay(140); + Rhum.asserts.assertStrictEquals(results[0], 1); + Rhum.asserts.assertStrictEquals(results[1], 1); + Rhum.asserts.assertStrictEquals(results[2], 1); + Rhum.asserts.assertStrictEquals(results[3], 3); + Rhum.asserts.assertStrictEquals(results[4], 3); + }); + + Rhum.testCase( + "throttle triggers trailing call when invoked repeatedly", + async () => { let counter = 0; + let limit = 48; const incr = () => { counter++; }; const throttledIncr = throttle(incr, 32); - throttledIncr(); - throttledIncr(); - Rhum.asserts.assertStrictEquals( - counter, - 1, - "incr was called immediately", - ); - await delay(64); - Rhum.asserts.assertStrictEquals(counter, 2); - }); + let stamp = Date.now(); + while (Date.now() - stamp < limit) { + throttledIncr(); + } + let lastCount = counter; + Rhum.asserts.assertEquals(true, counter > 1); - Rhum.testCase("throttle arguments", async () => { - let value = 0; - const update = function (val: number) { - value = val; - }; - const throttledUpdate = throttle(update, 32); - throttledUpdate(1); - throttledUpdate(2); - Rhum.asserts.assertStrictEquals(value, 1); - await delay(64); - throttledUpdate(3); - await delay(104); - Rhum.asserts.assertStrictEquals(value, 3); - }); - - Rhum.testCase("throttle once", async () => { - let counter = 0; - const incr = () => { - return ++counter; - }; - const throttledIncr = throttle(incr, 32); - const result = throttledIncr(); - await delay(64); - Rhum.asserts.assertStrictEquals( - result, - 1, - "throttled functions return their value", - ); - Rhum.asserts.assertStrictEquals(counter, 1); - }); + await delay(96); + Rhum.asserts.assertEquals(true, counter > lastCount); + }, + ); - Rhum.testCase("more throttling", async () => { + Rhum.testCase( + "throttle does not trigger leading call when leading is set to false", + async () => { let counter = 0; const incr = () => { counter++; }; - const throttledIncr = throttle(incr, 30); + const throttledIncr = throttle(incr, 60, { leading: false }); + throttledIncr(); throttledIncr(); + Rhum.asserts.assertStrictEquals(counter, 0); + + await delay(96); Rhum.asserts.assertStrictEquals(counter, 1); - await delay(85); - Rhum.asserts.assertStrictEquals(counter, 2); - throttledIncr(); - Rhum.asserts.assertStrictEquals(counter, 3); - }); + }, + ); - Rhum.testCase("throttle repeatedly with results", async () => { + Rhum.testCase( + "more throttle does not trigger leading call when leading is set to false", + async () => { let counter = 0; const incr = () => { - return ++counter; - }; - const throttledIncr = throttle(incr, 100); - let results: any = []; - const saveResult = () => { - results.push(throttledIncr()); + counter++; }; - saveResult(); // 0 - saveResult(); // 1 + const throttledIncr = throttle(incr, 100, { leading: false }); + + throttledIncr(); await delay(50); - saveResult(); // 2 - await delay(200); - saveResult(); // 3 + throttledIncr(); await delay(10); - saveResult(); // 4 + throttledIncr(); + Rhum.asserts.assertStrictEquals(counter, 0); await delay(140); - Rhum.asserts.assertStrictEquals(results[0], 1); - Rhum.asserts.assertStrictEquals(results[1], 1); - Rhum.asserts.assertStrictEquals(results[2], 1); - Rhum.asserts.assertStrictEquals(results[3], 3); - Rhum.asserts.assertStrictEquals(results[4], 3); - }); - - Rhum.testCase( - "throttle triggers trailing call when invoked repeatedly", - async () => { - let counter = 0; - let limit = 48; - const incr = () => { - counter++; - }; - const throttledIncr = throttle(incr, 32); - - let stamp = Date.now(); - while (Date.now() - stamp < limit) { - throttledIncr(); - } - let lastCount = counter; - Rhum.asserts.assertEquals(true, counter > 1); - - await delay(96); - Rhum.asserts.assertEquals(true, counter > lastCount); - }, - ); - - Rhum.testCase( - "throttle does not trigger leading call when leading is set to false", - async () => { - let counter = 0; - const incr = () => { - counter++; - }; - const throttledIncr = throttle(incr, 60, { leading: false }); - - throttledIncr(); - throttledIncr(); - Rhum.asserts.assertStrictEquals(counter, 0); - - await delay(96); - Rhum.asserts.assertStrictEquals(counter, 1); - }, - ); - - Rhum.testCase( - "more throttle does not trigger leading call when leading is set to false", - async () => { - let counter = 0; - const incr = () => { - counter++; - }; - const throttledIncr = throttle(incr, 100, { leading: false }); + throttledIncr(); - throttledIncr(); - await delay(50); - throttledIncr(); - await delay(10); - throttledIncr(); - Rhum.asserts.assertStrictEquals(counter, 0); - await delay(140); - throttledIncr(); + Rhum.asserts.assertStrictEquals(counter, 1); - Rhum.asserts.assertStrictEquals(counter, 1); + await delay(100); + Rhum.asserts.assertStrictEquals(counter, 2); + }, + ); + + Rhum.testCase("one more throttle with leading: false test", async () => { + let counter = 0; + const incr = () => { + counter++; + }; + const throttledIncr = throttle(incr, 100, { leading: false }); + + let time = Date.now(); + while (Date.now() - time < 350) { + throttledIncr(); + } + Rhum.asserts.assertEquals(true, counter <= 3); - await delay(100); - Rhum.asserts.assertStrictEquals(counter, 2); - }, - ); + await delay(200); + Rhum.asserts.assertEquals(true, counter <= 4); + }); - Rhum.testCase("one more throttle with leading: false test", async () => { + Rhum.testCase( + "throttle does not trigger trailing call when trailing is set to false", + async () => { let counter = 0; const incr = () => { counter++; }; - const throttledIncr = throttle(incr, 100, { leading: false }); - - let time = Date.now(); - while (Date.now() - time < 350) { - throttledIncr(); - } - Rhum.asserts.assertEquals(true, counter <= 3); - - await delay(200); - Rhum.asserts.assertEquals(true, counter <= 4); - }); - - Rhum.testCase( - "throttle does not trigger trailing call when trailing is set to false", - async () => { - let counter = 0; - const incr = () => { - counter++; - }; - const throttledIncr = throttle(incr, 60, { trailing: false }); + const throttledIncr = throttle(incr, 60, { trailing: false }); - throttledIncr(); - throttledIncr(); - throttledIncr(); - Rhum.asserts.assertStrictEquals(counter, 1); + throttledIncr(); + throttledIncr(); + throttledIncr(); + Rhum.asserts.assertStrictEquals(counter, 1); - await delay(96); - Rhum.asserts.assertStrictEquals(counter, 1); + await delay(96); + Rhum.asserts.assertStrictEquals(counter, 1); - throttledIncr(); - throttledIncr(); - Rhum.asserts.assertStrictEquals(counter, 2); + throttledIncr(); + throttledIncr(); + Rhum.asserts.assertStrictEquals(counter, 2); - await delay(96); - Rhum.asserts.assertStrictEquals(counter, 2); - }, - ); - }); + await delay(96); + Rhum.asserts.assertStrictEquals(counter, 2); + }, + ); +}); ``` - - \ No newline at end of file diff --git a/documentation/lang/cloneDeep.md b/documentation/lang/cloneDeep.md index 7a03803..6b81baf 100644 --- a/documentation/lang/cloneDeep.md +++ b/documentation/lang/cloneDeep.md @@ -1,17 +1,20 @@ - ## cloneDeep #### import + ```typescript -import cloneDeep from "https://deno.land/x/denodash@0.1.2/src/lang/cloneDeep.ts" +import cloneDeep from "https://deno.land/x/denodash@0.1.3/src/lang/cloneDeep.ts"; ``` #### signature + ```typescript cloneDeep = (value: any): any | never ``` -Creates a deep clone of the value passed into it. All primatives are copied by value, but new objects are created instead of passing these elements by reference. +Creates a deep clone of the value passed into it. All primatives are copied by +value, but new objects are created instead of passing these elements by +reference. #### Source: @@ -48,34 +51,31 @@ const cloneDeep = (value: any): any | never => { }; export default cloneDeep; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("cloneDeep()", () => { - Rhum.testCase("should create a deep clone of an object", () => { - const testObj = { - a: { - b: { - c: "foo", - }, +Rhum.testSuite("cloneDeep()", () => { + Rhum.testCase("should create a deep clone of an object", () => { + const testObj = { + a: { + b: { + c: "foo", }, - d: { - e: { - f: "bar", - }, + }, + d: { + e: { + f: "bar", }, - }; - const testClone = cloneDeep(testObj); - Rhum.asserts.assertStrictEquals(testObj.a.b.c, testClone.a.b.c); - Rhum.asserts.assertEquals(testObj, testClone); - Rhum.asserts.assertNotStrictEquals(testObj, testClone); - Rhum.asserts.assertEquals(testObj.a, testClone.a); - Rhum.asserts.assertNotStrictEquals(testObj.a, testClone.a); - }); + }, + }; + const testClone = cloneDeep(testObj); + Rhum.asserts.assertStrictEquals(testObj.a.b.c, testClone.a.b.c); + Rhum.asserts.assertEquals(testObj, testClone); + Rhum.asserts.assertNotStrictEquals(testObj, testClone); + Rhum.asserts.assertEquals(testObj.a, testClone.a); + Rhum.asserts.assertNotStrictEquals(testObj.a, testClone.a); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/lang/isEqual.md b/documentation/lang/isEqual.md index 26fd11b..d3ed015 100644 --- a/documentation/lang/isEqual.md +++ b/documentation/lang/isEqual.md @@ -1,17 +1,19 @@ - ## isEqual #### import + ```typescript -import isEqual from "https://deno.land/x/denodash@0.1.2/src/lang/isEqual.ts" +import isEqual from "https://deno.land/x/denodash@0.1.3/src/lang/isEqual.ts"; ``` #### signature + ```typescript isEqual = Comparator // i.e., (a: any, b: any) => boolean ``` -Determines if the value of a and b are equal, even if the references are not equal. +Determines if the value of a and b are equal, even if the references are not +equal. #### Source: @@ -71,115 +73,112 @@ const isEqual: Comparator = (a, b) => { }; export default isEqual; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("isEqual()", () => { - Rhum.testCase("should correctly compare primatives", () => { - // test code from Lodash - const symbol1 = Symbol("a"); - const symbol2 = Symbol("b"); - const pairs = [ - [1, 1, true], - [1, "1", false], - [1, 2, false], - [-0, -0, true], - [0, 0, true], - [-0, 0, true], - [0, "0", false], - [0, null, false], - [NaN, NaN, true], - [NaN, "a", false], - [NaN, Infinity, false], - ["a", "a", true], - ["a", "b", false], - ["a", ["a"], false], - [true, true, true], - [true, 1, false], - [true, "a", false], - [false, false, true], - [false, 0, false], - [false, "", false], - [symbol1, symbol1, true], - [symbol1, symbol2, false], - [null, null, true], - [null, undefined, false], - [null, {}, false], - [null, "", false], - [undefined, undefined, true], - [undefined, null, false], - [undefined, "", false], - ]; - - for (let pair of pairs) { - const [a, b, expected] = pair; - Rhum.asserts.assertStrictEquals(isEqual(a, b), expected); - } - }); - Rhum.testCase("should compare arrays", () => { - let array1: any[] = [true, null, 1, "a", undefined]; - let array2: any[] = [true, null, 1, "a", undefined]; - Rhum.asserts.assertStrictEquals(isEqual(array1, array2), true); - - array1 = [[1, 2, 3], new Date(2012, 4, 23), /x/, { e: 1 }]; - array2 = [[1, 2, 3], new Date(2012, 4, 23), /x/, { e: 1 }]; - - Rhum.asserts.assertStrictEquals(isEqual(array1, array2), true); - - array1 = [1, 2, 3]; - array2 = [3, 2, 1]; - - Rhum.asserts.assertStrictEquals(isEqual(array1, array2), false); - - array1 = [1, 2]; - array2 = [1, 2, 3]; - - Rhum.asserts.assertStrictEquals(isEqual(array1, array2), false); - }); - - Rhum.testCase("should compare objects", () => { - let obj1: any = { - alpha: true, - beta: null, - gamma: 1, - delta: "a", - epsilon: undefined, - }; - let obj2: any = { - alpha: true, - beta: null, - gamma: 1, - delta: "a", - epsilon: undefined, - }; - Rhum.asserts.assertStrictEquals(isEqual(obj1, obj2), true); - - obj1 = { - ...obj1, - squirrelGirl: { - secretId: "Doreen Green", - powers: "Squirrel", - }, - }; - obj2 = { ...obj1 }; - - Rhum.asserts.assertStrictEquals(isEqual(obj1, obj2), true); - - obj2 = { - ...obj2, - squirrelGirl: { ...obj2.squirrelGirl, secretId: "Doreen Allene Green" }, - }; - - Rhum.asserts.assertStrictEquals(isEqual(obj1, obj2), false); - - obj2 = { ...obj1, "newPropThatDoesntExistOnObj1": "sf" }; - - Rhum.asserts.assertStrictEquals(isEqual(obj1, obj2), false); - }); +Rhum.testSuite("isEqual()", () => { + Rhum.testCase("should correctly compare primatives", () => { + // test code from Lodash + const symbol1 = Symbol("a"); + const symbol2 = Symbol("b"); + const pairs = [ + [1, 1, true], + [1, "1", false], + [1, 2, false], + [-0, -0, true], + [0, 0, true], + [-0, 0, true], + [0, "0", false], + [0, null, false], + [NaN, NaN, true], + [NaN, "a", false], + [NaN, Infinity, false], + ["a", "a", true], + ["a", "b", false], + ["a", ["a"], false], + [true, true, true], + [true, 1, false], + [true, "a", false], + [false, false, true], + [false, 0, false], + [false, "", false], + [symbol1, symbol1, true], + [symbol1, symbol2, false], + [null, null, true], + [null, undefined, false], + [null, {}, false], + [null, "", false], + [undefined, undefined, true], + [undefined, null, false], + [undefined, "", false], + ]; + + for (let pair of pairs) { + const [a, b, expected] = pair; + Rhum.asserts.assertStrictEquals(isEqual(a, b), expected); + } }); -``` + Rhum.testCase("should compare arrays", () => { + let array1: any[] = [true, null, 1, "a", undefined]; + let array2: any[] = [true, null, 1, "a", undefined]; + Rhum.asserts.assertStrictEquals(isEqual(array1, array2), true); + + array1 = [[1, 2, 3], new Date(2012, 4, 23), /x/, { e: 1 }]; + array2 = [[1, 2, 3], new Date(2012, 4, 23), /x/, { e: 1 }]; - \ No newline at end of file + Rhum.asserts.assertStrictEquals(isEqual(array1, array2), true); + + array1 = [1, 2, 3]; + array2 = [3, 2, 1]; + + Rhum.asserts.assertStrictEquals(isEqual(array1, array2), false); + + array1 = [1, 2]; + array2 = [1, 2, 3]; + + Rhum.asserts.assertStrictEquals(isEqual(array1, array2), false); + }); + + Rhum.testCase("should compare objects", () => { + let obj1: any = { + alpha: true, + beta: null, + gamma: 1, + delta: "a", + epsilon: undefined, + }; + let obj2: any = { + alpha: true, + beta: null, + gamma: 1, + delta: "a", + epsilon: undefined, + }; + Rhum.asserts.assertStrictEquals(isEqual(obj1, obj2), true); + + obj1 = { + ...obj1, + squirrelGirl: { + secretId: "Doreen Green", + powers: "Squirrel", + }, + }; + obj2 = { ...obj1 }; + + Rhum.asserts.assertStrictEquals(isEqual(obj1, obj2), true); + + obj2 = { + ...obj2, + squirrelGirl: { ...obj2.squirrelGirl, secretId: "Doreen Allene Green" }, + }; + + Rhum.asserts.assertStrictEquals(isEqual(obj1, obj2), false); + + obj2 = { ...obj1, "newPropThatDoesntExistOnObj1": "sf" }; + + Rhum.asserts.assertStrictEquals(isEqual(obj1, obj2), false); + }); +}); +``` diff --git a/documentation/object/findKeys.md b/documentation/object/findKeys.md index 7eb2f57..809e462 100644 --- a/documentation/object/findKeys.md +++ b/documentation/object/findKeys.md @@ -1,12 +1,13 @@ - ## findKeys #### import + ```typescript -import findKeys from "https://deno.land/x/denodash@0.1.2/src/object/findKeys.ts" +import findKeys from "https://deno.land/x/denodash@0.1.3/src/object/findKeys.ts"; ``` #### signature + ```typescript findKeys = ( obj: Record, @@ -14,7 +15,8 @@ findKeys = ( ): string[] ``` -Returns a sorted list of all keys where the value at the key, run through predicate, returns true +Returns a sorted list of all keys where the value at the key, run through +predicate, returns true #### Source: @@ -35,24 +37,21 @@ export const findKeys = ( }; export default findKeys; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("findKeys()", () => { - Rhum.testCase("should find all keys where the value holds true", () => { - Rhum.asserts.assertEquals( - findKeys({ a: 1, b: 2, c: 3 }, (x: number) => x % 2 === 1), - ["a", "c"], - ); - Rhum.asserts.assertEquals( - findKeys({ c: 1, b: 2, a: 3 }, (x: number) => x % 2 === 1), - ["a", "c"], - ); - }); +Rhum.testSuite("findKeys()", () => { + Rhum.testCase("should find all keys where the value holds true", () => { + Rhum.asserts.assertEquals( + findKeys({ a: 1, b: 2, c: 3 }, (x: number) => x % 2 === 1), + ["a", "c"], + ); + Rhum.asserts.assertEquals( + findKeys({ c: 1, b: 2, a: 3 }, (x: number) => x % 2 === 1), + ["a", "c"], + ); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/object/get.md b/documentation/object/get.md index 5971a67..7b4aa9f 100644 --- a/documentation/object/get.md +++ b/documentation/object/get.md @@ -1,12 +1,13 @@ - ## get #### import + ```typescript -import get from "https://deno.land/x/denodash@0.1.2/src/object/get.ts" +import get from "https://deno.land/x/denodash@0.1.3/src/object/get.ts"; ``` #### signature + ```typescript get = ( obj: Record, @@ -15,7 +16,9 @@ get = ( ): any ``` -Gets the value of an object at the property path, which may be expressed as a string or an array of string | number. If the property path does not exist, returns the "retValueIfUndef" value provided. +Gets the value of an object at the property path, which may be expressed as a +string or an array of string | number. If the property path does not exist, +returns the "retValueIfUndef" value provided. #### Source: @@ -73,58 +76,55 @@ export const get = ( }; export default get; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("get()", () => { - const testObj = { - a: { - b: 2, - c: { - d: false, - e: ["foo", "bar", { baz: "quux" }], - }, - }, - }; - Rhum.testCase("should get objects by a path", () => { - Rhum.asserts.assertStrictEquals( - get(testObj, ["a", "c", "e", 2, "baz"], "nope"), - "quux", - ); - }); - Rhum.testCase("should return whole objects", () => { - Rhum.asserts.assertEquals(get(testObj, ["a", "c"], "nope"), { +Rhum.testSuite("get()", () => { + const testObj = { + a: { + b: 2, + c: { d: false, e: ["foo", "bar", { baz: "quux" }], - }); - }); - Rhum.testCase("should a default if path does not exist", () => { - Rhum.asserts.assertStrictEquals( - get(testObj, ["a", "q", "c"], "nope"), - "nope", - ); - Rhum.asserts.assertStrictEquals( - get(testObj, ["a", "b", "c"], "nope"), - "nope", - ); - }); - Rhum.testCase("should work with strings", () => { - Rhum.asserts.assertEquals(get(testObj, "a.c.e", "nope"), [ - "foo", - "bar", - { baz: "quux" }, - ]); - }); - Rhum.testCase("should work with a mix of arrays and strings", () => { - Rhum.asserts.assertStrictEquals( - get(testObj, ["a.c", "e", 2, "baz"], "nope"), - "quux", - ); + }, + }, + }; + Rhum.testCase("should get objects by a path", () => { + Rhum.asserts.assertStrictEquals( + get(testObj, ["a", "c", "e", 2, "baz"], "nope"), + "quux", + ); + }); + Rhum.testCase("should return whole objects", () => { + Rhum.asserts.assertEquals(get(testObj, ["a", "c"], "nope"), { + d: false, + e: ["foo", "bar", { baz: "quux" }], }); }); + Rhum.testCase("should a default if path does not exist", () => { + Rhum.asserts.assertStrictEquals( + get(testObj, ["a", "q", "c"], "nope"), + "nope", + ); + Rhum.asserts.assertStrictEquals( + get(testObj, ["a", "b", "c"], "nope"), + "nope", + ); + }); + Rhum.testCase("should work with strings", () => { + Rhum.asserts.assertEquals(get(testObj, "a.c.e", "nope"), [ + "foo", + "bar", + { baz: "quux" }, + ]); + }); + Rhum.testCase("should work with a mix of arrays and strings", () => { + Rhum.asserts.assertStrictEquals( + get(testObj, ["a.c", "e", 2, "baz"], "nope"), + "quux", + ); + }); +}); ``` - - \ No newline at end of file diff --git a/documentation/object/invert.md b/documentation/object/invert.md index c3c3bdf..04d2205 100644 --- a/documentation/object/invert.md +++ b/documentation/object/invert.md @@ -1,17 +1,19 @@ - ## invert #### import + ```typescript -import invert from "https://deno.land/x/denodash@0.1.2/src/object/invert.ts" +import invert from "https://deno.land/x/denodash@0.1.3/src/object/invert.ts"; ``` #### signature + ```typescript invert = (obj: Record): Record ``` -Inverts the keys and values of the record provided. Throws an error if any value is not serializable to a string +Inverts the keys and values of the record provided. Throws an error if any value +is not serializable to a string #### Source: @@ -45,20 +47,17 @@ export const invert = (obj: Record): Record => ); export default invert; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("invert()", () => { - Rhum.testCase("should invert an objects keys and values", () => { - Rhum.asserts.assertEquals( - invert({ Moe: "Moses", Larry: "Louis", Curly: "Jerome" }), - { Moses: "Moe", Louis: "Larry", Jerome: "Curly" }, - ); - }); +Rhum.testSuite("invert()", () => { + Rhum.testCase("should invert an objects keys and values", () => { + Rhum.asserts.assertEquals( + invert({ Moe: "Moses", Larry: "Louis", Curly: "Jerome" }), + { Moses: "Moe", Louis: "Larry", Jerome: "Curly" }, + ); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/object/mapObject.md b/documentation/object/mapObject.md index 988af34..6960afe 100644 --- a/documentation/object/mapObject.md +++ b/documentation/object/mapObject.md @@ -1,12 +1,13 @@ - ## mapObject #### import + ```typescript -import mapObject from "https://deno.land/x/denodash@0.1.2/src/object/mapObject.ts" +import mapObject from "https://deno.land/x/denodash@0.1.3/src/object/mapObject.ts"; ``` #### signature + ```typescript mapObject = ( obj: Record, @@ -14,7 +15,8 @@ mapObject = ( ): Record ``` -Maps over the object in a record and returns a new record with the results of that function on the same keys. Execution order is not guaranteed. +Maps over the object in a record and returns a new record with the results of +that function on the same keys. Execution order is not guaranteed. #### Source: @@ -35,30 +37,27 @@ export const mapObject = ( ); export default mapObject; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("mapObject()", () => { - Rhum.testCase("should map an object", () => { - const testObj = { - one: 1, - two: 2, - three: 3, - }; - const returned = mapObject( - testObj, - (n: number): string => (n * n * 100).toString(), - ); - Rhum.asserts.assertEquals(returned, { - one: "100", - two: "400", - three: "900", - }); +Rhum.testSuite("mapObject()", () => { + Rhum.testCase("should map an object", () => { + const testObj = { + one: 1, + two: 2, + three: 3, + }; + const returned = mapObject( + testObj, + (n: number): string => (n * n * 100).toString(), + ); + Rhum.asserts.assertEquals(returned, { + one: "100", + two: "400", + three: "900", }); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/object/omit.md b/documentation/object/omit.md index 2c321cf..aaa63c2 100644 --- a/documentation/object/omit.md +++ b/documentation/object/omit.md @@ -1,12 +1,13 @@ - ## omit #### import + ```typescript -import omit from "https://deno.land/x/denodash@0.1.2/src/object/omit.ts" +import omit from "https://deno.land/x/denodash@0.1.3/src/object/omit.ts"; ``` #### signature + ```typescript omit = ( obj: Record, @@ -14,7 +15,8 @@ omit = ( ): Record ``` -Returns a copy of the record (obj) provided, without the keys specified in props. +Returns a copy of the record (obj) provided, without the keys specified in +props. #### Source: @@ -32,44 +34,41 @@ export const omit = ( }; export default omit; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("omit()", () => { - const testObj = { - f: "f", +Rhum.testSuite("omit()", () => { + const testObj = { + f: "f", + g: "g", + h: "h", + i: "i", + j: 0, + k: false, + }; + Rhum.testCase("picks values from an object", () => { + Rhum.asserts.assertEquals(omit(testObj, ["f", "j", "k"]), { g: "g", h: "h", i: "i", - j: 0, - k: false, - }; - Rhum.testCase("picks values from an object", () => { - Rhum.asserts.assertEquals(omit(testObj, ["f", "j", "k"]), { - g: "g", - h: "h", - i: "i", - }); }); - Rhum.testCase("ignores non-existant values", () => { - Rhum.asserts.assertEquals(omit(testObj, ["f", "j", "k", "aluminium"]), { - g: "g", - h: "h", - i: "i", - }); + }); + Rhum.testCase("ignores non-existant values", () => { + Rhum.asserts.assertEquals(omit(testObj, ["f", "j", "k", "aluminium"]), { + g: "g", + h: "h", + i: "i", }); - Rhum.testCase("correctly gets falsey values", () => { - Rhum.asserts.assertEquals(omit(testObj, ["f", "i"]), { - g: "g", - h: "h", - j: 0, - k: false, - }); + }); + Rhum.testCase("correctly gets falsey values", () => { + Rhum.asserts.assertEquals(omit(testObj, ["f", "i"]), { + g: "g", + h: "h", + j: 0, + k: false, }); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/object/pick.md b/documentation/object/pick.md index 3903fb2..62db0fc 100644 --- a/documentation/object/pick.md +++ b/documentation/object/pick.md @@ -1,12 +1,13 @@ - ## pick #### import + ```typescript -import pick from "https://deno.land/x/denodash@0.1.2/src/object/pick.ts" +import pick from "https://deno.land/x/denodash@0.1.3/src/object/pick.ts"; ``` #### signature + ```typescript pick = ( obj: Record, @@ -14,7 +15,8 @@ pick = ( ): Record ``` -Returns a copy of the record (obj) provided, with ONLY the keys specified in props. +Returns a copy of the record (obj) provided, with ONLY the keys specified in +props. #### Source: @@ -33,43 +35,40 @@ export const pick = ( }; export default pick; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("pick()", () => { - const testObj = { +Rhum.testSuite("pick()", () => { + const testObj = { + f: "f", + g: "g", + h: "h", + i: "i", + j: 0, + k: false, + }; + Rhum.testCase("picks values from an object", () => { + Rhum.asserts.assertEquals(pick(testObj, ["f", "h", "i"]), { f: "f", - g: "g", h: "h", i: "i", - j: 0, - k: false, - }; - Rhum.testCase("picks values from an object", () => { - Rhum.asserts.assertEquals(pick(testObj, ["f", "h", "i"]), { - f: "f", - h: "h", - i: "i", - }); }); - Rhum.testCase("ignores non-existant values", () => { - Rhum.asserts.assertEquals(pick(testObj, ["f", "h", "i", "aluminium"]), { - f: "f", - h: "h", - i: "i", - }); + }); + Rhum.testCase("ignores non-existant values", () => { + Rhum.asserts.assertEquals(pick(testObj, ["f", "h", "i", "aluminium"]), { + f: "f", + h: "h", + i: "i", }); - Rhum.testCase("correctly gets falsey values", () => { - Rhum.asserts.assertEquals(pick(testObj, ["f", "j", "k", "aluminium"]), { - f: "f", - j: 0, - k: false, - }); + }); + Rhum.testCase("correctly gets falsey values", () => { + Rhum.asserts.assertEquals(pick(testObj, ["f", "j", "k", "aluminium"]), { + f: "f", + j: 0, + k: false, }); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/utils/comparatorChain.md b/documentation/utils/comparatorChain.md index 57a363b..8fbe941 100644 --- a/documentation/utils/comparatorChain.md +++ b/documentation/utils/comparatorChain.md @@ -1,19 +1,21 @@ - ## comparatorChain #### import + ```typescript -import comparatorChain from "https://deno.land/x/denodash@0.1.2/src/utils/comparatorChain.ts" +import comparatorChain from "https://deno.land/x/denodash@0.1.3/src/utils/comparatorChain.ts"; ``` #### signature + ```typescript comparatorChain = ( ...comparators: SortComparator[] ): SortComparator ``` -Creates a single sort comparator out of several sort parameters. The first sort comparator passed in has priority, followed by the rest. +Creates a single sort comparator out of several sort parameters. The first sort +comparator passed in has priority, followed by the rest. #### Source: @@ -36,53 +38,50 @@ export const comparatorChain = ( }; export default comparatorChain; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("comparatorChain()", () => { - Rhum.testCase("creates a chain of prioritized comparators", () => { - type NameAge = { name: string; age: number }; - const testArr: NameAge[] = [ +Rhum.testSuite("comparatorChain()", () => { + Rhum.testCase("creates a chain of prioritized comparators", () => { + type NameAge = { name: string; age: number }; + const testArr: NameAge[] = [ + { + name: "alex", + age: 25, + }, + { + name: "betty", + age: 15, + }, + { + name: "carl", + age: 25, + }, + ]; + Rhum.asserts.assertEquals( + testArr.sort( + comparatorChain( + (a: NameAge, b: NameAge) => a.age - b.age, + (a: NameAge, b: NameAge) => a.name.localeCompare(b.name), + ), + ), + [ { - name: "alex", - age: 25, + age: 15, + name: "betty", }, { - name: "betty", - age: 15, + age: 25, + name: "alex", }, { - name: "carl", age: 25, + name: "carl", }, - ]; - Rhum.asserts.assertEquals( - testArr.sort( - comparatorChain( - (a: NameAge, b: NameAge) => a.age - b.age, - (a: NameAge, b: NameAge) => a.name.localeCompare(b.name), - ), - ), - [ - { - age: 15, - name: "betty", - }, - { - age: 25, - name: "alex", - }, - { - age: 25, - name: "carl", - }, - ], - ); - }); + ], + ); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/utils/delay.md b/documentation/utils/delay.md index 756814e..6c5f015 100644 --- a/documentation/utils/delay.md +++ b/documentation/utils/delay.md @@ -1,17 +1,19 @@ - ## delay #### import + ```typescript -import delay from "https://deno.land/x/denodash@0.1.2/src/utils/delay.ts" +import delay from "https://deno.land/x/denodash@0.1.3/src/utils/delay.ts"; ``` #### signature + ```typescript delay = (time: number, fn?: Function, ...args: any[]): Promise ``` -Creates a promise which resolves only after a number of milliseconds (time) has passed. +Creates a promise which resolves only after a number of milliseconds (time) has +passed. #### Source: @@ -28,29 +30,26 @@ export const delay = ( }); export default delay; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("delay()", () => { - Rhum.testCase("delays execution of a function", async () => { - let count = 0; - setTimeout(() => { - count = 10; - }, 20); - Rhum.asserts.assertStrictEquals(count, 0); - await delay(30); - Rhum.asserts.assertStrictEquals(count, 10); - delay(20, () => { - count = 20; - }); - Rhum.asserts.assertStrictEquals(count, 10); - await delay(30); - Rhum.asserts.assertStrictEquals(count, 20); +Rhum.testSuite("delay()", () => { + Rhum.testCase("delays execution of a function", async () => { + let count = 0; + setTimeout(() => { + count = 10; + }, 20); + Rhum.asserts.assertStrictEquals(count, 0); + await delay(30); + Rhum.asserts.assertStrictEquals(count, 10); + delay(20, () => { + count = 20; }); + Rhum.asserts.assertStrictEquals(count, 10); + await delay(30); + Rhum.asserts.assertStrictEquals(count, 20); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/utils/identity.md b/documentation/utils/identity.md index 50d32ea..2a1dff0 100644 --- a/documentation/utils/identity.md +++ b/documentation/utils/identity.md @@ -1,17 +1,19 @@ - ## identity #### import + ```typescript -import identity from "https://deno.land/x/denodash@0.1.2/src/utils/identity.ts" +import identity from "https://deno.land/x/denodash@0.1.3/src/utils/identity.ts"; ``` #### signature + ```typescript -identity = (x: T): T +identity = (x: T): T ``` -A function which returns the value passed in as a parameter. (Useful as a default function) +A function which returns the value passed in as a parameter. (Useful as a +default function) #### Source: @@ -19,25 +21,22 @@ A function which returns the value passed in as a parameter. (Useful as a defaul export const identity = (x: T): T => x; export default identity; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("identity()", () => { - Rhum.testCase("returns what is passed to it.", () => { - Rhum.asserts.assertStrictEquals(identity(0), 0); - Rhum.asserts.assertStrictEquals(identity(42), 42); - Rhum.asserts.assertStrictEquals(identity("foo"), "foo"); - const referenceToArray = [1, 2, 3]; - Rhum.asserts.assertEquals(identity(referenceToArray), referenceToArray); - Rhum.asserts.assertStrictEquals( - identity(referenceToArray), - referenceToArray, - ); - }); +Rhum.testSuite("identity()", () => { + Rhum.testCase("returns what is passed to it.", () => { + Rhum.asserts.assertStrictEquals(identity(0), 0); + Rhum.asserts.assertStrictEquals(identity(42), 42); + Rhum.asserts.assertStrictEquals(identity("foo"), "foo"); + const referenceToArray = [1, 2, 3]; + Rhum.asserts.assertEquals(identity(referenceToArray), referenceToArray); + Rhum.asserts.assertStrictEquals( + identity(referenceToArray), + referenceToArray, + ); }); +}); ``` - - \ No newline at end of file diff --git a/documentation/utils/randomOf.md b/documentation/utils/randomOf.md index 2a16b13..ca2ed8b 100644 --- a/documentation/utils/randomOf.md +++ b/documentation/utils/randomOf.md @@ -1,12 +1,13 @@ - ## randomOf #### import + ```typescript -import randomOf from "https://deno.land/x/denodash@0.1.2/src/utils/randomOf.ts" +import randomOf from "https://deno.land/x/denodash@0.1.3/src/utils/randomOf.ts"; ``` #### signature + ```typescript randomOf = (max: number): number ``` @@ -20,20 +21,17 @@ export const randomOf = (max: number): number => Math.floor((Math.random() * max)); export default randomOf; - ``` -#### Test Examples: +#### Test Examples: ```typescript - Rhum.testSuite("randomOf()", () => { - Rhum.testCase("gets a random integer number from 0 to n - 1", () => { - const r = randomOf(10); - for (let i = 0; i < 50; i++) { - Rhum.asserts.assertArrayContains([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], r); - } - }); +Rhum.testSuite("randomOf()", () => { + Rhum.testCase("gets a random integer number from 0 to n - 1", () => { + const r = randomOf(10); + for (let i = 0; i < 50; i++) { + Rhum.asserts.assertArrayContains([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], r); + } }); +}); ``` - - \ No newline at end of file diff --git a/documentation/writeDocumentation.ts b/documentation/writeDocumentation.ts index e8104a9..0b80739 100644 --- a/documentation/writeDocumentation.ts +++ b/documentation/writeDocumentation.ts @@ -1,6 +1,7 @@ import type { DocObject } from "./documentationObjects.ts"; -const VERSION = `0.1.2`; +const VERSION = await Deno.readTextFile("./VERSION.txt"); +console.info(`VERSION ${VERSION}`); const justParens = (line: string): number => { const openCount = (line.match(/\(/g) || []).length; diff --git a/src/collection.test.ts b/src/collection.test.ts index b202158..6d95737 100644 --- a/src/collection.test.ts +++ b/src/collection.test.ts @@ -8,6 +8,7 @@ import flatMapDepth from "./collection/flatMapDepth.ts"; import groupBy from "./collection/groupBy.ts"; import keyBy from "./collection/keyBy.ts"; import sortBy from "./collection/sortBy.ts"; +import sortByProperty from "./collection/sortByProperty.ts"; import shuffle from "./collection/shuffle.ts"; import sample from "./collection/sample.ts"; import sampleOne from "./collection/sampleOne.ts"; @@ -74,13 +75,10 @@ Rhum.testPlan("collection/*", () => { "6": [6.1, 6.3], }); Rhum.asserts.assertEquals( - groupBy((n: number): string => (n % 2 === 0 ? "even" : "odd"), [ - 1, - 2, - 3, - 4, - 5, - ]), + groupBy( + (n: number): string => (n % 2 === 0 ? "even" : "odd"), + [1, 2, 3, 4, 5], + ), { even: [2, 4], odd: [1, 3, 5], @@ -155,6 +153,32 @@ Rhum.testPlan("collection/*", () => { }, ); }); + Rhum.testSuite("sortByProperty()", () => { + Rhum.testCase( + "sorts the order of the iteratees by a string provided", + () => { + type TestType = { user: string; age: number }; + const testArray: TestType[] = [ + { user: "fred", age: 48 }, + { user: "barney", age: 34 }, + { user: "fred", age: 40 }, + { user: "barney", age: 36 }, + ]; + Rhum.asserts.assertEquals(sortByProperty(testArray, "age"), [ + { user: "barney", age: 34 }, + { user: "barney", age: 36 }, + { user: "fred", age: 40 }, + { user: "fred", age: 48 }, + ]); + Rhum.asserts.assertEquals(sortByProperty(testArray, "user"), [ + { user: "barney", age: 34 }, + { user: "barney", age: 36 }, + { user: "fred", age: 48 }, + { user: "fred", age: 40 }, + ]); + }, + ); + }); Rhum.testSuite("sample()", () => { Rhum.testCase("gets N unique elements of an array", () => { const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; diff --git a/src/collection/sortByProperty.ts b/src/collection/sortByProperty.ts new file mode 100644 index 0000000..a5ab94a --- /dev/null +++ b/src/collection/sortByProperty.ts @@ -0,0 +1,13 @@ +export const sortByProperty = ( + array: T[], + property: string, +): T[] | never => + array.slice().sort((a, b) => { + if (typeof a[property] === "string") { + return a[property].localeCompare(b[property]); + } else { + return a[property] - b[property]; + } + }); + +export default sortByProperty;