Skip to content

Commit 60930d1

Browse files
committed
format
1 parent 91d8413 commit 60930d1

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed

src/lib/transforms/jitter.test.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ describe('jitterX', () => {
6969

7070
const data = [{ y: 5 }, { y: 10 }];
7171
// @ts-ignore - Bypassing type checking for tests
72-
const result = jitterX(
73-
{ data, y: 'y' },
74-
{ source: mockRandom }
75-
);
72+
const result = jitterX({ data, y: 'y' }, { source: mockRandom });
7673

7774
// The result should be the same as the input
7875
expect(result.data).toEqual(data);
@@ -85,17 +82,13 @@ describe('jitterX', () => {
8582

8683
const data = [{ x: new Date(Date.UTC(2020, 0, 1)) }, { x: new Date(Date.UTC(2021, 0, 1)) }];
8784
// @ts-ignore - Bypassing type checking for tests
88-
const result = jitterX(
89-
{ data, x: 'x' },
90-
{ source: mockRandom, width: '1 month' }
91-
);
85+
const result = jitterX({ data, x: 'x' }, { source: mockRandom, width: '1 month' });
9286

9387
const { x } = result;
94-
expect(result.data[0][x]).toBeTypeOf("object");
95-
expect(result.data[0][x].getTime).toBeTypeOf("function");
88+
expect(result.data[0][x]).toBeTypeOf('object');
89+
expect(result.data[0][x].getTime).toBeTypeOf('function');
9690
expect(result.data[0][x]).toStrictEqual(new Date(Date.UTC(2020, 0, 16)));
9791
});
98-
9992
});
10093

10194
describe('jitterY', () => {
@@ -163,10 +156,7 @@ describe('jitterY', () => {
163156

164157
const data = [{ x: 5 }, { x: 10 }];
165158
// @ts-ignore - Bypassing type checking for tests
166-
const result = jitterY(
167-
{ data, x: 'x' },
168-
{ source: mockRandom }
169-
);
159+
const result = jitterY({ data, x: 'x' }, { source: mockRandom });
170160

171161
// The result should be the same as the input
172162
expect(result.data).toEqual(data);

src/lib/transforms/jitter.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type JitterOptions = {
1313
width: number;
1414
/** standard deviation for normal jittering */
1515
std: number;
16-
/**
16+
/**
1717
* optional random number source that produces values in range [0,1)
1818
* useful for testing with a deterministic source
1919
*/
@@ -48,9 +48,10 @@ export function jitter(
4848

4949
// Use the provided source or default to Math.random
5050
const rng = options?.source ?? Math.random;
51-
const random = type === 'uniform'
52-
? randomUniform.source(rng)(-width, width)
53-
: randomNormal.source(rng)(0, std);
51+
const random =
52+
type === 'uniform'
53+
? randomUniform.source(rng)(-width, width)
54+
: randomNormal.source(rng)(0, std);
5455

5556
const accKey = channel === 'x' ? JITTER_X : JITTER_Y;
5657
return {
@@ -62,8 +63,8 @@ export function jitter(
6263
typeof value === 'number'
6364
? value + random()
6465
: isDate(value)
65-
? new Date(value.getTime() + random())
66-
: value
66+
? new Date(value.getTime() + random())
67+
: value
6768
};
6869
}),
6970
...channels,

src/routes/transforms/jitter/+page.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ The jitter transform accepts the following options:
6464
- **type**: Distribution type, either `'uniform'` (default) or `'normal'`
6565
- **width**: Width of the uniform distribution (default: `0.35`); used when `type` is `'uniform'`
6666
- **std**: Standard deviation for the normal distribution (default: `0.15`); used when `type` is `'normal'`
67-
- **source**: Optional random number source that produces values in range [0,1).
67+
- **source**: Optional random number source that produces values in range [0,1).
6868

6969
## jitterX
7070

@@ -102,23 +102,39 @@ Jittering also works for temporal data. When jittering Date objects, random time
102102
let { bmi } = $derived(page.data.data);
103103
104104
// Use a subset of the data for this example
105-
const data = $derived(bmi.filter(d => d.year > 2018).map(d => ({ ...d, year: new Date(d.year,0,1)})));
105+
const data = $derived(
106+
bmi
107+
.filter((d) => d.year > 2018)
108+
.map((d) => ({
109+
...d,
110+
year: new Date(d.year, 0, 1)
111+
}))
112+
);
106113
107114
let type = $state('uniform');
108115
let width = $state('1 month');
109116
110-
const timeIntervals = ['1 day', '3 days', '1 week', '2 weeks', '3 weeks', '1 month', '2 months', '1 quarter', '1 year']
117+
const timeIntervals = [
118+
'1 day',
119+
'3 days',
120+
'1 week',
121+
'2 weeks',
122+
'3 weeks',
123+
'1 month',
124+
'2 months',
125+
'1 quarter',
126+
'1 year'
127+
];
111128
</script>
112129
113130
<Select
114131
bind:value={type}
115132
options={['uniform', 'normal']}
116133
label="Distribution type" />
117-
<Select
118-
options={timeIntervals}
119-
bind:value={width}
120-
label={type === 'uniform' ? 'Width' : 'Std'}
121-
/>
134+
<Select
135+
options={timeIntervals}
136+
bind:value={width}
137+
label={type === 'uniform' ? 'Width' : 'Std'} />
122138
123139
<Plot inset={20} x={{ type: 'time' }} y={{ grid: true }}>
124140
<Dot
@@ -130,7 +146,8 @@ Jittering also works for temporal data. When jittering Date objects, random time
130146
},
131147
{
132148
type,
133-
[type === 'uniform' ? 'width' : 'std']: width,
149+
[type === 'uniform' ? 'width' : 'std']:
150+
width
134151
}
135152
)} />
136153
</Plot>

0 commit comments

Comments
 (0)