├── .gitignore ├── test.js ├── package.json ├── index.js ├── .travis.yml ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var test = require('ava') 4 | var isValidBirthdate = require('./') 5 | 6 | test('valid birthdate', t => { 7 | t.true(isValidBirthdate('1998/02/26')) 8 | }) 9 | 10 | test('invalid birthdate', t => { 11 | t.false(isValidBirthdate(new Date())) 12 | }) 13 | 14 | test('invalid birthdate if maximum age is 70', t => { 15 | t.false(isValidBirthdate('1900-02-26', { maxAge: 70 })) 16 | }) 17 | 18 | test('invalid birthdate if minimum age is 18', t => { 19 | t.false(isValidBirthdate('2015-07-30', { minAge: 18 })) 20 | }) 21 | 22 | test('valid birthdate if minimum age is 18', t => { 23 | t.true(isValidBirthdate('1992-11-05', { minAge: 18 })) 24 | }) 25 | 26 | test('throw error for invalid date argument', t => { 27 | t.throws(() => isValidBirthdate(28)) 28 | }) 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-valid-birthdate", 3 | "version": "0.1.0", 4 | "description": "Check if a string or date object is a valid birthdate.", 5 | "author": "Diéssica Gurskas", 6 | "license": "MIT", 7 | "main": "index.js", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/diessica/is-valid-birthdate.git" 11 | }, 12 | "bugs": { 13 | "url": "https://github.com/diessica/is-valid-birthdate/issues" 14 | }, 15 | "scripts": { 16 | "test": "standard && ava test.js" 17 | }, 18 | "keywords": [ 19 | "is birthdate", 20 | "is birth date", 21 | "valid birth date", 22 | "valid birthdate", 23 | "birthdate check", 24 | "birth date check" 25 | ], 26 | "devDependencies": { 27 | "ava": "^0.15.2", 28 | "standard": "^7.1.2" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var now = new Date() 4 | var defaults = { maxAge: 120, minAge: 0 } 5 | 6 | module.exports = function isValidBirthdate (date, options) { 7 | options = Object.assign({}, defaults, options) 8 | 9 | if (typeof (date) === 'string') { 10 | date = new Date(date) 11 | } 12 | 13 | if (date instanceof Date === false) { 14 | throw new Error( 15 | 'isValidBirthdate(): First argument (date) expects a date object.' 16 | ) 17 | } 18 | 19 | if (typeof (options) !== 'object') { 20 | throw new Error( 21 | 'isValidBirthdate(): Second argument (options) expects an object.' 22 | ) 23 | } 24 | 25 | var age = now.getFullYear() - date.getFullYear() 26 | 27 | return ( 28 | date < now && 29 | age >= options.minAge && 30 | age <= options.maxAge 31 | ) 32 | } 33 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - stable 5 | deploy: 6 | provider: npm 7 | email: diessicode@gmail.com 8 | api_key: 9 | secure: dUgBt/J3xrRqD8jmaL1leWOa7XyqI0+sSLTevSYYIBOrNtGR7x9Ysfis3XDGVqHvekIkNC15ihw84PNulVVlF9upO3KuPJF1qPSqpFCCImikHlxo67kxltZii/ZD/2/l0LuxY8o5J93K3UGrhxK0iJWXweQyEmuaC84GL+LULkNqC+WQ4INR8op0ApmYgjMamvjYoX91peNp0+CcT+oxq9N8SQW+eIPL8PHHQ196b/1FkUnLblr57iVpDafG2/xlCCX2SmdoVHRTbDVu3mGrr9GfVfRMPd0go1cUVNj95f9HZ2uNZysMAoZAkcC9XZwbH8z/DTFLkHxVZg2kg/KLqZ5nsCSRb4OFkIxwAmKL+fMRHu/9XMKkRsmbPf/hTd6eD4gqyDICFESeM5/9olbF0fw0o4/TWCpJgqsYFudekS9YQEA66eU9C0SLuqabB7877xlVvgWIxptdXXLVUqZnUxFk8grzL5pdpPAsL0KRsSnrc/5VCq40ol4j09kN901Wp3e9r49sHxKr490ZLEnS6FJk0LtX8GfguIrFwvnEEcBKDQeSTz0KFLyHy12rzhTeNuy8mTvJSMG3JJDAFVzaCQq45nEEuX3DlWfV8EZrwG9B3t818WYAiK12dWdylCncBiT+evbR11F0qw8JVya6aTOLVqrrmCpKTZMTeUqdkt0= 10 | on: 11 | tags: true 12 | branch: master 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # is-valid-birthdate [![npm version](https://img.shields.io/npm/v/is-valid-birthdate.svg)](https://www.npmjs.com/package/is-valid-birthdate) [![Build Status](https://travis-ci.org/diessica/is-valid-birthdate.svg?branch=master)](https://travis-ci.org/diessica/is-valid-birthdate) 2 | 3 | > Check if a string or date object is a valid birthdate. 4 | 5 | ## Install 6 | 7 | ```sh 8 | $ npm install --save is-valid-birthdate 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | const isValidBirthdate = require('is-valid-birthdate') 15 | 16 | isValidBirthdate('1998/02/26') // => true 17 | isValidBirthdate('2014/12/03', { minAge: 18 }) // => false 18 | isValidBirthdate(new Date()) // => false 19 | isValidBirthdate('2025-12-26') // => false 20 | ``` 21 | 22 | ## API 23 | ### `isValidBirthdate(date[, options])` 24 | 25 | #### date 26 | Type: `string` or `Date object` 27 | 28 | #### options 29 | Type: `object`
30 | Default: `{ maxAge: 120, minAge: 0 }` 31 | 32 | ## License 33 | 34 | MIT 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Diéssica Gurskas 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 all 13 | 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 THE 21 | SOFTWARE. 22 | 23 | --------------------------------------------------------------------------------