Methods related to handling arrays, such as groupBy, unique, deleting arrays, and immutable methods for adding to arrays.
Has immutability and type inference
Insert data into the first entry of the array
import {pull} from '@acrool/js-utils/array'
pull(['A', 'B'], 'C')
// ['C', 'A', 'B']
Insert data into the last entry of the array
import {push} from '@acrool/js-utils/array'
push(['A', 'B'], 'C')
// ['A', 'B', 'C']
Insert data into array
import {insert} from '@acrool/js-utils/array'
insert(['A', 'B', 'C'], 1, 'D')
// ['A', 'D', 'B', 'C']
Move array order
import {move} from '@acrool/js-utils/array'
move(['A', 'B', 'C'], 1, 2)
// ['A', 'C', 'B']
Delete an item of data in the array
import {removeFind} from '@acrool/js-utils/array'
removeFind([
{id: 1, name: 'jack'},
{id: 2, name: 'imagine'},
{id: 3, name: 'gary'},
], row => row.id === 2)
// [
// {id: 1, name: 'jack'},
// {id: 3, name: 'gary'}
// ]
Change an item in an array
import {updateFind} from '@acrool/js-utils/array'
updateFind([
{id: 1, name: 'jack'},
{id: 2, name: 'imagine'},
{id: 3, name: 'gary'},
], {
finder: row => row.id === 1,
updater: row => row.name = 'jack wu'
})
// [
// {id: 1, name: 'jack wu'},
// {id: 2, name: 'imagine'},
// {id: 3, name: 'gary'}
// ]
Delete an item of data in the array
import {removeByIndex} from '@acrool/js-utils/array'
removeByIndex(['A', 'B', 'C'], 1)
// ['A', 'C']
Change an item in an array
import {updateByIndex} from '@acrool/js-utils/array'
updateByIndex([
{id: 1, name: 'jack'},
{id: 2, name: 'imagine'},
{id: 3, name: 'gary'},
], 0, row => row.name = 'jack wu')
// [
// {id: 1, name: 'jack wu'},
// {id: 2, name: 'imagine'},
// {id: 3, name: 'gary'}
// ]
Get unique value in array
import {unique} from '@acrool/js-utils/array'
unique(['a', 'b', 'b', 'c', 'c'])
// ['a', 'b', 'c']
Array to string
import {arrayJoin} from '@acrool/js-utils/array'
arrayJoin(['uploads', 'profile'], '/')
// uploads/profile
split array
import {splitArray} from '@acrool/js-utils/array'
splitArray([1, 2, 3, 4, 5, 6, 7, 8, 9], 2)
// [ [1,2], [3,4], [5,6], [7,8], [9] ]
Classify the data in the array
import {groupBy} from '@acrool/js-utils/array'
groupBy([
{id: 1, name: 'Alice'},
{id: 2, name: 'Bob'},
{id: 3, name: 'Alice'},
{id: 4, name: 'Charlie'},
],(item) => item.name)
// {
// Alice: [
// {id: 1, name: 'Alice'},
// {id: 3, name: 'Alice'},
// ],
// Bob: [
// {id: 2, name: 'Bob'},
// ],
// Charlie: [
// {id: 4, name: 'Charlie'},
// ],
// }
Classify the data in the array
import {groupTreeBy} from '@acrool/js-utils/array'
groupTreeBy([
{id: 1, name: 'Alice', team: {id: 'A', name: 'frontend'}},
{id: 2, name: 'Bob', team: {id: 'A', name: 'frontend'}},
{id: 3, name: 'Alice', team: {id: 'B', name: 'backend'}},
{id: 4, name: 'Charlie', team: {id: 'B', name: 'backend'}},
], (item) => {
const {team, ...child} = item;
return {
groupKey: team.id,
groupData: {
id: team.id,
text: team.name
},
child: {
id: item.id,
nickName: item.name,
},
};
})
// [
// {
// id: 'A',
// text: 'frontend',
// children: [
// {id: 1, nickName: 'Alice'},
// {id: 2, nickName: 'Bob'}
// ],
// },
// {
// id: 'B',
// text: 'backend',
// children: [
// {id: 3, nickName: 'Alice'},
// {id: 4, nickName: 'Charlie'}
// ],
// }
// ]
(immuable)
array in order
import {sort} from '@acrool/js-utils/array'
sort([
{id: 1, name: 'jack'},
{id: 3, name: 'gary'},
{id: 2, name: 'imagine'},
], (a, b) => a.id - b.id)
// [
// {id: 1, name: 'jack'},
// {id: 2, name: 'imagine'},
// {id: 3, name: 'gary'},
// ]
Generate sorting methods and then compare data sorting
import {generateSortByProperty} from '@acrool/js-utils/array'
interface ISourceArray {
id: number,
name: string
team: {id: string, name: string}
}
const arrayData: ISourceArray[] = [
{id: 1, name: 'Alice', team: {id: 'A', name: 'frontend'}},
{id: 2, name: 'Bob', team: {id: 'A', name: 'frontend'}},
{id: 3, name: 'Alice', team: {id: 'B', name: 'backend'}},
{id: 4, name: 'Charlie', team: {id: 'B', name: 'backend'}},
];
const sortFn = generateSortByProperty<ISourceArray>((item) => item.id, 'ASC');
sortFn(arrayData[0], arrayData[1]);
// -1
sortFn(arrayData[2], arrayData[1]);
// 1
sort(arrayData, sortFn);
Generate count item
import {generatorArray} from '@acrool/js-utils/array'
generatorArray(3);
// ['skeleton_0', 'skeleton_1', 'skeleton_2']
toggle array to next -> next -> next loop
import {generatorArray} from '@acrool/js-utils/array'
const toggle = generatorArrayToggle(['a','b','c']);
toggle(a)
// b
toggle(b)
// c
toggle(c)
// a