Skip to content

Commit

Permalink
Merge pull request #2 from tsekityam/tsekityam/patch-1
Browse files Browse the repository at this point in the history
Include module definition in package
  • Loading branch information
tsekityam authored Aug 7, 2021
2 parents 73c8847 + 98a6f57 commit 6a7daa7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*

!lib/**/*
!src/**/*
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ Generate and Validate HKID
[![codecov](https://codecov.io/gh/tsekityam/hkid/branch/main/graph/badge.svg?token=34ZuXbF3md)](https://codecov.io/gh/tsekityam/hkid)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Ftsekityam%2Fhkid.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Ftsekityam%2Fhkid?ref=badge_shield)

## Installation

`yarn add hkid`

## Usage

```ts
import * as hkid from "hkid";

// generate valid HKID randomly
console.log(hkid.random());

// validate HKID
console.log(hkid.validate("h349781(0)")); // false
console.log(hkid.validate("H349781(1)")); // true
console.log(hkid.validate("h349781(1)")); // true, case doesn't matter
console.log(hkid.validate("H3497811")); // true, brackets is optional
```

## License
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Ftsekityam%2Fhkid.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Ftsekityam%2Fhkid?ref=badge_large)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hkid",
"version": "1.0.0",
"version": "1.0.1",
"description": "Generate and Validate HKID",
"main": "lib/index.js",
"repository": "https://github.com/tsekityam/hkid.git",
Expand Down
50 changes: 25 additions & 25 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import assert from "assert";
import { random, validate } from "../lib";
import * as hkid from "../lib";

describe("hkid", function () {
describe("validate", function () {
[
{ hkid: "A1234", expected: false }, // incorrect length
{ hkid: "A123456789", expected: false }, // incorrect length
{ hkid: "L555555[0]", expected: false }, // invalid char `[`
{ hkid: "g323@22a", expected: false }, // invalid char `@`
{ hkid: "AB987654(3)", expected: false }, // invalid prefix `AB`
{ hkid: "K123BBB(3)", expected: false }, // invalid pattern `BBB`
{ hkid: "L555555(0)", expected: false }, // invalid prefix `L`
{ hkid: "A5632440", expected: false }, // incorrect check digit `0`
{ hkid: "D5355770", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
{ hkid: "Y477744a", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
{ hkid: "P067688(1)", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
{ hkid: "Y8312883", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
{ hkid: "b6518406", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
{ candidate: "A1234", expected: false }, // incorrect length
{ candidate: "A123456789", expected: false }, // incorrect length
{ candidate: "L555555[0]", expected: false }, // invalid char `[`
{ candidate: "g323@22a", expected: false }, // invalid char `@`
{ candidate: "AB987654(3)", expected: false }, // invalid prefix `AB`
{ candidate: "K123BBB(3)", expected: false }, // invalid pattern `BBB`
{ candidate: "L555555(0)", expected: false }, // invalid prefix `L`
{ candidate: "A5632440", expected: false }, // incorrect check digit `0`
{ candidate: "D5355770", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
{ candidate: "Y477744a", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
{ candidate: "P067688(1)", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
{ candidate: "Y8312883", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
{ candidate: "b6518406", expected: true }, // valid hkid, verified by https://webb-site.com/dbpub/idcheck.asp
].forEach((param) => {
const { hkid, expected } = param;
const result = validate(hkid);
const { candidate, expected } = param;
const result = hkid.validate(candidate);

it(`should return ${expected} for validate(${hkid})`, function () {
it(`should return ${expected} for validate(${candidate})`, function () {
assert.equal(result, expected);
});
});
});

describe("random", function () {
[
{ hkid: random(), expected: true },
{ hkid: random(), expected: true },
{ hkid: random(), expected: true },
{ hkid: random(), expected: true },
{ hkid: random(), expected: true },
{ candidate: hkid.random(), expected: true },
{ candidate: hkid.random(), expected: true },
{ candidate: hkid.random(), expected: true },
{ candidate: hkid.random(), expected: true },
{ candidate: hkid.random(), expected: true },
].forEach((param) => {
const { hkid, expected } = param;
const result = validate(hkid);
const { candidate, expected } = param;
const result = hkid.validate(candidate);

it(`should return ${expected} for validate(random()) when random() is ${hkid}`, function () {
it(`should return ${expected} for validate(hkid.random()) when random() is ${candidate}`, function () {
assert.equal(result, expected);
});
});
Expand Down

0 comments on commit 6a7daa7

Please sign in to comment.