dorian-sort-json 0.4.0 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/sort-json +38 -2
  3. metadata +5 -6
  4. data/lib/dorian/sort-json.rb +0 -44
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 044bd64a6db1e47c642b1495bd3f03e0217555a37a9fc1a186a8e5001b252f4a
4
- data.tar.gz: ada1eadd785f91c4469f034c15b3143bb44677aa36dce794501ce1d2427f1678
3
+ metadata.gz: b2c9c09d7d5908ef504e57d26c9d108d60642ffbea7c71d590835886d4029a6c
4
+ data.tar.gz: 78a42459ed5923c5837d6a9ea888ef9e8b7576bc0d0a0c869e54731001e99f20
5
5
  SHA512:
6
- metadata.gz: 9efa6990cc8b57177e8d40a965461131e101095612a3dfe41ff77363d04e432e4c587f96372850534841d0ee4349518fdac6118072692eab9a07df94007abe50
7
- data.tar.gz: 6ea7b8307b8997cd0251b82111979b2b8b47909d884e4ac2d68763e992a9e9564a239d61c3392fe1f79985491df1b550158a109ce70d73ab84f2449f5fbd447b
6
+ metadata.gz: 37382fd05135561c232b658df85854b345af9431ba1ad51b396c83d6075e4a8813057e7cada142f3fc19f25a4f658bb61249de113cef6731064ddc7ec46aa40f
7
+ data.tar.gz: 627b9b8b77576c4dc7425aef3bd24646e4e159e517617bd03271f75a890ec63cbafca6ee082f11f2d26b034144542771bdff8eaa9a54e1efffc36fce3f67ef9f
data/bin/sort-json CHANGED
@@ -1,4 +1,40 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require 'dorian/sort-json'
4
- Dorian::SortJson.run
4
+ def sort_json(data)
5
+ if data.is_a?(Array)
6
+ data.map { |element| sort_json(element) }
7
+ elsif data.is_a?(Hash)
8
+ data
9
+ .sort_by { |key, _value| key }
10
+ .to_h
11
+ .transform_values { |value| sort_json(value) }
12
+ else
13
+ data
14
+ end
15
+ end
16
+
17
+ if ARGV[0] == "--help" || ARGV[0] == "-h"
18
+ puts "USAGE: sort-json FILES..."
19
+ puts "USAGE: ... | sort-json"
20
+ exit
21
+ end
22
+
23
+ inputs = ARGV
24
+
25
+ if inputs.empty?
26
+ inputs = $stdin.each_line.to_a
27
+
28
+ inputs =
29
+ if File.exist?(inputs.first.strip)
30
+ inputs.map(&:strip)
31
+ else
32
+ [inputs.join]
33
+ end
34
+ end
35
+
36
+ inputs.each do |input|
37
+ content = File.exist?(input) ? File.read(input) : input
38
+ json = "#{JSON.pretty_generate(sort_json(JSON.parse(content)))}\n"
39
+ File.exist?(input) ? File.write(input, json) : puts(json)
40
+ end
metadata CHANGED
@@ -1,28 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-sort-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dorian Marié
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-19 00:00:00.000000000 Z
11
+ date: 2024-03-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  Sorts keys of hashes of JSON files
15
15
 
16
16
  e.g. `sort-json package.json`
17
- email: dorian@dorianmarie.fr
17
+ email: dorian@dorianmarie.com
18
18
  executables:
19
19
  - sort-json
20
20
  extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
23
  - bin/sort-json
24
- - lib/dorian/sort-json.rb
25
- homepage: https://github.com/dorianmariefr/sort-json
24
+ homepage: https://github.com/dorianmariecom/sort-json
26
25
  licenses:
27
26
  - MIT
28
27
  metadata:
@@ -42,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
41
  - !ruby/object:Gem::Version
43
42
  version: '0'
44
43
  requirements: []
45
- rubygems_version: 3.2.22
44
+ rubygems_version: 3.5.3
46
45
  signing_key:
47
46
  specification_version: 4
48
47
  summary: Sorts keys of hashes of JSON files
@@ -1,44 +0,0 @@
1
- require "json"
2
-
3
- module Dorian
4
- class SortJson
5
- def self.run
6
- if ARGV[0] == "--help" || ARGV[0] == "-h"
7
- puts "USAGE: sort-json FILES..."
8
- puts "USAGE: ... | sort-json"
9
- exit
10
- end
11
-
12
- inputs = ARGV
13
-
14
- if inputs.size.zero?
15
- inputs = STDIN.each_line.to_a
16
-
17
- if File.exist?(inputs.first.strip)
18
- inputs = inputs.map(&:strip)
19
- else
20
- inputs = [inputs.join]
21
- end
22
- end
23
-
24
- inputs.each do |input|
25
- content = File.exist?(input) ? File.read(input) : input
26
- json = JSON.pretty_generate(sort_json(JSON.parse(content))) + "\n"
27
- File.exist?(input) ? File.write(input, json) : puts(json)
28
- end
29
- end
30
-
31
- def self.sort_json(data)
32
- if data.is_a?(Array)
33
- data.map { |element| sort_json(element) }
34
- elsif data.is_a?(Hash)
35
- data
36
- .sort_by { |key, _value| key }
37
- .to_h
38
- .transform_values { |value| sort_json(value) }
39
- else
40
- data
41
- end
42
- end
43
- end
44
- end