Comparing version 4.0.0-alpha.1 to 4.0.0
@@ -41,3 +41,12 @@ #!/usr/bin/env node | ||
const result = cdigit[algo][cmd.name()](str); | ||
let result; | ||
try { | ||
result = cdigit[algo][cmd.name()](str); | ||
} catch (err) { | ||
if (err instanceof SyntaxError) { | ||
cmd.error(`error: ${err.message}`); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
if (cmd.name() === "validate") { | ||
@@ -44,0 +53,0 @@ process.exitCode = result ? 0 : 1; |
@@ -26,3 +26,6 @@ /** | ||
computeFromNumVals(ns) { | ||
if (ns.some((e) => e < 0 || e > 9 || !Number.isInteger(e))) { | ||
if (ns.length === 0) { | ||
throw new SyntaxError("string to be protected is empty"); | ||
} | ||
else if (ns.some((e) => e < 0 || e > 9 || !Number.isInteger(e))) { | ||
throw new SyntaxError("invalid numerical value detected"); | ||
@@ -38,4 +41,9 @@ } | ||
parse(s) { | ||
const ds = String(s); | ||
return [ds.slice(0, -1), ds.slice(-1)]; | ||
const m = String(s).match(/^(.*)([0-9])$/s); | ||
if (m != null) { | ||
return [m[1], m[2]]; | ||
} | ||
else { | ||
throw new SyntaxError("could not find check character(s)"); | ||
} | ||
} | ||
@@ -42,0 +50,0 @@ generate(s) { |
@@ -13,3 +13,6 @@ /** | ||
computeFromNumVals(ns) { | ||
if (ns.some((e) => e < 0 || e > 9 || !Number.isInteger(e))) { | ||
if (ns.length === 0) { | ||
throw new SyntaxError("string to be protected is empty"); | ||
} | ||
else if (ns.some((e) => e < 0 || e > 9 || !Number.isInteger(e))) { | ||
throw new SyntaxError("invalid numerical value detected"); | ||
@@ -35,4 +38,9 @@ } | ||
parse(s) { | ||
const ds = String(s); | ||
return [ds.slice(0, -1), ds.slice(-1)]; | ||
const m = String(s).match(/^(.*)([0-9])$/s); | ||
if (m != null) { | ||
return [m[1], m[2]]; | ||
} | ||
else { | ||
throw new SyntaxError("could not find check character(s)"); | ||
} | ||
} | ||
@@ -39,0 +47,0 @@ generate(s) { |
@@ -45,3 +45,6 @@ /** | ||
: this.alphabet.length; | ||
if (ns.some((e) => e < 0 || e >= maxNumVal || !Number.isInteger(e))) { | ||
if (ns.length === 0) { | ||
throw new SyntaxError("string to be protected is empty"); | ||
} | ||
else if (ns.some((e) => e < 0 || e >= maxNumVal || !Number.isInteger(e))) { | ||
throw new SyntaxError("invalid numerical value detected"); | ||
@@ -78,6 +81,11 @@ } | ||
parse(s) { | ||
const ds = String(s); | ||
return this.flavor === "TWO_CCS" | ||
? [ds.slice(0, -2), ds.slice(-2)] | ||
: [ds.slice(0, -1), ds.slice(-1)]; | ||
const charMap = getCharMap(this.alphabet); | ||
const n = this.flavor === "TWO_CCS" ? 2 : 1; | ||
const cc = s.slice(-n); | ||
if (cc.length === n && [...cc].every((c) => charMap[c] != null)) { | ||
return [s.slice(0, -n), cc]; | ||
} | ||
else { | ||
throw new SyntaxError("could not find check character(s)"); | ||
} | ||
} | ||
@@ -101,3 +109,6 @@ generate(s) { | ||
const mod = this.alphabet.length; | ||
if (ns.some((e) => e < 0 || e >= mod || !Number.isInteger(e))) { | ||
if (ns.length === 0) { | ||
throw new SyntaxError("string to be protected is empty"); | ||
} | ||
else if (ns.some((e) => e < 0 || e >= mod || !Number.isInteger(e))) { | ||
throw new SyntaxError("invalid numerical value detected"); | ||
@@ -125,4 +136,10 @@ } | ||
parse(s) { | ||
const ds = String(s); | ||
return [ds.slice(0, -1), ds.slice(-1)]; | ||
const charMap = getCharMap(this.alphabet); | ||
const cc = s.slice(-1); | ||
if (cc.length === 1 && charMap[cc] != null) { | ||
return [s.slice(0, -1), cc]; | ||
} | ||
else { | ||
throw new SyntaxError("could not find check character(s)"); | ||
} | ||
} | ||
@@ -129,0 +146,0 @@ generate(s) { |
@@ -15,3 +15,6 @@ /** | ||
computeFromNumVals(ns) { | ||
if (ns.some((e) => e < 0 || e > 9 || !Number.isInteger(e))) { | ||
if (ns.length === 0) { | ||
throw new SyntaxError("string to be protected is empty"); | ||
} | ||
else if (ns.some((e) => e < 0 || e > 9 || !Number.isInteger(e))) { | ||
throw new SyntaxError("invalid numerical value detected"); | ||
@@ -37,4 +40,9 @@ } | ||
parse(s) { | ||
const ds = String(s); | ||
return [ds.slice(0, -1), ds.slice(-1)]; | ||
const m = String(s).match(/^(.*)([0-9])$/s); | ||
if (m != null) { | ||
return [m[1], m[2]]; | ||
} | ||
else { | ||
throw new SyntaxError("could not find check character(s)"); | ||
} | ||
} | ||
@@ -41,0 +49,0 @@ generate(s) { |
@@ -39,3 +39,6 @@ /** | ||
computeFromNumVals(ns) { | ||
if (ns.some((e) => e < 0 || e > 9 || !Number.isInteger(e))) { | ||
if (ns.length === 0) { | ||
throw new SyntaxError("string to be protected is empty"); | ||
} | ||
else if (ns.some((e) => e < 0 || e > 9 || !Number.isInteger(e))) { | ||
throw new SyntaxError("invalid numerical value detected"); | ||
@@ -56,4 +59,9 @@ } | ||
parse(s) { | ||
const ds = String(s); | ||
return [ds.slice(0, -1), ds.slice(-1)]; | ||
const m = String(s).match(/^(.*)([0-9])$/s); | ||
if (m != null) { | ||
return [m[1], m[2]]; | ||
} | ||
else { | ||
throw new SyntaxError("could not find check character(s)"); | ||
} | ||
} | ||
@@ -60,0 +68,0 @@ generate(s) { |
@@ -20,2 +20,5 @@ /** | ||
* @returns String with check character(s) | ||
* @throws `SyntaxError` if an algorithm-specific syntax error occurs. Note | ||
* that the bundled algorithm objects do not generally throw errors because | ||
* they ignore the unknown letters in the string to be protected. | ||
*/ | ||
@@ -28,2 +31,6 @@ generate(strWithoutCheckChars: string): string; | ||
* @returns True if the argument is valid | ||
* @throws `SyntaxError` if the argument does not contain check character(s) | ||
* or any other algorithm-specific syntax error occurs. Note that the bundled | ||
* algorithm objects do not generally throw errors because they ignore the | ||
* unknown letters in the string to be protected. | ||
*/ | ||
@@ -37,2 +44,5 @@ validate(strWithCheckChars: string): boolean; | ||
* @returns Check character(s) | ||
* @throws `SyntaxError` if an algorithm-specific syntax error occurs. Note | ||
* that the bundled algorithm objects do not generally throw errors because | ||
* they ignore the unknown letters in the string to be protected. | ||
*/ | ||
@@ -49,3 +59,4 @@ compute(strWithoutCheckChars: string): string; | ||
* @returns Check character(s) decoded to an array of numerical values | ||
* @throws `SyntaxError` if the argument contains an invalid numerical value. | ||
* @throws `SyntaxError` if the argument contains an invalid numerical value | ||
* or any other algorithm-specific syntax error occurs. | ||
*/ | ||
@@ -59,4 +70,8 @@ computeFromNumVals(numValsWithoutCheckChars: number[]): number[]; | ||
* @returns Tuple of [string without check character(s), check character(s)] | ||
* @throws `SyntaxError` if the argument does not contain check character(s) | ||
* or any other algorithm-specific syntax error occurs. Note that the bundled | ||
* algorithm objects do not generally throw errors because they ignore the | ||
* unknown letters in the string to be protected. | ||
*/ | ||
parse(strWithCheckChars: string): [string, string]; | ||
} |
{ | ||
"name": "cdigit", | ||
"version": "4.0.0-alpha.1", | ||
"version": "4.0.0", | ||
"description": "Collection of check digit algorithms implemented in JavaScript", | ||
@@ -16,2 +16,3 @@ "type": "module", | ||
"LICENSE-MIT", | ||
"example.js", | ||
"lib" | ||
@@ -18,0 +19,0 @@ ], |
@@ -126,2 +126,13 @@ # cdigit: Check Digit Algorithms in JS | ||
### computeFromNumVals(numValsWithoutCheckChars: number[]): number[] | ||
Generates the check character(s) from the argument using the algorithm. This | ||
method is an alphabet-independent equivalent of `compute()`, where the return | ||
value and argument are both represented as arrays of each digit's numerical | ||
value. | ||
```javascript | ||
console.log(mod97_10.computeFromNumVals([1, 2, 3, 4])); // [8, 2] | ||
``` | ||
See [example.js](https://npm.runkit.com/cdigit) for usage examples. | ||
@@ -128,0 +139,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
46881
20
760
0
179
0