dorian-sort-yaml 0.2.2 → 0.3.0

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-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: 3a8e2db05d6c40e7ac8ca91793699c22b6fba871b3223d96ca379f8a2f065d3a
4
+ data.tar.gz: 8fbc2e4adfed3e7ffb0cd53ad6247174869fa0a543d7000efc55d101513c0145
5
5
  SHA512:
6
- metadata.gz: 536e5cd1fb4e79368a13526000743e3f3de2fa64cfb4c49e6cbba676f7f67de6d5c613853e501722c7eeb7d1b745a4a08b75d7e057d921a66785193859ff9fe1
7
- data.tar.gz: dd5192f93e615322a9d68996722c6a73d116e573165364aa88dfda1104fcc611b2a45a5179d6aae452b235dc83a7002abd70286baa897f57d335433bb87bc54e
6
+ metadata.gz: 6e57da1e308e4b7149908eb1b332c80281cb5a2407c13a7b470a7382db3f3d4a011ab099da699e56e0124e9645dd21570f72b5aa6ac56c971911be791aede564
7
+ data.tar.gz: 78802f796358ef285f4efbac9287d9b9e1aab1e7e1ab6dfadccc881851bdc032cfc69275a67fa7f9f5af695c14ecec983512f09aa81554bfed796a6824a761a1
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.0
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-08 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