├── .gitignore ├── .travis.yml ├── .editorconfig ├── lib └── sorted-object.js ├── package.json ├── README.md ├── test └── tests.js ├── LICENSE.txt └── .eslintrc.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /npm-debug.log 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - stable 4 | script: 5 | npm run lint && npm test 6 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | trim_trailing_whitespace = true 7 | charset = utf-8 8 | indent_style = space 9 | indent_size = 4 10 | -------------------------------------------------------------------------------- /lib/sorted-object.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | module.exports = function (input) { 4 | var output = {}; 5 | 6 | Object.keys(input).sort().forEach(function (key) { 7 | output[key] = input[key]; 8 | }); 9 | 10 | return output; 11 | }; 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sorted-object", 3 | "description": "Returns a copy of an object with its keys sorted", 4 | "keywords": [ 5 | "sort", 6 | "keys", 7 | "object" 8 | ], 9 | "version": "2.0.1", 10 | "author": "Domenic Denicola (https://domenic.me/)", 11 | "license": "(WTFPL OR MIT)", 12 | "repository": "domenic/sorted-object", 13 | "main": "lib/sorted-object.js", 14 | "files": [ 15 | "lib/" 16 | ], 17 | "scripts": { 18 | "test": "tape test/tests.js", 19 | "lint": "eslint ." 20 | }, 21 | "devDependencies": { 22 | "eslint": "^2.4.0", 23 | "tape": "^4.5.1" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Get a Version of an Object with Sorted Keys 2 | 3 | Although objects in JavaScript are theoretically unsorted, in practice most engines use insertion order—at least, ignoring numeric keys. This manifests itself most prominently when dealing with an object's JSON serialization. 4 | 5 | So, for example, you might be trying to serialize some object to a JSON file. But every time you write it, it ends up being output in a different order, depending on how you created it in the first place! This makes for some ugly diffs. 6 | 7 | **sorted-object** gives you the answer. Just use this package to create a version of your object with its keys sorted before serializing, and you'll get a consistent order every time. 8 | 9 | ```js 10 | var sortedObject = require("sorted-object"); 11 | 12 | var objectToSerialize = generateStuffNondeterministically(); 13 | 14 | // Before: 15 | fs.writeFileSync("dest.json", JSON.stringify(objectToSerialize)); 16 | 17 | // After: 18 | var sortedVersion = sortedObject(objectToSerialize); 19 | fs.writeFileSync("dest.json", JSON.stringify(sortedVersion)); 20 | ``` 21 | -------------------------------------------------------------------------------- /test/tests.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var test = require("tape"); 4 | var sortedObject = require(".."); 5 | 6 | test("does not return the same object", function (t) { 7 | var input = {}; 8 | 9 | t.notEqual(sortedObject(input), input); 10 | t.end(); 11 | }); 12 | 13 | test("works for empty objects", function (t) { 14 | t.deepEqual(sortedObject({}), {}); 15 | t.end(); 16 | }); 17 | 18 | test("returns an object with Object.prototype as its prototype", function (t) { 19 | t.equal(Object.getPrototypeOf(sortedObject({})), Object.prototype); 20 | t.end(); 21 | }); 22 | 23 | test("does not disturb already-sorted object", function (t) { 24 | var input = { a: 1, b: 2, c: 3 }; 25 | var output = sortedObject(input); 26 | var desired = { a: 1, b: 2, c: 3 }; 27 | 28 | t.deepEqual(output, desired); 29 | t.end(); 30 | }); 31 | 32 | test("sorts out-of-order object", function (t) { 33 | var input = { c: 3, b: 2, a: 1 }; 34 | var output = sortedObject(input); 35 | var desired = { a: 1, b: 2, c: 3 }; 36 | 37 | t.deepEqual(output, desired); 38 | t.end(); 39 | }); 40 | 41 | test("sorts case-sensitively", function (t) { 42 | var input = { hello: 3, Hi: 2, HELLO: 1, hi: 4 }; 43 | var output = sortedObject(input); 44 | var desired = { HELLO: 1, Hi: 2, hello: 3, hi: 4 }; 45 | 46 | t.deepEqual(output, desired); 47 | t.end(); 48 | }); 49 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Dual licensed under WTFPL and MIT: 2 | 3 | --- 4 | 5 | Copyright © 2014–2016 Domenic Denicola 6 | 7 | This work is free. You can redistribute it and/or modify it under the 8 | terms of the Do What The Fuck You Want To Public License, Version 2, 9 | as published by Sam Hocevar. See below for more details. 10 | 11 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 12 | Version 2, December 2004 13 | 14 | Copyright (C) 2004 Sam Hocevar 15 | 16 | Everyone is permitted to copy and distribute verbatim or modified 17 | copies of this license document, and changing it is allowed as long 18 | as the name is changed. 19 | 20 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 21 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 22 | 23 | 0. You just DO WHAT THE FUCK YOU WANT TO. 24 | 25 | --- 26 | 27 | The MIT License (MIT) 28 | 29 | Copyright © 2014–2016 Domenic Denicola 30 | 31 | Permission is hereby granted, free of charge, to any person obtaining a copy 32 | of this software and associated documentation files (the "Software"), to deal 33 | in the Software without restriction, including without limitation the rights 34 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 35 | copies of the Software, and to permit persons to whom the Software is 36 | furnished to do so, subject to the following conditions: 37 | 38 | The above copyright notice and this permission notice shall be included in all 39 | copies or substantial portions of the Software. 40 | 41 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 42 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 43 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 44 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 45 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 46 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 47 | SOFTWARE. 48 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "env": { 4 | "node": true, 5 | "es6": false 6 | }, 7 | "parserOptions": { 8 | "ecmaVersion": 5 9 | }, 10 | "rules": { 11 | // Possible errors 12 | "comma-dangle": [2, "never"], 13 | "no-cond-assign": [2, "except-parens"], 14 | "no-console": 2, 15 | "no-constant-condition": 2, 16 | "no-control-regex": 2, 17 | "no-debugger": 2, 18 | "no-dupe-args": 2, 19 | "no-dupe-keys": 2, 20 | "no-duplicate-case": 2, 21 | "no-empty": 2, 22 | "no-empty-character-class": 2, 23 | "no-ex-assign": 2, 24 | "no-extra-boolean-cast": 2, 25 | "no-extra-parens": 0, // https://github.com/eslint/eslint/issues/3065 26 | "no-extra-semi": 2, 27 | "no-func-assign": 2, 28 | "no-inner-declarations": 0, 29 | "no-invalid-regexp": 2, 30 | "no-irregular-whitespace": 2, 31 | "no-negated-in-lhs": 2, 32 | "no-obj-calls": 2, 33 | "no-regex-spaces": 2, 34 | "no-sparse-arrays": 2, 35 | "no-unexpected-multiline": 2, 36 | "no-unreachable": 2, 37 | "use-isnan": 2, 38 | "valid-jsdoc": 0, 39 | "valid-typeof": 2, 40 | 41 | // Best practices 42 | "accessor-pairs": 2, 43 | "array-callback-return": 2, 44 | "block-scoped-var": 0, 45 | "complexity": 0, 46 | "consistent-return": 2, 47 | "curly": [2, "all"], 48 | "default-case": 0, 49 | "dot-location": [2, "property"], 50 | "dot-notation": 2, 51 | "eqeqeq": 2, 52 | "guard-for-in": 0, 53 | "no-alert": 2, 54 | "no-caller": 2, 55 | "no-case-declarations": 2, 56 | "no-div-regex": 0, 57 | "no-else-return": 2, 58 | "no-empty-pattern": 2, 59 | "no-eq-null": 2, 60 | "no-eval": 2, 61 | "no-extend-native": 2, 62 | "no-extra-bind": 2, 63 | "no-extra-label": 2, 64 | "no-fallthrough": 2, 65 | "no-floating-decimal": 2, 66 | "no-implicit-coercion": 2, 67 | "no-implicit-globals": 2, 68 | "no-implied-eval": 0, 69 | "no-invalid-this": 2, 70 | "no-iterator": 2, 71 | "no-labels": [2, { "allowLoop": true }], 72 | "no-lone-blocks": 2, 73 | "no-loop-func": 0, 74 | "no-magic-numbers": 2, 75 | "no-multi-spaces": 2, 76 | "no-multi-str": 2, 77 | "no-native-reassign": 2, 78 | "no-new": 2, 79 | "no-new-func": 2, 80 | "no-new-wrappers": 2, 81 | "no-octal": 2, 82 | "no-octal-escape": 2, 83 | "no-param-reassign": 0, 84 | "no-process-env": 2, 85 | "no-proto": 2, 86 | "no-redeclare": 2, 87 | "no-return-assign": [2, "always"], 88 | "no-script-url": 2, 89 | "no-self-assign": 2, 90 | "no-self-compare": 2, 91 | "no-sequences": 2, 92 | "no-throw-literal": 2, 93 | "no-unmodified-loop-condition": 2, 94 | "no-unused-expressions": 2, 95 | "no-unused-labels": 2, 96 | "no-useless-call": 2, 97 | "no-useless-concat": 2, 98 | "no-void": 2, 99 | "no-warning-comments": 0, 100 | "no-with": 2, 101 | "radix": [2, "as-needed"], 102 | "vars-on-top": 0, 103 | "wrap-iife": [2, "outside"], 104 | "yoda": [2, "never"], 105 | 106 | // Strict Mode 107 | "strict": [2, "global"], 108 | 109 | // Variables 110 | "init-declarations": 0, 111 | "no-catch-shadow": 2, 112 | "no-delete-var": 2, 113 | "no-label-var": 2, 114 | "no-shadow": 2, 115 | "no-shadow-restricted-names": 2, 116 | "no-undef": 2, 117 | "no-undef-init": 2, 118 | "no-undefined": 0, 119 | "no-unused-vars": 2, 120 | "no-use-before-define": [2, "nofunc"], 121 | 122 | // Node.js and CommonJS 123 | "callback-return": 0, 124 | "global-require": 2, 125 | "handle-callback-err": 2, 126 | "no-mixed-requires": [2, true], 127 | "no-new-require": 2, 128 | "no-path-concat": 2, 129 | "no-process-exit": 2, 130 | "no-restricted-imports": 0, 131 | "no-restricted-modules": 0, 132 | "no-sync": 0, 133 | 134 | // Stylistic Issues 135 | "array-bracket-spacing": [2, "never"], 136 | "block-spacing": [2, "always"], 137 | "brace-style": [2, "1tbs", { "allowSingleLine": false }], 138 | "camelcase": [2, { "properties": "always" }], 139 | "comma-spacing": [2, { "before": false, "after": true }], 140 | "comma-style": [2, "last"], 141 | "computed-property-spacing": [2, "never"], 142 | "consistent-this": 0, 143 | "eol-last": 2, 144 | "func-names": 0, 145 | "func-style": [2, "declaration"], 146 | "id-blacklist": 0, 147 | "id-length": 0, 148 | "id-match": 0, 149 | "indent": [2, 4, { "SwitchCase": 1 }], 150 | "jsx-quotes": 0, 151 | "key-spacing": [2, { "beforeColon": false, "afterColon": true, "mode": "strict" }], 152 | "keyword-spacing": [2, { "before": true, "after": true }], 153 | "linebreak-style": [2, "unix"], 154 | "lines-around-comment": 0, 155 | "max-depth": 0, 156 | "max-len": [2, 120], 157 | "max-nested-callbacks": 0, 158 | "max-params": 0, 159 | "max-statements": 0, 160 | "new-cap": 2, 161 | "new-parens": 2, 162 | "newline-after-var": 0, 163 | "newline-per-chained-call": 0, 164 | "no-array-constructor": 2, 165 | "no-bitwise": 0, 166 | "no-continue": 0, 167 | "no-inline-comments": 0, 168 | "no-lonely-if": 2, 169 | "no-mixed-spaces-and-tabs": 2, 170 | "no-multiple-empty-lines": 2, 171 | "no-negated-condition": 0, 172 | "no-nested-ternary": 2, 173 | "no-new-object": 2, 174 | "no-plusplus": 0, 175 | "no-restricted-syntax": 0, 176 | "no-spaced-func": 2, 177 | "no-ternary": 0, 178 | "no-trailing-spaces": 2, 179 | "no-underscore-dangle": 2, 180 | "no-unneeded-ternary": 2, 181 | "no-whitespace-before-property": 2, 182 | "object-curly-spacing": [2, "always"], 183 | "one-var": [2, "never"], 184 | "one-var-declaration-per-line": [2, "initializations"], 185 | "operator-assignment": [2, "always"], 186 | "operator-linebreak": [2, "after"], 187 | "padded-blocks": [2, "never"], 188 | "quote-props": [2, "as-needed"], 189 | "quotes": [2, "double"], 190 | "require-jsdoc": 0, 191 | "semi": [2, "always"], 192 | "semi-spacing": 2, 193 | "sort-imports": 0, 194 | "sort-vars": 0, 195 | "space-before-blocks": [2, "always"], 196 | "space-before-function-paren": [2, { "anonymous": "always", "named": "never" }], 197 | "space-in-parens": [2, "never"], 198 | "space-infix-ops": 2, 199 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 200 | "spaced-comment": [2, "always", { "markers": ["///"] }], 201 | "wrap-regex": 0 202 | } 203 | } 204 | --------------------------------------------------------------------------------