Last active
September 24, 2019 09:20
-
-
Save iamandrewluca/efe2d236994642cb362160ec48ae9a29 to your computer and use it in GitHub Desktop.
Move array elements to start of array
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
import { moveElementsToStart } from '../move-elements-to-start' | |
describe('moveElementsToStart', () => { | |
it('should move simple elements to start', () => { | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], [])).toEqual([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
]) | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], ['AU', 'RO'])).toEqual([ | |
'AU', 'RO', 'CH', 'IT', 'RU', 'MD', 'US' | |
]) | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], ['RO', 'AU'])).toEqual([ | |
'RO', 'AU', 'CH', 'IT', 'RU', 'MD', 'US' | |
]) | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], ['CH', 'IT'])).toEqual([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
]) | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], ['US', 'RO'])).toEqual([ | |
'US', 'RO', 'CH', 'IT', 'RU', 'MD', 'AU' | |
]) | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], [], i => i)).toEqual([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
]) | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], ['AU', 'RO'], i => i)).toEqual([ | |
'AU', 'RO', 'CH', 'IT', 'RU', 'MD', 'US' | |
]) | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], ['CH', 'IT'], i => i)).toEqual([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
]) | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], ['US', 'RO'], i => i)).toEqual([ | |
'US', 'RO', 'CH', 'IT', 'RU', 'MD', 'AU' | |
]) | |
}) | |
it('should move complex elements to start', () => { | |
expect(moveElementsToStart([ | |
{ code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'AU' }, { code: 'US' }, { code: 'RO' }, | |
], [], i => i.code)).toEqual([ | |
{ code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'AU' }, { code: 'US' }, { code: 'RO' }, | |
]) | |
expect(moveElementsToStart([ | |
{ code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'AU' }, { code: 'US' }, { code: 'RO' }, | |
], ['AU', 'RO'], i => i.code)).toEqual([ | |
{ code: 'AU' }, { code: 'RO' }, { code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'US' }, | |
]) | |
expect(moveElementsToStart([ | |
{ code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'AU' }, { code: 'US' }, { code: 'RO' }, | |
], ['RO', 'AU'], i => i.code)).toEqual([ | |
{ code: 'RO' }, { code: 'AU' }, { code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'US' }, | |
]) | |
expect(moveElementsToStart([ | |
{ code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'AU' }, { code: 'US' }, { code: 'RO' }, | |
], ['CH', 'IT'], i => i.code)).toEqual([ | |
{ code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'AU' }, { code: 'US' }, { code: 'RO' }, | |
]) | |
expect(moveElementsToStart([ | |
{ code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'AU' }, { code: 'US' }, { code: 'RO' }, | |
], ['US', 'RO'], i => i.code)).toEqual([ | |
{ code: 'US' }, { code: 'RO' }, { code: 'CH' }, { code: 'IT' }, { code: 'RU' }, { code: 'MD' }, { code: 'AU' }, | |
]) | |
}) | |
it('should do nothing if id not found', () => { | |
expect(moveElementsToStart([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
], ['USA'])).toEqual([ | |
'CH', 'IT', 'RU', 'MD', 'AU', 'US', 'RO' | |
]) | |
}) | |
it('should do nothing if complex object and not getId arg', () => { | |
expect(moveElementsToStart([ | |
{ code: 'CH' }, { code: 'IT' }, { code: 'RU' }, | |
], ['RU'])).toEqual([ | |
{ code: 'CH' }, { code: 'IT' }, { code: 'RU' }, | |
]) | |
}) | |
}) |
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
export function moveElementsToStart<T = any>(items: T[], ids: unknown[], getId: (item: T) => unknown = item => item): T[] { | |
items = items.slice() | |
const extractedItems = ids | |
.map(id => items.find(item => getId(item) === id)) | |
.filter((item): item is T => item !== undefined) | |
.map(item => items.splice(items.indexOf(item), 1)[0]) | |
return [...extractedItems, ...items] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment