dorian-sort-yaml 0.2.2 → 0.3.1

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 +4 -4
  2. data/bin/sort-yaml +39 -2
  3. metadata +3 -4
  4. data/lib/dorian/sort-yaml.rb +0 -47
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 829181992a0373bdadff5c9bd77d2d227ff5223bebe2fa95725f3c331c5960f4
4
- data.tar.gz: 3eaad49286c72ecf5fb1788bcd07cbb41c355e1aa626f5b033f012576e24c4e1
3
+ metadata.gz: d9713ebedbbc8833da6a38c63b3c6b4556211cb3e1679b05efd6b7d9c19047eb
4
+ data.tar.gz: d917a93abb23ba0a9bd2643c199b3f255c482b44d65b1c85000eeccc63dbb983
5
5
  SHA512:
6
- metadata.gz: 536e5cd1fb4e79368a13526000743e3f3de2fa64cfb4c49e6cbba676f7f67de6d5c613853e501722c7eeb7d1b745a4a08b75d7e057d921a66785193859ff9fe1
7
- data.tar.gz: dd5192f93e615322a9d68996722c6a73d116e573165364aa88dfda1104fcc611b2a45a5179d6aae452b235dc83a7002abd70286baa897f57d335433bb87bc54e
6
+ metadata.gz: 89fc07531b2ab1e3971d8f374886b0208b80f08d1ea95f2f0abc2c7f8da55adc15c3e1af6260c5ec7e73d3b05276bcea36a6b7069dae2985484fc83607f89315
7
+ data.tar.gz: 15d697318be14b38e6f9c40c5543a9f8f08937c387ab980f01ce67218d0a96243c165733ad7fb636286dabf01c9cefbc2aa33b88e0e5271a62df6a8eb68d5eec
data/bin/sort-yaml CHANGED
@@ -1,5 +1,42 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require "dorian/sort-yaml"
5
- Dorian::SortYaml.run
4
+ require "yaml"
5
+
6
+ def sort_yaml(data)
7
+ if data.is_a?(Array)
8
+ data.map { |element| sort_yaml(element) }
9
+ elsif data.is_a?(Hash)
10
+ data
11
+ .sort_by { |key, _value| key }
12
+ .to_h
13
+ .transform_values { |value| sort_yaml(value) }
14
+ else
15
+ data
16
+ end
17
+ end
18
+
19
+ if ARGV[0] == "--help" || ARGV[0] == "-h"
20
+ puts "USAGE: sort-yaml FILES..."
21
+ puts "USAGE: ... | sort-yaml"
22
+ exit
23
+ end
24
+
25
+ inputs = ARGV
26
+
27
+ if inputs.empty?
28
+ inputs = $stdin.each_line.to_a
29
+
30
+ inputs =
31
+ if File.exist?(inputs.first.strip)
32
+ inputs.map(&:strip)
33
+ else
34
+ [inputs.join]
35
+ end
36
+ end
37
+
38
+ inputs.each do |input|
39
+ content = File.exist?(input) ? File.read(input) : input
40
+ yaml = sort_yaml(YAML.safe_load(content)).to_yaml
41
+ File.exist?(input) ? File.write(input, yaml) : puts(yaml)
42
+ end
metadata CHANGED
@@ -1,27 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dorian-sort-yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.1
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-02-08 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 YAML files
15
15
 
16
16
  e.g. `sort-yaml config/locales/*`
17
- email: dorian@dorianmarie.fr
17
+ email: dorian@dorianmarie.com
18
18
  executables:
19
19
  - sort-yaml
20
20
  extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
23
  - bin/sort-yaml
24
- - lib/dorian/sort-yaml.rb
25
24
  homepage: https://github.com/dorianmariecom/sort-yaml
26
25
  licenses:
27
26
  - MIT
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "yaml"
4
-
5
- module Dorian
6
- class SortYaml
7
- def self.run
8
- if ARGV[0] == "--help" || ARGV[0] == "-h"
9
- puts "USAGE: sort-yaml FILES..."
10
- puts "USAGE: ... | sort-yaml"
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
- yaml = sort_yaml(YAML.safe_load(content)).to_yaml
30
- File.exist?(input) ? File.write(input, yaml) : puts(yaml)
31
- end
32
- end
33
-
34
- def self.sort_yaml(data)
35
- if data.is_a?(Array)
36
- data.map { |element| sort_yaml(element) }
37
- elsif data.is_a?(Hash)
38
- data
39
- .sort_by { |key, _value| key }
40
- .to_h
41
- .transform_values { |value| sort_yaml(value) }
42
- else
43
- data
44
- end
45
- end
46
- end
47
- end