Skip to content

Commit f035c52

Browse files
committed
refactor: percentage branded type with controls
Signed-off-by: Adam Setch <[email protected]>
1 parent 7afaeff commit f035c52

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

src/renderer/utils/notifications/sound.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { Percentage } from '../../types';
22
import {
33
canDecreaseVolume,
44
canIncreaseVolume,
5+
decreaseVolume,
6+
increaseVolume,
57
volumePercentageToLevel,
68
} from './sound';
79

@@ -19,10 +21,24 @@ describe('renderer/utils/notifications/sound.ts', () => {
1921
expect(canDecreaseVolume(100 as Percentage)).toBe(true);
2022
});
2123

24+
it('should decrease volume by step amount', () => {
25+
expect(decreaseVolume(100 as Percentage)).toBe(90);
26+
expect(decreaseVolume(50 as Percentage)).toBe(40);
27+
expect(decreaseVolume(0 as Percentage)).toBe(0);
28+
expect(decreaseVolume(-10 as Percentage)).toBe(0);
29+
});
30+
2231
it('can increase volume percentage', () => {
2332
expect(canIncreaseVolume(10 as Percentage)).toBe(true);
2433
expect(canIncreaseVolume(90 as Percentage)).toBe(true);
2534
expect(canIncreaseVolume(100 as Percentage)).toBe(false);
2635
expect(canIncreaseVolume(110 as Percentage)).toBe(false);
2736
});
37+
38+
it('should increase volume by step amount', () => {
39+
expect(increaseVolume(0 as Percentage)).toBe(10);
40+
expect(increaseVolume(50 as Percentage)).toBe(60);
41+
expect(increaseVolume(100 as Percentage)).toBe(100);
42+
expect(increaseVolume(110 as Percentage)).toBe(100);
43+
});
2844
});

src/renderer/utils/notifications/sound.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@ export function volumePercentageToLevel(percentage: Percentage): number {
1919
return percentage / 100;
2020
}
2121

22+
/**
23+
* Returns true if can decrease volume percentage further
24+
*/
25+
export function canDecreaseVolume(volumePercentage: Percentage) {
26+
return volumePercentage - VOLUME_STEP >= MINIMUM_VOLUME_PERCENTAGE;
27+
}
28+
29+
/**
30+
* Returns true if can increase volume percentage further
31+
*/
32+
export function canIncreaseVolume(volumePercentage: Percentage) {
33+
return volumePercentage + VOLUME_STEP <= MAXIMUM_VOLUME_PERCENTAGE;
34+
}
35+
2236
/**
2337
* Decrease volume by step amount
2438
*/
@@ -27,7 +41,7 @@ export function decreaseVolume(volume: Percentage) {
2741
return volume - VOLUME_STEP;
2842
}
2943

30-
return volume;
44+
return MINIMUM_VOLUME_PERCENTAGE;
3145
}
3246

3347
/**
@@ -38,19 +52,5 @@ export function increaseVolume(volume: Percentage) {
3852
return volume + VOLUME_STEP;
3953
}
4054

41-
return volume;
42-
}
43-
44-
/**
45-
* Returns true if can increase volume percentage further
46-
*/
47-
export function canIncreaseVolume(volumePercentage: Percentage) {
48-
return volumePercentage + VOLUME_STEP <= MAXIMUM_VOLUME_PERCENTAGE;
49-
}
50-
51-
/**
52-
* Returns true if can decrease volume percentage further
53-
*/
54-
export function canDecreaseVolume(volumePercentage: Percentage) {
55-
return volumePercentage - VOLUME_STEP >= MINIMUM_VOLUME_PERCENTAGE;
55+
return MAXIMUM_VOLUME_PERCENTAGE;
5656
}

src/renderer/utils/zoom.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ export function zoomLevelToPercentage(zoom: number): Percentage {
3636
RECOMMENDED_ZOOM_PERCENTAGE) as Percentage;
3737
}
3838

39+
/**
40+
* Returns true if can decrease zoom percentage further
41+
*/
42+
export function canDecreaseZoom(zoomPercentage: Percentage) {
43+
return zoomPercentage - ZOOM_STEP >= MINIMUM_ZOOM_PERCENTAGE;
44+
}
45+
46+
/**
47+
* Returns true if can increase zoom percentage further
48+
*/
49+
export function canIncreaseZoom(zoomPercentage: Percentage) {
50+
return zoomPercentage + ZOOM_STEP <= MAXIMUM_ZOOM_PERCENTAGE;
51+
}
52+
3953
/**
4054
* Decrease zoom by step amount
4155
*/
@@ -57,17 +71,3 @@ export function increaseZoom(zoomPercentage: Percentage) {
5771
);
5872
}
5973
}
60-
61-
/**
62-
* Returns true if can increase zoom percentage further
63-
*/
64-
export function canIncreaseZoom(zoomPercentage: Percentage) {
65-
return zoomPercentage + ZOOM_STEP <= MAXIMUM_ZOOM_PERCENTAGE;
66-
}
67-
68-
/**
69-
* Returns true if can decrease zoom percentage further
70-
*/
71-
export function canDecreaseZoom(zoomPercentage: Percentage) {
72-
return zoomPercentage - ZOOM_STEP >= MINIMUM_ZOOM_PERCENTAGE;
73-
}

0 commit comments

Comments
 (0)