Skip to content

Commit 2b19b71

Browse files
committed
refactor: make array types in public API readonly
Mark all array properties in the public API as readonly since the library is not supposed to mutate any of the props. Update internal function signatures to accept readonly arrays accordingly.
1 parent 07b80bc commit 2b19b71

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function traverse(nodes: Node[], target: string, apply: (node: Node) => Node[] |
3030

3131
export function withPlugins(
3232
root: Node[],
33-
plugins: Plugin[] = [],
33+
plugins: readonly Plugin[] = [],
3434
augmentations: Augmentation[] = [],
3535
): { config: Node[]; augmentation: Augmentation } {
3636
let config = root;

src/plugins/video/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ declare module "../../types.js" {
3838
/** disables the capability of remote playback */
3939
disableRemotePlayback?: boolean;
4040
/** an array of video files */
41-
sources: {
41+
sources: readonly {
4242
/** video source URL */
4343
src: string;
4444
/** video source type (e.g., `video/mp4`) */

src/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ export interface LightboxProps {
2828
/** starting slide index */
2929
index: number;
3030
/** slides to display in the lightbox */
31-
slides: Slide[];
31+
slides: readonly Slide[];
3232
/** custom render functions */
3333
render: Render;
3434
/** custom UI labels / translations */
3535
labels: Labels;
3636
/** enabled plugins */
37-
plugins: Plugin[];
37+
plugins: readonly Plugin[];
3838
/** toolbar settings */
3939
toolbar: ToolbarSettings;
4040
/** carousel settings */
@@ -87,7 +87,7 @@ export interface SlideImage extends GenericSlide {
8787
/** `object-fit` setting */
8888
imageFit?: ImageFit;
8989
/** alternative images to be passed to the 'srcSet' */
90-
srcSet?: ImageSource[];
90+
srcSet?: readonly ImageSource[];
9191
}
9292

9393
/** Image source */
@@ -281,14 +281,14 @@ export type LightboxStateSwipeAction = {
281281
/** Lightbox state update action */
282282
export type LightboxStateUpdateAction = {
283283
type: "update";
284-
slides: Slide[];
284+
slides: readonly Slide[];
285285
index: number;
286286
};
287287

288288
/** Lightbox state */
289289
export interface LightboxState {
290290
/** lightbox slides */
291-
slides: Slide[];
291+
slides: readonly Slide[];
292292
/** current slide index */
293293
currentIndex: number;
294294
/** current slide index in the (-∞, +∞) range */
@@ -414,7 +414,7 @@ export type Label = keyof Labels;
414414
/** Toolbar settings */
415415
export interface ToolbarSettings {
416416
/** buttons to render in the toolbar */
417-
buttons: (ToolbarButtonKey | React.ReactNode)[];
417+
buttons: readonly (ToolbarButtonKey | React.ReactNode)[];
418418
}
419419

420420
export type ToolbarButtonKey = keyof ToolbarButtonKeys;
@@ -469,7 +469,7 @@ export type DeepPartial<T extends object, K extends keyof T = keyof T, E extends
469469
};
470470

471471
// eslint-disable-next-line @typescript-eslint/no-explicit-any
472-
export type DeepPartialValue<T, E extends string = never> = T extends any[]
472+
export type DeepPartialValue<T, E extends string = never> = T extends readonly any[]
473473
? T
474474
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
475475
T extends (...props: any[]) => any

src/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function label(labels: Labels | undefined, defaultLabel: Label) {
4343
return translateLabel(labels, defaultLabel);
4444
}
4545

46-
export function translateSlideCounter(labels: Labels | undefined, slides: Slide[], index: number) {
46+
export function translateSlideCounter(labels: Labels | undefined, slides: readonly Slide[], index: number) {
4747
return translateLabel(labels, "{index} of {total}")
4848
.replace(/\{index}/g, `${getSlideIndex(index, slides.length) + 1}`)
4949
.replace(/\{total}/g, `${slides.length}`);
@@ -120,15 +120,15 @@ export function getSlideIndex(index: number, slidesCount: number) {
120120
return slidesCount > 0 ? ((index % slidesCount) + slidesCount) % slidesCount : 0;
121121
}
122122

123-
export function hasSlides(slides: Slide[]): slides is [Slide, ...Slide[]] {
123+
export function hasSlides(slides: readonly Slide[]): slides is [Slide, ...Slide[]] {
124124
return slides.length > 0;
125125
}
126126

127127
export function getSlide(slides: [Slide, ...Slide[]], index: number) {
128128
return slides[getSlideIndex(index, slides.length)];
129129
}
130130

131-
export function getSlideIfPresent(slides: Slide[], index: number) {
131+
export function getSlideIfPresent(slides: readonly Slide[], index: number) {
132132
return hasSlides(slides) ? getSlide(slides, index) : undefined;
133133
}
134134

@@ -162,7 +162,7 @@ export function stopNavigationEventsPropagation() {
162162
return { onPointerDown: stopPropagation, onKeyDown: stopPropagation, onWheel: stopPropagation };
163163
}
164164

165-
export function calculatePreload(carousel: CarouselSettings, slides: Slide[], minimum = 0) {
165+
export function calculatePreload(carousel: CarouselSettings, slides: readonly Slide[], minimum = 0) {
166166
return Math.min(
167167
carousel.preload,
168168
Math.max(carousel.finite ? slides.length - 1 : Math.floor(slides.length / 2), minimum),

0 commit comments

Comments
 (0)