Skip to content

Commit

Permalink
chore: Simplify processAspectRatio to not use a custom RegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldonadel committed Sep 23, 2022
1 parent d349f18 commit 60895da
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`processAspectRatio should not accept invalid formats 1`] = `"aspectRatio must either be a number or a ratio. You passed: -1"`;
exports[`processAspectRatio should not accept invalid formats 1`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: 0a"`;

exports[`processAspectRatio should not accept invalid formats 2`] = `"aspectRatio must either be a number or a ratio. You passed: 0a"`;
exports[`processAspectRatio should not accept invalid formats 2`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: 1 / 1 1"`;

exports[`processAspectRatio should not accept invalid formats 3`] = `"aspectRatio must either be a number or a ratio. You passed: / 0"`;

exports[`processAspectRatio should not accept invalid formats 4`] = `"aspectRatio must either be a number or a ratio. You passed: 1 / 1 1"`;
exports[`processAspectRatio should not accept invalid formats 3`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: auto 1/1"`;
9 changes: 7 additions & 2 deletions Libraries/StyleSheet/__tests__/processAspectRatio-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ describe('processAspectRatio', () => {
expect(processAspectRatio(' 0 ')).toBe(0);
});

it('should accept `auto`', () => {
expect(processAspectRatio('auto')).toBe(undefined);
expect(processAspectRatio(' auto')).toBe(undefined);
expect(processAspectRatio(' auto ')).toBe(undefined);
});

it('should accept ratios', () => {
expect(processAspectRatio('+1/1')).toBe(1);
expect(processAspectRatio('0 / 10')).toBe(0);
Expand All @@ -37,9 +43,8 @@ describe('processAspectRatio', () => {
});

it('should not accept invalid formats', () => {
expect(() => processAspectRatio('-1')).toThrowErrorMatchingSnapshot();
expect(() => processAspectRatio('0a')).toThrowErrorMatchingSnapshot();
expect(() => processAspectRatio('/ 0')).toThrowErrorMatchingSnapshot();
expect(() => processAspectRatio('1 / 1 1')).toThrowErrorMatchingSnapshot();
expect(() => processAspectRatio('auto 1/1')).toThrowErrorMatchingSnapshot();
});
});
28 changes: 19 additions & 9 deletions Libraries/StyleSheet/processAspectRatio.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,37 @@ function processAspectRatio(aspectRatio: number | string): ?number {
return aspectRatio;
}

const match = new RegExp(
/(^\s*[+]?\d+([.]\d+)?\s*$)|(^\s*([+]?\d+([.]\d+)?)\s*\/\s*([+]?\d+([.]\d+)?)\s*$)/,
).exec(aspectRatio);
const matches = aspectRatio.split('/').map(s => s.trim());

if (matches.includes('auto')) {
if (__DEV__) {
invariant(
matches.length,
'aspectRatio does not support `auto <ratio>`. You passed: %s',
aspectRatio,
);
}
return;
}

const hasNonNumericValues = matches.some(n => Number.isNaN(Number(n)));
if (__DEV__) {
invariant(
Boolean(match),
'aspectRatio must either be a number or a ratio. You passed: %s',
!hasNonNumericValues && (matches.length === 1 || matches.length === 2),
'aspectRatio must either be a number, a ratio or `auto`. You passed: %s',
aspectRatio,
);
}

if (!match) {
if (hasNonNumericValues) {
return;
}

if (match[4] !== undefined) {
return Number(match[4]) / Number(match[6]);
if (matches.length === 2) {
return Number(matches[0]) / Number(matches[1]);
}

return Number(match[1]);
return Number(matches[0]);
}

module.exports = processAspectRatio;

0 comments on commit 60895da

Please sign in to comment.