From be117b115b26ccb796965d6f1da792ef5d52317e Mon Sep 17 00:00:00 2001 From: Fredrik Forsmo Date: Thu, 23 Mar 2023 21:16:37 +0100 Subject: [PATCH] Add bin to debug personnummer --- bin/personnummer | 29 +++++++++++++++++++++++++++++ package.json | 3 +++ 2 files changed, 32 insertions(+) create mode 100644 bin/personnummer diff --git a/bin/personnummer b/bin/personnummer new file mode 100644 index 0000000..ec22d8b --- /dev/null +++ b/bin/personnummer @@ -0,0 +1,29 @@ +#!/usr/bin/env node + +const fs = require("fs"); +const Personnummer = fs.existsSync(`${__dirname}/../dist/cjs/index.js`) + ? require(`${__dirname}/../dist/cjs/index.js`) + : require(`${__dirname}/../src/index.ts`); + +const main = () => { + const argv = process.argv.slice(2); + if (argv.length < 1) { + console.log("Usage: npx personnummer "); + return; + } + + try { + const pnr = Personnummer.parse(argv[0]); + console.log(` Short format: ${pnr.format()} + Long format: ${pnr.format(true)} + Age: ${pnr.getAge()} + Sex: ${pnr.isMale() ? "male" : "female"} +Coordination number: ${pnr.isCoordinationNumber()} + Interim number: ${pnr.isInterimNumber()}`); + } catch (err) { + console.log(err); + console.log("Invalid personal identity number provided"); + } +}; + +main(); diff --git a/package.json b/package.json index 6fd1f0b..90e15ed 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,9 @@ "type": "git", "url": "git://github.com/personnummer/js.git" }, + "bin": { + "personnummer": "bin/personnummer" + }, "files": [ "dist/**/*" ],