dorian-sort-json 0.1.0

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/sort-json +4 -0
  3. data/lib/dorian/sort-json.rb +33 -0
  4. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 593c7abd174aae6b4c56ee3793296ddd73a0df8212471a12f646ddd7b22d9b9a
4
+ data.tar.gz: 851ad6aac98770aaf060c6be7df99e99d4905bcbf8cc9480c5bb4e887920ef60
5
+ SHA512:
6
+ metadata.gz: b0868044f9a658562ce8bc8322bc0abc870557ab111fde02ea54e4e176d1436d651db4e27685ed04b652a3693d8df5c876c5f8e3d47633c729bf7379ebef76e2
7
+ data.tar.gz: 84acbecf575d1c0c6100e257b8411ebb895bd6049ccc7d301fb6715522d489b5c3d9d6378a43c478fd7310868d14a0ed10f27899a6a8d2f8abdd467f87a87f8b
data/bin/sort-json ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dorian/sort-json'
4
+ Dorian::SortJson.run
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+
3
+ module Dorian
4
+ class SortJson
5
+ def self.run
6
+ if ARGV.size < 1
7
+ puts 'USAGE: sort-json FILE [FILE...]'
8
+ exit
9
+ end
10
+
11
+ ARGV.each do |filepath|
12
+ File.write(
13
+ filepath,
14
+ JSON.pretty_generate(sort_json(JSON.parse(File.read(filepath)))) +
15
+ "\n"
16
+ )
17
+ end
18
+ end
19
+
20
+ def self.sort_json(data)
21
+ if data.is_a?(Array)
22
+ data.map { |element| sort_json(element) }
23
+ elsif data.is_a?(Hash)
24
+ data
25
+ .sort_by { |key, _value| key }
26
+ .to_h
27
+ .transform_values { |value| sort_json(value) }
28
+ else
29
+ data
30
+ end
31
+ end
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dorian-sort-json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dorian Marié
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ Sorts keys of hashes of JSON files
15
+
16
+ e.g. `sort-json package.json`
17
+ email: dorian@dorianmarie.fr
18
+ executables:
19
+ - sort-json
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - bin/sort-json
24
+ - lib/dorian/sort-json.rb
25
+ homepage: https://github.com/dorianmariefr/sort-json
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.2.22
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Sorts keys of hashes of JSON files
48
+ test_files: []