├── .editorconfig ├── .gitignore ├── .travis.yml ├── LICENSE.md ├── README.md ├── index.js ├── package.json └── test └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - 'iojs' 5 | - '0.12' 6 | - '0.10' 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 John Otander 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hsl-regex 2 | 3 | [![Build Status](https://secure.travis-ci.org/regexps/hsl-regex.png?branch=master)](https://travis-ci.org/regexps/hsl-regex) 4 | 5 | Regex for matching HSL colors. 6 | 7 | ## Installation 8 | 9 | ```bash 10 | npm install --save hsl-regex 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```javascript 16 | var hslRegex = require('hsl-regex'); 17 | 18 | hslRegex({ exact: true }).test('hsl(123, 45%, 67%)'); // => true 19 | hslRegex({ exact: true }).test('foo bar'); // => false 20 | 21 | hslRegex({ exact: true }).exec('hsl(1, 1.111%, 1.1111%)'); 22 | // => [ 23 | // 'hsl(1, 1.111%, 1.1111%)', 24 | // '1', 25 | // '1.111%', 26 | // '1.1111%', 27 | // index: 0, 28 | // input: 'hsl(1, 1.111%, 1.1111%)' 29 | // ] 30 | 31 | 'hsl(123, 45%, 67%) cats and dogs'.match(hslRegex()); 32 | // = ['hsl(123, 45%, 67%)'] 33 | ``` 34 | 35 | ## License 36 | 37 | MIT 38 | 39 | ## Contributing 40 | 41 | 1. Fork it 42 | 2. Create your feature branch (`git checkout -b my-new-feature`) 43 | 3. Commit your changes (`git commit -am 'Add some feature'`) 44 | 4. Push to the branch (`git push origin my-new-feature`) 45 | 5. Create new Pull Request 46 | 47 | Crafted with <3 by John Otander ([@4lpine](https://twitter.com/4lpine)). 48 | 49 | *** 50 | 51 | > This package was initially generated with [yeoman](http://yeoman.io) and the [p generator](https://github.com/johnotander/generator-p.git). 52 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function hslRegex(options) { 4 | options = options || {}; 5 | 6 | return options.exact ? 7 | /^hsl\(\s*(\d+)\s*,\s*(\d*(?:\.\d+)?%)\s*,\s*(\d*(?:\.\d+)?%)\)$/ : 8 | /hsl\(\s*(\d+)\s*,\s*(\d*(?:\.\d+)?%)\s*,\s*(\d*(?:\.\d+)?%)\)/ig; 9 | } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hsl-regex", 3 | "description": "Regex for matching HSL colors.", 4 | "author": "John Otander", 5 | "version": "1.0.0", 6 | "main": "index.js", 7 | "directories": { 8 | "test": "test" 9 | }, 10 | "scripts": { 11 | "test": "mocha test" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/regexps/hsl-regex.git" 16 | }, 17 | "keywords": [ 18 | "hsl", 19 | "regex", 20 | "regexp", 21 | "color", 22 | "css" 23 | ], 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/regexps/hsl-regex/issues" 27 | }, 28 | "homepage": "https://github.com/regexps/hsl-regex", 29 | "dependencies": {}, 30 | "devDependencies": { 31 | "mocha": "*" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var hslRegex = require('..'); 3 | 4 | var hslStrings = [ 5 | 'hsl(111, 12.343%, 0.9%)', 6 | 'hsl(123, 45%, 67%)', 7 | 'hsl(1, 1.111%, 1.1111%)', 8 | 'hsl(1, .111%, .1111%)' 9 | ]; 10 | 11 | var inexactHslStrings = [ 12 | 'hsl(,,,)', 13 | 'hsl(12,,)', 14 | 'hsl(1, 1.111%, 1.1111%) ', 15 | ' hSl(1, 1.111%, 1.1111%)', 16 | 'hsla(1, .111%, .1111%, .9)' 17 | ]; 18 | 19 | describe('hsl-regex', function() { 20 | 21 | describe('exact: true', function() { 22 | 23 | it('should return a regex that matches exact hsl strings', function() { 24 | hslStrings.forEach(function(hsl) { 25 | assert.ok(hslRegex({ exact: true }).test(hsl)); 26 | }); 27 | }); 28 | 29 | it('should return a regex that does not match invalid hsl strings', function() { 30 | inexactHslStrings.forEach(function(invalidHsl) { 31 | assert.ok(!hslRegex({ exact: true }).test(invalidHsl)); 32 | }); 33 | }); 34 | }); 35 | 36 | describe('g', function() { 37 | 38 | it('should match hsl strings', function() { 39 | assert.deepEqual( 40 | hslStrings.join('foobar').match(hslRegex()), 41 | hslStrings 42 | ) 43 | }); 44 | 45 | it('should not match non hsl strings', function() { 46 | assert.deepEqual( 47 | inexactHslStrings.join('foobar').match(hslRegex()), 48 | ['hsl(1, 1.111%, 1.1111%)', 'hSl(1, 1.111%, 1.1111%)'] 49 | ); 50 | }); 51 | }); 52 | }); 53 | --------------------------------------------------------------------------------