@applicaster/zapp-react-native-ui-components
Version:
Applicaster Zapp React Native ui components for the Quick Brick App
55 lines (44 loc) • 1.08 kB
text/typescript
import { Image as RNImage } from "react-native";
import { isNil } from "ramda";
type Size = {
width: number;
height: number;
};
export const UNKNOWN_ASPECT_RATIO = undefined;
export const UNKNOWN_DIMENSION = undefined;
export const getImageSize = (
uri: string,
width: number,
height: Nullable<number>
): Promise<Size> => {
if (!isNil(width) && !isNil(height)) {
const size: Size = { width, height };
return Promise.resolve(size);
}
return new Promise((resolve, reject) => {
RNImage.getSize(
uri,
(width: number, height: number) => {
const size: Size = { width, height };
resolve(size);
},
reject
);
});
};
export const getAspectRatio = ({ width, height }) => {
if (!isNil(width) && !isNil(height)) {
return width / height;
}
return UNKNOWN_ASPECT_RATIO;
};
export const getDimension = ({ width, height }) => {
if (!isNil(width) && !isNil(height)) {
return {
aspectRatio: getAspectRatio({ width, height }),
width,
height,
};
}
return UNKNOWN_DIMENSION;
};