Skip to content

Commit b1a3b4f

Browse files
authored
feat(isHSL): added isHSL validator (#1159)
closes #1245 ==== * isHSL WIP * Added another valid test case. * Updated readme to include isHSL. * WIP on validating degree values beyond [0,360] * Added + sign test case * More comprehensive HSL check WIP * WIP, new tests are needed. * Added ezkemboi test cases * Added ezkemboi test cases * Added ezkemboi test cases * isHSL WIP * Added another valid test case. * Updated readme to include isHSL. * WIP on validating degree values beyond [0,360] * Added + sign test case * More comprehensive HSL check WIP * WIP, new tests are needed. * Added ezkemboi test cases * Added ezkemboi test cases * Added ezkemboi test cases * Updated description of isHSL in readme. * Fixed readme
1 parent f92c08b commit b1a3b4f

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Validator | Description
110110
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']`
111111
**isHexadecimal(str)** | check if the string is a hexadecimal number.
112112
**isHexColor(str)** | check if the string is a hexadecimal color.
113+
**isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).<br/><br/>Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`).
113114
**isRgbColor(str, [, includePercentValues])** | check if the string is a rgb or rgba color.<br/><br/>`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false.
114115
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'zh-TW', 'he-IL']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
115116
**isIn(str, values)** | check if the string is in a array of allowed values.
@@ -187,8 +188,8 @@ In general, we follow the "fork-and-pull" Git workflow.
187188
2. Clone the project to your own machine
188189
3. Work on your fork
189190
1. Make your changes and additions
190-
- Most of your changes should be focused on `src/` and `test/` folders and/or `README.md`.
191-
- Files such as `validator.js`, `validator.min.js` and files in `lib/` folder are autogenerated when running tests (`npm test`) and need not to be changed **manually**.
191+
- Most of your changes should be focused on `src/` and `test/` folders and/or `README.md`.
192+
- Files such as `validator.js`, `validator.min.js` and files in `lib/` folder are autogenerated when running tests (`npm test`) and need not to be changed **manually**.
192193
2. Change or add tests if needed
193194
3. Run tests and make sure they pass
194195
4. Add changes to README.md if needed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import isDivisibleBy from './lib/isDivisibleBy';
4141

4242
import isHexColor from './lib/isHexColor';
4343
import isRgbColor from './lib/isRgbColor';
44+
import isHSL from './lib/isHSL';
4445

4546
import isISRC from './lib/isISRC';
4647

@@ -155,6 +156,7 @@ const validator = {
155156
isDivisibleBy,
156157
isHexColor,
157158
isRgbColor,
159+
isHSL,
158160
isISRC,
159161
isMD5,
160162
isHash,

src/lib/isHSL.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import assertString from './util/assertString';
2+
3+
4+
const hslcomma = /^(hsl)a?\(\s*((\+|\-)?([0-9]+(\.[0-9]+)?(e(\+|\-)?[0-9]+)?|\.[0-9]+(e(\+|\-)?[0-9]+)?))(deg|grad|rad|turn|\s*)(\s*,\s*(\+|\-)?([0-9]+(\.[0-9]+)?(e(\+|\-)?[0-9]+)?|\.[0-9]+(e(\+|\-)?[0-9]+)?)%){2}\s*(,\s*((\+|\-)?([0-9]+(\.[0-9]+)?(e(\+|\-)?[0-9]+)?|\.[0-9]+(e(\+|\-)?[0-9]+)?)%?)\s*)?\)$/i;
5+
const hslspace = /^(hsl)a?\(\s*((\+|\-)?([0-9]+(\.[0-9]+)?(e(\+|\-)?[0-9]+)?|\.[0-9]+(e(\+|\-)?[0-9]+)?))(deg|grad|rad|turn|\s)(\s*(\+|\-)?([0-9]+(\.[0-9]+)?(e(\+|\-)?[0-9]+)?|\.[0-9]+(e(\+|\-)?[0-9]+)?)%){2}\s*(\/\s*((\+|\-)?([0-9]+(\.[0-9]+)?(e(\+|\-)?[0-9]+)?|\.[0-9]+(e(\+|\-)?[0-9]+)?)%?)\s*)?\)$/i;
6+
7+
export default function isHSL(str) {
8+
assertString(str);
9+
return hslcomma.test(str) || hslspace.test(str);
10+
}

test/validators.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,6 +3126,54 @@ describe('Validators', () => {
31263126
});
31273127
});
31283128

3129+
it('should validate HSL color strings', () => {
3130+
test({
3131+
validator: 'isHSL',
3132+
valid: [
3133+
'hsl(360,0000000000100%,000000100%)',
3134+
'hsl(000010, 00000000001%, 00000040%)',
3135+
'HSL(00000,0000000000100%,000000100%)',
3136+
'hsL(0, 0%, 0%)',
3137+
'hSl( 360 , 100% , 100% )',
3138+
'Hsl( 00150 , 000099% , 01% )',
3139+
'hsl(01080, 03%, 4%)',
3140+
'hsl(-540, 03%, 4%)',
3141+
'hsla(+540, 03%, 4%)',
3142+
'hsla(+540, 03%, 4%, 500)',
3143+
'hsl(+540deg, 03%, 4%, 500)',
3144+
'hsl(+540gRaD, 03%, 4%, 500)',
3145+
'hsl(+540.01e-98rad, 03%, 4%, 500)',
3146+
'hsl(-540.5turn, 03%, 4%, 500)',
3147+
'hsl(+540, 03%, 4%, 500e-01)',
3148+
'hsl(+540, 03%, 4%, 500e+80)',
3149+
'hsl(4.71239rad, 60%, 70%)',
3150+
'hsl(270deg, 60%, 70%)',
3151+
'hsl(200, +.1%, 62%, 1)',
3152+
'hsl(270 60% 70%)',
3153+
'hsl(200, +.1e-9%, 62e10%, 1)',
3154+
'hsl(.75turn, 60%, 70%)',
3155+
// 'hsl(200grad+.1%62%/1)', //supposed to pass, but need to handle delimiters
3156+
'hsl(200grad +.1% 62% / 1)',
3157+
'hsl(270, 60%, 50%, .15)',
3158+
'hsl(270, 60%, 50%, 15%)',
3159+
'hsl(270 60% 50% / .15)',
3160+
'hsl(270 60% 50% / 15%)',
3161+
],
3162+
invalid: [
3163+
'hsl (360,0000000000100%,000000100%)',
3164+
'hsl(0260, 100 %, 100%)',
3165+
'hsl(0160, 100%, 100%, 100 %)',
3166+
'hsl(-0160, 100%, 100a)',
3167+
'hsl(-0160, 100%, 100)',
3168+
'hsl(-0160 100%, 100%, )',
3169+
'hsl(270 deg, 60%, 70%)',
3170+
'hsl( deg, 60%, 70%)',
3171+
'hsl(, 60%, 70%)',
3172+
'hsl(3000deg, 70%)',
3173+
],
3174+
});
3175+
});
3176+
31293177
it('should validate rgb color strings', () => {
31303178
test({
31313179
validator: 'isRgbColor',

0 commit comments

Comments
 (0)