regex-color-hexadecimal
v1.0.0
Published
Regular expression to test for a hexadecimal color.
Downloads
28
Maintainers
Readme
Hexadecimal Color
Regular expression to test for a hexadecimal color.
Installation
$ npm install regex-color-hexadecimal
Usage
var re = require( 'regex-color-hexadecimal' );
re
Regular expression to test for a hexadecimal color.
var bool = re.test( 'ffffff' );
// returns true
bool = re.test( '000' );
// returns false
re.shorthand
Regular expression to test for a shorthand hexadecimal color.
var bool = re.test( '000' );
// returns true
re.either
Regular expression to test for either a shorthand or full length hexadecimal color.
var bool = re.test( '474747' );
// returns true
bool = re.test( '000' );
// returns true
Examples
var re = require( 'regex-color-hexadecimal' );
function isHex( value, len ) {
if ( len === 3 ) {
return re.shorthand.test( value );
}
if ( len === 6 ) {
return re.test( value );
}
return re.either.test( value );
}
console.log( isHex( 'ffffff' ) );
// returns true
console.log( isHex( '474747' ) );
// returns true
console.log( isHex( '0A5BBE', 6 ) );
// returns true
console.log( isHex( '000' ) );
// returns true
console.log( isHex( 'F7b', 3 ) );
// returns true
console.log( isHex( 'abcd' ) );
// returns false
console.log( isHex( '' ) );
// returns false
console.log( isHex( null ) );
// returns false
To run the example code from the top-level application directory,
$ node ./examples/index.js
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
Copyright
Copyright © 2015. Athan Reines.