Skip to content

Commit

Permalink
feat: accept undefined value in the pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Mhp23 committed Feb 25, 2024
1 parent 281b1bf commit e834a18
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
9 changes: 9 additions & 0 deletions __tests__/parseValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ describe('parse value tests', () => {
it('should parse responsive height correctly', () => {
expect(parseValue('10rh')).toEqual(rh(10));
});
it('should parse responsive scale with undefined in the pattern correctly', () => {
expect(parseValue(`${undefined}rs`)).toBe(undefined);
});
it('should parse responsive width with undefined in the pattern correctly', () => {
expect(parseValue(`${undefined}rw`)).toBe(undefined);
});
it('should parse responsive height with undefined in the pattern correctly', () => {
expect(parseValue(`${undefined}rh`)).toBe(undefined);
});
it('should parse values are float or have sign correctly', () => {
expect(parseValue('+10rs')).toEqual(rs(10));
expect(parseValue('-10rs')).toEqual(rs(-10));
Expand Down
3 changes: 3 additions & 0 deletions src/layout/createRStyle/parseValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export const parseValue = (
if (!executed) {
return value;
}
if (executed[1] === 'undefined') {
return undefined;
}
const styleValue = parseFloat(executed[1] as string);

const suffix = executed[4];
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type NamedStyles<T> = {
| WithPattern<ImageStyle>;
};
export type Pattern = 'rs' | 'rw' | 'rh';
export type ResponsivePattern = `${number}${Pattern}`;
export type ResponsivePattern = `${number | undefined}${Pattern}`;
export type ValuePattern = string | number | ResponsivePattern;
export type MethodType = 'linear' | 'recursive';
export type CreateStyleConfig = {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const PATTERN_REGEX = /^([+-]?\d+(\.\d+)?|(\.\d+))(rs|rw|rh)$/;
export const PATTERN_REGEX = /^(undefined|[+-]?\d+(\.\d+)?|(\.\d+))(rs|rw|rh)$/;

0 comments on commit e834a18

Please sign in to comment.