10000 chore: es6 parser (#98) · lance/sdk-javascript@cd6decd · GitHub
[go: up one dir, main page]

Skip to content

Commit cd6decd

Browse files
authored
chore: es6 parser (cloudevents#98)
Signed-off-by: Grant Timmerman <timmerman+devrel@google.com>
1 parent e83db29 commit cd6decd

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

lib/formats/json/parser.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,35 @@ const {
44
isStringOrObjectOrThrow
55
} = require("../../utils/fun.js");
66

7-
function JSONParser(decorator) {
8-
this.decorator = decorator;
9-
}
10-
117
const invalidPayloadTypeError =
128
new Error("invalid payload type, allowed are: string or object");
13-
149
const nullOrIndefinedPayload =
1510
new Error("null or undefined payload");
1611

17-
// Function
1812
const asJSON = (v) => (isString(v) ? JSON.parse(v) : v);
1913

20-
// Level 0 of validation: is that string? is that JSON?
21-
function validateAndParse(payload) {
22-
const json =
23-
Array.of(payload)
14+
class JSONParser {
15+
constructor(decorator) {
16+
this.decorator = decorator;
17+
}
18+
19+
/**
20+
* Parses the payload with an optional decorator
21+
* @param {object|string} payload the JSON payload
22+
* @return {object} the parsed JSON payload.
23+
*/
24+
parse(payload) {
25+
if (this.decorator) {
26+
payload = this.decorator.parse(payload);
27+
}
28+
29+
return Array.of(payload)
30+
2431
.filter((p) => isDefinedOrThrow(p, nullOrIndefinedPayload))
2532
.filter((p) => isStringOrObjectOrThrow(p, invalidPayloadTypeError))
2633
.map(asJSON)
2734
.shift();
28-
29-
return json;
30-
}
31-
32-
JSONParser.prototype.parse = function(payload) {
33-
let toparse = payload;
34-
35-
if (this.decorator) {
36-
toparse = this.decorator.parse(payload);
3735
}
38-
39-
// is that string? is that JSON?
40-
const valid = validateAndParse(toparse);
41-
42-
return valid;
43-
};
36+
}
4437

4538
module.exports = JSONParser;

0 commit comments

Comments
 (0)
0