Skip to content

Commit

Permalink
Change to named export; Update JSDoc of modules; Fix ESLint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Edditoria committed Mar 31, 2023
1 parent 788bb7e commit e7817fc
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 181 deletions.
23 changes: 11 additions & 12 deletions esm/cnid.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
* DS101: Remove unnecessary use of Array.from
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
import normalize from './utils/normalize.mjs';
import isDateValid from './utils/is-date-valid.mjs';
import { normalize } from './utils/normalize.mjs';
import { isDateValid } from './utils/is-date-valid.mjs';

/**
Validate ID card number of China (2nd generation)
@module core/cnid
@param {string} id
@return {boolean}
Original name: Resident Identity Card of the People's Republic of China (PRC)
Format of card id: LLLLLLYYYYMMDD000X
*/
export default (function(id) {
* Validate ID card number of China (2nd generation).
* - Original name: Resident Identity Card of the People's Republic of China (PRC).
* - Format: "LLLLLLYYYYMMDD000X".
* @module core/cnid
* @param {string} id
* @returns {boolean}
*/
export function cnid(id) {

// isLengthValid = (id) -> id.length is 18

Expand Down Expand Up @@ -43,4 +42,4 @@ export default (function(id) {
id = normalize(id);
// return isLengthValid(id) and isFormatValid(id) and isThisDateValid(id) and isChecksumValid(id)
return isFormatValid(id) && isThisDateValid(id) && isChecksumValid(id);
});
}
19 changes: 9 additions & 10 deletions esm/hkid.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
* DS101: Remove unnecessary use of Array.from
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
import normalize from './utils/normalize.mjs';
import { normalize } from './utils/normalize.mjs';

/**
Validate ID card number of Hong Kong
@module core/hkid
@param {string} id
@return {boolean}
Format of card id: X123456(A) or XY123456(A)
*/
export default (function(id) {
* Validate ID card number of Hong Kong.
* Format: "X123456(A)" or "XY123456(A)".
* @module core/hkid
* @param {string} id
* @returns {boolean}
*/
export function hkid(id) {

/*
charCode = { A: 65, B: 66... Z: 90 }
Expand Down Expand Up @@ -52,4 +51,4 @@ export default (function(id) {
id = normalize(id);
// return isLengthValid(id) and isFormatValid(id) and isChecksumValid(id)
return isFormatValid(id) && isChecksumValid(id);
});
}
41 changes: 20 additions & 21 deletions esm/index.mjs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import cnid from './cnid.mjs';
import twid from './twid.mjs';
import twrc from './twrc.mjs';
import twrcLegacy from './twrc-legacy.mjs';
import hkid from './hkid.mjs';
import krid from './krid.mjs';
import { cnid } from './cnid.mjs';
import { hkid } from './hkid.mjs';
import { krid } from './krid.mjs';
import { twid } from './twid.mjs';
import { twrc } from './twrc.mjs';
import { twrcLegacy } from './twrc-legacy.mjs';

import normalize from './utils/normalize.mjs';
import isDateValid from './utils/is-date-valid.mjs';
import getMaxDate from './utils/get-max-date.mjs';
import getTwrcFormat from './utils/get-twrc-format.mjs';
import isTwidChecksumValid from './utils/is-twid-checksum-valid.mjs';
import { normalize } from './utils/normalize.mjs';
import { isDateValid } from './utils/is-date-valid.mjs';
import { getMaxDate } from './utils/get-max-date.mjs';
import { getTWRCFormat } from './utils/get-twrc-format.mjs';
import { isTWIDChecksumValid } from './utils/is-twid-checksum-valid.mjs';

/**
Throw an error when validid.tools is called. This is a temporarily function in v2.
*/
* Throw an error when validid.tools is called. This is a temporarily function in v2.
*/
const depreciatedError = function() {
throw new Error('validid.tools is depreciated. Please use validid.utils instead');
return null;
};

const validid = () => null;
Expand All @@ -25,23 +24,23 @@ validid.utils = {
normalize,
isDateValid,
getMaxDate,
isTwidChecksumValid,
getTwrcFormat
isTWIDChecksumValid,
getTWRCFormat
};
//todo: Remove in v3
validid.tools = {
normalize: depreciatedError,
isDateValid: depreciatedError,
getMaxDate: depreciatedError,
isTwidChecksumValid: depreciatedError,
getTwrcFormat: depreciatedError
isTWIDChecksumValid: depreciatedError,
getTWRCFormat: depreciatedError
};

validid.cnid = cnid;
validid.twid = twid;
validid.twrc = twrc;
validid.hkid = hkid;
validid.krid = krid;
validid.twid = twid;
validid.twrc = twrc;
validid.twrcLegacy = twrcLegacy;

export default validid;
export { validid };
25 changes: 12 additions & 13 deletions esm/krid.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
* DS205: Consider reworking code to avoid use of IIFEs
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
import normalize from './utils/normalize.mjs';
import getMaxDate from './utils/get-max-date.mjs';
import isDateValid from './utils/is-date-valid.mjs';
import { normalize } from './utils/normalize.mjs';
import { getMaxDate } from './utils/get-max-date.mjs';
import { isDateValid } from './utils/is-date-valid.mjs';

/**
Validate ID card number of South Korea
@module core/krid
@param {string} id
@return {boolean}
Original name: Resident Registration Number (RRN)
Format of card id: YYMMDD-SBBBBNC
*/
export default (function(id) {
* Validate ID card number of South Korea.
* - Original name: Resident Registration Number (RRN).
* - Format: "YYMMDD-SBBBBNC".
* @module core/krid
* @param {string} id
* @returns {boolean}
*/
export function krid(id) {

// isLengthValid = (id) -> id.length is 13

Expand Down Expand Up @@ -51,4 +50,4 @@ export default (function(id) {
id = normalize(id);
// return isLengthValid(id) and isFormatValid(id) and isThisDateValid(id) and isChecksumValid(id)
return isFormatValid(id) && isThisDateValid(id) && isChecksumValid(id);
});
}
34 changes: 17 additions & 17 deletions esm/twid.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import normalize from './utils/normalize.mjs';
import isChecksumValid from './utils/is-twid-checksum-valid.mjs';
import { normalize } from './utils/normalize.mjs';
import { isTWIDChecksumValid } from './utils/is-twid-checksum-valid.mjs';

/**
Validate ID card number of Taiwan
@module core/twid
@param {string} id
@return {boolean}
Original name: National Identification Card of the Republic of China
Format of card id: A123456789
There is another system called Taiwan Resident Certificate (Uniform ID Numbers)
@see module:core/twrc
*/
export default (function(id) {
* Validate ID card number of Taiwan.
* - Original name: National Identification Card of the Republic of China.
* - Format: "A123456789".
*
* There is another system called Taiwan Resident Certificate (Uniform ID Numbers).
* @see module:core/twrc
*
* @module core/twid
* @param {string} id
* @returns {boolean}
*/
export function twid(id) {
// isLengthValid = (id) -> id.length is 10
const isFormatValid = id => /^[A-Z][12][0-9]{8}$/.test(id);

id = normalize(id);
// return isLengthValid(id) and isFormatValid(id) and isChecksumValid(id, 1)
return isFormatValid(id) && isChecksumValid(id, 1);
});
// return isLengthValid(id) and isFormatValid(id) and isTWIDChecksumValid(id, 1)
return isFormatValid(id) && isTWIDChecksumValid(id, 1);
}
34 changes: 17 additions & 17 deletions esm/twrc-legacy.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import normalize from './utils/normalize.mjs';
import isChecksumValid from './utils/is-twid-checksum-valid.mjs';
import { normalize } from './utils/normalize.mjs';
import { isTWIDChecksumValid } from './utils/is-twid-checksum-valid.mjs';

/**
Validate ID number of Taiwan Resident Certificate (Uniform ID Numbers).
Only validate ID in or before 2020.
@module core/twrc
@param {string} id
@return {boolean}
Format of the id: AB12345678
In Taiwan, there is another system called National Identification Card
@see module:core/twid-legacy
*/
export default (function(id) {
* Validate ID number of Taiwan Resident Certificate (Uniform ID Numbers).
* Only validate ID in or before 2020.
* Format of the id: "AB12345678"
*
* In Taiwan, there is another system called National Identification Card.
* @see module:core/twid
*
* @module core/twrc-legacy
* @param {string} id
* @returns {boolean}
*/
export function twrcLegacy(id) {
// isLengthValid = (id) -> id.length is 10
const isFormatValid = id => /^[A-Z][A-D][0-9]{8}$/.test(id);

id = normalize(id);
// return isLengthValid(id) and isFormatValid(id) and isChecksumValid(id, 2)
return isFormatValid(id) && isChecksumValid(id, 2);
});
// return isLengthValid(id) && isFormatValid(id) && isTWIDChecksumValid(id, 2)
return isFormatValid(id) && isTWIDChecksumValid(id, 2);
}
41 changes: 21 additions & 20 deletions esm/twrc.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import normalize from './utils/normalize.mjs';
import getFormat from './utils/get-twrc-format.mjs';
import isChecksumValid from './utils/is-twid-checksum-valid.mjs';
import { normalize } from './utils/normalize.mjs';
import { getTWRCFormat } from './utils/get-twrc-format.mjs';
import { isTWIDChecksumValid } from './utils/is-twid-checksum-valid.mjs';

/**
Validate ID number of Taiwan Resident Certificate (Uniform ID Numbers).
@module core/twrc
@param {string} id
@return {boolean}
Format of the id:
- A123456789 (new ID in 2020)
- AB12345678 (legacy but still valid)
In Taiwan, there is another system called National Identification Card
@see module:core/twid
*/
export default (function(id) {
* Validate ID number of Taiwan Resident Certificate (Uniform ID Numbers).
*
* Format of the ID:
* - "A123456789" - New ID in 2020.
* - "AB12345678" - Legacy but still valid.
*
* In Taiwan, there is another system called National Identification Card.
* @see module:core/twid
*
* @module core/twrc
* @param {string} id
* @return {boolean}
*/
export function twrc(id) {
// isLengthValid = (id) -> id.length is 10

id = normalize(id);
/** @type {string|boolean} - Either 'new', 'old' or false */
const idFormat = getFormat(id);
const idFormat = getTWRCFormat(id);
if (idFormat === 'old') {
return isChecksumValid(id, 2);
return isTWIDChecksumValid(id, 2);
}
if (idFormat === 'new') {
return isChecksumValid(id, 1);
return isTWIDChecksumValid(id, 1);
}
// else: idFormat is false
return false;
});
}
17 changes: 8 additions & 9 deletions esm/utils/get-max-date.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/**
Calculate the expected birthday by providing year only
Useful for putting maxDate in isDateValid()
@module utils/get-max-date
@param {number} yearsOld - Should be a whole number
@return {Object} An Date() object
*/
export default (function(yearsOld) {
* Calculate the expected birthday by providing year only.
* Useful for putting `maxDate` in `isDateValid()`.
* @module utils/get-max-date
* @param {number} yearsOld - Should be a whole number.
* @returns {Date}
*/
export function getMaxDate(yearsOld) {
const now = new Date();
const year = now.getFullYear() - yearsOld;
return new Date(year, now.getMonth(), now.getDate());
});
}
12 changes: 6 additions & 6 deletions esm/utils/get-twrc-format.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
Check if the format of TWRC is old (before 2021), new (from 2021) or invalid.
@param {string} id - Expect the ID is normalized.
@return {string|boolean} - Either 'old', 'new' or false.
*/
export default (function(id) {
* Check if the format of TWRC is old (before 2021), new (from 2021) or invalid.
* @param {string} id - Expect the ID is normalized.
* @returns {(string|boolean)} - Either 'old', 'new' or false.
*/
export function getTWRCFormat(id) {
if (/^[A-Z][A-D][0-9]{8}$/.test(id)) {
return 'old';
}
if (/^[A-Z][89][0-9]{8}$/.test(id)) {
return 'new';
}
return false;
});
}
Loading

0 comments on commit e7817fc

Please sign in to comment.