neatjson 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +44 -12
- data/neatjson.gemspec +1 -1
- data/test/test_neatjson.js +4 -2
- data/test/tests.js +3 -0
- data/test/tests.rb +4 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f556263adaff6b70e7bfd65cf52dcc2062762d12
|
4
|
+
data.tar.gz: 5779d4d99498f69d6725e976def3027b8e31d1f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a241006728605936eaeab0f932b701a881d920c14d5f753e0781848f38b180224e218ea05c19c6c448d501b82830ea039f9721dde892ca9ae506f014c758731
|
7
|
+
data.tar.gz: e9b96893f587a3003d5949567322ece126e19015775948d4b0e525a8e07533b68795703c2569b3ab7fc1e95cf0260b6a88baa15ed4ba2bcba32d9e70a98669e6
|
data/README.md
CHANGED
@@ -3,9 +3,22 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/neatjson.svg)](http://badge.fury.io/rb/neatjson)
|
4
4
|
[![Gem Downloads](http://ruby-gem-downloads-badge.herokuapp.com/neatjson?type=total&color=brightgreen)](https://rubygems.org/gems/neatjson)
|
5
5
|
|
6
|
-
Pretty-print your JSON in Ruby with more power than is provided by `JSON.pretty_generate
|
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
7
|
|
8
|
-
|
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:
|
9
22
|
|
10
23
|
~~~ json
|
11
24
|
{
|
@@ -36,7 +49,9 @@ Here's an excerpt (from a much larger JSON):
|
|
36
49
|
|
37
50
|
## Installation
|
38
51
|
|
39
|
-
`gem install neatjson`
|
52
|
+
* Ruby: `gem install neatjson`
|
53
|
+
* JavaScript: Clone the GitHub repo and copy `javascript/neatjson.js`
|
54
|
+
* _Sorry, no NodeJS package yet._
|
40
55
|
|
41
56
|
## Examples
|
42
57
|
|
@@ -116,6 +131,22 @@ puts JSON.neat_generate( a, wrap:true, short:true )
|
|
116
131
|
#=> [3,
|
117
132
|
#=> 4,
|
118
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 } } ]
|
119
150
|
~~~
|
120
151
|
|
121
152
|
## Options
|
@@ -126,7 +157,8 @@ You may pass any of the following option symbols to `neat_generate`:
|
|
126
157
|
* `:indent_last` — Indent the closing bracket/brace for arrays and objects? Default: `false`
|
127
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`
|
128
159
|
* _This causes the `:indent` and `:indent_last` options to be ignored, instead basing indentation on array and object padding._
|
129
|
-
* `:sort` — Sort
|
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 } }`
|
130
162
|
* `:aligned` — When wrapping objects, line up the colons (per object)? Default: `false`
|
131
163
|
* `:decimals` — Decimal precision to use for non-integer numbers; use `false` to keep float values precise. Default: `false`
|
132
164
|
* `:array_padding` — Number of spaces to put inside brackets for arrays. Default: `0`
|
@@ -143,13 +175,7 @@ You may pass any of the following option symbols to `neat_generate`:
|
|
143
175
|
* `:after_colon` — Shorthand to set both `:after_colon_1` and `:after_colon_n`. Default: `0`
|
144
176
|
* `:around_colon` — Shorthand to set both `:before_colon` and `:after_colon`. Default: `0`
|
145
177
|
|
146
|
-
|
147
|
-
|
148
|
-
* The string name of the object key.
|
149
|
-
* The associated value.
|
150
|
-
* The object being sorted.
|
151
|
-
|
152
|
-
For example:
|
178
|
+
You may omit the 'value' and/or 'object' parameters in your `sort` lambda if desired. For example:
|
153
179
|
|
154
180
|
~~~ ruby
|
155
181
|
# Ruby sorting examples
|
@@ -161,7 +187,7 @@ JSON.neat_generate obj, sort:true # sort by key nam
|
|
161
187
|
JSON.neat_generate obj, sort:->(k){ k } # sort by key name (long way)
|
162
188
|
#=> {"a":2,"b":2,"c":3,"d":1,"e":3,"f":3}
|
163
189
|
|
164
|
-
JSON.neat_generate obj, sort:->(k,v){ [-v,k] } # sort by descending value, ascending key
|
190
|
+
JSON.neat_generate obj, sort:->(k,v){ [-v,k] } # sort by descending value, then by ascending key
|
165
191
|
#=> {"c":3,"e":3,"f":3,"a":2,"b":2,"d":1}
|
166
192
|
|
167
193
|
JSON.neat_generate obj, sort:->(k,v,h){ h.values.count(v) } # sort by count of keys with same value
|
@@ -208,6 +234,12 @@ For other communication you can [email the author directly](mailto:!@phrogz.net?
|
|
208
234
|
|
209
235
|
## HISTORY
|
210
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
|
+
|
211
243
|
* **v0.8.1** - April 22nd, 2016
|
212
244
|
* Make NeatJSON work with [Opal](http://opalrb.org) (by removing all in-place string mutations)
|
213
245
|
|
data/neatjson.gemspec
CHANGED
data/test/test_neatjson.js
CHANGED
@@ -4,7 +4,9 @@ var count=0, pass=0;
|
|
4
4
|
require('./tests.js').tests.forEach(function(valTest){
|
5
5
|
var value = valTest.value;
|
6
6
|
valTest.tests.forEach(function(test){
|
7
|
-
var
|
7
|
+
var cmdOpts = test.opts ? JSON.parse(JSON.stringify(test.opts)) : '';
|
8
|
+
if (test.opts && typeof test.opts.sort==='function') cmdOpts.sort = test.opts.sort+'';
|
9
|
+
var cmd = "neatJSON("+JSON.stringify(value)+(test.opts?","+JSON.stringify(cmdOpts):'')+")";
|
8
10
|
try{
|
9
11
|
if (test.opts){
|
10
12
|
var opts = {};
|
@@ -22,7 +24,7 @@ require('./tests.js').tests.forEach(function(valTest){
|
|
22
24
|
console.log('ACTUAL');
|
23
25
|
console.log(json,"\n");
|
24
26
|
}
|
25
|
-
} catch
|
27
|
+
} catch(e) {
|
26
28
|
console.log("Error running "+cmd);
|
27
29
|
console.log(e.stack);
|
28
30
|
console.log("")
|
data/test/tests.js
CHANGED
@@ -98,6 +98,9 @@ exports.tests = [
|
|
98
98
|
|
99
99
|
{value:{ b:17, a:42 }, tests:[
|
100
100
|
{ json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sorted:true,short:true} },
|
101
|
+
{ json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sort:true, short:true} },
|
102
|
+
{ json:"{\n \"a\":42,\n \"b\":17\n}", opts:{wrap:1,sorted:true} },
|
103
|
+
{ json:"{\n \"a\":42,\n \"b\":17\n}", opts:{wrap:1,sort:true} },
|
101
104
|
{ json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:function(k){ return k } } },
|
102
105
|
{ json:"{\"b\":17,\"a\":42}", opts:{wrap:false,sort:function(k,v){ return v } } },
|
103
106
|
{ json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:function(k,v){ return -v } } },
|
data/test/tests.rb
CHANGED
@@ -93,7 +93,10 @@ TESTS = [
|
|
93
93
|
]},
|
94
94
|
|
95
95
|
{value:{ b:17, a:42 }, tests:[
|
96
|
-
{ json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,
|
96
|
+
{ json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sorted:true,short:true} },
|
97
|
+
{ json:"{\"a\":42,\n \"b\":17}", opts:{wrap:10,sort:true, short:true} },
|
98
|
+
{ json:"{\n \"a\":42,\n \"b\":17\n}", opts:{wrap:1,sorted:true} },
|
99
|
+
{ json:"{\n \"a\":42,\n \"b\":17\n}", opts:{wrap:1,sort:true} },
|
97
100
|
{ json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:->(k){ k } } },
|
98
101
|
{ json:"{\"b\":17,\"a\":42}", opts:{wrap:false,sort:->(k,v){ v } } },
|
99
102
|
{ json:"{\"a\":42,\"b\":17}", opts:{wrap:false,sort:->(k,v){ -v } } },
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: neatjson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gavin Kistner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: 'Generate JSON strings from Ruby objects with flexible formatting options.
|
14
14
|
Key features: keep arrays and objects on a single line when they fit; format floats
|