Skip to content

Commit 05a76d4

Browse files
committed
Refactor as factory function
1 parent 3a5d669 commit 05a76d4

11 files changed

Lines changed: 450 additions & 53 deletions

File tree

lib/node_modules/@stdlib/regexp/color-hexadecimal/README.md

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,77 @@ limitations under the License.
2727
## Usage
2828

2929
```javascript
30-
var RE_COLOR_HEXADECIMAL = require( '@stdlib/regexp/color-hexadecimal' );
30+
var reColorHexadecimal = require( '@stdlib/regexp/color-hexadecimal' );
3131
```
3232

33-
#### RE_COLOR_HEXADECIMAL
33+
#### reColorHexadecimal( \[mode] )
34+
35+
Returns a [regular expression][mdn-regexp] to match a full hexadecimal color.
36+
37+
```javascript
38+
var RE = reColorHexadecimal();
39+
// returns <RegExp>
40+
41+
var bool = RE.test( 'ffffff' );
42+
// returns true
43+
44+
bool = RE.test( '000' );
45+
// returns false
46+
```
47+
48+
To return a [regular expression][mdn-regexp] that matches a shorthand hexadecimal color, set the `mode` argument to `shorthand`.
49+
50+
```javascript
51+
var RE = reColorHexadecimal( 'shorthand' );
52+
// returns <RegExp>
53+
54+
var bool = RE.test( '000' );
55+
// returns true
56+
```
57+
58+
To return a [regular expression][mdn-regexp] that matches **either** a shorthand or a full length hexadecimal color, set the `mode` argument to `either`.
59+
60+
```javascript
61+
var RE = reColorHexadecimal( 'either' );
62+
// returns <RegExp>
63+
64+
var bool = RE.test( '000' );
65+
// returns true
66+
```
67+
68+
#### reColorHexadecimal.REGEXP
3469

3570
[Regular expression][mdn-regexp] to match a full length hexadecimal color.
3671

3772
```javascript
38-
var bool = RE_COLOR_HEXADECIMAL.test( 'ffffff' );
73+
var bool = reColorHexadecimal.REGEXP.test( 'ffffff' );
3974
// returns true
4075

41-
bool = RE_COLOR_HEXADECIMAL.test( '000' );
76+
bool = reColorHexadecimal.REGEXP.test( '000' );
4277
// returns false
4378
```
4479

45-
#### RE_COLOR_HEXADECIMAL.shorthand
80+
#### reColorHexadecimal.REGEXP_SHORTHAND
4681

4782
[Regular expression][mdn-regexp] to match a shorthand hexadecimal color.
4883

4984
```javascript
50-
var bool = RE_COLOR_HEXADECIMAL.test( 'ffffff' );
85+
var bool = reColorHexadecimal.REGEXP_SHORTHAND.test( 'ffffff' );
5186
// returns false
5287

53-
bool = RE_COLOR_HEXADECIMAL.test( '000' );
88+
bool = reColorHexadecimal.REGEXP_SHORTHAND.test( '000' );
5489
// returns true
5590
```
5691

57-
#### RE_COLOR_HEXADECIMAL.either
92+
#### reColorHexadecimal.REGEXP_EITHER
5893

5994
[Regular expression][mdn-regexp] to match **either** a shorthand or a full length hexadecimal color.
6095

6196
```javascript
62-
var bool = RE_COLOR_HEXADECIMAL.test( 'ffffff' );
97+
var bool = reColorHexadecimal.REGEXP_EITHER.test( 'ffffff' );
6398
// returns true
6499

65-
bool = RE_COLOR_HEXADECIMAL.test( '000' );
100+
bool = reColorHexadecimal.REGEXP_EITHER.test( '000' );
66101
// returns true
67102
```
68103

@@ -78,19 +113,19 @@ bool = RE_COLOR_HEXADECIMAL.test( '000' );
78113

79114
```javascript
80115
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
81-
var RE_COLOR_HEXADECIMAL = require( '@stdlib/regexp/color-hexadecimal' );
116+
var reColorHexadecimal = require( '@stdlib/regexp/color-hexadecimal' );
82117

83118
function isHexColor( value, mode ) {
84119
if ( !isString( value ) ) {
85120
return false;
86121
}
87122
if ( mode === 'shorthand' ) {
88-
return RE_COLOR_HEXADECIMAL.shorthand.test( value );
123+
return reColorHexadecimal.REGEXP_SHORTHAND.test( value );
89124
}
90125
if ( mode === 'either' ) {
91-
return RE_COLOR_HEXADECIMAL.either.test( value );
126+
return reColorHexadecimal.REGEXP_EITHER.test( value );
92127
}
93-
return RE_COLOR_HEXADECIMAL.test( value );
128+
return reColorHexadecimal.REGEXP.test( value );
94129
}
95130

96131
var bool = isHexColor( 'ffffff', 'full' );

lib/node_modules/@stdlib/regexp/color-hexadecimal/benchmark/benchmark.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var bench = require( '@stdlib/bench' );
2424
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
2525
var fromCodePoint = require( '@stdlib/string/from-code-point' );
2626
var pkg = require( './../package.json' ).name;
27-
var RE_COLOR_HEXADECIMAL = require( './../lib' );
27+
var reColorHexadecimal = require( './../lib' );
2828

2929

3030
// MAIN //
@@ -37,7 +37,7 @@ bench( pkg, function benchmark( b ) {
3737
b.tic();
3838
for ( i = 0; i < b.iterations; i++ ) {
3939
str = 'ababa'+fromCodePoint( 97 + (i%26) );
40-
bool = RE_COLOR_HEXADECIMAL.test( str );
40+
bool = reColorHexadecimal.REGEXP.test( str );
4141
if ( !isBoolean( bool ) ) {
4242
b.fail( 'should return a boolean' );
4343
}
@@ -58,7 +58,7 @@ bench( pkg+'shorthand', function benchmark( b ) {
5858
b.tic();
5959
for ( i = 0; i < b.iterations; i++ ) {
6060
str = 'ab'+fromCodePoint( 97 + (i%26) );
61-
bool = RE_COLOR_HEXADECIMAL.shorthand.test( str );
61+
bool = reColorHexadecimal.REGEXP_SHORTHAND.test( str );
6262
if ( !isBoolean( bool ) ) {
6363
b.fail( 'should return a boolean' );
6464
}
@@ -79,7 +79,7 @@ bench( pkg+':either', function benchmark( b ) {
7979
b.tic();
8080
for ( i = 0; i < b.iterations; i++ ) {
8181
str = 'ababa'+fromCodePoint( 97 + (i%26) );
82-
bool = RE_COLOR_HEXADECIMAL.either.test( str );
82+
bool = reColorHexadecimal.REGEXP_EITHER.test( str );
8383
if ( !isBoolean( bool ) ) {
8484
b.fail( 'should return a boolean' );
8585
}

lib/node_modules/@stdlib/regexp/color-hexadecimal/docs/repl.txt

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,65 @@
11

2-
{{alias}}
3-
Regular expression to match a hexadecimal color.
2+
{{alias}}( [mode] )
3+
Returns a regular expression to match a hexadecimal color.
4+
5+
Parameters
6+
----------
7+
mode: string (optional)
8+
Color format (`full`, `shorthand`, or `either`).
9+
10+
Returns
11+
-------
12+
re: RegExp
13+
Regular expression.
14+
15+
Examples
16+
--------
17+
> var RE = {{alias}}();
18+
> var bool = RE.test( 'ffffff' )
19+
true
20+
> bool = RE.test( '000' )
21+
false
22+
> bool = RE.test( 'beep' )
23+
false
24+
25+
26+
{{alias}}.REGEXP
27+
Regular expression to match a full hexadecimal color.
428

529
Examples
630
--------
7-
> var bool = {{alias}}.test( 'ffffff' )
31+
> var bool = {{alias}}.REGEXP.test( 'ffffff' )
832
true
9-
> bool = {{alias}}.test( '000' )
33+
> bool = {{alias}}.REGEXP.test( '000' )
1034
false
11-
> bool = {{alias}}.test( 'beep' )
35+
> bool = {{alias}}.REGEXP.test( 'beep' )
1236
false
1337

1438

15-
{{alias}}.shorthand
39+
{{alias}}.REGEXP_SHORTHAND
1640
Regular expression to match a shorthand hexadecimal color.
1741

1842
Examples
1943
--------
20-
> var bool = {{alias}}.shorthand.test( 'ffffff' )
44+
> var bool = {{alias}}.REGEXP_SHORTHAND.test( 'ffffff' )
2145
false
22-
> bool = {{alias}}.shorthand.test( '000' )
46+
> bool = {{alias}}.REGEXP_SHORTHAND.test( '000' )
2347
true
24-
> bool = {{alias}}.shorthand.test( 'beep' )
48+
> bool = {{alias}}.REGEXP_SHORTHAND.test( 'beep' )
2549
false
2650

2751

28-
{{alias}}.either
52+
{{alias}}.REGEXP_EITHER
2953
Regular expression to match either a shorthand or full length hexadecimal
3054
color.
3155

3256
Examples
3357
--------
34-
> var bool = {{alias}}.either.test( 'ffffff' )
58+
> var bool = {{alias}}.REGEXP_EITHER.test( 'ffffff' )
3559
true
36-
> bool = {{alias}}.either.test( '000' )
60+
> bool = {{alias}}.REGEXP_EITHER.test( '000' )
3761
true
38-
> bool = {{alias}}.either.test( 'beep' )
62+
> bool = {{alias}}.REGEXP_EITHER.test( 'beep' )
3963
false
4064

4165
See Also
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
type MODES = 'full' | 'shorthand' | 'either';
22+
23+
24+
/**
25+
* Interface for a regular expression to match a hexadecimal color.
26+
*/
27+
interface ReColorHexadecimal {
28+
/**
29+
* Returns a regular expression to match a hexadecimal color.
30+
*
31+
* @param mode - color format (`full`, `shorthand`, or `either`)
32+
* @returns regular expression
33+
*
34+
* @example
35+
* var RE = reColorHexadecimal();
36+
* // returns <RegExp>
37+
*
38+
* var bool = RE.test( 'ffffff' );
39+
* // returns true
40+
*
41+
* bool = RE.test( '000' );
42+
* // returns false
43+
*
44+
* @example
45+
* var RE = reColorHexadecimal( 'shorthand' );
46+
* // returns <RegExp>
47+
*
48+
* var bool = RE.test( '000' );
49+
* // returns true
50+
*
51+
* @example
52+
* var RE = reColorHexadecimal( 'either' );
53+
* // returns <RegExp>
54+
*
55+
* var bool = RE.test( '000' );
56+
* // returns true
57+
*/
58+
( mode?: MODES ): RegExp;
59+
60+
/**
61+
* Regular expression to match a full hexadecimal color.
62+
*
63+
* @example
64+
* var bool = reColorHexadecimal.REGEXP.test( 'ffffff' );
65+
* // returns true
66+
*/
67+
REGEXP: RegExp;
68+
69+
/**
70+
* Regular expression to match a shorthand hexadecimal color.
71+
*
72+
* @example
73+
* var bool = reColorHexadecimal.REGEXP_SHORTHAND.test( 'ffffff' );
74+
* // returns false
75+
*/
76+
REGEXP_SHORTHAND: RegExp;
77+
78+
/**
79+
* Regular expression to match **either** a shorthand or a full length hexadecimal color.
80+
*
81+
* @example
82+
* var bool = reColorHexadecimal.REGEXP_EITHER.test( 'ffffff' );
83+
* // returns true
84+
*/
85+
REGEXP_EITHER: RegExp;
86+
}
87+
88+
/**
89+
* Returns a regular expression to match a hexadecimal color.
90+
*
91+
* @param mode - color format (`full`, `shorthand`, or `either`)
92+
* @returns regular expression
93+
*
94+
* @example
95+
* var RE = reColorHexadecimal();
96+
* // returns <RegExp>
97+
*
98+
* var bool = RE.test( 'ffffff' );
99+
* // returns true
100+
*
101+
* bool = RE.test( '000' );
102+
* // returns false
103+
*
104+
* @example
105+
* var bool = reColorHexadecimal.REGEXP.test( 'ffffff' );
106+
* // returns true
107+
*/
108+
declare var reColorHexadecimal: ReColorHexadecimal;
109+
110+
111+
// EXPORTS //
112+
113+
export = reColorHexadecimal;

0 commit comments

Comments
 (0)