Skip to content

Commit 4890799

Browse files
committed
Make sure to check for nulls when calling toString in MatchError
1 parent fdda83f commit 4890799

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

lib/tailored.js

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
{
22
"name": "tailored",
3-
"version": "2.4.6",
3+
"version": "2.4.7",
44
"description": "Pattern matching library",
55
"main": "lib/tailored.js",
66
"jsnext:main": "src/index.js",
77
"scripts": {
88
"build": "rollup -c rollup.config.js",
99
"test": "mocha test --recursive --compilers js:babel-register"
1010
},
11-
"keywords": [
12-
"pattern-matching",
13-
"functional"
14-
],
11+
"keywords": ["pattern-matching", "functional"],
1512
"repository": {
1613
"type": "git",
1714
"url": "https://github.com/elixirscript/tailored.git"
@@ -30,4 +27,4 @@
3027
"dependencies": {
3128
"erlang-types": "^1.0.0"
3229
}
33-
}
30+
}

src/tailored/defmatch.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ export class MatchError extends Error {
1010
if (typeof arg === 'symbol') {
1111
this.message = 'No match for: ' + arg.toString();
1212
} else if (Array.isArray(arg)) {
13-
let mappedValues = arg.map(x => x.toString());
13+
let mappedValues = arg.map(x => {
14+
if (x === null) {
15+
return 'null';
16+
} else if (typeof x === 'undefined') {
17+
return 'undefined';
18+
}
19+
20+
return x.toString();
21+
});
22+
1423
this.message = 'No match for: ' + mappedValues;
1524
} else {
1625
this.message = 'No match for: ' + arg;
@@ -81,7 +90,7 @@ export function defmatchAsync(...clauses) {
8190
args = fillInOptionalValues(
8291
args,
8392
processedClause.arity,
84-
processedClause.optionals,
93+
processedClause.optionals
8594
);
8695

8796
if (
@@ -118,7 +127,7 @@ function findMatchingFunction(args, arities) {
118127
args = fillInOptionalValues(
119128
args,
120129
processedClause.arity,
121-
processedClause.optionals,
130+
processedClause.optionals
122131
);
123132

124133
if (
@@ -231,7 +240,7 @@ export function match_or_default(
231240
pattern,
232241
expr,
233242
guard = () => true,
234-
default_value = null,
243+
default_value = null
235244
) {
236245
let result = [];
237246
let processedPattern = buildMatch(pattern);

0 commit comments

Comments
 (0)