-
-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort translations after filtering them out.
Close #691.
- Loading branch information
Showing
3 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module I18nJS | ||
def self.sort_hash(hash) | ||
return hash unless hash.is_a?(Hash) | ||
|
||
hash.keys.sort_by(&:to_s).each_with_object({}) do |key, seed| | ||
value = hash[key] | ||
seed[key] = value.is_a?(Hash) ? sort_hash(value) : value | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class SortHashTest < Minitest::Test | ||
test "returns non-hash objects" do | ||
assert_equal 1, I18nJS.sort_hash(1) | ||
end | ||
|
||
test "sorts shallow hash" do | ||
expected = {a: 1, b: 2, c: 3} | ||
|
||
assert_equal expected, I18nJS.sort_hash(c: 3, a: 1, b: 2) | ||
end | ||
|
||
test "sorts nested hash" do | ||
expected = {a: {b: 1, c: 2}, d: 3} | ||
|
||
assert_equal expected, I18nJS.sort_hash(d: 3, a: {c: 2, b: 1}) | ||
end | ||
end |