Skip to content

Commit e691252

Browse files
committed
feat: handle non-string inputs
1 parent 1e8498e commit e691252

4 files changed

Lines changed: 24 additions & 0 deletions

File tree

lib/errors.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function createError(name, message) {
2+
const error = new Error();
3+
error.message = message;
4+
error.name = name;
5+
return error;
6+
}
7+
const codes = {
8+
FST_QS_INVALID_INPUT: createError("FST_QS_INVALID_INPUT", "Invalid Input"),
9+
};
10+
11+
module.exports = {
12+
codes,
13+
};

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"use strict";
22

3+
const errors = require("./errors");
34
const parse = require("./parse");
45

56
module.exports = {
7+
errors,
68
parse,
79
};

lib/parse.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
"use strict";
22

3+
const errors = require("./errors");
4+
35
/**
46
* @callback parse
57
* @param {string} input
68
*/
79
function parse(input) {
10+
if (typeof input !== "string") {
11+
throw errors.codes.FST_QS_INVALID_INPUT;
12+
}
813
const result = Object.create(null);
914

1015
let key = "";

test/basics.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ test("should parse the basics", () => {
2525
assert.deepEqual(qs.parse(t[0]), t[1]);
2626
});
2727
});
28+
29+
test("should throw error on invalid type", () => {
30+
assert.throws(() => qs.parse(5), "Invalid Input");
31+
});

0 commit comments

Comments
 (0)