Skip to content

Commit

Permalink
feat(lib): implement sorting of config diff object (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ta2edchimp committed Apr 20, 2016
1 parent 6704d62 commit dc6994d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/lib/sort-rules.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
'use strict'

function getSortedRules(rules) {
return rules.sort(function sort(a, b) {
return a > b ? 1 : -1
})
return Array.isArray(rules) ? getSortedRulesArray(rules) : transformIntoSortedRulesArray(rules)
}

function getSortedRulesArray(rules) {
return rules.sort(sortAlphabetically)
}

function transformIntoSortedRulesArray(rules) {
var sortedRules = []

Object.keys(rules)
.sort(sortAlphabetically)
.forEach(function mapRules(ruleName) {
var rule = {}
rule[ruleName] = rules[ruleName]
sortedRules.push(rule)
})

return sortedRules
}

function sortAlphabetically(a, b) {
return a > b ? 1 : -1
}

module.exports = getSortedRules
11 changes: 11 additions & 0 deletions test/lib/sort-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ describe('sort-rules', function() {
['a', 'aa', 'ab', 'b', 'c']
)
})

it('should return sorted rule configs', function() {
assert.deepEqual(
sortRules({'bar-rule': {config1: '1', config2: '2'}, 'baz-rule': {config1: '3', config2: '4'}}),
[{'bar-rule': {config1: '1', config2: '2'}}, {'baz-rule': {config1: '3', config2: '4'}}]
)
assert.deepEqual(
sortRules({'foo-rule': {config1: '1', config2: '2'}, 'bar-rule': {config1: '3', config2: '4'}}),
[{'bar-rule': {config1: '3', config2: '4'}}, {'foo-rule': {config1: '1', config2: '2'}}]
)
})
})

0 comments on commit dc6994d

Please sign in to comment.