-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π Enable contain strict when width and height is provided (#3273)
* π Enable contain strict when width and height is provided Enables 100% width without requiring any custom code * π Add px suffix when passing only numbers as size
- Loading branch information
Showing
4 changed files
with
79 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { addPxSuffixIfInputHasNoPrefix, isNumberOnlyString } from '../utils' | ||
|
||
test('Should validate number string correctly', () => { | ||
expect(isNumberOnlyString('1')).toBe(true) | ||
|
||
expect(isNumberOnlyString('1s')).toBe(false) | ||
|
||
expect(isNumberOnlyString('sa')).toBe(false) | ||
|
||
expect(isNumberOnlyString('1.1')).toBe(true) | ||
|
||
expect(isNumberOnlyString('1,1')).toBe(false) | ||
|
||
expect(isNumberOnlyString('11 ')).toBe(true) | ||
}) | ||
|
||
test('Should add px suffix in correct cases', () => { | ||
expect(addPxSuffixIfInputHasNoPrefix('1')).toEqual('1px') | ||
expect(addPxSuffixIfInputHasNoPrefix('1px')).toEqual('1px') | ||
expect(addPxSuffixIfInputHasNoPrefix('1.1')).toEqual('1.1px') | ||
expect(addPxSuffixIfInputHasNoPrefix('100%')).toEqual('100%') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Function returning wether a string only contains number. Allows leading or trailing spaces. | ||
* | ||
* Examples: | ||
* | ||
* ``` | ||
* isNumberOnlyString("10") // true | ||
* isNumberOnlyString("10.10") // true | ||
* isNumberOnlyString("10px") // false | ||
* isNumberOnlyString("10%") // false | ||
* isNumberOnlyString("10 ") // true | ||
* ``` | ||
* | ||
* @param number | ||
* @returns | ||
*/ | ||
export function isNumberOnlyString(number: string) { | ||
return !isNaN(Number(number)) && !isNaN(parseFloat(number)) | ||
} | ||
|
||
export function addPxSuffixIfInputHasNoPrefix(size: number | string) { | ||
if (typeof size === 'number' || isNumberOnlyString(size)) { | ||
return `${size}px` | ||
} | ||
|
||
return size | ||
} |