-
Notifications
You must be signed in to change notification settings - Fork 4
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
16 changed files
with
174 additions
and
38 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,9 +1,45 @@ | ||
import { mapPick } from '../src/array'; | ||
import { mapPick, arrayToTree } from '../src/array'; | ||
|
||
describe('array', () => { | ||
test('get', () => { | ||
test('mapPick', () => { | ||
expect(mapPick([{ id: 1, name: 'xx' }], { label: ['name'], value: ['id'] })).toStrictEqual([ | ||
{ label: 'xx', value: 1 }, | ||
]); | ||
}); | ||
|
||
test('arrayToTree', () => { | ||
const arr = [ | ||
{ id: 1, parentId: 0 }, | ||
{ id: 2, parentId: 1 }, | ||
{ id: 3, parentId: 2 }, | ||
{ id: 4, parentId: 3 }, | ||
]; | ||
expect(arrayToTree(arr)[0]).toStrictEqual({ | ||
children: [ | ||
{ | ||
id: 1, | ||
parentId: 0, | ||
children: [ | ||
{ | ||
id: 2, | ||
parentId: 1, | ||
children: [ | ||
{ | ||
id: 3, | ||
parentId: 2, | ||
children: [ | ||
// @ts-ignore | ||
{ | ||
id: 4, | ||
parentId: 3, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |
Empty file.
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 @@ | ||
/** | ||
* 移动数组中元素位置 | ||
* @param arr | ||
* @param from | ||
* @param to | ||
*/ | ||
function arrayMove<T>(arr: T[], from: number, to: number) { | ||
const startIndex = from < 0 ? arr.length + from : from; | ||
|
||
if (startIndex >= 0 && startIndex < arr.length) { | ||
const endIndex = to < 0 ? arr.length + to : to; | ||
|
||
const [item] = arr.splice(from, 1); | ||
arr.splice(endIndex, 0, item); | ||
} | ||
} | ||
|
||
export default arrayMove; |
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 @@ | ||
/** | ||
* 从数组中随机获取一项 | ||
* @param arr {T[]} | ||
* @return {T} | ||
*/ | ||
function arrayRandom<T>(arr: T[]): T { | ||
return arr[Math.floor(Math.random() * arr.length)]; | ||
} | ||
|
||
export default arrayRandom; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { Dictionary } from '../type'; | ||
|
||
function flipObject<T extends Dictionary<any>>(obj: T): WeakMap<T[keyof T], keyof T> { | ||
return Object.keys(obj).reduce((acc, key) => { | ||
acc[obj[key]] = key; | ||
return acc; | ||
}, new WeakMap()); | ||
} | ||
|
||
export default flipObject; |
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 @@ | ||
export { default as TimeoutMap } from './timeout-map'; | ||
export { default as mapObject } from './map-object'; | ||
export { default as flipObject } from './flip-object'; |
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 @@ | ||
import type { Dictionary } from '../type'; | ||
|
||
/** | ||
* 遍历obj返回新的对象 | ||
* @param obj | ||
* @param predicate | ||
*/ | ||
function mapObject<U extends Record<keyof T, any>, T extends Dictionary<any>>( | ||
obj: T, | ||
predicate: (key: keyof T, value: T[keyof T], obj: T) => U[keyof U], | ||
): U { | ||
return Object.keys(obj).reduce<U>((acc, key) => { | ||
acc[key as keyof U] = predicate(key, obj[key], obj); | ||
return acc; | ||
}, {} as U); | ||
} | ||
|
||
export default mapObject; |
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,9 @@ | ||
/** | ||
* 首字母大写 | ||
* @param str | ||
*/ | ||
function capitalize(str: string) { | ||
return str.charAt(0).toUpperCase() + str.slice(1); | ||
} | ||
|
||
export default capitalize; |
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,3 +1,4 @@ | ||
export { default as byteLength } from './byte-length'; | ||
export { default as byteToReadableSize } from './byte-to-readable-size'; | ||
export { default as camelCase } from './camel-case'; | ||
export { default as camelcase } from './camelcase'; | ||
export { default as capitalize } from './capitalize'; |
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