https://github.com/jonschlinkert/is-valid-year
Returns true if a year is valid based on a given year, range of years, sequences of years, or any mixture of these.
https://github.com/jonschlinkert/is-valid-year
date dates range validate year
Last synced: 23 days ago
JSON representation
Returns true if a year is valid based on a given year, range of years, sequences of years, or any mixture of these.
- Host: GitHub
- URL: https://github.com/jonschlinkert/is-valid-year
- Owner: jonschlinkert
- License: mit
- Created: 2015-08-07T14:22:35.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-08-07T14:37:20.000Z (over 9 years ago)
- Last Synced: 2025-03-18T17:44:42.530Z (about 1 month ago)
- Topics: date, dates, range, validate, year
- Language: JavaScript
- Size: 117 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# is-valid-year [](http://badge.fury.io/js/is-valid-year)
> Returns true if a year is valid based on a given year, range of years, sequences of years, or any mixture of these. Useful for validating copyrights, dates that are parsed from physical documents, etc.
## Install
Install with [npm](https://www.npmjs.com/)
```sh
$ npm i is-valid-year --save
```## Usage
Supports years as numbers or strings:
```js
var isValidYear = require('is-valid-year');isValidYear(2011, '2010-2015');
//=> true
isValidYear(2011, '1999-2001');
//=> false
```**Acceptable formats**
Pass an array or list or years or year ranges (years must increase from left to right).
* The first argument is the year to validate
* Everything else is a valid year to test against
* Year ranges must always be a string separated by `-`, as in `2001-2005`
* Non-ranges (sequential or otherwise) may be separated by `,` or `|` when passed as a string, or as must be passed as individual array elements.All of the following should work:
```js
isValidYear(2011, 2015);
//=> false
isValidYear(2015, 2015);
//=> trueisValidYear(2015, [1999, 2000, 2001]);
//=> false
isValidYear(2001, [1999, 2000, 2001]);
//=> trueisValidYear(2009, '2010-2015');
//=> false
isValidYear(2010, '2010-2015');
//=> trueisValidYear(2008, '2009,2010-2015');
//=> false
isValidYear(2009, '2009,2010-2015');
//=> trueisValidYear(2011, [1997, '1999-2009', 2010]);
//=> false
isValidYear(2010, [1997, '1999-2009', 2010]);
//=> true
```## Related projects
* [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)
* [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)
* [repeat-element](https://github.com/jonschlinkert/repeat-element): Create an array by repeating the given value n times.
* [repeat-string](https://github.com/jonschlinkert/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string.
* [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)## Running tests
Install dev dependencies:
```sh
$ npm i -d && npm test
```## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/is-valid-year/issues/new)
## Author
**Jon Schlinkert**
+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert)## License
Copyright © 2015 Jon Schlinkert
Released under the MIT license.***
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on August 07, 2015._