-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f99a11e
commit 4354b39
Showing
14 changed files
with
191 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opentf/utils": minor | ||
--- | ||
|
||
Added arrIns, arrReplace, arrRm & countBy array util fns. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Callout } from "nextra/components"; | ||
import REPL from "../../components/REPL"; | ||
|
||
> Counts the items in the given array and groups them by the name provided. | ||
## Syntax | ||
|
||
```ts | ||
import { countBy } from '@opentf/utils'; | ||
|
||
countBy<T>( | ||
arr: T[] = [], | ||
by: ((val: T) => string) | string | ||
) | ||
``` | ||
|
||
## Examples | ||
|
||
```ts | ||
countBy([]) //=> {} | ||
|
||
countBy([1, 2, 3, 4, 5, 6, 7, 8, 9], (v) => | ||
v % 2 === 0 ? 'Even' : 'Odd' | ||
) | ||
//=> { Even: 4, Odd: 5 }); | ||
|
||
countBy(['Apple', 'Mango', 'Orange'], 'length') | ||
//=> | ||
// { | ||
// '5': 2, | ||
// '6': 1, | ||
// } | ||
|
||
const inventory = [ | ||
{ name: 'asparagus', type: 'vegetables', qty: 5 }, | ||
{ name: 'bananas', type: 'fruit', qty: 0 }, | ||
{ name: 'goat', type: 'meat', qty: 23 }, | ||
{ name: 'cherries', type: 'fruit', qty: 5 }, | ||
{ name: 'fish', type: 'meat', qty: 22 }, | ||
]; | ||
countBy(inventory, ({ qty }) => (qty === 0 ? 'OutOfStock' : 'InStock')) | ||
//=> { OutOfStock: 1, InStock: 4 }); | ||
|
||
const letters = ['a', 'b', 'A', 'a', 'B', 'c']; | ||
countBy(letters, (l) => l.toLowerCase()) | ||
//=> | ||
// { | ||
// a: 3, | ||
// b: 2, | ||
// c: 1, | ||
// } | ||
``` | ||
|
||
## Try | ||
|
||
<REPL code={`const { countBy } = require('@opentf/utils'); | ||
const letters = ['a', 'b', 'A', 'a', 'B', 'c']; | ||
countBy(letters, (l) => l.toLowerCase()); | ||
`} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"asyncFilter": "asyncFilter" | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { countBy } from '../../src'; | ||
|
||
describe('Array', () => { | ||
test('countBy', () => { | ||
expect(countBy([])).toEqual({}); | ||
expect( | ||
countBy([1, 2, 3, 4, 5, 6, 7, 8, 9], (v) => | ||
v % 2 === 0 ? 'Even' : 'Odd' | ||
) | ||
).toEqual({ Even: 4, Odd: 5 }); | ||
|
||
expect(countBy(['Apple', 'Mango', 'Orange'], 'length')).toEqual({ | ||
'5': 2, | ||
'6': 1, | ||
}); | ||
|
||
const inventory = [ | ||
{ name: 'asparagus', type: 'vegetables', qty: 5 }, | ||
{ name: 'bananas', type: 'fruit', qty: 0 }, | ||
{ name: 'goat', type: 'meat', qty: 23 }, | ||
{ name: 'cherries', type: 'fruit', qty: 5 }, | ||
{ name: 'fish', type: 'meat', qty: 22 }, | ||
]; | ||
expect( | ||
countBy(inventory, ({ qty }) => (qty === 0 ? 'OutOfStock' : 'InStock')) | ||
).toEqual({ OutOfStock: 1, InStock: 4 }); | ||
|
||
const letters = ['a', 'b', 'A', 'a', 'B', 'c']; | ||
|
||
expect(countBy(letters, (l) => l.toLowerCase())).toEqual({ | ||
a: 3, | ||
b: 2, | ||
c: 1, | ||
}); | ||
}); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import isFn from '../types/isFn'; | ||
|
||
/** | ||
* Counts the items in the given array and groups them by the name provided. | ||
* | ||
* @example | ||
* | ||
* countBy([1, 2, 3, 4, 5], n => n % 2 === 0 ? 'Even' : 'Odd') //=> { Odd: 3, Even: 2 } | ||
*/ | ||
export default function countBy<T>( | ||
arr: T[] = [], | ||
by: ((val: T) => string) | string | ||
) { | ||
return arr.reduce((acc: Record<string, number>, cur: T) => { | ||
const k = isFn(by) ? by(cur) : (cur[by as keyof T] as string); | ||
const count = acc[k] ? acc[k] + 1 : 1; | ||
return { ...acc, [k]: count }; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters