Skip to content

Commit df2ce89

Browse files
committed
feat: Update applyLanes() util to support nested string key and function accessors for start/end properties. Add as top-level export
1 parent b4d4fa3 commit df2ce89

5 files changed

Lines changed: 64 additions & 2 deletions

File tree

.changeset/slow-streets-look.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': patch
3+
---
4+
5+
feat: Update `applyLanes()` util to support nested string key and function accessors for start/end properties

.changeset/solid-badgers-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'layerchart': patch
3+
---
4+
5+
fix: Add `applyLanes()` as top-level export

packages/layerchart/src/lib/utils/array.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,50 @@ describe('applyLanes', () => {
9292
]);
9393
});
9494

95+
it('should work with nested string keys for start and end', () => {
96+
const data = [
97+
{ name: 'Task 1', duration: { start: new Date('2023-01-01'), end: new Date('2023-01-02') } },
98+
{ name: 'Task 2', duration: { start: new Date('2023-01-03'), end: new Date('2023-01-04') } },
99+
];
100+
101+
const result = applyLanes(data, { start: 'duration.start', end: 'duration.end' });
102+
103+
expect(result).toEqual([
104+
{
105+
name: 'Task 1',
106+
duration: { start: new Date('2023-01-01'), end: new Date('2023-01-02') },
107+
lane: 0,
108+
},
109+
{
110+
name: 'Task 2',
111+
duration: { start: new Date('2023-01-03'), end: new Date('2023-01-04') },
112+
lane: 0,
113+
},
114+
]);
115+
});
116+
117+
it('should work with function accessors for start and end', () => {
118+
const data = [
119+
{ name: 'Task 1', duration: { start: new Date('2023-01-01'), end: new Date('2023-01-02') } },
120+
{ name: 'Task 2', duration: { start: new Date('2023-01-03'), end: new Date('2023-01-04') } },
121+
];
122+
123+
const result = applyLanes(data, { start: (d) => d.duration.start, end: (d) => d.duration.end });
124+
125+
expect(result).toEqual([
126+
{
127+
name: 'Task 1',
128+
duration: { start: new Date('2023-01-01'), end: new Date('2023-01-02') },
129+
lane: 0,
130+
},
131+
{
132+
name: 'Task 2',
133+
duration: { start: new Date('2023-01-03'), end: new Date('2023-01-04') },
134+
lane: 0,
135+
},
136+
]);
137+
});
138+
95139
it('should handle empty array', () => {
96140
const data: any[] = [];
97141
const result = applyLanes(data);

packages/layerchart/src/lib/utils/array.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Numeric } from 'd3-array';
22
import { extent as d3extent } from 'd3-array';
3+
import { accessor, type Accessor } from './common.js';
34

45
/**
56
* Wrapper around d3-array's `extent()` but remove [undefined, undefined] return type
@@ -27,14 +28,20 @@ export function arraysEqual(arr1: unknown[], arr2: unknown[]) {
2728
*/
2829
export function applyLanes<T extends Record<string, any>>(
2930
data: T[],
30-
options: { start: keyof T; end: keyof T } = { start: 'start' as keyof T, end: 'end' as keyof T }
31+
options: { start: Accessor<T>; end: Accessor<T> } = {
32+
start: 'start',
33+
end: 'end',
34+
}
3135
) {
3236
const result: (T & { lane: number })[] = [];
3337
let stack: T[] = [];
3438

39+
const startAccessor = accessor(options.start as any);
40+
const endAccessor = accessor(options.end as any);
41+
3542
for (const d of data) {
3643
let lane = stack.findIndex(
37-
(s) => s[options.end] <= d[options.start] && s[options.start] < d[options.start]
44+
(s) => endAccessor(s) <= startAccessor(d) && startAccessor(s) < startAccessor(d)
3845
);
3946
if (lane === -1) {
4047
lane = stack.length;

packages/layerchart/src/lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { applyLanes } from './array.js';
12
export * from './canvas.js';
23
export * from './common.js';
34
export * from './geo.js';

0 commit comments

Comments
 (0)