Giter Site home page Giter Site logo

csharp's Introduction

Personnummer

GitHub Workflow Status NuGet Downloads

Validate Swedish social security numbers.

Installation

dotnet add package Personnummer

Examples

Validation

using Personnummer;

class Test 
{
  public void TestValidation() 
  {
    Personnummer.Valid("191212121212");     // => True
    Personnummer.Valid("121212+1212")       // => True
    Personnummer.Valid("20121212-1212")     // => True
  }
}

Format

using Personnummer;

// Short format (YYMMDD-XXXX)
(new Personnummer("1212121212")).Format();
//=> 121212-1212

// Short format for 100+ years old
(new Personnummer("191212121212")).Format();
//=> 121212+1212

// Long format (YYYYMMDDXXXX)
(new Personnummer("1212121212")).Format(true);
//=> 201212121212

Age

using Personnummer;

(new Personnummer("1212121212")).Age;
//=> 7

Get sex

(new Personnummer("1212121212")).IsMale;
//=> true
(new Personnummer("1212121212")).IsFemale;
//=> false

Coordination numbers

The package supports coordination numbers. This feature is enabled by default and can be disabled by passing an option object to the constructor, parse or validate function.

Personnummer.Parse("193405619996"); // True.
Personnummer.Parse("193405619996", new { AllowCoordinationNumber = true }); // False.

Interim / T -numbers

The package supports interim (aka T) -numbers. This feature is not enabled by default as they are commonly used, but can be enabled by passing an option object to the constructor, parse of validate function.

Personnummer.Parse("20000101T220", new { AllowInterimNumber = true }); // True.

See Personnummer.Test/PersonnummerTest.cs for more examples.

License

MIT License

Copyright (c) Personnummer and Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

csharp's People

Contributors

dependabot[bot] avatar frozzare avatar johannestegner avatar rasmusbe avatar renovate-bot avatar renovate[bot] avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

csharp's Issues

Wrong Age calculation

Hi

I'm trying to calculate the age of a Swedish Personummer, and I found your code.

But unfortunately your calculation of century in the constructor is wrong.

A person with this personnummer 1602079970, was born in 1916 not 2016

Add coordination number flag.

We need a flag on the ::Valid method making it possible to turn of validation for coordination number (+60 on day). The flag should default to enabling them as that is how it is currently working.

See personnummer/meta#9 for meta discussion.

Add Personnummer::Format(...).

Some of the other packages have a format function which returns the personnummer formatted, that should exist in this package too.

An idea is to allow for a formatting generic flag, i.e.,:

Format<long>('...'); // Returns long.
Format<string>(123123); // Returns string (with - or +).

@frozzare your input on that thought is appreciated :)

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

github-actions
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/csharp.yml
  • actions/checkout v4
  • actions/setup-dotnet v4
.github/workflows/release.yml
  • actions/checkout v4
  • actions/setup-dotnet v4
nuget
Personnummer.Tests/Personnummer.Tests.csproj
  • xunit.runner.visualstudio 2.8.0
  • xunit.runner.utility 2.8.0
  • xunit.core 2.8.0
  • xunit 2.8.0
  • Newtonsoft.Json 13.0.3
  • Microsoft.NET.Test.Sdk 17.9.0

  • Check this box to trigger a request for Renovate to run again on this repository

[BUG] -

Context

This was found in version 3.0.2 of the component.
The OS where found is Windows 10 Professional 64 bit.
As the bug was found in the main code path, it affects all environments (debug, production etc).

Expected outcome

The IsCoordinationNumber property should properly indicate if the current ssn is a "samordningsnummer"/coordination number.

Actual outcome

The IsCoordinationNumber property is always true when coordination numbers are enabled/allowed.

Description

The code always sets the IsCoordinationNumber to true when coordination numbers are allowed.
This is due to the below code.

if (options.Value.AllowCoordinationNumber)
{
  num1 = num1 > 60 ? num1 - 60 : num1;
  this.IsCoordinationNumber = true;
}

It should instead be:

if (options.Value.AllowCoordinationNumber)
{
  this.IsCoordinationNumber = num1 > 60;
  num1 = num1 > 60 ? num1 - 60 : num1;
}

Minimal reproducible test case

var ssn = new Personnummer.Personnummer("700102-5316");
Assert.IsFalse(ssn.IsCoordinationNumber);

Incorrect validation

This is a valid personnummer but the unittest fails :-/

[InlineData("550207-3900", true)]
 public void TestPersonnummerString(string value, bool expected)
        {
            Assert.Equal(Personnummer.Valid(value), expected);
        }

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.