-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): 新增 sampleBy, sampleIndex
- Loading branch information
Showing
6 changed files
with
67 additions
and
0 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
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,12 @@ | ||
import { nthArg } from 'lodash-uni' | ||
import { sampleBy } from './sampleBy' | ||
|
||
describe('sampleBy', () => { | ||
test('ok', () => { | ||
expect(sampleBy([1, 2], (_, i) => i)).toBeOneOf([0, 1]) | ||
expect(sampleBy([1, 2], nthArg(1))).toBeOneOf([0, 1]) | ||
expect(sampleBy([], (_, i) => i)).toBeUndefined() | ||
expect(sampleBy({ x: 1, y: 'dd' }, (_, k) => k)).toBeOneOf(['x', 'y']) | ||
expect(sampleBy({}, (_, k) => k)).toBeUndefined() | ||
}) | ||
}) |
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,26 @@ | ||
import { forOwn, isArray, isObject, sample } from 'lodash-uni' | ||
|
||
/** | ||
* 从集合中随机获得一个元素的迭代值。 | ||
* | ||
* @param collection 集合 | ||
* @param iteratee 迭代函数 | ||
*/ | ||
export function sampleBy<T, X>( | ||
collection: T[], | ||
iteratee: (element: T, index: number) => X, | ||
): X | undefined | ||
export function sampleBy<T extends Record<any, any>, X>( | ||
collection: T, | ||
iteratee: <K extends keyof T>(value: T[K], key: K) => X, | ||
): X | undefined | ||
export function sampleBy(collection: any, iteratee: any): any { | ||
if (isArray(collection)) { | ||
return sample(collection.map(iteratee)) | ||
} else if (isObject(collection)) { | ||
const values: any[] = [] | ||
forOwn(collection, (v, k) => values.push(iteratee(v, k))) | ||
return sample(values) | ||
} | ||
return undefined | ||
} |
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,10 @@ | ||
import { sampleIndex } from './sampleIndex' | ||
|
||
describe('sampleIndex', () => { | ||
test('ok', () => { | ||
expect(sampleIndex([1, 2])).toBeOneOf([0, 1]) | ||
expect(sampleIndex([])).toBeUndefined() | ||
expect(sampleIndex({ x: 1, y: 'o' })).toBeOneOf(['x', 'y']) | ||
expect(sampleIndex({})).toBeUndefined() | ||
}) | ||
}) |
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,15 @@ | ||
import { nthArg } from 'lodash-uni' | ||
import { sampleBy } from './sampleBy' | ||
|
||
/** | ||
* 从集合中随机获得一个元素的索引(数组)或键(对象)。 | ||
* | ||
* @param collection 集合 | ||
*/ | ||
export function sampleIndex<T>(collection: T[]): number | undefined | ||
export function sampleIndex<T extends Record<any, any>>( | ||
collection: T, | ||
): keyof T | undefined | ||
export function sampleIndex(collection: any): any { | ||
return sampleBy(collection, nthArg(1)) | ||
} |