Sha256: 72aa871dcdc47560a5b04a49174b20cde090581d01efe46e46ca4a711ded5948

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

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

module.exports = function (obj, opts) {
    if (!opts) opts = {};
    if (typeof opts === 'function') opts = { cmp: opts };
    var cmp = opts.cmp && (function (f) {
        return function (node) {
            return function (a, b) {
                var aobj = { key: a, value: node[a] };
                var bobj = { key: b, value: node[b] };
                return f(aobj, bobj);
            };
        };
    })(opts.cmp);
    
    return (function stringify (node) {
        if (typeof node !== 'object' || node === null) {
            return json.stringify(node);
        }
        if (isArray(node)) {
            var out = [];
            for (var i = 0; i < node.length; i++) {
                out.push(stringify(node[i]));
            }
            return '[' + out.join(',') + ']';
        }
        else {
            var keys = objectKeys(node).sort(cmp && cmp(node));
            var out = [];
            for (var i = 0; i < keys.length; i++) {
                var key = keys[i];
                out.push(stringify(key) + ':' + stringify(node[key]));
            }
            return '{' + out.join(',') + '}';
        }
    })(obj);
};

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;
};

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
minimum_viable_product-0.0.11 test/dummy/node_modules/json-stable-stringify/index.js
brwy_rails-0.0.6 test/dummy/node_modules/json-stable-stringify/index.js
brwy_rails-0.0.5 test/dummy/node_modules/json-stable-stringify/index.js
brwy_rails-0.0.4 test/dummy/node_modules/json-stable-stringify/index.js
brwy_rails-0.0.3 test/dummy/node_modules/json-stable-stringify/index.js
brwy_rails-0.0.2 test/dummy/node_modules/json-stable-stringify/index.js
brwy_rails-0.0.1 test/dummy/node_modules/json-stable-stringify/index.js