Skip to content

Commit 23d4ad7

Browse files
committed
Add splitBySeparator util
1 parent 97465c9 commit 23d4ad7

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { expect } from 'chai';
2+
import { splitBySeparator } from './utils';
3+
4+
describe('item action utils', function () {
5+
describe('splitBySeparator', function () {
6+
it('returns an empty array for an empty input', function () {
7+
const result = splitBySeparator([]);
8+
expect(result).is.empty;
9+
});
10+
11+
it('returns a single item for a single input', function () {
12+
const result = splitBySeparator([{ label: 'Foo', action: 'foo' }]);
13+
expect(result).deep.equal([[{ label: 'Foo', action: 'foo' }]]);
14+
});
15+
16+
it('splits four items separated by a separator', function () {
17+
const result = splitBySeparator([
18+
{ label: 'Foo', action: 'foo' },
19+
{ label: 'Bar', action: 'bar' },
20+
{ separator: true },
21+
{ label: 'Baz', action: 'baz' },
22+
{ label: 'Qux', action: 'qux' },
23+
]);
24+
expect(result).deep.equal([
25+
[
26+
{ label: 'Foo', action: 'foo' },
27+
{ label: 'Bar', action: 'bar' },
28+
],
29+
[
30+
{ label: 'Baz', action: 'baz' },
31+
{ label: 'Qux', action: 'qux' },
32+
],
33+
]);
34+
});
35+
36+
it('disregards leading separators', function () {
37+
const result = splitBySeparator([
38+
{ separator: true },
39+
{ label: 'Foo', action: 'foo' },
40+
]);
41+
expect(result).deep.equal([[{ label: 'Foo', action: 'foo' }]]);
42+
});
43+
44+
it('disregards trailing separators', function () {
45+
const result = splitBySeparator([
46+
{ label: 'Foo', action: 'foo' },
47+
{ separator: true },
48+
]);
49+
expect(result).deep.equal([[{ label: 'Foo', action: 'foo' }]]);
50+
});
51+
});
52+
});

packages/compass-components/src/components/actions/utils.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ItemSeparator, ItemAction, MenuAction } from './types';
1+
import type { ItemBase, ItemSeparator, MenuAction } from './types';
22

33
export function isSeparatorMenuAction(value: unknown): value is ItemSeparator {
44
return (
@@ -12,3 +12,24 @@ export function isSeparatorMenuAction(value: unknown): value is ItemSeparator {
1212
export function actionTestId(dataTestId: string | undefined, action: string) {
1313
return dataTestId ? `${dataTestId}-${action}-action` : undefined;
1414
}
15+
16+
export function splitBySeparator<Action extends string>(
17+
actions: MenuAction<Action>[]
18+
) {
19+
const result: ItemBase<Action>[][] = [];
20+
let currentGroup: ItemBase<Action>[] = [];
21+
for (const action of actions) {
22+
if (isSeparatorMenuAction(action)) {
23+
if (currentGroup.length > 0) {
24+
result.push(currentGroup);
25+
currentGroup = [];
26+
}
27+
} else {
28+
currentGroup.push(action);
29+
}
30+
}
31+
if (currentGroup.length > 0) {
32+
result.push(currentGroup);
33+
}
34+
return result;
35+
}

packages/compass-components/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export type {
5757
ItemSeparator,
5858
MenuAction,
5959
} from './components/actions/types';
60+
export { splitBySeparator } from './components/actions/utils';
6061
export type { GroupedItemAction } from './components/actions/item-action-group';
6162

6263
export { ItemActionControls } from './components/actions/item-action-controls';

0 commit comments

Comments
 (0)