Skip to content

Commit 9e1380e

Browse files
authored
Fix default params value (#336)
* 🔧 Ignore history extension folder * 🐛 Set all default options Previously, if createResizedImage was called with option param with only one key defined the app was crashing. Indeed all options key need to be defined on native side.
1 parent 4433769 commit 9e1380e

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,6 @@ android/keystores/debug.keystore
6565

6666
# generated by bob
6767
lib/
68+
69+
# VSCode history extension
70+
.history

src/index.tsx

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NativeModules } from 'react-native';
2-
import type { ResizeFormat, ResizeMode, Response } from './types';
2+
import type { Options, ResizeFormat, ResizeMode, Response } from './types';
33
export type { ResizeFormat, ResizeMode, Response } from './types';
44

55
// @ts-expect-error
@@ -9,6 +9,11 @@ const ImageResizer = isTurboModuleEnabled
99
? require('./NativeImageResizer').default
1010
: NativeModules.ImageResizer;
1111

12+
const defaultOptions: Options = {
13+
mode: 'contain',
14+
onlyScaleDown: false,
15+
};
16+
1217
function createResizedImage(
1318
uri: string,
1419
width: number,
@@ -18,32 +23,9 @@ function createResizedImage(
1823
rotation?: number,
1924
outputPath?: string,
2025
keepMeta?: boolean,
21-
options: {
22-
/**
23-
* Either `contain` (the default), `cover`, or `stretch`. Similar to
24-
* [react-native <Image>'s resizeMode](https://reactnative.dev/docs/image#resizemode)
25-
*
26-
* - `contain` will fit the image within `width` and `height`,
27-
* preserving its ratio
28-
* - `cover` will make sure at least one dimension fits `width` or
29-
* `height`, and the other is larger, also preserving its ratio.
30-
* - `stretch` will resize the image to exactly `width` and `height`.
31-
*
32-
* (Default: 'contain')
33-
*/
34-
mode?: ResizeMode;
35-
/**
36-
* Whether to avoid resizing the image to be larger than the original.
37-
* (Default: false)
38-
*/
39-
onlyScaleDown?: boolean;
40-
} = {
41-
mode: 'contain',
42-
onlyScaleDown: false,
43-
}
26+
options: Options = defaultOptions
4427
): Promise<Response> {
45-
const mode = options.mode;
46-
const onlyScaleDown = options.onlyScaleDown;
28+
const { mode, onlyScaleDown } = { ...defaultOptions, ...options };
4729

4830
return ImageResizer.createResizedImage(
4931
uri,

src/types.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,24 @@ export interface Response {
99

1010
export type ResizeFormat = 'PNG' | 'JPEG' | 'WEBP';
1111
export type ResizeMode = 'contain' | 'cover' | 'stretch';
12+
13+
export type Options = {
14+
/**
15+
* Either `contain` (the default), `cover`, or `stretch`. Similar to
16+
* [react-native <Image>'s resizeMode](https://reactnative.dev/docs/image#resizemode)
17+
*
18+
* - `contain` will fit the image within `width` and `height`,
19+
* preserving its ratio
20+
* - `cover` will make sure at least one dimension fits `width` or
21+
* `height`, and the other is larger, also preserving its ratio.
22+
* - `stretch` will resize the image to exactly `width` and `height`.
23+
*
24+
* (Default: 'contain')
25+
*/
26+
mode?: ResizeMode;
27+
/**
28+
* Whether to avoid resizing the image to be larger than the original.
29+
* (Default: false)
30+
*/
31+
onlyScaleDown?: boolean;
32+
};

0 commit comments

Comments
 (0)