Giter VIP home page Giter VIP logo

finnish-ssn's Introduction

Finnish SSN validation and creation

Build Status 0 deps Downloads License

  • A micro Javascript library for validating and creating Finnish social security numbers
  • Zero dependencies

Installation

npm install finnish-ssn --save

Usage

ES6 / TypeScript

import { FinnishSSN } from 'finnish-ssn'
const isValid = FinnishSSN.validate('010101-100X')
console.log(isValid) //  Yields true

Examples

Validate an SSN

//  This is valid SSN
console.log('valid ssn returns ' + FinnishSSN.validate('290296-7808'))
//  'valid ssn returns true'

//  This is invalid SSN
console.log('invalid ssn returns ' + FinnishSSN.validate('010198-1000'))
//  'invalid ssn returns false'

Parse SSN

//  This is valid SSN
var parsedSsn =  FinnishSSN.parse('290296-7808')
//  This is invalid SSN
console.log(parsedSsn)
{
  valid: true,
  sex: 'female',
  ageInYears: 19,
  dateOfBirth: Thu Feb 29 1996 00:00:00 GMT+0200 (EET)
}

Create an SSN for person that is 20 years old.

console.log('SSN for person that is 20 years old ' + FinnishSSN.createWithAge(20))
//  SSN for person that is 20 years old 010195-XXXX

Functions

#validate(ssn)

  • Validates parameter given SSN. Returns true if SSN is valid, otherwise false

#parse(ssn)

  • Parses parameter given SSN. Returns object {valid: boolean, sex: "male|female", ageInYears: Number, dateOfBirth: Date }
{
  valid: false,
  sex: null,
  ageInYears: null,
  dateOfBirth: null
}
{
  valid: true,
  sex: 'male',
  ageInYears: 15,
  dateOfBirth: Tue Feb 29 2000 00:00:00 GMT+0200 (EET)
}
{
  valid: true,
  sex: 'female',
  ageInYears: 15,
  dateOfBirth: Mon Feb 28 2000 00:00:00 GMT+0200 (EET)
}

#createWithAge(age)

  • Creates a valid SSN using the given age (Integer). Generates randomly male and female SSN'n.

Building

npm run dist

# Run tests
npm run test

# Run tests in watch-mode
npm run test:watch

Changelog

2.1.2

2.1.1

2.1.0

2.0.3

2.0.2

  • Using TypeScript
  • Minor version in x.y.2 thanks to hazzle with npm publish and artifacts

1.2.0

  • Generate SSNs with random month and day for given age. Also takes into account whether the randomized birth date has already passed and adjusts birth year accordingly, so that the returned SSN really has the given age on the day of generation.

1.1.1

1.1.0

  • Sources ported from ES5 --> ES6
  • Distributed js is transpiled to ES5 for backwards compatibility
  • API should still be backwards compatible with 1.0.3. Bumping minor-version to be on the safe side.

1.0.3

1.0.2

1.0.1

  • Clean semicolons, removed lodash

1.0.0

  • Initial release

License

MIT License

finnish-ssn's People

Contributors

aleksiuu avatar haukurmar avatar miikka-niemela avatar mjviljan avatar npmcdn-to-unpkg-bot avatar renovate-bot avatar salunn avatar sebazai avatar vkomulai avatar xird avatar xkr47 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

finnish-ssn's Issues

Built sources missing from published package (v2.0.3)

Latest release does not contain built javascript files, only TypeScript sources. The package's package.json declares dist/index.js and dist/index.d.ts as the main script and typings so as a consequence the release is not working.

Upcomming reform of personal identity code

It's possible that from 1.1.2023 there will be new symbols to represent 20th and 21st century.
Full proposal: https://dvv.fi/en/reform-of-personal-identity-code

Important part:

The new punctuation marks are:
For those born in the 2000s: B, C, D, E and F, from letter B to replace current letter A
For those born in the 20th century, Y, X, W, V, U, from the letter Y to replace the current symbol -.

These changes are did not pass through parliament yet but it would be nice if this library is ready for them.

Bug in calculating age

As today's date is 2017-01-13, age is calculated wrong for ssn shown below:

FinnishSSN.parse('050295-1111').ageInYears
<- 22

However, if the day part of the birthdate is greater than day of the current date, it works:

FinnishSSN.parse('150295-1212').ageInYears
<- 21

Client side usage

How can I make this library (version 2.0.4) to work on browsers like Chrome or Firefox?

Length is not verified.

As far as I know maximum length of Finnish SSN is 11 characters. Currently the library doesn't verify the length of the SNN.

If we take for example this test, it will return true. And it is okay.

FinnishSSN.validate('010114A173M')

But if we append anything at the end it will still validate it as correct, while it's not.

FinnishSSN.validate('010114A173M89edyhasd')

createWithAge can create an invalid SSN

There is a possibility that generating the SSN based on age does not create valid SSN.

    for (let i = 0; i < 20; i++) {
        const ssn = FinnishSSN.createWithAge(20)
        const isValid = FinnishSSN.validate(ssn)
        console.log(`${i}: ${ssn} is valid: ${isValid}`)
    }

outputs:
0: 020200A5012 is valid: true
1: 200999A843W is valid: false
2: 161299A1103 is valid: false
3: 020300A623T is valid: true
4: 140400A7692 is valid: true
5: 080200A7266 is valid: true
6: 130200A488R is valid: true
7: 140600A142E is valid: true
8: 180200A672W is valid: true
9: 170300A230T is valid: true
10: 220100A357H is valid: true
11: 030500A805X is valid: true
12: 250899A181Y is valid: false
13: 301199A392D is valid: false
14: 060200A780M is valid: true
15: 061199A777B is valid: false
16: 190300A810T is valid: true
17: 090999A1105 is valid: false
18: 211299A6026 is valid: false
19: 010999A443R is valid: false

Wrong SSN validation(

Hello! I have probably found the small bug in your library.
When I type this SSN 000931-5511, your library returns{ valid: true }, but as i know it's not valid ssn.

It's probably, because you allow to type:

  1. 00 as a day
  2. 00 as a month

As i can see, this bug is caused by this regex, which allows to enter such data.

Confirmation of my words:
image

createWithAge should use SSN rolling id from 9xx number space

createWithAge uses the rolling id uses also real assignable rolling ids and therefor the createWithAge can accidentally create a SSN that is used by a real person.

Functionality should be changed that the default behaviour would generate the SSN with the rolling id only from the 9xx space and creating the rolling id from the 0xx - 8xx space should be given as a parameter.

in Finnish SSN the 9xx space is reserved for temporary SSNs.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.