Skip to content

Commit

Permalink
fix: 解决一些 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 10, 2019
1 parent 2db1403 commit 867fa82
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
35 changes: 17 additions & 18 deletions src/groupBy.ts
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,
)
}
2 changes: 1 addition & 1 deletion src/isEqualArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function isEqualArray(...arrs: any[][]): boolean {
}

if (arrs[i] === arrs[0]) {
return true
continue
}

if (arrs[i].length !== arrs[0].length) {
Expand Down
7 changes: 4 additions & 3 deletions src/isUrl.ts
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)
}
13 changes: 9 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,20 +936,25 @@ describe('isUrl', () => {
[
'http://foo.bar',
'http://foo.bar:80',
'http://foo.bar:8878878',
'http://foo.bar/oop?ddd#cc',
'https://foo.bar',
'ftp://foo.bar',
'http://39.137.107.98:22/hello',
].forEach(item => {
expect(vtils.isUrl(item)).toBeTruthy()
})
})
test('不是', () => {
[
'http://127.0.0.1',
'http://foo.bar:8878878',
'wx://foo.bar',
'foo.bar',
'http://',
'https://',
'ftp://foo.bar',
'http://1111.0.1.22',
'大口大口http://foo.bar',
'http://foo.bar:80得到了',
].forEach(item => {
expect(vtils.isUrl(item)).toBeFalsy()
})
Expand Down Expand Up @@ -1450,8 +1455,8 @@ describe('chunk', () => {
describe('groupBy', () => {
test('ok', () => {
expect(vtils.groupBy([6.1, 4.2, 6.3], Math.floor)).toEqual({ 4: [4.2], 6: [6.1, 6.3] })
expect(vtils.groupBy(['one', 'two', 'three'], 'length')).toEqual({ 3: ['one', 'two'], 5: ['three'] })
expect(vtils.groupBy([{ i: 1 }, { i: 2 }, { i: 2, x: 0 }], 'i')).toEqual({ 1: [{ i: 1 }], 2: [{ i: 2 }, { i: 2, x: 0 }] })
expect(vtils.groupBy(['one', 'two', 'three'], item => item.length)).toEqual({ 3: ['one', 'two'], 5: ['three'] })
expect(vtils.groupBy([{ i: 1 }, { i: 2 }, { i: 2, x: 0 }], item => item.i)).toEqual({ 1: [{ i: 1 }], 2: [{ i: 2 }, { i: 2, x: 0 }] })
})
})

Expand Down

0 comments on commit 867fa82

Please sign in to comment.