source: imaps-frontend/node_modules/core-js/modules/esnext.json.raw-json.js@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 3.2 KB
Line 
1'use strict';
2var $ = require('../internals/export');
3var FREEZING = require('../internals/freezing');
4var NATIVE_RAW_JSON = require('../internals/native-raw-json');
5var getBuiltIn = require('../internals/get-built-in');
6var call = require('../internals/function-call');
7var uncurryThis = require('../internals/function-uncurry-this');
8var isCallable = require('../internals/is-callable');
9var isRawJSON = require('../internals/is-raw-json');
10var toString = require('../internals/to-string');
11var createProperty = require('../internals/create-property');
12var parseJSONString = require('../internals/parse-json-string');
13var getReplacerFunction = require('../internals/get-json-replacer-function');
14var uid = require('../internals/uid');
15var setInternalState = require('../internals/internal-state').set;
16
17var $String = String;
18var $SyntaxError = SyntaxError;
19var parse = getBuiltIn('JSON', 'parse');
20var $stringify = getBuiltIn('JSON', 'stringify');
21var create = getBuiltIn('Object', 'create');
22var freeze = getBuiltIn('Object', 'freeze');
23var at = uncurryThis(''.charAt);
24var slice = uncurryThis(''.slice);
25var push = uncurryThis([].push);
26
27var MARK = uid();
28var MARK_LENGTH = MARK.length;
29var ERROR_MESSAGE = 'Unacceptable as raw JSON';
30
31var isWhitespace = function (it) {
32 return it === ' ' || it === '\t' || it === '\n' || it === '\r';
33};
34
35// `JSON.parse` method
36// https://tc39.es/proposal-json-parse-with-source/#sec-json.israwjson
37// https://github.com/tc39/proposal-json-parse-with-source
38$({ target: 'JSON', stat: true, forced: !NATIVE_RAW_JSON }, {
39 rawJSON: function rawJSON(text) {
40 var jsonString = toString(text);
41 if (jsonString === '' || isWhitespace(at(jsonString, 0)) || isWhitespace(at(jsonString, jsonString.length - 1))) {
42 throw new $SyntaxError(ERROR_MESSAGE);
43 }
44 var parsed = parse(jsonString);
45 if (typeof parsed == 'object' && parsed !== null) throw new $SyntaxError(ERROR_MESSAGE);
46 var obj = create(null);
47 setInternalState(obj, { type: 'RawJSON' });
48 createProperty(obj, 'rawJSON', jsonString);
49 return FREEZING ? freeze(obj) : obj;
50 }
51});
52
53// `JSON.stringify` method
54// https://tc39.es/ecma262/#sec-json.stringify
55// https://github.com/tc39/proposal-json-parse-with-source
56if ($stringify) $({ target: 'JSON', stat: true, arity: 3, forced: !NATIVE_RAW_JSON }, {
57 stringify: function stringify(text, replacer, space) {
58 var replacerFunction = getReplacerFunction(replacer);
59 var rawStrings = [];
60
61 var json = $stringify(text, function (key, value) {
62 // some old implementations (like WebKit) could pass numbers as keys
63 var v = isCallable(replacerFunction) ? call(replacerFunction, this, $String(key), value) : value;
64 return isRawJSON(v) ? MARK + (push(rawStrings, v.rawJSON) - 1) : v;
65 }, space);
66
67 if (typeof json != 'string') return json;
68
69 var result = '';
70 var length = json.length;
71
72 for (var i = 0; i < length; i++) {
73 var chr = at(json, i);
74 if (chr === '"') {
75 var end = parseJSONString(json, ++i).end - 1;
76 var string = slice(json, i, end);
77 result += slice(string, 0, MARK_LENGTH) === MARK
78 ? rawStrings[slice(string, MARK_LENGTH)]
79 : '"' + string + '"';
80 i = end;
81 } else result += chr;
82 }
83
84 return result;
85 }
86});
Note: See TracBrowser for help on using the repository browser.