Comparing version 1.1.0 to 1.2.0
@@ -1,2 +0,6 @@ | ||
export declare const validate: (hkid: string) => boolean; | ||
interface IValidationOption { | ||
checkPrefix?: boolean; | ||
} | ||
export declare const validate: (hkid: string, option?: IValidationOption | undefined) => boolean; | ||
export declare const random: () => string; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.random = exports.validate = void 0; | ||
const validate = (hkid) => { | ||
var _a; | ||
const validate = (hkid, option) => { | ||
var _a, _b; | ||
const shouldCheckPrefix = (_a = option === null || option === void 0 ? void 0 : option.checkPrefix) !== null && _a !== void 0 ? _a : false; | ||
const regexp = `^([A-Z]{1,2})([0-9]{6})(([0-9A])|\\(([0-9A])\\))$`; | ||
@@ -13,4 +14,4 @@ const found = hkid.toUpperCase().match(regexp); | ||
const content = found[2]; | ||
const checkDigit = (_a = found[4]) !== null && _a !== void 0 ? _a : found[5]; | ||
if (checkPrefix(prefix) === false) { | ||
const checkDigit = (_b = found[4]) !== null && _b !== void 0 ? _b : found[5]; | ||
if (shouldCheckPrefix === true && checkPrefix(prefix) === false) { | ||
return false; | ||
@@ -17,0 +18,0 @@ } |
{ | ||
"name": "hkid", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Generate and Validate HKID", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -19,12 +19,30 @@ # hkid | ||
import * as hkid from "hkid"; | ||
``` | ||
// generate valid HKID randomly | ||
### `random()` | ||
generate valid HKID randomly | ||
```ts | ||
console.log(hkid.random()); // H3497811 | ||
``` | ||
// validate HKID | ||
### `validate(candidate: string, option?: { checkPrefix?: boolean})` | ||
validate HKID | ||
- `checkPrefix`: check candidate against [the known prefix list](./src/index.ts#L38-L67). If the prefix of candidate is not in the list, consider it as validation failure. _(default: false)_ | ||
**Note: the known prefix list may be incomplete, so false negative may be returned if `checkPrefix` is set to `true`.** | ||
```ts | ||
console.log(hkid.validate("H3497811")); // true | ||
console.log(hkid.validate("h3497811")); // true | ||
console.log(hkid.validate("H349781(1)")); // true | ||
console.log(hkid.validate("h349781(1)")); // true | ||
console.log(hkid.validate("H3497810")); // false | ||
console.log(hkid.validate("YK1597716"); // true | ||
console.log(hkid.validate("YK1597716", { checkPrefix: false})); // true | ||
console.log(hkid.validate("YK1597716", { checkPrefix: true})); // false | ||
``` | ||
@@ -37,5 +55,5 @@ | ||
- [æééĻ港čēĢäģŊččįĸŧįįšæŽčąæåé æäģŖ襨įææåįŧåēæé](https://accessinfo.hk/en/request/you_guan_xiang_gang_shen_fen_zhe_2) | ||
## 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) |
7123
94
58