Skip to content

Commit dfb917b

Browse files
committed
ゼロ幅スペースで頑張る
1 parent 7eac374 commit dfb917b

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/post/renderer.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,18 @@ describe("Renderer", () => {
485485
expect(match).not.toBeNull();
486486
const labels: string[] = JSON.parse(match![1]);
487487

488+
const hasVisibleCharacters = (label: string): boolean =>
489+
/[0-9:]/.test(label);
490+
488491
expect(labels.length).toBe(times.length);
489492
expect(labels[0]).toBe(firstLabel);
490493
expect(labels[labels.length - 1]).toBe(lastLabel);
491-
const visibleLabelCount: number = labels.filter(
492-
(label: string): boolean => label.trim().length > 0,
493-
).length;
494+
const visibleLabelCount: number = labels.filter(hasVisibleCharacters).length;
494495
expect(visibleLabelCount).toBeLessThanOrEqual(MAX_VISIBLE_TIME_LABELS);
495-
expect(
496-
labels.some((label: string): boolean => label.trim().length === 0),
497-
).toBe(true);
496+
const hiddenLabels: string[] = labels.filter(
497+
(label: string): boolean => !hasVisibleCharacters(label),
498+
);
499+
expect(hiddenLabels.length).toBe(times.length - visibleLabelCount);
500+
expect(new Set(hiddenLabels).size).toBe(hiddenLabels.length);
498501
});
499502
});

src/post/renderer.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import type {
77
} from "./lib";
88

99
export const MAX_VISIBLE_TIME_LABELS: number = 12;
10-
const EMPTY_TIME_LABEL_PLACEHOLDER: string = " ";
10+
const ZERO_WIDTH_ZERO: string = "\u200b";
11+
const ZERO_WIDTH_ONE: string = "\u200c";
12+
const ZERO_WIDTH_SENTINEL: string = "\u200d";
1113

1214
const formatTimeLabels = (times: Date[]): string[] => {
1315
if (times.length === 0) {
@@ -25,16 +27,27 @@ const formatTimeLabels = (times: Date[]): string[] => {
2527
const usableSlots: number = Math.max(MAX_VISIBLE_TIME_LABELS - 2, 1);
2628
const spacing: number = Math.ceil((formattedTimes.length - 2) / usableSlots);
2729

30+
const encodeHiddenLabel = (index: number): string => {
31+
const binary: string = index.toString(2);
32+
return (
33+
ZERO_WIDTH_SENTINEL +
34+
binary
35+
.split("")
36+
.map((digit: string): string =>
37+
digit === "0" ? ZERO_WIDTH_ZERO : ZERO_WIDTH_ONE,
38+
)
39+
.join("")
40+
);
41+
};
42+
2843
return formattedTimes.map(
2944
(label: string, index: number, array: string[]): string => {
3045
if (index === 0 || index === array.length - 1) {
3146
return label;
3247
}
3348

3449
const normalizedIndex: number = index - 1;
35-
return normalizedIndex % spacing === 0
36-
? label
37-
: EMPTY_TIME_LABEL_PLACEHOLDER;
50+
return normalizedIndex % spacing === 0 ? label : encodeHiddenLabel(index);
3851
},
3952
);
4053
};

0 commit comments

Comments
 (0)