dorian-sort-json 0.4.1 → 0.4.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 +4 -4
- data/bin/sort-json +37 -2
- metadata +3 -4
- data/lib/dorian/sort-json.rb +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2c9c09d7d5908ef504e57d26c9d108d60642ffbea7c71d590835886d4029a6c
|
4
|
+
data.tar.gz: 78a42459ed5923c5837d6a9ea888ef9e8b7576bc0d0a0c869e54731001e99f20
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37382fd05135561c232b658df85854b345af9431ba1ad51b396c83d6075e4a8813057e7cada142f3fc19f25a4f658bb61249de113cef6731064ddc7ec46aa40f
|
7
|
+
data.tar.gz: 627b9b8b77576c4dc7425aef3bd24646e4e159e517617bd03271f75a890ec63cbafca6ee082f11f2d26b034144542771bdff8eaa9a54e1efffc36fce3f67ef9f
|
data/bin/sort-json
CHANGED
@@ -1,5 +1,40 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
5
|
-
|
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,27 +1,26 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dorian-sort-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
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: 2024-
|
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.
|
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
24
|
homepage: https://github.com/dorianmariecom/sort-json
|
26
25
|
licenses:
|
27
26
|
- MIT
|
data/lib/dorian/sort-json.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "json"
|
4
|
-
|
5
|
-
module Dorian
|
6
|
-
class SortJson
|
7
|
-
def self.run
|
8
|
-
if ARGV[0] == "--help" || ARGV[0] == "-h"
|
9
|
-
puts "USAGE: sort-json FILES..."
|
10
|
-
puts "USAGE: ... | sort-json"
|
11
|
-
exit
|
12
|
-
end
|
13
|
-
|
14
|
-
inputs = ARGV
|
15
|
-
|
16
|
-
if inputs.empty?
|
17
|
-
inputs = $stdin.each_line.to_a
|
18
|
-
|
19
|
-
inputs =
|
20
|
-
if File.exist?(inputs.first.strip)
|
21
|
-
inputs.map(&:strip)
|
22
|
-
else
|
23
|
-
[inputs.join]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
inputs.each do |input|
|
28
|
-
content = File.exist?(input) ? File.read(input) : input
|
29
|
-
json = "#{JSON.pretty_generate(sort_json(JSON.parse(content)))}\n"
|
30
|
-
File.exist?(input) ? File.write(input, json) : puts(json)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.sort_json(data)
|
35
|
-
if data.is_a?(Array)
|
36
|
-
data.map { |element| sort_json(element) }
|
37
|
-
elsif data.is_a?(Hash)
|
38
|
-
data
|
39
|
-
.sort_by { |key, _value| key }
|
40
|
-
.to_h
|
41
|
-
.transform_values { |value| sort_json(value) }
|
42
|
-
else
|
43
|
-
data
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|