Skip to content

Commit

Permalink
Decaffeinate: Improve code compiled by npx decaffeinate
Browse files Browse the repository at this point in the history
  • Loading branch information
Edditoria committed Apr 1, 2023
1 parent e7817fc commit 6e2bbe0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 42 deletions.
7 changes: 1 addition & 6 deletions esm/cnid.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
* decaffeinate suggestions:
* 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';

Expand Down Expand Up @@ -31,7 +26,7 @@ export function cnid(id) {
const getWeight = n => Math.pow(2, n - 1) % 11;
let weightedSum = 0;
let index = id.length;
for (var char of Array.from(identifier)) {
for (var char of identifier) {
weightedSum += +char * getWeight(index);
index--;
}
Expand Down
7 changes: 1 addition & 6 deletions esm/hkid.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
* decaffeinate suggestions:
* 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';

/**
Expand Down Expand Up @@ -39,7 +34,7 @@ export function hkid(id) {
let weightedSum = weight === 8 ? 324 : 0;
const identifier = id.slice(0, -1);
const checkDigit = id.slice(-1) === 'A' ? 10 : +id.slice(-1);
for (var char of Array.from(identifier)) {
for (var char of identifier) {
var charValue = isLetter(char) ? getLetterValue(char) : +char;
weightedSum += charValue * weight;
weight--;
Expand Down
19 changes: 7 additions & 12 deletions esm/krid.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* 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';
Expand All @@ -25,11 +19,12 @@ export function krid(id) {
// Parse the date into 'YYYYMMDD' according to 'S' digit
const isThisDateValid = function(id) {
const sDigit = id.substring(6,7);
const yearPrefix = (() => { switch (sDigit) {
case '1':case '2':case '5':case '6': return '19';
case '3':case '4':case '7':case '8': return '20';
default: return '18';
} })();
let yearPrefix;
switch (sDigit) {
case '1':case '2':case '5':case '6': yearPrefix = '19'; break;
case '3':case '4':case '7':case '8': yearPrefix = '20'; break;
default: yearPrefix = '18';
};
const date = yearPrefix + id.substring(0,6);
const maxDate = getMaxDate(17); // 17 years old to register for an ID
return isDateValid(date, 'default', maxDate);
Expand All @@ -39,7 +34,7 @@ export function krid(id) {
const weight = [2,3,4,5,6,7,8,9,2,3,4,5,0]; // 0 is added for check digit
let weightedSum = 0;
let index = 0;
for (var char of Array.from(id)) {
for (var char of id) {
weightedSum += +char * weight[index];
index++;
}
Expand Down
15 changes: 4 additions & 11 deletions esm/utils/is-date-valid.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
/* TODO:
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS205: Consider reworking code to avoid use of IIFEs
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/

/**
* Check if the date is reasonably valid.
*
Expand Down Expand Up @@ -52,8 +44,8 @@ export function isDateValid(idDate, minDate, maxDate) {
const date = new Date(year, +month - 1, day); // a Date object

// 2. Is day valid?
/** Do not use Array.indexOf() because of the suck IE */
const maxDay = (() => {
/** TODO: Refactor. */
const getDaysInMonth = (year, month) => {
if ('01,03,05,07,08,10,12'.indexOf(month) >= 0) {
return 31;
} else if ('04,06,09,11'.indexOf(month) >= 0) {
Expand All @@ -64,7 +56,8 @@ export function isDateValid(idDate, minDate, maxDate) {
if (isLeapYear) { return 29;
} else { return 28; }
}
})();
};
const maxDay = getDaysInMonth(year, month);
const isDayValid = (day > 0) && (day <= maxDay);
if (!isDayValid) { return false; }

Expand Down
8 changes: 1 addition & 7 deletions esm/utils/is-twid-checksum-valid.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/* TODO:
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/

/**
* Validate checksum for TWID and TWRC.
* @module utils/is-twid-checksum-valid
Expand All @@ -28,7 +22,7 @@ export function isTWIDChecksumValid(id, letterNum) {

// weightedSum for idNumbers
let weight = idLen - idLetters.length - 1; // Minus letter digit and check digit
for (var char of Array.from(idNumbers)) {
for (var char of idNumbers) {
weightedSum += +char * weight;
weight--;
}
Expand Down

0 comments on commit 6e2bbe0

Please sign in to comment.