Skip to content

Commit 60895da

Browse files
chore: Simplify processAspectRatio to not use a custom RegExp
1 parent d349f18 commit 60895da

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

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

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

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

Libraries/StyleSheet/__tests__/processAspectRatio-test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ describe('processAspectRatio', () => {
2828
expect(processAspectRatio(' 0 ')).toBe(0);
2929
});
3030

31+
it('should accept `auto`', () => {
32+
expect(processAspectRatio('auto')).toBe(undefined);
33+
expect(processAspectRatio(' auto')).toBe(undefined);
34+
expect(processAspectRatio(' auto ')).toBe(undefined);
35+
});
36+
3137
it('should accept ratios', () => {
3238
expect(processAspectRatio('+1/1')).toBe(1);
3339
expect(processAspectRatio('0 / 10')).toBe(0);
@@ -37,9 +43,8 @@ describe('processAspectRatio', () => {
3743
});
3844

3945
it('should not accept invalid formats', () => {
40-
expect(() => processAspectRatio('-1')).toThrowErrorMatchingSnapshot();
4146
expect(() => processAspectRatio('0a')).toThrowErrorMatchingSnapshot();
42-
expect(() => processAspectRatio('/ 0')).toThrowErrorMatchingSnapshot();
4347
expect(() => processAspectRatio('1 / 1 1')).toThrowErrorMatchingSnapshot();
48+
expect(() => processAspectRatio('auto 1/1')).toThrowErrorMatchingSnapshot();
4449
});
4550
});

Libraries/StyleSheet/processAspectRatio.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,37 @@ function processAspectRatio(aspectRatio: number | string): ?number {
1717
return aspectRatio;
1818
}
1919

20-
const match = new RegExp(
21-
/(^\s*[+]?\d+([.]\d+)?\s*$)|(^\s*([+]?\d+([.]\d+)?)\s*\/\s*([+]?\d+([.]\d+)?)\s*$)/,
22-
).exec(aspectRatio);
20+
const matches = aspectRatio.split('/').map(s => s.trim());
21+
22+
if (matches.includes('auto')) {
23+
if (__DEV__) {
24+
invariant(
25+
matches.length,
26+
'aspectRatio does not support `auto <ratio>`. You passed: %s',
27+
aspectRatio,
28+
);
29+
}
30+
return;
31+
}
2332

33+
const hasNonNumericValues = matches.some(n => Number.isNaN(Number(n)));
2434
if (__DEV__) {
2535
invariant(
26-
Boolean(match),
27-
'aspectRatio must either be a number or a ratio. You passed: %s',
36+
!hasNonNumericValues && (matches.length === 1 || matches.length === 2),
37+
'aspectRatio must either be a number, a ratio or `auto`. You passed: %s',
2838
aspectRatio,
2939
);
3040
}
3141

32-
if (!match) {
42+
if (hasNonNumericValues) {
3343
return;
3444
}
3545

36-
if (match[4] !== undefined) {
37-
return Number(match[4]) / Number(match[6]);
46+
if (matches.length === 2) {
47+
return Number(matches[0]) / Number(matches[1]);
3848
}
3949

40-
return Number(match[1]);
50+
return Number(matches[0]);
4151
}
4252

4353
module.exports = processAspectRatio;

0 commit comments

Comments
 (0)