Skip to content

Commit

Permalink
Array to record (#76)
Browse files Browse the repository at this point in the history
* arrayToRecord function added

* arrayToRecord tests

* Removing unneeded import

* Prettier

* arrayToRecord renamed by keyArrayBy

* Exposing keyArrayBy

* Adding doc

* Sonar fix

* groupBy improved

* groupByUnique method

* Removing keyArrayBy

* exposing groupByUnique

* Test fixes

* Controlling type error

* Doc

* Throwing error in case of duplicated value

* Adding doc for groupBy and groupByUnique

* Tests fix
  • Loading branch information
CarlosGamero authored Nov 11, 2023
1 parent e3fed48 commit 7ced158
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 236 deletions.
2 changes: 1 addition & 1 deletion docs/array-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ Return a copy of the given array without falsy values (eg: false, 0, '', null, u
```typescript
const array = ['', false, null, 'valid', 1, undefined, 0]
console.log(removeFalsy(array)) // result: ['valid', 1]
```
```
8 changes: 6 additions & 2 deletions docs/object-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ Returns an object which contains fields specified in `propNames` with the same v

Returns true if `params` has no own properties or only own properties with value `undefined`, false otherwise.

`groupBy<T>(inputArray: T[], propName: string): Record<string, T[]>`
`export function groupBy<T extends { [K in keyof T]: string | number | symbol | null | undefined }, K extends keyof T>(array: T[], selector: K): Record<string | number | symbol, T[]>`

Returns an object composed of keys generated from the values of `propName` field for the `inputArray` elements. The order of grouped values is determined by the order they occur in collection.
The `groupBy` function takes an array of objects and a `selector`, groups the objects based on selected key and returns an object with unique keys from the selector and corresponding groups as arrays.

`export function groupByUnique<T extends { [K in keyof T]: string | number | symbol | null | undefined }, K extends keyof T>(array: T[], selector: K): Record<string | number | symbol, T>`

Similar to `groupBy`, but the value is a single element, in case of duplicated values for the same selector the method will throw a `InternalError`
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type { Either } from './src/errors/either'
export { chunk, callChunked, removeFalsy, removeNullish } from './src/utils/arrayUtils'
export {
groupBy,
groupByUnique,
pick,
pickWithoutUndefined,
copyWithoutUndefined,
Expand Down
45 changes: 0 additions & 45 deletions src/utils/__snapshots__/objectUtils.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,19 @@ exports[`groupBy > Correctly groups by string values 1`] = `
{
"a": [
{
"id": 1,
"name": "a",
},
{
"id": 4,
"name": "a",
},
],
"b": [
{
"id": 3,
"name": "b",
},
],
"c": [
{
"id": 2,
"name": "c",
},
],
Expand Down Expand Up @@ -98,44 +94,3 @@ exports[`objectUtils > copyWithoutUndefined > Removes undefined fields 1`] = `
},
}
`;

exports[`pick > Ignores missing fields 1`] = `
{
"a": "a",
}
`;

exports[`pick > Includes undefined fields 1`] = `
{
"a": "a",
"b": undefined,
}
`;

exports[`pick > Picks specified fields 1`] = `
{
"a": "a",
"c": " ",
"e": {},
}
`;

exports[`pickWithoutUndefined > Ignores missing fields 1`] = `
{
"a": "a",
}
`;

exports[`pickWithoutUndefined > Picks specified fields 1`] = `
{
"a": "a",
"c": " ",
"e": {},
}
`;

exports[`pickWithoutUndefined > Skips undefined fields 1`] = `
{
"a": "a",
}
`;
2 changes: 0 additions & 2 deletions src/utils/arrayUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { describe, vitest } from 'vitest'

import { callChunked, chunk, removeFalsy, removeNullish } from './arrayUtils'

describe('arrayUtils', () => {
Expand Down
Loading

0 comments on commit 7ced158

Please sign in to comment.