-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(isRgbColor): add allowSpaces
option to allow/disallow spaces between color values
#2029
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
56db78f
fix: isRgbColor now strips spaces
a-h-i b3beff3
isRgbColor no longer ignores spacing between rgba? at start
a-h-i bae863f
updates isRGB test cases
a-h-i cbe2bc6
Fixes typo
a-h-i bc534c1
adds strict mode to preserve old behaviour
a-h-i 4eeb798
updates README.md with isRgbColor strict param
a-h-i 414f743
implements optional options param for isRgbColor
a-h-i 0cc22f8
updates readme
a-h-i 6a40c9e
Update README.md
a-h-i 9343713
Update README.md
a-h-i 4673f58
flips order of object arguments check
a-h-i 5325127
adds more isRgbColor test examples
a-h-i 1b982fe
removes strict support for arguments array
a-h-i dd2b080
sets options default in function signature
a-h-i e10d3a1
options object defaults now in signature
a-h-i 15f80b8
Revert "options object defaults now in signature"
a-h-i 718872e
Revert "sets options default in function signature"
a-h-i b27bff7
Revert "removes strict support for arguments array"
a-h-i 7e5225c
changes strict to be options object only param
a-h-i dbd5260
Update src/lib/isRgbColor.js
a-h-i bfca688
changes strict to allowSpaces
a-h-i File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,12 +1,35 @@ | ||||||
/* eslint-disable prefer-rest-params */ | ||||||
import assertString from './util/assertString'; | ||||||
|
||||||
const rgbColor = /^rgb\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\)$/; | ||||||
const rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)$/; | ||||||
const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)$/; | ||||||
const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)$/; | ||||||
const startsWithRgb = /^rgba?/; | ||||||
|
||||||
export default function isRgbColor(str, includePercentValues = true) { | ||||||
export default function isRgbColor(str, options) { | ||||||
a-h-i marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
assertString(str); | ||||||
// default options to true for percent and false for spaces | ||||||
let allowSpaces = false; | ||||||
let includePercentValues = true; | ||||||
if (typeof options !== 'object') { | ||||||
if (arguments.length >= 2) { | ||||||
includePercentValues = arguments[1]; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} else { | ||||||
allowSpaces = options.allowSpaces !== undefined ? options.allowSpaces : allowSpaces; | ||||||
includePercentValues = options.includePercentValues !== undefined ? | ||||||
options.includePercentValues : includePercentValues; | ||||||
} | ||||||
|
||||||
if (allowSpaces) { | ||||||
// make sure it starts with continous rgba? without spaces before stripping | ||||||
if (!startsWithRgb.test(str)) { | ||||||
return false; | ||||||
} | ||||||
// strip all whitespace | ||||||
str = str.replace(/\s/g, ''); | ||||||
} | ||||||
|
||||||
if (!includePercentValues) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return rgbColor.test(str) || rgbaColor.test(str); | ||||||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you add this? Isn't it already checked in the first section of the other regexes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. It's because the newly added test is invalid. Why is that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so that we can check that it does not haves spaces between r g b and a only for the rest of the string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figured
'r g b( 0, 251, 222 )'
failing validation but'rgb( 0, 251, 222 )'
passing is more inline with what an rgb color string is