iso-datestring-validator
Advanced tools
Comparing version 1.0.3 to 2.0.0
{ | ||
"devDependencies": { | ||
"@babel/core": "^7.5.0", | ||
"@babel/preset-env": "^7.5.0", | ||
"@babel/preset-typescript": "^7.3.3", | ||
"@types/jest": "^24.0.15", | ||
"babel-jest": "^24.8.0", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2", | ||
"jest": "^24.8.0", | ||
"moment": "^2.24.0" | ||
"moment": "^2.24.0", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.5.2" | ||
}, | ||
@@ -16,6 +23,13 @@ "babel": { | ||
}, | ||
"files": [ | ||
], | ||
"jest": { | ||
"verbose": true | ||
}, | ||
"files": [], | ||
"scripts": { | ||
"test": "jest" | ||
"test": "jest", | ||
"test-year-month": "jest --t 'isValidYearMonth'", | ||
"test-date": "jest --t 'isValidDate'", | ||
"test-time": "jest --t 'isValidTime'", | ||
"test-zones": "jest --t 'isValidZoneOffset'", | ||
"test-iso": "jest --t 'isValidISODateString'" | ||
}, | ||
@@ -28,3 +42,3 @@ "name": "iso-datestring-validator", | ||
"license": "MIT", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"keywords": [ | ||
@@ -39,8 +53,8 @@ "date", | ||
], | ||
"main": "iso-8601-date-string-validator.js", | ||
"description": "A simple function for ISO9601 date string validation. It validates any YYYY-MM-DD date from 0001-01-01 up to 9999-12-31 with a regular expression, leap years friendly.", | ||
"main": "iso-datestring-validator.js", | ||
"description": "The goal of the package is to provide lightweight tools for validating strings denotings dates and time. It includes ISO 8601 datestring validation, simple YYYY-MM-DD date validation and time validation in hh:mm:ss.fff format. See details in readme.", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Bwca/iso-8601-date-string-validator.git" | ||
"url": "https://github.com/Bwca/iso-datestring-validator.git" | ||
} | ||
} |
# iso-datestring-validator | ||
(the package will be republished on npm soon) | ||
## What is it | ||
A simple function for ISO 8601 date string validation. It validates any YYYY-MM-DD date from 0001-01-01 up to 9999-12-31 with a regular expression, leap years friendly. | ||
A simple package for validating strings denoting dates and time, including ISO 8601 format. The package provides the following functions: | ||
The function itself takes two arguments, first being the date string, the second being an optional one symbol separator, by default it is | ||
1. Date validation. YYYY-MM-DD format from 0001-01-01 to 9999-12-31, leap year friendly. Custom digit separators and null separators supported: YYYY/MM/DD or YYYYMMDD is no problem. | ||
2. Time validation. HH:mm:ss.fff format, seconds and fractions of seconds being optional. Custom digit separators supported for HHmmss as well (no custom separator for fractions, it is dot). | ||
3. Year-month validation. | ||
4. **ISO 8601 datestring validation** with timezones, with and without separators: | ||
* 2019-07-09T15:03:36.000+00:00 | ||
* 2019-07-09T15:03:36Z | ||
* 20190709T150336Z | ||
## Installation | ||
``` | ||
function(date, separator = "-") | ||
npm i --save iso-datestring-validator | ||
``` | ||
or | ||
``` | ||
yarn add iso-datestring-validator | ||
``` | ||
The validation is done with a regular expression, which has been covered by jest tests. It correctly validates all dates between 0001-01-01 and 9999-12-31, correctly recognizing leap years. | ||
## Import | ||
``` | ||
const isoDatestringValidator = require("iso-datestring-validator"); | ||
``` | ||
## Install | ||
## Usage | ||
### Date validation | ||
Pass a **YYYY-MM-DD** date string to the **isValidDate** function to check it. To validate dates that use a custom digit separator, pass it as the second argument. | ||
``` | ||
npm i --save iso-datestring-validator | ||
const isoDatestringValidator = require("iso-datestring-validator"); | ||
isoDatestringValidator.isValidDate("2019-01-31"); | ||
// true | ||
isoDatestringValidator.isValidDate("20190131"); | ||
// false, no custom digit separator provided, hyphen separator not found in the string | ||
isoDatestringValidator.isValidDate("20190131", ''); | ||
// true | ||
isoDatestringValidator.isValidDate("2019/01/31", '/'); | ||
// true | ||
``` | ||
or | ||
### Time validation | ||
Time string in HH:mm:ss.fff format can be validated with the **isValidTime** function. Seconds and fractions are optional. However, if using fractions min number of numbers is 1 and max is 9. | ||
``` | ||
yarn add iso-datestring-validator | ||
const isoDatestringValidator = require("iso-datestring-validator"); | ||
isoDatestringValidator.isValidTime("13:00"); | ||
// true | ||
isoDatestringValidator.isValidTime("13:00:00"); | ||
// true | ||
isoDatestringValidator.isValidTime("13:00:00.000000000"); | ||
// true | ||
``` | ||
## Import and use | ||
### Year and month validation | ||
These are validated by the **isValidYearMonth** function. Rules same as in the previous case: a string **YYYY-MM** and a custom digit separator if required. | ||
``` | ||
const isIsoDatestring = require("iso-datestring-validator"); | ||
const isoDatestringValidator = require("iso-datestring-validator"); | ||
isIsoDatestring('2019-01-01'); | ||
isoDatestringValidator.isValidYearMonth("2019/01", '/'); | ||
// true | ||
isoDatestringValidator.isValidYearMonth("2019-01"); | ||
// true | ||
``` | ||
isIsoDatestring('1900-02-29'); | ||
// false (1900 was not a leap year, so the date is invalid) | ||
### ISO 8601 datestring validation | ||
Pass a string to **isValidISODateString** to see if it is valid. | ||
``` | ||
const isoDatestringValidator = require("iso-datestring-validator"); | ||
That's all there's to tell about this package. | ||
isoDatestringValidator.isValidISODateString('2019-07-09T15:03:36.000+00:00'); | ||
// true | ||
isoDatestringValidator.isValidISODateString('20190709T150336Z'); | ||
// true | ||
``` | ||
That's all about this package. Have fun, feel free to contribute with some test :] |
7078
94
10