Skip to content

Commit

Permalink
[eslint] more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 25, 2022
1 parent c97e78c commit c162117
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 69 deletions.
18 changes: 4 additions & 14 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,19 @@
"extends": "@ljharb",

"rules": {
"brace-style": 1,
"consistent-return": 1,
"curly": 1,
"function-paren-newline": 1,
"max-lines-per-function": 0,
"max-params": 1,
"max-statements-per-line": 1,
"max-statements-per-line": [2, { "max": 2 }],
"no-continue": 1,
"no-else-return": 1,
"no-extra-parens": 1,
"no-extra-semi": 1,
"no-negated-condition": 1,
"no-param-reassign": 1,
"no-redeclare": 1,
"no-restricted-syntax": 1,
"no-unused-vars": 1,
"no-use-before-define": 1,
"object-curly-newline": 1,
"operator-linebreak": 1,
"semi": 1,
"object-curly-newline": 0,
"max-statements": 1,
"operator-linebreak": 0,
"sort-keys": 1,
"strict": 1,
"wrap-iife": 1,
},

"overrides": [
Expand Down
2 changes: 2 additions & 0 deletions example/key_cmp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var stringify = require('../');

var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
Expand Down
2 changes: 2 additions & 0 deletions example/nested.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var stringify = require('../');

var obj = { c: 8, b: [{ z: 6, y: 5, x: 4 }, 7], a: 3 };
Expand Down
2 changes: 2 additions & 0 deletions example/str.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var stringify = require('../');

var obj = { c: 6, b: [4, 5], a: 3 };
Expand Down
2 changes: 2 additions & 0 deletions example/value_cmp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var stringify = require('../');

var obj = { d: 6, c: 5, b: [{ z: 3, y: 2, x: 1 }, 9], a: 10 };
Expand Down
75 changes: 38 additions & 37 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
'use strict';

var json = typeof JSON !== 'undefined' ? JSON : require('jsonify');

var isArray = Array.isArray || function (x) {
return {}.toString.call(x) === '[object Array]';
};

var objectKeys = Object.keys || function (obj) {
var has = Object.prototype.hasOwnProperty || function () { return true; };
var keys = [];
for (var key in obj) {
if (has.call(obj, key)) { keys.push(key); }
}
return keys;
};

module.exports = function (obj, opts) {
if (!opts) opts = {};
if (typeof opts === 'function') opts = { cmp: opts };
if (!opts) { opts = {}; }
if (typeof opts === 'function') { opts = { cmp: opts }; }
var space = opts.space || '';
if (typeof space === 'number') space = Array(space + 1).join(' ');
var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false;
if (typeof space === 'number') { space = Array(space + 1).join(' '); }
var cycles = typeof opts.cycles === 'boolean' ? opts.cycles : false;
var replacer = opts.replacer || function (key, value) { return value; };

var cmp = opts.cmp && (function (f) {
Expand All @@ -16,11 +31,11 @@ module.exports = function (obj, opts) {
return f(aobj, bobj);
};
};
})(opts.cmp);
}(opts.cmp));

var seen = [];
return (function stringify(parent, key, node, level) {
var indent = space ? ('\n' + new Array(level + 1).join(space)) : '';
var indent = space ? '\n' + new Array(level + 1).join(space) : '';
var colonSeparator = space ? ': ' : ':';

if (node && node.toJSON && typeof node.toJSON === 'function') {
Expand All @@ -43,42 +58,28 @@ module.exports = function (obj, opts) {
}
return '[' + out.join(',') + indent + ']';
}
else {
if (seen.indexOf(node) !== -1) {
if (cycles) return json.stringify('__cycle__');
throw new TypeError('Converting circular structure to JSON');
}
else seen.push(node);

var keys = objectKeys(node).sort(cmp && cmp(node));
var out = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = stringify(node, key, node[key], level + 1);
if (seen.indexOf(node) !== -1) {
if (cycles) { return json.stringify('__cycle__'); }
throw new TypeError('Converting circular structure to JSON');
} else { seen.push(node); }

if (!value) continue;
var keys = objectKeys(node).sort(cmp && cmp(node));
var out = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = stringify(node, key, node[key], level + 1);

var keyValue = json.stringify(key)
if (!value) { continue; }

var keyValue = json.stringify(key)
+ colonSeparator
+ value;
;
out.push(indent + space + keyValue);
}
seen.splice(seen.indexOf(node), 1);
return '{' + out.join(',') + indent + '}';
}
})({ '': obj }, '', obj, 0);
};

var isArray = Array.isArray || function (x) {
return {}.toString.call(x) === '[object Array]';
};
out.push(indent + space + keyValue);
}
seen.splice(seen.indexOf(node), 1);
return '{' + out.join(',') + indent + '}';

var objectKeys = Object.keys || function (obj) {
var has = Object.prototype.hasOwnProperty || function () { return true };
var keys = [];
for (var key in obj) {
if (has.call(obj, key)) keys.push(key);
}
return keys;
}({ '': obj }, '', obj, 0));
};
2 changes: 2 additions & 0 deletions test/cmp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var test = require('tape');
var stringify = require('../');

Expand Down
2 changes: 2 additions & 0 deletions test/nested.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var test = require('tape');
var stringify = require('../');

Expand Down
20 changes: 11 additions & 9 deletions test/replacer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

var test = require('tape');
var stringify = require('../');

test('replace root', function (t) {
t.plan(1);

var obj = { a: 1, b: 2, c: false };
var replacer = function (key, value) { return 'one'; };
var replacer = function () { return 'one'; };

t.equal(stringify(obj, { replacer: replacer }), '"one"');
});
Expand All @@ -15,8 +17,8 @@ test('replace numbers', function (t) {

var obj = { a: 1, b: 2, c: false };
var replacer = function (key, value) {
if (value === 1) return 'one';
if (value === 2) return 'two';
if (value === 1) { return 'one'; }
if (value === 2) { return 'two'; }
return value;
};

Expand All @@ -28,8 +30,8 @@ test('replace with object', function (t) {

var obj = { a: 1, b: 2, c: false };
var replacer = function (key, value) {
if (key === 'b') return { d: 1 };
if (value === 1) return 'one';
if (key === 'b') { return { d: 1 }; }
if (value === 1) { return 'one'; }
return value;
};

Expand All @@ -41,7 +43,7 @@ test('replace with undefined', function (t) {

var obj = { a: 1, b: 2, c: false };
var replacer = function (key, value) {
if (value === false) return;
if (value === false) { return; }
return value;
};

Expand All @@ -53,7 +55,7 @@ test('replace with array', function (t) {

var obj = { a: 1, b: 2, c: false };
var replacer = function (key, value) {
if (key === 'b') return ['one', 'two'];
if (key === 'b') { return ['one', 'two']; }
return value;
};

Expand All @@ -65,8 +67,8 @@ test('replace array item', function (t) {

var obj = { a: 1, b: 2, c: [1, 2] };
var replacer = function (key, value) {
if (value === 1) return 'one';
if (value === 2) return 'two';
if (value === 1) { return 'one'; }
if (value === 2) { return 'two'; }
return value;
};

Expand Down
31 changes: 22 additions & 9 deletions test/space.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
'use strict';

var test = require('tape');
var stringify = require('../');

test('space parameter', function (t) {
t.plan(1);
var obj = { one: 1, two: 2 };
t.equal(stringify(obj, { space: ' ' }), ''
+ '{\n'
+ ' "one": 1,\n'
+ ' "two": 2\n'
+ '}'
t.equal(
stringify(obj, { space: ' ' }),
''
+ '{\n'
+ ' "one": 1,\n'
+ ' "two": 2\n'
+ '}'
);
});

test('space parameter (with tabs)', function (t) {
t.plan(1);
var obj = { one: 1, two: 2 };
t.equal(stringify(obj, { space: '\t' }), ''
t.equal(
stringify(obj, { space: '\t' }),
''
+ '{\n'
+ '\t"one": 1,\n'
+ '\t"two": 2\n'
Expand All @@ -26,7 +32,9 @@ test('space parameter (with tabs)', function (t) {
test('space parameter (with a number)', function (t) {
t.plan(1);
var obj = { one: 1, two: 2 };
t.equal(stringify(obj, { space: 3 }), ''
t.equal(
stringify(obj, { space: 3 }),
''
+ '{\n'
+ ' "one": 1,\n'
+ ' "two": 2\n'
Expand All @@ -37,7 +45,9 @@ test('space parameter (with a number)', function (t) {
test('space parameter (nested objects)', function (t) {
t.plan(1);
var obj = { one: 1, two: { b: 4, a: [2, 3] } };
t.equal(stringify(obj, { space: ' ' }), ''
t.equal(
stringify(obj, { space: ' ' }),
''
+ '{\n'
+ ' "one": 1,\n'
+ ' "two": {\n'
Expand All @@ -55,5 +65,8 @@ test('space parameter (same as native)', function (t) {
t.plan(1);
// for this test, properties need to be in alphabetical order
var obj = { one: 1, two: { a: [2, 3], b: 4 } };
t.equal(stringify(obj, { space: ' ' }), JSON.stringify(obj, null, ' '));
t.equal(
stringify(obj, { space: ' ' }),
JSON.stringify(obj, null, ' ')
);
});
2 changes: 2 additions & 0 deletions test/str.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var test = require('tape');
var stringify = require('../');

Expand Down
2 changes: 2 additions & 0 deletions test/to-json.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var test = require('tape');
var stringify = require('../');

Expand Down

0 comments on commit c162117

Please sign in to comment.