neatjson 0.8.2 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f556263adaff6b70e7bfd65cf52dcc2062762d12
4
- data.tar.gz: 5779d4d99498f69d6725e976def3027b8e31d1f1
3
+ metadata.gz: cd71a0bca8dacc2bbb100e03944cce9dd2e9af90
4
+ data.tar.gz: 02f77183e8e587d981c06c5c2c6fa2a999a08309
5
5
  SHA512:
6
- metadata.gz: 0a241006728605936eaeab0f932b701a881d920c14d5f753e0781848f38b180224e218ea05c19c6c448d501b82830ea039f9721dde892ca9ae506f014c758731
7
- data.tar.gz: e9b96893f587a3003d5949567322ece126e19015775948d4b0e525a8e07533b68795703c2569b3ab7fc1e95cf0260b6a88baa15ed4ba2bcba32d9e70a98669e6
6
+ metadata.gz: 60b221078927b2628277e20215dafd4def6ab411c3204cb767da4e8b78e9b882bb9f9a0eb34d1464526e577db46bbd4239678821f370f69ccc7b33a2c01ddb4d
7
+ data.tar.gz: 726176200c5fef15c51f42cda4fa880fc2e77655822628797ebdc7e5e64d1e0a29abbe04a000682c5277bb47c5efdc5d4479336247cacf9702d907c11da38f9a
data/README.md CHANGED
@@ -1,293 +1,297 @@
1
- # NeatJSON
2
-
3
- [![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)
4
- [![Gem Downloads](http://ruby-gem-downloads-badge.herokuapp.com/neatjson?type=total&color=brightgreen)](https://rubygems.org/gems/neatjson)
5
-
6
- Pretty-print your JSON in Ruby or JavaScript with more power than is provided by `JSON.pretty_generate` (Ruby) or `JSON.stringify` (JS). For example, like Ruby's `pp` (pretty print), NeatJSON can keep objects on one line if they fit, but break them over multiple lines if needed.
7
-
8
- **Features (all optional):**
9
-
10
- * Keep values on one line, with variable wrap width.
11
- * Format numeric values to specified precision.
12
- * Sort object keys to be in alphabetical order.
13
- * Arbitrary whitespace (or really, any string) for indentation.
14
- * "Short" wrapping uses fewer lines, indentation based on values. (See last example below.)
15
- * Indent final closing bracket/brace for each array/object.
16
- * Adjust number of spaces inside array/object braces.
17
- * Adjust number of spaces before/after commas and colons (adjustable for single-line vs. multi-line )
18
- * Line up the values for an object across lines.
19
- * [Online webpage](http://phrogz.net/JS/NeatJSON) for conversions and experimenting with options.
20
-
21
- Here's a short example of output showing the power of proper wrapping:
22
-
23
- ~~~ json
24
- {
25
- "navigation.createroute.poi":[
26
- {"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
27
- {"text":"Take me to the airport","params":{"poi":"airport"}},
28
- {"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
29
- {"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
30
- {"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
31
- {
32
- "text":"Go to the Hilton by the Airport",
33
- "params":{"poi":"Hilton","location":"Airport"}
34
- },
35
- {
36
- "text":"Take me to the Fry's in Fresno",
37
- "params":{"poi":"Fry's","location":"Fresno"}
38
- }
39
- ],
40
- "navigation.eta":[
41
- {"text":"When will we get there?"},
42
- {"text":"When will I arrive?"},
43
- {"text":"What time will I get to the destination?"},
44
- {"text":"What time will I reach the destination?"},
45
- {"text":"What time will it be when I arrive?"}
46
- ]
47
- }
48
- ~~~
49
-
50
- ## Installation
51
-
52
- * Ruby: `gem install neatjson`
53
- * JavaScript: Clone the GitHub repo and copy `javascript/neatjson.js`
54
- * _Sorry, no NodeJS package yet._
55
-
56
- ## Examples
57
-
58
- ~~~ ruby
59
- require 'neatjson'
60
-
61
- o = { b:42.005, a:[42,17], longer:true, str:"yes
62
- please" }
63
-
64
- puts JSON.neat_generate(o)
65
- #=> {"b":42.005,"a":[42,17],"longer":true,"str":"yes\nplease"}
66
-
67
- puts JSON.neat_generate(o,sort:true)
68
- #=> {"a":[42,17],"b":42.005,"longer":true,"str":"yes\nplease"}
69
-
70
- puts JSON.neat_generate(o,sort:true,padding:1,after_comma:1)
71
- #=> { "a":[ 42, 17 ], "b":42.005, "longer":true, "str":"yes\nplease" }
72
-
73
- puts JSON.neat_generate(o,sort:true,wrap:40)
74
- #=> {
75
- #=> "a":[42,17],
76
- #=> "b":42.005,
77
- #=> "longer":true,
78
- #=> "str":"yes\nplease"
79
- #=> }
80
-
81
- puts JSON.neat_generate(o,sort:true,wrap:40,decimals:2)
82
- #=> {
83
- #=> "a":[42,17],
84
- #=> "b":42.01,
85
- #=> "longer":true,
86
- #=> "str":"yes\nplease"
87
- #=> }
88
-
89
- puts JSON.neat_generate(o,sort:->(k){ k.length },wrap:40,aligned:true)
90
- #=> {
91
- #=> "a" :[42,17],
92
- #=> "b" :42.005,
93
- #=> "str" :"yes\nplease",
94
- #=> "longer":true
95
- #=> }
96
-
97
- puts JSON.neat_generate(o,sort:true,wrap:40,aligned:true,around_colon:1)
98
- #=> {
99
- #=> "a" : [42,17],
100
- #=> "b" : 42.005,
101
- #=> "longer" : true,
102
- #=> "str" : "yes\nplease"
103
- #=> }
104
-
105
- puts JSON.neat_generate(o,sort:true,wrap:40,aligned:true,around_colon:1,short:true)
106
- #=> {"a" : [42,17],
107
- #=> "b" : 42.005,
108
- #=> "longer" : true,
109
- #=> "str" : "yes\nplease"}
110
-
111
- a = [1,2,[3,4,[5]]]
112
- puts JSON.neat_generate(a)
113
- #=> [1,2,[3,4,[5]]]
114
-
115
- puts JSON.pretty_generate(a) # oof!
116
- #=> [
117
- #=> 1,
118
- #=> 2,
119
- #=> [
120
- #=> 3,
121
- #=> 4,
122
- #=> [
123
- #=> 5
124
- #=> ]
125
- #=> ]
126
- #=> ]
127
-
128
- puts JSON.neat_generate( a, wrap:true, short:true )
129
- #=> [1,
130
- #=> 2,
131
- #=> [3,
132
- #=> 4,
133
- #=> [5]]]
134
-
135
- data = ["foo","bar",{dogs:42,piggies:{color:'pink', tasty:true},
136
- barn:{jimmy:[1,2,3,4,5],jammy:3.141592653,hot:"pajammy"},cats:7}]
137
-
138
- opts = { short:true, wrap:60, decimals:3, sort:true, aligned:true,
139
- padding:1, after_comma:1, around_colon_n:1 }
140
-
141
- puts JSON.neat_generate( data, opts )
142
- #=> [ "foo",
143
- #=> "bar",
144
- #=> { "barn" : { "hot" : "pajammy",
145
- #=> "jammy" : 3.142,
146
- #=> "jimmy" : [ 1, 2, 3, 4, 5 ] },
147
- #=> "cats" : 7,
148
- #=> "dogs" : 42,
149
- #=> "piggies" : { "color":"pink", "tasty":true } } ]
150
- ~~~
151
-
152
- ## Options
153
- You may pass any of the following option symbols to `neat_generate`:
154
-
155
- * `:wrap` — The maximum line width before wrapping. Use `false` to never wrap, or `true` (or `-1`) to always wrap. Default: `80`
156
- * `:indent` — Whitespace used to indent each level when wrapping. Default: `" "` (two spaces)
157
- * `:indent_last` — Indent the closing bracket/brace for arrays and objects? Default: `false`
158
- * `:short` — Keep the output 'short' when wrapping? This puts opening brackets on the same line as the first value, and closing brackets on the same line as the last. Default: `false`
159
- * _This causes the `:indent` and `:indent_last` options to be ignored, instead basing indentation on array and object padding._
160
- * `:sort` — Sort objects' keys in alphabetical order (`true`), or supply a lambda for custom sorting. Default: `false`
161
- * If you supply a lambda to the `sort` option, it will be passed three values: the (string) name of the key, the associated value, and the object being sorted, e.g. `{ sort:->(key,value,hash){ Float(value) rescue Float::MAX } }`
162
- * `:aligned` — When wrapping objects, line up the colons (per object)? Default: `false`
163
- * `:decimals` — Decimal precision to use for non-integer numbers; use `false` to keep float values precise. Default: `false`
164
- * `:array_padding` — Number of spaces to put inside brackets for arrays. Default: `0`
165
- * `:object_padding` — Number of spaces to put inside braces for objects. Default: `0`
166
- * `:padding` — Shorthand to set both `:array_padding` and `:object_padding`. Default: `0`
167
- * `:before_comma` — Number of spaces to put before commas (for both arrays and objects). Default: `0`
168
- * `:after_comma` — Number of spaces to put after commas (for both arrays and objects). Default: `0`
169
- * `:around_comma` — Shorthand to set both `:before_comma` and `:after_comma`. Default: `0`
170
- * `:before_colon_1` — Number of spaces before a colon when the object is on one line. Default: `0`
171
- * `:after_colon_1` — Number of spaces after a colon when the object is on one line. Default: `0`
172
- * `:before_colon_n` — Number of spaces before a colon when the object is on multiple lines. Default: `0`
173
- * `:after_colon_n` — Number of spaces after a colon when the object is on multiple lines. Default: `0`
174
- * `:before_colon` — Shorthand to set both `:before_colon_1` and `:before_colon_n`. Default: `0`
175
- * `:after_colon` — Shorthand to set both `:after_colon_1` and `:after_colon_n`. Default: `0`
176
- * `:around_colon` — Shorthand to set both `:before_colon` and `:after_colon`. Default: `0`
177
-
178
- You may omit the 'value' and/or 'object' parameters in your `sort` lambda if desired. For example:
179
-
180
- ~~~ ruby
181
- # Ruby sorting examples
182
- obj = {e:3, a:2, c:3, b:2, d:1, f:3}
183
-
184
- JSON.neat_generate obj, sort:true # sort by key name
185
- #=> {"a":2,"b":2,"c":3,"d":1,"e":3,"f":3}
186
-
187
- JSON.neat_generate obj, sort:->(k){ k } # sort by key name (long way)
188
- #=> {"a":2,"b":2,"c":3,"d":1,"e":3,"f":3}
189
-
190
- JSON.neat_generate obj, sort:->(k,v){ [-v,k] } # sort by descending value, then by ascending key
191
- #=> {"c":3,"e":3,"f":3,"a":2,"b":2,"d":1}
192
-
193
- JSON.neat_generate obj, sort:->(k,v,h){ h.values.count(v) } # sort by count of keys with same value
194
- #=> {"d":1,"a":2,"b":2,"e":3,"c":3,"f":3}
195
- ~~~
196
-
197
- ~~~ js
198
- // JavaScript sorting examples
199
- var obj = {e:3, a:2, c:3, b:2, d:1, f:3};
200
-
201
- neatJSON( obj, {sort:true} ); // sort by key name
202
- // {"a":2,"b":2,"c":3,"d":1,"e":3,"f":3}
203
-
204
- neatJSON( obj, { sort:function(k){ return k }} ); // sort by key name (long way)
205
- // {"a":2,"b":2,"c":3,"d":1,"e":3,"f":3}
206
-
207
- neatJSON( obj, { sort:function(k,v){ return -v }} ); // sort by descending value
208
- // {"e":3,"c":3,"f":3,"a":2,"b":2,"d":1}
209
-
210
- var countByValue = {};
211
- for (var k in obj) countByValue[obj[k]] = (countByValue[obj[k]]||0) + 1;
212
- neatJSON( obj, { sort:function(k,v,h){ return countByValue[v] } } ); // sort by count of same value
213
- // {"d":1,"a":2,"b":2,"e":3,"c":3,"f":3}
214
- ~~~
215
-
216
- _Note that the JavaScript version of NeatJSON does not provide a mechanism for cascading sort in the same manner as Ruby._
217
-
218
-
219
- ## License & Contact
220
-
221
- NeatJSON is copyright ©2015–2016 by Gavin Kistner and is released under
222
- the [MIT License](http://www.opensource.org/licenses/mit-license.php).
223
- See the LICENSE.txt file for more details.
224
-
225
- For bugs or feature requests please open [issues on GitHub][1].
226
- For other communication you can [email the author directly](mailto:!@phrogz.net?subject=NeatJSON).
227
-
228
- ## TODO (aka Known Limitations)
229
-
230
- * Figure out the best way to play with custom objects that use `to_json` for their representation.
231
- * Detect circular references.
232
- * Possibly allow illegal JSON values like `NaN` or `Infinity`.
233
- * Possibly allow "JSON5" output (legal identifiers unquoted, etc.)
234
-
235
- ## HISTORY
236
-
237
- * **v0.8.2** - December 16th, 2016
238
- * Fix issue #22: Sorting keys on multi-line object does not work without "short" [JS only]
239
- * Update online interface to support tabs as well as spaces.
240
- * Update online interface to use a textarea for the output (easier to select and copy).
241
- * Update online interface turn off spell checking for input and output.
242
-
243
- * **v0.8.1** - April 22nd, 2016
244
- * Make NeatJSON work with [Opal](http://opalrb.org) (by removing all in-place string mutations)
245
-
246
- * **v0.8** - April 21st, 2016
247
- * Allow `sort` to take a lambda for customized sorting of object key/values.
248
-
249
- * **v0.7.2** - April 14th, 2016
250
- * Fix JavaScript library to support objects without an `Object` constructor (e.g. `location`).
251
- * Online HTML converter accepts arbitrary JavaScript values as input in addition to JSON.
252
-
253
- * **v0.7.1** - April 6th, 2016
254
- * Fix Ruby library to work around bug in Opal.
255
-
256
- * **v0.7** - March 26th, 2016
257
- * Add `indentLast`/`indent_last` feature.
258
-
259
- * **v0.6.2** - February 8th, 2016
260
- * Use memoization to avoid performance stalls when wrapping deeply-nested objects/arrays.
261
- _Thanks @chroche_
262
-
263
- * **v0.6.1** - October 12th, 2015
264
- * Fix handling of nested empty objects and arrays. (Would cause a runtime error in many cases.)
265
- * _This change causes empty arrays in a tight wrapping scenario to appear on a single line where they would previously take up three lines._
266
-
267
- * **v0.6** - April 26th, 2015
268
- * Added `before_colon_1` and `before_colon_n` to distinguish between single-line and multi-line objects.
269
-
270
- * **v0.5** - April 19th, 2015
271
- * Do not format integers (or floats that equal their integer) using `decimals` option.
272
- * Make `neatJSON()` JavaScript available to Node.js as well as web browsers.
273
- * Add (Node-based) testing for the JavaScript version.
274
-
275
- * **v0.4** - April 18th, 2015
276
- * Add JavaScript version with online runner.
277
-
278
- * **v0.3.2** - April 16th, 2015
279
- * Force YARD to use Markdown for documentation.
280
-
281
- * **v0.3.1** - April 16th, 2015
282
- * Remove some debugging code accidentally left in.
283
-
284
- * **v0.3** - April 16th, 2015
285
- * Fix another bug with `short:true` and wrapping array values inside objects.
286
-
287
- * **v0.2** - April 16th, 2015
288
- * Fix bug with `short:true` and wrapping values inside objects.
289
-
290
- * **v0.1** - April 15th, 2015
291
- * Initial release.
292
-
293
- [1]: https://github.com/Phrogz/NeatJSON/issues
1
+ # NeatJSON
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)
4
+ [![Gem Downloads](http://ruby-gem-downloads-badge.herokuapp.com/neatjson?type=total&color=brightgreen)](https://rubygems.org/gems/neatjson)
5
+
6
+ Pretty-print your JSON in Ruby or JavaScript with more power than is provided by `JSON.pretty_generate` (Ruby) or `JSON.stringify` (JS). For example, like Ruby's `pp` (pretty print), NeatJSON can keep objects on one line if they fit, but break them over multiple lines if needed.
7
+
8
+ **Features (all optional):**
9
+
10
+ * Keep values on one line, with variable wrap width.
11
+ * Format numeric values to specified precision.
12
+ * Sort object keys to be in alphabetical order.
13
+ * Arbitrary whitespace (or really, any string) for indentation.
14
+ * "Short" wrapping uses fewer lines, indentation based on values. (See last example below.)
15
+ * Indent final closing bracket/brace for each array/object.
16
+ * Adjust number of spaces inside array/object braces.
17
+ * Adjust number of spaces before/after commas and colons (adjustable for single-line vs. multi-line )
18
+ * Line up the values for an object across lines.
19
+ * [Online webpage](http://phrogz.net/JS/NeatJSON) for conversions and experimenting with options.
20
+
21
+ Here's a short example of output showing the power of proper wrapping:
22
+
23
+ ~~~ json
24
+ {
25
+ "navigation.createroute.poi":[
26
+ {"text":"Lay in a course to the Hilton","params":{"poi":"Hilton"}},
27
+ {"text":"Take me to the airport","params":{"poi":"airport"}},
28
+ {"text":"Let's go to IHOP","params":{"poi":"IHOP"}},
29
+ {"text":"Show me how to get to The Med","params":{"poi":"The Med"}},
30
+ {"text":"Create a route to Arby's","params":{"poi":"Arby's"}},
31
+ {
32
+ "text":"Go to the Hilton by the Airport",
33
+ "params":{"poi":"Hilton","location":"Airport"}
34
+ },
35
+ {
36
+ "text":"Take me to the Fry's in Fresno",
37
+ "params":{"poi":"Fry's","location":"Fresno"}
38
+ }
39
+ ],
40
+ "navigation.eta":[
41
+ {"text":"When will we get there?"},
42
+ {"text":"When will I arrive?"},
43
+ {"text":"What time will I get to the destination?"},
44
+ {"text":"What time will I reach the destination?"},
45
+ {"text":"What time will it be when I arrive?"}
46
+ ]
47
+ }
48
+ ~~~
49
+
50
+ ## Installation
51
+
52
+ * Ruby: `gem install neatjson`
53
+ * JavaScript: Clone the GitHub repo and copy `javascript/neatjson.js`
54
+ * _Sorry, no NodeJS package yet._
55
+
56
+ ## Examples
57
+
58
+ ~~~ ruby
59
+ require 'neatjson'
60
+
61
+ o = { b:42.005, a:[42,17], longer:true, str:"yes
62
+ please" }
63
+
64
+ puts JSON.neat_generate(o)
65
+ #=> {"b":42.005,"a":[42,17],"longer":true,"str":"yes\nplease"}
66
+
67
+ puts JSON.neat_generate(o,sort:true)
68
+ #=> {"a":[42,17],"b":42.005,"longer":true,"str":"yes\nplease"}
69
+
70
+ puts JSON.neat_generate(o,sort:true,padding:1,after_comma:1)
71
+ #=> { "a":[ 42, 17 ], "b":42.005, "longer":true, "str":"yes\nplease" }
72
+
73
+ puts JSON.neat_generate(o,sort:true,wrap:40)
74
+ #=> {
75
+ #=> "a":[42,17],
76
+ #=> "b":42.005,
77
+ #=> "longer":true,
78
+ #=> "str":"yes\nplease"
79
+ #=> }
80
+
81
+ puts JSON.neat_generate(o,sort:true,wrap:40,decimals:2)
82
+ #=> {
83
+ #=> "a":[42,17],
84
+ #=> "b":42.01,
85
+ #=> "longer":true,
86
+ #=> "str":"yes\nplease"
87
+ #=> }
88
+
89
+ puts JSON.neat_generate(o,sort:->(k){ k.length },wrap:40,aligned:true)
90
+ #=> {
91
+ #=> "a" :[42,17],
92
+ #=> "b" :42.005,
93
+ #=> "str" :"yes\nplease",
94
+ #=> "longer":true
95
+ #=> }
96
+
97
+ puts JSON.neat_generate(o,sort:true,wrap:40,aligned:true,around_colon:1)
98
+ #=> {
99
+ #=> "a" : [42,17],
100
+ #=> "b" : 42.005,
101
+ #=> "longer" : true,
102
+ #=> "str" : "yes\nplease"
103
+ #=> }
104
+
105
+ puts JSON.neat_generate(o,sort:true,wrap:40,aligned:true,around_colon:1,short:true)
106
+ #=> {"a" : [42,17],
107
+ #=> "b" : 42.005,
108
+ #=> "longer" : true,
109
+ #=> "str" : "yes\nplease"}
110
+
111
+ a = [1,2,[3,4,[5]]]
112
+ puts JSON.neat_generate(a)
113
+ #=> [1,2,[3,4,[5]]]
114
+
115
+ puts JSON.pretty_generate(a) # oof!
116
+ #=> [
117
+ #=> 1,
118
+ #=> 2,
119
+ #=> [
120
+ #=> 3,
121
+ #=> 4,
122
+ #=> [
123
+ #=> 5
124
+ #=> ]
125
+ #=> ]
126
+ #=> ]
127
+
128
+ puts JSON.neat_generate( a, wrap:true, short:true )
129
+ #=> [1,
130
+ #=> 2,
131
+ #=> [3,
132
+ #=> 4,
133
+ #=> [5]]]
134
+
135
+ data = ["foo","bar",{dogs:42,piggies:{color:'pink', tasty:true},
136
+ barn:{jimmy:[1,2,3,4,5],jammy:3.141592653,hot:"pajammy"},cats:7}]
137
+
138
+ opts = { short:true, wrap:60, decimals:3, sort:true, aligned:true,
139
+ padding:1, after_comma:1, around_colon_n:1 }
140
+
141
+ puts JSON.neat_generate( data, opts )
142
+ #=> [ "foo",
143
+ #=> "bar",
144
+ #=> { "barn" : { "hot" : "pajammy",
145
+ #=> "jammy" : 3.142,
146
+ #=> "jimmy" : [ 1, 2, 3, 4, 5 ] },
147
+ #=> "cats" : 7,
148
+ #=> "dogs" : 42,
149
+ #=> "piggies" : { "color":"pink", "tasty":true } } ]
150
+ ~~~
151
+
152
+ ## Options
153
+ You may pass any of the following option symbols to `neat_generate`:
154
+
155
+ * `:wrap` — The maximum line width before wrapping. Use `false` to never wrap, or `true` (or `-1`) to always wrap. Default: `80`
156
+ * `:indent` — Whitespace used to indent each level when wrapping. Default: `" "` (two spaces)
157
+ * `:indent_last` — Indent the closing bracket/brace for arrays and objects? Default: `false`
158
+ * `:short` — Keep the output 'short' when wrapping? This puts opening brackets on the same line as the first value, and closing brackets on the same line as the last. Default: `false`
159
+ * _This causes the `:indent` and `:indent_last` options to be ignored, instead basing indentation on array and object padding._
160
+ * `:sort` — Sort objects' keys in alphabetical order (`true`), or supply a lambda for custom sorting. Default: `false`
161
+ * If you supply a lambda to the `sort` option, it will be passed three values: the (string) name of the key, the associated value, and the object being sorted, e.g. `{ sort:->(key,value,hash){ Float(value) rescue Float::MAX } }`
162
+ * `:aligned` — When wrapping objects, line up the colons (per object)? Default: `false`
163
+ * `:decimals` — Decimal precision to use for non-integer numbers; use `false` to keep float values precise. Default: `false`
164
+ * `:array_padding` — Number of spaces to put inside brackets for arrays. Default: `0`
165
+ * `:object_padding` — Number of spaces to put inside braces for objects. Default: `0`
166
+ * `:padding` — Shorthand to set both `:array_padding` and `:object_padding`. Default: `0`
167
+ * `:before_comma` — Number of spaces to put before commas (for both arrays and objects). Default: `0`
168
+ * `:after_comma` — Number of spaces to put after commas (for both arrays and objects). Default: `0`
169
+ * `:around_comma` — Shorthand to set both `:before_comma` and `:after_comma`. Default: `0`
170
+ * `:before_colon_1` — Number of spaces before a colon when the object is on one line. Default: `0`
171
+ * `:after_colon_1` — Number of spaces after a colon when the object is on one line. Default: `0`
172
+ * `:before_colon_n` — Number of spaces before a colon when the object is on multiple lines. Default: `0`
173
+ * `:after_colon_n` — Number of spaces after a colon when the object is on multiple lines. Default: `0`
174
+ * `:before_colon` — Shorthand to set both `:before_colon_1` and `:before_colon_n`. Default: `0`
175
+ * `:after_colon` — Shorthand to set both `:after_colon_1` and `:after_colon_n`. Default: `0`
176
+ * `:around_colon` — Shorthand to set both `:before_colon` and `:after_colon`. Default: `0`
177
+
178
+ You may omit the 'value' and/or 'object' parameters in your `sort` lambda if desired. For example:
179
+
180
+ ~~~ ruby
181
+ # Ruby sorting examples
182
+ obj = {e:3, a:2, c:3, b:2, d:1, f:3}
183
+
184
+ JSON.neat_generate obj, sort:true # sort by key name
185
+ #=> {"a":2,"b":2,"c":3,"d":1,"e":3,"f":3}
186
+
187
+ JSON.neat_generate obj, sort:->(k){ k } # sort by key name (long way)
188
+ #=> {"a":2,"b":2,"c":3,"d":1,"e":3,"f":3}
189
+
190
+ JSON.neat_generate obj, sort:->(k,v){ [-v,k] } # sort by descending value, then by ascending key
191
+ #=> {"c":3,"e":3,"f":3,"a":2,"b":2,"d":1}
192
+
193
+ JSON.neat_generate obj, sort:->(k,v,h){ h.values.count(v) } # sort by count of keys with same value
194
+ #=> {"d":1,"a":2,"b":2,"e":3,"c":3,"f":3}
195
+ ~~~
196
+
197
+ ~~~ js
198
+ // JavaScript sorting examples
199
+ var obj = {e:3, a:2, c:3, b:2, d:1, f:3};
200
+
201
+ neatJSON( obj, {sort:true} ); // sort by key name
202
+ // {"a":2,"b":2,"c":3,"d":1,"e":3,"f":3}
203
+
204
+ neatJSON( obj, { sort:function(k){ return k }} ); // sort by key name (long way)
205
+ // {"a":2,"b":2,"c":3,"d":1,"e":3,"f":3}
206
+
207
+ neatJSON( obj, { sort:function(k,v){ return -v }} ); // sort by descending value
208
+ // {"e":3,"c":3,"f":3,"a":2,"b":2,"d":1}
209
+
210
+ var countByValue = {};
211
+ for (var k in obj) countByValue[obj[k]] = (countByValue[obj[k]]||0) + 1;
212
+ neatJSON( obj, { sort:function(k,v,h){ return countByValue[v] } } ); // sort by count of same value
213
+ // {"d":1,"a":2,"b":2,"e":3,"c":3,"f":3}
214
+ ~~~
215
+
216
+ _Note that the JavaScript version of NeatJSON does not provide a mechanism for cascading sort in the same manner as Ruby._
217
+
218
+
219
+ ## License & Contact
220
+
221
+ NeatJSON is copyright ©2015–2016 by Gavin Kistner and is released under
222
+ the [MIT License](http://www.opensource.org/licenses/mit-license.php).
223
+ See the LICENSE.txt file for more details.
224
+
225
+ For bugs or feature requests please open [issues on GitHub][1].
226
+ For other communication you can [email the author directly](mailto:!@phrogz.net?subject=NeatJSON).
227
+
228
+ ## TODO (aka Known Limitations)
229
+
230
+ * Figure out the best way to play with custom objects that use `to_json` for their representation.
231
+ * Detect circular references.
232
+ * Possibly allow illegal JSON values like `NaN` or `Infinity`.
233
+ * Possibly allow "JSON5" output (legal identifiers unquoted, etc.)
234
+
235
+ ## HISTORY
236
+
237
+ * **v0.8.3** February 20, 2017
238
+ * Fix issue #25: Sorting keys on multi-line object **using function** does not work without "short" [JS only]
239
+ * _Thanks Bernhard Weichel_
240
+
241
+ * **v0.8.2** - December 16th, 2016
242
+ * Fix issue #22: Sorting keys on multi-line object does not work without "short" [JS only]
243
+ * Update online interface to support tabs as well as spaces.
244
+ * Update online interface to use a textarea for the output (easier to select and copy).
245
+ * Update online interface turn off spell checking for input and output.
246
+
247
+ * **v0.8.1** - April 22nd, 2016
248
+ * Make NeatJSON work with [Opal](http://opalrb.org) (by removing all in-place string mutations)
249
+
250
+ * **v0.8** - April 21st, 2016
251
+ * Allow `sort` to take a lambda for customized sorting of object key/values.
252
+
253
+ * **v0.7.2** - April 14th, 2016
254
+ * Fix JavaScript library to support objects without an `Object` constructor (e.g. `location`).
255
+ * Online HTML converter accepts arbitrary JavaScript values as input in addition to JSON.
256
+
257
+ * **v0.7.1** - April 6th, 2016
258
+ * Fix Ruby library to work around bug in Opal.
259
+
260
+ * **v0.7** - March 26th, 2016
261
+ * Add `indentLast`/`indent_last` feature.
262
+
263
+ * **v0.6.2** - February 8th, 2016
264
+ * Use memoization to avoid performance stalls when wrapping deeply-nested objects/arrays.
265
+ _Thanks @chroche_
266
+
267
+ * **v0.6.1** - October 12th, 2015
268
+ * Fix handling of nested empty objects and arrays. (Would cause a runtime error in many cases.)
269
+ * _This change causes empty arrays in a tight wrapping scenario to appear on a single line where they would previously take up three lines._
270
+
271
+ * **v0.6** - April 26th, 2015
272
+ * Added `before_colon_1` and `before_colon_n` to distinguish between single-line and multi-line objects.
273
+
274
+ * **v0.5** - April 19th, 2015
275
+ * Do not format integers (or floats that equal their integer) using `decimals` option.
276
+ * Make `neatJSON()` JavaScript available to Node.js as well as web browsers.
277
+ * Add (Node-based) testing for the JavaScript version.
278
+
279
+ * **v0.4** - April 18th, 2015
280
+ * Add JavaScript version with online runner.
281
+
282
+ * **v0.3.2** - April 16th, 2015
283
+ * Force YARD to use Markdown for documentation.
284
+
285
+ * **v0.3.1** - April 16th, 2015
286
+ * Remove some debugging code accidentally left in.
287
+
288
+ * **v0.3** - April 16th, 2015
289
+ * Fix another bug with `short:true` and wrapping array values inside objects.
290
+
291
+ * **v0.2** - April 16th, 2015
292
+ * Fix bug with `short:true` and wrapping values inside objects.
293
+
294
+ * **v0.1** - April 15th, 2015
295
+ * Initial release.
296
+
297
+ [1]: https://github.com/Phrogz/NeatJSON/issues