forked from ibmruntimes/yieldable-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·123 lines (113 loc) · 3.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* **************************************************************************
*
* (c) Copyright IBM Corp. 2017
*
* This program and the accompanying materials are made available
* under the terms of the Apache License v2.0 which accompanies
* this distribution.
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* Contributors:
* Multiple authors (IBM Corp.) - initial implementation and documentation
***************************************************************************/
'use strict';
const pa = require('./yieldable-parser');
const ps = require('./yieldable-stringify');
/**
* Checks whether the provided space
* @param { string or number } space
* @return { string or number }
*/
let validateSpace = (space) => {
if (typeof space === 'number') {
space = Math.round(space);
if (space >= 1 && space <= 10)
return space;
else if (space < 1)
return 0;
else
return 10;
} else {
if (space.length <= 10)
return space;
else
return space.substr(0, 9);
}
};
/**
* Checks whether the provided intensity
* @param { number } intensity
* @return { number }
*/
let validateIntensity = (intensity) => {
intensity = Math.round(intensity);
if (intensity > 0 && intensity <= 32)
return intensity;
else if (intensity <= 0)
return 1;
else
return 32;
};
module.exports = {
/**
* Error checking and call of appropriate functions for JSON parse
* @param { primitive data types } data
* @param { function or array } reviver
* @param { number } intensity
* @param { function } callback
* @return { function } parseWrapper
*/
parseAsync(data, reviver, intensity, callback) {
const argv = arguments;
//Bring parity with the in-built parser, that takes both string and buffer
if(Buffer.isBuffer(data))
data = data.toString();
if (argv.length < 2)
throw new Error('Missing Callback');
if (typeof argv[argv.length - 1] === 'function') {
callback = argv[argv.length - 1];
reviver = null;
intensity = 1;
} else
throw new TypeError('Callback is not a function');
if (argv.length > 2) {
let i = 1;
if (typeof argv[i] === 'function')
reviver = argv[i++];
if (typeof argv[i] === 'number')
intensity = validateIntensity(argv[i]);
}
return pa.parseWrapper(data, reviver, intensity, callback);
},
/**
* Error checking and call of appropriate functions for JSON stringify API
* @param { primitive data types } data
* @param { function or array } replacer
* @param { number or string } space
* @param { number } intensity
* @param { function } callback
* @return { function } stringifyWrapper
*/
stringifyAsync(data, replacer, space, intensity, callback) {
const argv = arguments;
if (typeof argv[argv.length - 1] === 'function') {
callback = argv[argv.length - 1];
replacer = null;
intensity = 1;
} else
throw new TypeError('Callback is not a function');
if (argv.length > 2) {
let i = 1;
if (typeof argv[i] === 'function' || typeof argv[i] === 'object')
replacer = argv[i++];
if ((typeof argv[i] === 'number' || typeof argv[i] === 'string') &&
typeof argv[i++] === 'number')
space = validateSpace(argv[i++]);
if (typeof argv[i] === 'number')
intensity = validateIntensity(argv[i]);
}
return ps.stringifyWrapper(data, replacer, space, intensity, callback);
},
};