Skip to content

Commit f79142e

Browse files
committed
first commit
0 parents  commit f79142e

11 files changed

+352
-0
lines changed

.editorconfig

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = space
6+
end_of_line = lf
7+
charset = utf-8
8+
indent_size = 2
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.md]
13+
trim_trailing_whitespace = false
14+
insert_final_newline = false
15+
16+
[{,test/}{actual,fixtures}/**]
17+
trim_trailing_whitespace = false
18+
insert_final_newline = false
19+
20+
[templates/**]
21+
trim_trailing_whitespace = false
22+
insert_final_newline = false

.gitattributes

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Enforce Unix newlines
2+
* text eol=lf
3+
4+
# binaries
5+
*.ai binary
6+
*.psd binary
7+
*.jpg binary
8+
*.gif binary
9+
*.png binary
10+
*.jpeg binary

.gitignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.DS_Store
2+
*.sublime-*
3+
_gh_pages
4+
bower_components
5+
node_modules
6+
npm-debug.log
7+
actual
8+
test/actual
9+
temp
10+
tmp
11+
TODO.md
12+
vendor
13+
.idea
14+
benchmark
15+
coverage

.jshintrc

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"asi": false,
3+
"boss": true,
4+
"curly": true,
5+
"eqeqeq": true,
6+
"eqnull": true,
7+
"esnext": true,
8+
"immed": true,
9+
"latedef": false,
10+
"laxcomma": false,
11+
"mocha": true,
12+
"newcap": true,
13+
"noarg": true,
14+
"node": true,
15+
"sub": true,
16+
"undef": true,
17+
"unused": true
18+
}

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- "0.10"
5+
- "0.12"
6+
- "0.13"
7+
- "iojs"
8+
matrix:
9+
fast_finish: true
10+
allow_failures:
11+
- node_js: "0.13"

.verb.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# {%= name %} {%= badge("fury") %}
2+
3+
> {%= description %}
4+
5+
If you've ever tried to create a JavaScript regular expression for validating a range of years, you'll know it's not easy. This solves that problem.
6+
7+
## Install
8+
{%= include("install-npm", {save: true}) %}
9+
10+
## Usage
11+
12+
```js
13+
var yearRange = require('{%= name %}');
14+
15+
yearRegex(1992, 2015);
16+
//=> /^(199[2-9]|200[0-9]|201[0-5])$/
17+
18+
yearRange(0, 1999)
19+
//=> /^([0-9]|[1-9][0-9]|[1-9][0-9]{2}|1[0-9]{3})$/
20+
21+
yearRange(2016)
22+
//=> /^(2016)$/
23+
```
24+
25+
## Related projects
26+
{%= related(verb.related.list) %}
27+
28+
## Running tests
29+
{%= include("tests") %}
30+
31+
## Contributing
32+
{%= include("contributing") %}
33+
34+
## Author
35+
{%= include("author") %}
36+
37+
## License
38+
{%= copyright() %}
39+
{%= license() %}
40+
41+
***
42+
43+
{%= include("footer") %}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015, Jon Schlinkert.
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.

README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# year-range-regex [![NPM version](https://badge.fury.io/js/year-range-regex.svg)](http://badge.fury.io/js/year-range-regex)
2+
3+
> Generates a regular expression for validating a range of years.
4+
5+
If you've ever tried to create a JavaScript regular expression for validating a range of years, you'll know it's not easy. This solves that problem.
6+
7+
## Install
8+
9+
Install with [npm](https://www.npmjs.com/)
10+
11+
```sh
12+
$ npm i year-range-regex --save
13+
```
14+
15+
## Usage
16+
17+
```js
18+
var yearRange = require('year-range-regex');
19+
20+
yearRegex(1992, 2015);
21+
//=> /^(199[2-9]|200[0-9]|201[0-5])$/
22+
23+
yearRange(0, 1999)
24+
//=> /^([0-9]|[1-9][0-9]|[1-9][0-9]{2}|1[0-9]{3})$/
25+
26+
yearRange(2016)
27+
//=> /^(2016)$/
28+
```
29+
30+
## Related projects
31+
32+
* [expand-range](https://github.com/jonschlinkert/expand-range): Fast, bash-like range expansion. Expand a range of numbers or letters, uppercase or lowercase. See… [more](https://github.com/jonschlinkert/expand-range)
33+
* [fill-range](https://github.com/jonschlinkert/fill-range): Fill in a range of numbers or letters, optionally passing an increment or multiplier to… [more](https://github.com/jonschlinkert/fill-range)
34+
* [repeat-element](https://github.com/jonschlinkert/repeat-element): Create an array by repeating the given value n times.
35+
* [repeat-string](https://github.com/jonschlinkert/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string.
36+
* [to-regex-range](https://github.com/jonschlinkert/to-regex-range): Returns a regex-compatible range from two numbers, min and max. Useful for creating regular expressions… [more](https://github.com/jonschlinkert/to-regex-range)
37+
38+
## Running tests
39+
40+
Install dev dependencies:
41+
42+
```sh
43+
$ npm i -d && npm test
44+
```
45+
46+
## Contributing
47+
48+
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/regexps/year-range-regex/issues/new)
49+
50+
## Author
51+
52+
**Jon Schlinkert**
53+
54+
+ [github/jonschlinkert](https://github.com/jonschlinkert)
55+
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
56+
57+
## License
58+
59+
Copyright © 2015 Jon Schlinkert
60+
Released under the MIT license.
61+
62+
***
63+
64+
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 07, 2015._

index.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*!
2+
* year-range-regex <https://github.com/regexps/year-range-regex>
3+
*
4+
* Copyright (c) 2015, Jon Schlinkert.
5+
* Licensed under the MIT License.
6+
*/
7+
8+
'use strict';
9+
10+
var isNumber = require('is-number');
11+
var toRegexRange = require('to-regex-range');
12+
var cache = {};
13+
14+
module.exports = function (start, end) {
15+
if (!isNumber(start)) {
16+
throw new RangeError('invalid year range.');
17+
}
18+
19+
var key = start + '-' + end;
20+
if (cache.hasOwnProperty(key)) {
21+
return cache[key];
22+
}
23+
24+
if (!isNumber(end)) {
25+
end = start;
26+
}
27+
28+
var re = new RegExp('^(' + toRegexRange(start, end) + ')$');
29+
return cache[key] || (cache[key] = re);
30+
};
31+

package.json

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "year-range-regex",
3+
"description": "Generates a regular expression for validating a range of years.",
4+
"version": "0.1.0",
5+
"homepage": "https://github.com/regexps/year-range-regex",
6+
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
7+
"repository": "regexps/year-range-regex",
8+
"bugs": {
9+
"url": "https://github.com/regexps/year-range-regex/issues"
10+
},
11+
"license": "MIT",
12+
"files": [
13+
"index.js"
14+
],
15+
"main": "index.js",
16+
"engines": {
17+
"node": ">=0.10.0"
18+
},
19+
"scripts": {
20+
"test": "mocha"
21+
},
22+
"dependencies": {
23+
"is-number": "^2.0.2",
24+
"to-regex-range": "^0.1.1"
25+
},
26+
"devDependencies": {
27+
"mocha": "*"
28+
},
29+
"keywords": [
30+
"character",
31+
"class",
32+
"date",
33+
"expand",
34+
"expansion",
35+
"expression",
36+
"is",
37+
"match",
38+
"matches",
39+
"matching",
40+
"number",
41+
"numerical",
42+
"range",
43+
"ranges",
44+
"regex",
45+
"regexp",
46+
"regular",
47+
"regular-expression",
48+
"test",
49+
"valid",
50+
"validate",
51+
"validating",
52+
"verify",
53+
"year",
54+
"years"
55+
],
56+
"verb": {
57+
"related": {
58+
"list": [
59+
"to-regex-range",
60+
"fill-range",
61+
"expand-range",
62+
"repeat-element",
63+
"repeat-string"
64+
]
65+
}
66+
}
67+
}

test.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
/* deps: mocha */
4+
var assert = require('assert');
5+
var yearRange = require('./');
6+
console.log(yearRange(1992))
7+
8+
describe('yearRange', function () {
9+
it('should create a regex for a range of same-length years:', function () {
10+
assert.deepEqual(/^(199[2-9]|200[0-9]|201[0-5])$/, yearRange(1992, 2015));
11+
var re = yearRange(1992, 2015);
12+
13+
assert.equal(re.test('1992'), true);
14+
assert.equal(re.test('1993'), true);
15+
assert.equal(re.test('1999'), true);
16+
assert.equal(re.test('2001'), true);
17+
assert.equal(re.test('2008'), true);
18+
assert.equal(re.test('2011'), true);
19+
assert.equal(re.test('2015'), true);
20+
21+
assert.equal(re.test('12015'), false);
22+
assert.equal(re.test('1991'), false);
23+
assert.equal(re.test('2016'), false);
24+
assert.equal(re.test('2015.1'), false);
25+
assert.equal(re.test('20151'), false);
26+
});
27+
28+
it('should create a regex for a range of different-length years:', function () {
29+
assert.deepEqual(/^([0-9]|[1-9][0-9]|[1-9][0-9]{2}|1[0-9]{3})$/, yearRange(0, 1999));
30+
var re = yearRange(0, 1999);
31+
32+
assert.equal(re.test('0'), true);
33+
assert.equal(re.test('1'), true);
34+
assert.equal(re.test('200'), true);
35+
assert.equal(re.test('201'), true);
36+
assert.equal(re.test('1991'), true);
37+
assert.equal(re.test('1992'), true);
38+
assert.equal(re.test('1993'), true);
39+
assert.equal(re.test('1999'), true);
40+
41+
assert.equal(re.test('2001'), false);
42+
assert.equal(re.test('2008'), false);
43+
assert.equal(re.test('2011'), false);
44+
assert.equal(re.test('2015'), false);
45+
assert.equal(re.test('12015'), false);
46+
assert.equal(re.test('2016'), false);
47+
assert.equal(re.test('2015.1'), false);
48+
assert.equal(re.test('20151'), false);
49+
});
50+
});

0 commit comments

Comments
 (0)