Skip to content

Commit 2bc6bbc

Browse files
author
Isaac Ramirez
committed
- refactor defaultComparator to be a common function
1 parent 29c60b9 commit 2bc6bbc

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

src/algorithms/selection-sort/selection-sort.js

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
const { StdOut } = require('../../libs')
2-
3-
/**
4-
* A default comparing function that
5-
* returns -1 when `a` is less than `b`,
6-
* returns 1 when `a` is greater than `b` or
7-
* returns 0 when `a` is equal to `b`.
8-
* @param {*} a Comparable object `a`
9-
* @param {*} b Comparable object `b`
10-
*/
11-
const defaultComparator = (a, b) => {
12-
if (a === b) return 0
13-
14-
return a < b ? -1 : 1
15-
}
2+
const { defaultComparator } = require('../../common')
163

174
/**
185
* Selection

src/algorithms/selection-sort/selection-sort.spec.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
const Selection = require('./selection-sort')
22
const { newArrayOf } = require('../../utils')
33
const { StdRandom } = require('../../libs')
4-
5-
const comparator = (a, b) => {
6-
if (a < b) return -1
7-
else if (a > b) return 1
8-
return 0
9-
}
4+
const { defaultComparator: comparator } = require('../../common')
105

116
describe('Unit Tests: Selection Sort Algorithm', () => {
127
beforeEach(() => {

src/common/comparators.common.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* A default comparing function that
3+
* returns -1 when `a` is less than `b`,
4+
* returns 1 when `a` is greater than `b` or
5+
* returns 0 when `a` is equal to `b`.
6+
* @param {*} a Comparable object `a`
7+
* @param {*} b Comparable object `b`
8+
*/
9+
const defaultComparator = (a, b) => {
10+
if (a === b) return 0
11+
12+
return a < b ? -1 : 1
13+
}
14+
15+
module.exports = {
16+
defaultComparator
17+
}

src/common/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
...require('./comparators.common')
3+
}

0 commit comments

Comments
 (0)