-
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.
- Loading branch information
Showing
4 changed files
with
31 additions
and
26 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 |
---|---|---|
@@ -1,26 +1,25 @@ | ||
import { get } from './get' | ||
import { ToPathValue } from './toPath' | ||
export type GroupByIteratee<T, K> = (item: T, index: number) => K | ||
|
||
/** | ||
* 根据 `iteratee` 对 `arr` 进行分组。 | ||
* | ||
* @param arr 要分组的数据 | ||
* @param iteratee 迭代值,字符串或数字表示键路径,函数表示以该函数生成 `key` | ||
* @returns 分组结果 | ||
* @param iteratee 迭代函数 | ||
* @returns 返回分组结果 | ||
*/ | ||
export function groupBy<T>( | ||
export function groupBy<T, K extends keyof any>( | ||
arr: T[], | ||
iteratee: ToPathValue | ((item: T, index: number) => any), | ||
): { [key: string]: T[] } { | ||
const iterateeIsFunction = typeof iteratee === 'function' | ||
return arr.reduce<{ [key: string]: T[] }>((res, item, index) => { | ||
const key = iterateeIsFunction | ||
? (iteratee as any)(item, index) | ||
: get(item as any, iteratee as any) | ||
if (!res[key]) { | ||
res[key] = [] | ||
} | ||
res[key].push(item) | ||
return res | ||
}, {}) | ||
iteratee: GroupByIteratee<T, K>, | ||
) { | ||
return arr.reduce<Record<K, T[]>>( | ||
(res, item, index) => { | ||
const key = iteratee(item, index) | ||
if (!res[key]) { | ||
res[key] = [] | ||
} | ||
res[key].push(item) | ||
return res | ||
}, | ||
{} as any, | ||
) | ||
} |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
/** | ||
* 检查 `value` 是否是一个网址。 | ||
* 检查 `value` 是否是一个有效的网址,仅支持 `http`、`https` 协议,支持 `IP` 域名。 | ||
* | ||
* @param value 要检查的值 | ||
* @returns `value` 是网址返回 `true`,否则返回 `false` | ||
* @returns `value` 是有效的网址返回 `true`,否则返回 `false` | ||
* @see http://urlregex.com/ | ||
*/ | ||
export function isUrl(value: string): boolean { | ||
const re = /^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=+$,\w]+@)?[A-Za-z0-9.-]+|(?:www\.|[-;:&=+$,\w]+@)[A-Za-z0-9.-]+)((?:\/[+~%/.\w\-_]*)?\??(?:[-+=&;%@.\w_]*)#?(?:[.!/\\\w]*))?)$/ | ||
// http://urlregex.com/ ==> Ruby | ||
const re = /^(?:(?:https?):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i | ||
return re.test(value) | ||
} |
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