node_modules/json-diff/lib/index.js

Maintainability

51.65

Lines of code

222

Created with Raphaël 2.1.002550751002015-1-52014-12-42014-12-3

2015-5-18
Maintainability: 51.65

Created with Raphaël 2.1.00751502253002015-1-52014-12-42014-12-3

2015-5-18
Lines of Code: 222

Difficulty

107.80

Estimated Errors

2.63

Function weight

By Complexity

Created with Raphaël 2.1.0<anonymous>27

By SLOC

Created with Raphaël 2.1.0<anonymous>222
1
// Generated by IcedCoffeeScript 1.3.3f
2
(function() {
3
  var SequenceMatcher, arrayDiff, colorize, descalarize, diff, diffScore, diffString, diffWithScore, extendedTypeOf, findMatchingObject, isScalar, isScalarized, objectDiff, scalarize,
4
    __hasProp = {}.hasOwnProperty;
5
 
6
  SequenceMatcher = require('difflib').SequenceMatcher;
7
 
8
  extendedTypeOf = require('./util').extendedTypeOf;
9
 
10
  colorize = require('./colorize').colorize;
11
 
12
  isScalar = function(obj) {
13
    return typeof obj !== 'object';
14
  };
15
 
16
  objectDiff = function(obj1, obj2) {
17
    var change, key, keys1, keys2, result, score, subscore, value1, value2, _ref, _ref1;
18
    result = {};
19
    score = 0;
20
    keys1 = Object.keys(obj1);
21
    keys2 = Object.keys(obj2);
22
    for (key in obj1) {
23
      if (!__hasProp.call(obj1, key)) continue;
24
      value1 = obj1[key];
25
      if (!(!(key in obj2))) continue;
26
      result["" + key + "__deleted"] = value1;
27
      score -= 30;
28
    }
29
    for (key in obj2) {
30
      if (!__hasProp.call(obj2, key)) continue;
31
      value2 = obj2[key];
32
      if (!(!(key in obj1))) continue;
33
      result["" + key + "__added"] = value2;
34
      score -= 30;
35
    }
36
    for (key in obj1) {
37
      if (!__hasProp.call(obj1, key)) continue;
38
      value1 = obj1[key];
39
      if (!(key in obj2)) continue;
40
      score += 20;
41
      value2 = obj2[key];
42
      _ref = diffWithScore(value1, value2), subscore = _ref[0], change = _ref[1];
Column: 80 "Expected an assignment or function call and instead saw an expression."
43
      if (change) result[key] = change;
44
      score += Math.min(20, Math.max(-10, subscore / 5));
45
    }
46
    if (Object.keys(result).length === 0) {
47
      _ref1 = [100 * Object.keys(obj1).length, void 0], score = _ref1[0], result = _ref1[1];
Column: 91 "Expected an assignment or function call and instead saw an expression."
48
    } else {
49
      score = Math.max(0, score);
50
    }
51
    return [score, result];
52
  };
53
 
54
  findMatchingObject = function(item, fuzzyOriginals) {
55
    var bestMatch, candidate, key, score;
56
    bestMatch = null;
57
    for (key in fuzzyOriginals) {
58
      if (!__hasProp.call(fuzzyOriginals, key)) continue;
59
      candidate = fuzzyOriginals[key];
60
      if (key !== '__next') {
61
        if (extendedTypeOf(item) === extendedTypeOf(candidate)) {
62
          score = diffScore(item, candidate);
63
          if (!bestMatch || score > bestMatch.score) {
64
            bestMatch = {
65
              score: score,
66
              key: key
67
            };
68
          }
69
        }
70
      }
71
    }
72
    return bestMatch;
73
  };
74
 
75
  scalarize = function(array, originals, fuzzyOriginals) {
76
    var bestMatch, item, proxy, _i, _len, _results;
77
    _results = [];
78
    for (_i = 0, _len = array.length; _i < _len; _i++) {
79
      item = array[_i];
80
      if (isScalar(item)) {
81
        _results.push(item);
82
      } else if (fuzzyOriginals && (bestMatch = findMatchingObject(item, fuzzyOriginals)) && bestMatch.score > 40) {
83
        originals[bestMatch.key] = item;
84
        _results.push(bestMatch.key);
85
      } else {
86
        proxy = "__$!SCALAR" + originals.__next++;
87
        originals[proxy] = item;
88
        _results.push(proxy);
89
      }
90
    }
91
    return _results;
92
  };
93
 
94
  isScalarized = function(item, originals) {
95
    return (typeof item === 'string') && (item in originals);
96
  };
97
 
98
  descalarize = function(item, originals) {
99
    if (isScalarized(item, originals)) {
100
      return originals[item];
101
    } else {
102
      return item;
103
    }
104
  };
105
 
106
  arrayDiff = function(obj1, obj2, stats) {
107
    var allEqual, change, i, i1, i2, item, item1, item2, j, j1, j2, op, opcodes, originals1, originals2, result, score, seq1, seq2, _i, _j, _k, _l, _len, _m, _n, _ref;
108
    originals1 = {
109
      __next: 1
110
    };
111
    seq1 = scalarize(obj1, originals1);
112
    originals2 = {
113
      __next: originals1.__next
114
    };
115
    seq2 = scalarize(obj2, originals2, originals1);
116
    opcodes = new SequenceMatcher(null, seq1, seq2).getOpcodes();
117
    result = [];
118
    score = 0;
119
    allEqual = true;
120
    for (_i = 0, _len = opcodes.length; _i < _len; _i++) {
121
      _ref = opcodes[_i], op = _ref[0], i1 = _ref[1], i2 = _ref[2], j1 = _ref[3], j2 = _ref[4];
Column: 94 "Expected an assignment or function call and instead saw an expression."
122
      if (op !== 'equal') allEqual = false;
123
      switch (op) {
124
        case 'equal':
125
          for (i = _j = i1; i1 <= i2 ? _j < i2 : _j > i2; i = i1 <= i2 ? ++_j : --_j) {
126
            item = seq1[i];
127
            if (isScalarized(item, originals1)) {
128
              if (!isScalarized(item, originals2)) {
129
                throw new AssertionError("internal bug: isScalarized(item, originals1) != isScalarized(item, originals2) for item " + (JSON.stringify(item)));
130
              }
131
              item1 = descalarize(item, originals1);
132
              item2 = descalarize(item, originals2);
133
              change = diff(item1, item2);
134
              if (change) {
135
                result.push(['~', change]);
136
                allEqual = false;
137
              } else {
138
                result.push([' ']);
139
              }
140
            } else {
141
              result.push([' ', item]);
142
            }
143
            score += 10;
144
          }
145
          break;
146
        case 'delete':
147
          for (i = _k = i1; i1 <= i2 ? _k < i2 : _k > i2; i = i1 <= i2 ? ++_k : --_k) {
148
            result.push(['-', descalarize(seq1[i], originals1)]);
149
            score -= 5;
150
          }
151
          break;
152
        case 'insert':
153
          for (j = _l = j1; j1 <= j2 ? _l < j2 : _l > j2; j = j1 <= j2 ? ++_l : --_l) {
154
            result.push(['+', descalarize(seq2[j], originals2)]);
155
            score -= 5;
156
          }
157
          break;
158
        case 'replace':
159
          for (i = _m = i1; i1 <= i2 ? _m < i2 : _m > i2; i = i1 <= i2 ? ++_m : --_m) {
160
            result.push(['-', descalarize(seq1[i], originals1)]);
161
            score -= 5;
162
          }
163
          for (j = _n = j1; j1 <= j2 ? _n < j2 : _n > j2; j = j1 <= j2 ? ++_n : --_n) {
164
            result.push(['+', descalarize(seq2[j], originals2)]);
165
            score -= 5;
166
          }
167
      }
168
    }
169
    if (allEqual || (opcodes.length === 0)) {
170
      result = void 0;
171
      score = 100;
172
    } else {
173
      score = Math.max(0, score);
174
    }
175
    return [score, result];
176
  };
177
 
178
  diffWithScore = function(obj1, obj2) {
179
    var type1, type2;
180
    type1 = extendedTypeOf(obj1);
181
    type2 = extendedTypeOf(obj2);
182
    if (type1 === type2) {
183
      switch (type1) {
184
        case 'object':
185
          return objectDiff(obj1, obj2);
186
        case 'array':
187
          return arrayDiff(obj1, obj2);
188
      }
189
    }
190
    if (obj1 !== obj2) {
191
      return [
192
        0, {
193
          __old: obj1,
194
          __new: obj2
195
        }
196
      ];
197
    } else {
198
      return [100, void 0];
199
    }
200
  };
201
 
202
  diff = function(obj1, obj2) {
203
    var change, score, _ref;
204
    _ref = diffWithScore(obj1, obj2), score = _ref[0], change = _ref[1];
Column: 71 "Expected an assignment or function call and instead saw an expression."
205
    return change;
206
  };
207
 
208
  diffScore = function(obj1, obj2) {
209
    var change, score, _ref;
210
    _ref = diffWithScore(obj1, obj2), score = _ref[0], change = _ref[1];
Column: 71 "Expected an assignment or function call and instead saw an expression."
211
    return score;
212
  };
213
 
214
  diffString = function(obj1, obj2, options) {
215
    return colorize(diff(obj1, obj2), options);
216
  };
217
 
218
  module.exports = {
219
    diff: diff,
220
    diffString: diffString
221
  };
222
 
223
}).call(this);