Skip to content

Commit 9ef7d28

Browse files
committed
Remove resolveSource helper functions
1 parent 6429c1e commit 9ef7d28

File tree

1 file changed

+28
-52
lines changed
  • packages/react-native-web/src/exports/Image

1 file changed

+28
-52
lines changed

packages/react-native-web/src/exports/Image/index.js

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,29 @@ function resolveSource(source: ?Source): ImageSource {
109109
let resolvedSource = { uri: '' };
110110

111111
if (typeof source === 'number') {
112-
resolvedSource = resolveNumericSource(source);
112+
// get the URI from the packager
113+
const asset = getAssetByID(source);
114+
if (asset == null) {
115+
throw new Error(
116+
`Image: asset with ID "${source}" could not be found. Please check the image source or packager.`
117+
);
118+
}
119+
let scale = asset.scales[0];
120+
if (asset.scales.length > 1) {
121+
const preferredScale = PixelRatio.get();
122+
// Get the scale which is closest to the preferred scale
123+
scale = asset.scales.reduce((prev, curr) =>
124+
Math.abs(curr - preferredScale) < Math.abs(prev - preferredScale)
125+
? curr
126+
: prev
127+
);
128+
}
129+
130+
const scaleSuffix = scale !== 1 ? `@${scale}x` : '';
131+
const uri = `${asset.httpServerLocation}/${asset.name}${scaleSuffix}.${asset.type}`;
132+
resolvedSource = { uri, width: asset.width, height: asset.height };
113133
} else if (typeof source === 'string') {
114-
resolvedSource = resolveStringSource(source);
134+
resolvedSource.uri = source;
115135
} else if (Array.isArray(source)) {
116136
if (process.env.NODE_ENV !== 'production') {
117137
console.warn(
@@ -122,67 +142,23 @@ function resolveSource(source: ?Source): ImageSource {
122142

123143
return resolveSource(source[0]);
124144
} else if (source && typeof source.uri === 'string') {
125-
resolvedSource = resolveObjectSource(source);
145+
const { uri, width, height, headers } = source;
146+
resolvedSource = { uri, width, height, headers };
126147
}
127148

128149
if (resolvedSource.uri) {
129150
const match = resolvedSource.uri.match(svgDataUriPattern);
151+
// inline SVG markup may contain characters (e.g., #, ") that need to be escaped
130152
if (match) {
131-
resolvedSource = resolveSvgDataUriSource(resolvedSource, match);
153+
const [, prefix, svg] = match;
154+
const encodedSvg = encodeURIComponent(svg);
155+
resolvedSource.uri = `${prefix}${encodedSvg}`;
132156
}
133157
}
134158

135159
return resolvedSource;
136160
}
137161

138-
// get the URI from the packager
139-
function resolveNumericSource(source: number): ImageSource {
140-
const asset = getAssetByID(source);
141-
if (asset == null) {
142-
throw new Error(
143-
`Image: asset with ID "${source}" could not be found. Please check the image source or packager.`
144-
);
145-
}
146-
let scale = asset.scales[0];
147-
if (asset.scales.length > 1) {
148-
const preferredScale = PixelRatio.get();
149-
// Get the scale which is closest to the preferred scale
150-
scale = asset.scales.reduce((prev, curr) =>
151-
Math.abs(curr - preferredScale) < Math.abs(prev - preferredScale)
152-
? curr
153-
: prev
154-
);
155-
}
156-
157-
const scaleSuffix = scale !== 1 ? `@${scale}x` : '';
158-
const uri = `${asset.httpServerLocation}/${asset.name}${scaleSuffix}.${asset.type}`;
159-
const { height, width } = asset;
160-
161-
return { uri, height, width };
162-
}
163-
164-
function resolveStringSource(source: string): ImageSource {
165-
return { uri: source };
166-
}
167-
168-
function resolveObjectSource(source: Object): ImageSource {
169-
return (source: ImageSource);
170-
}
171-
172-
function resolveSvgDataUriSource(
173-
source: Object,
174-
match: Array<string>
175-
): ImageSource {
176-
const [, prefix, svg] = match;
177-
// inline SVG markup may contain characters (e.g., #, ") that need to be escaped
178-
const encodedSvg = encodeURIComponent(svg);
179-
180-
return {
181-
...source,
182-
uri: `${prefix}${encodedSvg}`
183-
};
184-
}
185-
186162
// resolve any URI that might have a local blob URL created with `createObjectURL`
187163
function resolveBlobUri(source: ImageSource): string {
188164
return ImageLoader.resolveBlobUri(source.uri);

0 commit comments

Comments
 (0)