Completed
Push — master ( 9a2275...38ec3f )
by Jean
57s
created

success.js ➔ parse   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 19
rs 9.4285
1
/**
2
 * @file Module 'codingame/parsers/success'
3
 * @author woshilapin <[email protected]>
4
 */
5
/**
6
 * Parse successful response from Codingame when test pass.
7
 *
8
 * The typical response in this case if of the following form.
9
 * ```json
10
 * {
11
 * 	success: {
12
 * 		"output": output,
13
 * 		"comparison": {
14
 * 			"success": true
15
 * 		}
16
 * 	}
17
 * }
18
 * ```
19
 * @module codingame/parsers/success
20
 */
21
22
let name = `success`;
23
24
/**
25
 * Attempt to parse the body of a successful request to Codingame test API.
26
 * This function will try to map a response for a successful test.
27
 *
28
 * @function parse
29
 * @param {Object} body Body of the response
30
 * @returns {Promise<string>} Resolve with the successful result value
31
 * @throws {Error} Throw is parsing failed
32
 * @instance
33
 */
34
let parse = function parse(body) {
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable parse already seems to be declared on line 34. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
35
	try {
36
		let {
37
			"output": output,
38
			"comparison": {
39
				"success": success
40
			}
41
		} = body;
42
		if (success) {
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable success is declared in the current environment, consider using typeof success === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
43
			return Promise.resolve(output);
0 ignored issues
show
Bug introduced by
The variable output seems to be never declared. If this is a global, consider adding a /** global: output */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
44
		} else {
45
			throw `Success property must be true`;
46
		}
47
	} catch(error) {
48
		let message = `The body is not of response type '${name}'\n`;
49
		message += error.message;
50
		throw new Error(message);
51
	}
52
};
53
54
export default {
55
	"name": name,
56
	"parse": parse
57
};
58