Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
json.js106 linesDownload Raw Back to diff
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3    var extendStatics = function (d, b) {
4        extendStatics = Object.setPrototypeOf ||
5            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7        return extendStatics(d, b);
8    };
9    return function (d, b) {
10        if (typeof b !== "function" && b !== null)
11            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12        extendStatics(d, b);
13        function __() { this.constructor = d; }
14        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15    };
16})();
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.jsonDiff = void 0;
19exports.diffJson = diffJson;
20exports.canonicalize = canonicalize;
21var base_js_1 = require("./base.js");
22var line_js_1 = require("./line.js");
23var JsonDiff = /** @class */ (function (_super) {
24    __extends(JsonDiff, _super);
25    function JsonDiff() {
26        var _this = _super !== null && _super.apply(this, arguments) || this;
27        _this.tokenize = line_js_1.tokenize;
28        return _this;
29    }
30    Object.defineProperty(JsonDiff.prototype, "useLongestToken", {
31        get: function () {
32            // Discriminate between two lines of pretty-printed, serialized JSON where one of them has a
33            // dangling comma and the other doesn't. Turns out including the dangling comma yields the nicest output:
34            return true;
35        },
36        enumerable: false,
37        configurable: true
38    });
39    JsonDiff.prototype.castInput = function (value, options) {
40        var undefinedReplacement = options.undefinedReplacement, _a = options.stringifyReplacer, stringifyReplacer = _a === void 0 ? function (k, v) { return typeof v === 'undefined' ? undefinedReplacement : v; } : _a;
41        return typeof value === 'string' ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), null, '  ');
42    };
43    JsonDiff.prototype.equals = function (left, right, options) {
44        return _super.prototype.equals.call(this, left.replace(/,([\r\n])/g, '$1'), right.replace(/,([\r\n])/g, '$1'), options);
45    };
46    return JsonDiff;
47}(base_js_1.default));
48exports.jsonDiff = new JsonDiff();
49function diffJson(oldStr, newStr, options) {
50    return exports.jsonDiff.diff(oldStr, newStr, options);
51}
52// This function handles the presence of circular references by bailing out when encountering an
53// object that is already on the "stack" of items being processed. Accepts an optional replacer
54function canonicalize(obj, stack, replacementStack, replacer, key) {
55    stack = stack || [];
56    replacementStack = replacementStack || [];
57    if (replacer) {
58        obj = replacer(key === undefined ? '' : key, obj);
59    }
60    var i;
61    for (i = 0; i < stack.length; i += 1) {
62        if (stack[i] === obj) {
63            return replacementStack[i];
64        }
65    }
66    var canonicalizedObj;
67    if ('[object Array]' === Object.prototype.toString.call(obj)) {
68        stack.push(obj);
69        canonicalizedObj = new Array(obj.length);
70        replacementStack.push(canonicalizedObj);
71        for (i = 0; i < obj.length; i += 1) {
72            canonicalizedObj[i] = canonicalize(obj[i], stack, replacementStack, replacer, String(i));
73        }
74        stack.pop();
75        replacementStack.pop();
76        return canonicalizedObj;
77    }
78    if (obj && obj.toJSON) {
79        obj = obj.toJSON();
80    }
81    if (typeof obj === 'object' && obj !== null) {
82        stack.push(obj);
83        canonicalizedObj = {};
84        replacementStack.push(canonicalizedObj);
85        var sortedKeys = [];
86        var key_1;
87        for (key_1 in obj) {
88            /* istanbul ignore else */
89            if (Object.prototype.hasOwnProperty.call(obj, key_1)) {
90                sortedKeys.push(key_1);
91            }
92        }
93        sortedKeys.sort();
94        for (i = 0; i < sortedKeys.length; i += 1) {
95            key_1 = sortedKeys[i];
96            canonicalizedObj[key_1] = canonicalize(obj[key_1], stack, replacementStack, replacer, key_1);
97        }
98        stack.pop();
99        replacementStack.pop();
100    }
101    else {
102        canonicalizedObj = obj;
103    }
104    return canonicalizedObj;
105}
106 
codekingpro/portable-devtools · Team Ai