All files parse-json-bignumber.ts

51.63% Statements 111/215
47.13% Branches 74/157
70.59% Functions 12/17
51.63% Lines 111/215
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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 5121x     11x       11x 5x               11x                       11x                   11x   151x       151x 151x 151x     11x     3x   3x       3x 127x 127x   3x           3x                       3x 3x 1x   2x     2x 1x   1x         11x       3x     3x 3x 6x 3x 3x   3x                                   3x             11x   18x 6x       11x                                                       11x                                                 11x     3x   3x 3x 3x 3x       3x 3x 3x 3x 3x     3x 3x 3x 3x 3x                 11x   6x 6x   3x               3x       11x     11x                                     14x 14x   2x 2x                             16x   16x 16x       16x           16x 2x   14x   4x           16x           16x   8x 2x   6x                                                     8x           8x 8x       8x                                               8x                                     8x 8x 8x 8x 8x                         8x         8x 8x       11x                 8x 8x         8x             8x             8x 8x                 8x         11x       3x 3x 3x 3x 3x 3x     3x                                   11x     1x                
const create = function <T>(options?: IOptions<T>) {
    'use strict';
 
    var _options = {
        'strict': false,  // not being strict means do not generate syntax errors for "duplicate key"
    };
 
    if (options !== undefined && options !== null) {
        Iif (options.strict === true) {
            _options.strict = true;
        }
    }
 
 
    var at,     // The index of the current character
        ch,     // The current character
        escapee = {
            '"': '"',
            '\\': '\\',
            '/': '/',
            b: '\b',
            f: '\f',
            n: '\n',
            r: '\r',
            t: '\t'
        },
        text,
 
        error = function (m) {
 
            throw {
                name: 'SyntaxError',
                message: m,
                at: at,
                text: text
            };
        },
 
        next = function (c?) {
 
            Iif (c && c !== ch) {
                error('Expected \'' + c + '\' instead of \'' + ch + '\'');
            }
 
            ch = text.charAt(at);
            at += 1;
            return ch;
        },
 
        number = function () {
 
            var number,
                string = '';
 
            Iif (ch === '-') {
                string = '-';
                next('-');
            }
            while (ch >= '0' && ch <= '9') {
                string += ch;
                next();
            }
            Iif (ch === '.') {
                string += '.';
                while (next() && ch >= '0' && ch <= '9') {
                    string += ch;
                }
            }
            Iif (ch === 'e' || ch === 'E') {
                string += ch;
                next();
                if (ch === '-' || ch === '+') {
                    string += ch;
                    next();
                }
                while (ch >= '0' && ch <= '9') {
                    string += ch;
                    next();
                }
            }
            number = +string;
            if (options && options.parse) {
                return options.parse(string)
            }
            Iif (!isFinite(number)) {
                error('Bad number');
            } else {
                if (string.length > 15) {
                    return string;
                } else {
                    return number;
                }
            }
        },
 
        string = function () {
 
            var hex,
                i,
                string = '',
                uffff;
 
            Eif (ch === '"') {
                while (next()) {
                    if (ch === '"') {
                        next();
                        return string;
                    }
                    Iif (ch === '\\') {
                        next();
                        if (ch === 'u') {
                            uffff = 0;
                            for (i = 0; i < 4; i += 1) {
                                hex = parseInt(next(), 16);
                                if (!isFinite(hex)) {
                                    break;
                                }
                                uffff = uffff * 16 + hex;
                            }
                            string += String.fromCharCode(uffff);
                        } else if (typeof escapee[ch] === 'string') {
                            string += escapee[ch];
                        } else {
                            break;
                        }
                    } else {
                        string += ch;
                    }
                }
            }
            error('Bad string');
        },
 
        white = function () {
 
            while (ch && ch <= ' ') {
                next();
            }
        },
 
        word = function () {
 
            switch (ch) {
                case 't':
                    next('t');
                    next('r');
                    next('u');
                    next('e');
                    return true;
                case 'f':
                    next('f');
                    next('a');
                    next('l');
                    next('s');
                    next('e');
                    return false;
                case 'n':
                    next('n');
                    next('u');
                    next('l');
                    next('l');
                    return null;
            }
            error('Unexpected \'' + ch + '\'');
        },
 
        value,  // Place holder for the value function.
 
        array = function () {
 
            var array = [];
 
            if (ch === '[') {
                next('[');
                white();
                if (ch === ']') {
                    next(']');
                    return array;   // empty array
                }
                while (ch) {
                    array.push(value());
                    white();
                    if (ch === ']') {
                        next(']');
                        return array;
                    }
                    next(',');
                    white();
                }
            }
            error('Bad array');
        },
 
        object = function () {
 
            var key,
                object = {};
 
            Eif (ch === '{') {
                next('{');
                white();
                Iif (ch === '}') {
                    next('}');
                    return object;   // empty object
                }
                while (ch) {
                    key = string();
                    white();
                    next(':');
                    Iif (_options.strict === true && Object.hasOwnProperty.call(object, key)) {
                        error('Duplicate key "' + key + '"');
                    }
                    object[key] = value();
                    white();
                    Eif (ch === '}') {
                        next('}');
                        return object;
                    }
                    next(',');
                    white();
                }
            }
            error('Bad object');
        };
 
    value = function () {
 
        white();
        switch (ch) {
            case '{':
                return object();
            case '[':
                return array();
            case '"':
                return string();
            case '-':
                return number();
            default:
                return ch >= '0' && ch <= '9' ? number() : word();
        }
    };
 
    var rx_escapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
    var gap;
    var indent;
    var meta = { // table of character substitutions
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"': '\\"',
        '\\': '\\\\'
    };
    var rep;
 
 
    function quote(string) {
 
        // If the string contains no control characters, no quote characters, and no
        // backslash characters, then we can safely slap some quotes around it.
        // Otherwise we must also replace the offending characters with safe escape
        // sequences.
 
        rx_escapable.lastIndex = 0;
        return rx_escapable.test(string) ?
            '"' + string.replace(rx_escapable, function (a) {
                var c = meta[a];
                return typeof c === 'string' ?
                    c :
                    '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4);
            }) + '"' :
            '"' + string + '"';
    }
 
    function str(key, holder) {
 
        // Produce a string from holder[key].
 
        var i; // The loop counter.
        var k; // The member key.
        var v; // The member value.
        var length;
        var mind = gap;
        var partial;
        var value = holder[key];
        var isBigNumber = options && options.isInstance && options.isInstance(value);
 
        // Check for NaN and Infinity
 
        Iif (isBigNumber && !value.isFinite()) {
            value = null;
        }
 
        // If the value has a toJSON method, call it to obtain a replacement value.
 
        if (isBigNumber) {
            value = options.stringify(value);
        }
        else if (value && typeof value === 'object' &&
            typeof value.toJSON === 'function') {
            value = value.toJSON(key);
        }
 
        // If we were called with a replacer function, then call the replacer to
        // obtain a replacement value.
 
        Iif (typeof rep === 'function') {
            value = rep.call(holder, key, value);
        }
 
        // What happens next depends on the value's type.
 
        switch (typeof value) {
            case 'string':
                if (isBigNumber) {
                    return value;
                } else {
                    return quote(value);
                }
 
            case 'number':
 
                // JSON numbers must be finite. Encode non-finite numbers as null.
 
                return isFinite(value) ?
                    String(value) :
                    'null';
 
            case 'boolean':
 
                // If the value is a boolean or null, convert it to a string. Note:
                // typeof null does not produce "null". The case is included here in
                // the remote chance that this gets fixed someday.
 
                return String(value);
 
            // If the type is "object", we might be dealing with an object or an array or
            // null.
 
            case 'object':
 
                // Due to a specification blunder in ECMAScript, typeof null is "object",
                // so watch out for that case.
 
                Iif (!value) {
                    return 'null';
                }
 
                // Make an array to hold the partial results of stringifying this object value.
 
                gap += indent;
                partial = [];
 
                // Is the value an array?
 
                Iif (Object.prototype.toString.apply(value) === '[object Array]') {
 
                    // The value is an array. Stringify every element. Use null as a placeholder
                    // for non-JSON values.
 
                    length = value.length;
                    for (i = 0; i < length; i += 1) {
                        partial[i] = str(i, value) || 'null';
                    }
 
                    // Join all of the elements together, separated with commas, and wrap them in
                    // brackets.
 
                    v = partial.length === 0 ?
                        '[]' :
                        gap ?
                            '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' :
                            '[' + partial.join(',') + ']';
                    gap = mind;
                    return v;
                }
 
                // If the replacer is an array, use it to select the members to be stringified.
 
                Iif (rep && typeof rep === 'object') {
                    length = rep.length;
                    for (i = 0; i < length; i += 1) {
                        if (typeof rep[i] === 'string') {
                            k = rep[i];
                            v = str(k, value);
                            if (v) {
                                partial.push(quote(k) + (
                                    gap ?
                                        ': ' :
                                        ':'
                                ) + v);
                            }
                        }
                    }
                } else {
 
                    // Otherwise, iterate through all of the keys in the object.
 
                    for (k in value) {
                        Eif (Object.prototype.hasOwnProperty.call(value, k)) {
                            v = str(k, value);
                            Eif (v) {
                                partial.push(quote(k) + (
                                    gap ?
                                        ': ' :
                                        ':'
                                ) + v);
                            }
                        }
                    }
                }
 
                // Join all of the member texts together, separated with commas,
                // and wrap them in braces.
 
                v = partial.length === 0 ?
                    '{}' :
                    gap ?
                        '{\n' + gap + partial.join(',\n' + gap) + '\n' + mind + '}' :
                        '{' + partial.join(',') + '}';
                gap = mind;
                return v;
        }
    }
 
    const stringify = (value, replacer, space) => {
 
        // The stringify method takes a value and an optional replacer, and an optional
        // space parameter, and returns a JSON text. The replacer can be a function
        // that can replace values, or an array of strings that will select the keys.
        // A default replacer method can be provided. Use of the space parameter can
        // produce text that is more easily readable.
 
        var i;
        gap = '';
        indent = '';
 
        // If the space parameter is a number, make an indent string containing that
        // many spaces.
 
        Iif (typeof space === 'number') {
            for (i = 0; i < space; i += 1) {
                indent += ' ';
            }
 
            // If the space parameter is a string, it will be used as the indent string.
 
        } else Iif (typeof space === 'string') {
            indent = space;
        }
 
        // If there is a replacer, it must be a function or an array.
        // Otherwise, throw an error.
 
        rep = replacer;
        Iif (replacer && typeof replacer !== 'function' &&
            (typeof replacer !== 'object' ||
                typeof replacer.length !== 'number')) {
            throw new Error('JSON.stringify');
        }
 
        // Make a fake root object containing our value under the key of "".
        // Return the result of stringifying the value.
 
        return str('', {
            '': value
        });
    };
 
    const parse = function (source, reviver?) {
 
        var result;
 
        text = source + '';
        at = 0;
        ch = ' ';
        result = value();
        white();
        Iif (ch) {
            error('Syntax error');
        }
        return typeof reviver === 'function'
            ? (function walk(holder, key) {
                var k, v, value = holder[key];
                if (value && typeof value === 'object') {
                    Object.keys(value).forEach(function (k) {
                        v = walk(value, k);
                        if (v !== undefined) {
                            value[k] = v;
                        } else {
                            delete value[k];
                        }
                    });
                }
                return reviver.call(holder, key, value);
            }({'': result}, ''))
            : result;
    };
 
    return {parse, stringify};
};
 
export = create;
 
interface IOptions<T> {
    strict: boolean;
    parse: (long: string) => T;
    stringify: (long: T) => string;
    isInstance: (some: any) => some is T;
}