Skip to content

Commit 0f69b7a

Browse files
committed
Add assert() function for throwing exceptions
1 parent 219842c commit 0f69b7a

4 files changed

Lines changed: 41 additions & 10 deletions

File tree

.github/workflows/run-spec-on-push.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ jobs:
1111
w3cHtmlValidator: dry-run #only run html validation locally
1212
steps:
1313
- uses: actions/checkout@v5 #see: https://github.com/actions/checkout/releases
14-
- uses: actions/setup-node@v4 #see: https://github.com/actions/setup-node/releases
14+
- uses: actions/setup-node@v6 #see: https://github.com/actions/setup-node/releases
1515
- run: npm install
1616
- run: npm test

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,26 @@
6565
"posttest": "html-validator spec docs"
6666
},
6767
"devDependencies": {
68-
"@eslint/js": "~9.38",
68+
"@eslint/js": "~9.39",
6969
"@fortawesome/fontawesome-free": "~7.1",
70-
"@types/node": "~24.9",
71-
"add-dist-header": "~1.5",
70+
"@types/node": "~24.10",
71+
"add-dist-header": "~1.6",
7272
"assert-deep-strict-equal": "~1.2",
7373
"copy-file-util": "~1.3",
7474
"copy-folder-util": "~1.1",
7575
"dna-engine": "~3.3",
76-
"eslint": "~9.38",
76+
"eslint": "~9.39",
7777
"esm-to-plain-js": "~1.1",
78-
"jsdom": "~27.0",
78+
"jsdom": "~27.1",
7979
"jshint": "~2.13",
8080
"mocha": "~11.7",
8181
"pretty-print-json": "~3.0",
82-
"rimraf": "~6.0",
82+
"rimraf": "~6.1",
8383
"run-scripts-util": "~1.3",
8484
"typescript": "~5.9",
8585
"typescript-eslint": "~8.46",
8686
"uglify-js": "~3.19",
87-
"w3c-html-validator": "~1.9",
87+
"w3c-html-validator": "~2.0",
8888
"web-ignition": "~2.4"
8989
}
9090
}

spec/package.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ describe('The "dist" folder', () => {
2626

2727
});
2828

29+
////////////////////////////////////////////////////////////////////////////////
30+
describe('Library module', () => {
31+
32+
it('is an object', () => {
33+
const actual = { constructor: clabe.constructor.name };
34+
const expected = { constructor: 'Object' };
35+
assertDeepStrictEqual(actual, expected);
36+
});
37+
38+
it('has the correct properties', () => {
39+
const module = clabe;
40+
const actual = Object.keys(module).sort().map(key => [key, typeof module[key]]);
41+
const expected = [
42+
['assert', 'function'],
43+
['banksMap', 'object'],
44+
['calculate', 'function'],
45+
['cities', 'object'],
46+
['citiesMap', 'object'],
47+
['computeChecksum', 'function'],
48+
['validate', 'function'],
49+
['version', 'string'],
50+
];
51+
assertDeepStrictEqual(actual, expected);
52+
});
53+
54+
});
55+
2956
////////////////////////////////////////////////////////////////////////////////
3057
describe('Current number of banks and cities', () => {
3158

src/clabe.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ const clabe = {
2929

3030
version: '{{package.version}}',
3131

32+
assert(ok: unknown, message: string | null) {
33+
if (!ok)
34+
throw new Error(`[clabe-validator] ${message}`);
35+
},
36+
3237
computeChecksum(clabeNum17: string): number | null {
3338
// Returns the checksum calculated from the first 17 characters of CLABE number.
3439
// Example:
@@ -50,8 +55,7 @@ const clabe = {
5055
bank: 'Invalid bank code: ',
5156
city: 'Invalid city code: ',
5257
};
53-
if (typeof clabeNum !== 'string')
54-
throw new Error('clabe.validator.check(clabeNum) -- Expected string, got: ' + typeof clabeNum);
58+
clabe.assert(typeof clabeNum === 'string', 'Expected string, got: ' + typeof clabeNum);
5559
const bankCode = clabeNum.substring(0, 3);
5660
const cityCode = clabeNum.substring(3, 6);
5761
const account = clabeNum.substring(6, 17);

0 commit comments

Comments
 (0)