-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #63 from jokester/stressline
stress: utils like lodash / underscore , but smaller
- Loading branch information
Showing
22 changed files
with
142 additions
and
373 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
This file was deleted.
Oops, something went wrong.
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
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
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,23 @@ | ||
export function* chunk<T>(elements: Iterable<T>, chunkSize: number): Iterable<T[]> { | ||
let currentChunk: T[] = []; | ||
for (const e of elements) { | ||
currentChunk.push(e); | ||
if (currentChunk.length >= chunkSize) { | ||
yield currentChunk; | ||
currentChunk = []; | ||
} | ||
} | ||
if (currentChunk.length) { | ||
yield currentChunk; | ||
} | ||
} | ||
|
||
export function* chunkArray<T>(elements: readonly T[], chunkSize: number): Iterable<T[]> { | ||
for (let s = 0; ; s += chunkSize) { | ||
const chunk = elements.slice(s, s + chunkSize); | ||
if (!chunk.length) { | ||
break; | ||
} | ||
yield chunk; | ||
} | ||
} |
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,11 @@ | ||
import { groupBy } from './groupBy'; | ||
|
||
describe(groupBy, () => { | ||
it('groups value with provided keyer()', () => { | ||
expect(groupBy([], () => 0)).toEqual({}); | ||
expect(groupBy(new Set([1, 2, 4]), (v) => v % 2)).toEqual({ | ||
1: [1], | ||
0: [2, 4], | ||
}); | ||
}); | ||
}); |
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,16 @@ | ||
import { DefaultMap } from '../collection/default-map'; | ||
|
||
export function groupBy<T, K extends string | number | symbol>( | ||
values: Iterable<T>, | ||
keyer: (value: T) => K, | ||
): Record<K, T[]> { | ||
return Object.fromEntries(groupByAsMap(values, keyer)) as Record<K, T[]>; | ||
} | ||
|
||
export function groupByAsMap<T, K>(values: Iterable<T>, keyer: (value: T) => K): ReadonlyMap<K, T[]> { | ||
const map = new DefaultMap<K, T[]>(() => []); | ||
for (const v of values) { | ||
map.getOrCreate(keyer(v)).push(v); | ||
} | ||
return map; | ||
} |
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,12 @@ | ||
import { sortBy } from './sortBy'; | ||
|
||
describe('orderBy', () => { | ||
it('sorts value DESC by "natural" JS ordering', () => { | ||
expect(sortBy([2, 3, 1], (v) => v, false)).toEqual([3, 2, 1]); | ||
expect(sortBy([1, 1, 2], (v) => v, false)).toEqual([2, 1, 1]); | ||
}); | ||
it('sorts value ASC by "natural" JS ordering', () => { | ||
expect(sortBy([2, 3, 1], (v) => v)).toEqual([1, 2, 3]); | ||
expect(sortBy([1, 1, 2], (v) => v)).toEqual([1, 1, 2]); | ||
}); | ||
}); |
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,18 @@ | ||
/** | ||
* (for more complicated case, consider fp-ts Ord<T> typeclass) | ||
* @param values | ||
* @param key (if it should be cached, caller should use a cached impl) | ||
* @param asc | ||
*/ | ||
export function sortBy<T, O>(values: T[], key: (v: T) => O, asc = true): T[] { | ||
const indexes = values.map((v, i) => ({ v, i })); | ||
return indexes.sort((a, b) => (asc ? compare(a.v, b.v) : -compare(a.v, b.v))).map((_) => _.v); | ||
} | ||
|
||
function compare(a: any, b: any): number { | ||
if (a < b) { | ||
return -1; | ||
} else if (a > b) { | ||
return 1; | ||
} else return 0; | ||
} |
Oops, something went wrong.