Skip to content

Commit 52b0c74

Browse files
committed
Added unit tests
1 parent 8f38d56 commit 52b0c74

10 files changed

+152
-33
lines changed

README.md

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ sortBy(arr);
118118
*/
119119
```
120120

121-
### Sorting DESC
121+
### Sorting DESC: numbers
122122

123123
```javascript
124124
let arr = [5, 1, 8, 0, 3, 7, 10, 4, 3, 8];
@@ -130,7 +130,7 @@ sortBy(arr, n => -n);
130130
*/
131131
```
132132

133-
### Sorting DESC date-strings as `Date`
133+
### Sorting DESC: date-strings as `Date`
134134

135135
```javascript
136136
let arr = ["1983/03/06", "1980/12/24", "1985/08/31", "1983/03/05"];
@@ -142,12 +142,12 @@ sortBy(arr, (s) => -new Date(s));
142142
*/
143143
```
144144

145-
### Sorting DESC strings
145+
### Sorting DESC: strings
146146

147147
Because we use the minus **(-)** symbol to specify a descending order,
148-
it will produce a `NaN` value if it is used with a `String` element.
149-
That's why the flag **`"desc:"`** (not case sensitive) is prefixed
150-
to the string element in the `parser` callback.
148+
it would produce a `NaN` value if used with a `String` value.
149+
So the flag **`"desc:"`** (not case sensitive) is prefixed
150+
to the string value in the `parser` callback.
151151

152152
```javascript
153153
var arr = ['único', 'cosas', 'Árbol', 'fútbol', 'algo'];
@@ -158,14 +158,15 @@ sortBy(arr, item => 'desc:' + item);
158158
* ["único", "fútbol", "cosas", "Árbol", "algo"]
159159
*/
160160

161+
// sorting ASC: accented words
161162
sortBy(arr);
162163
/**
163164
* expected:
164165
* ["algo", "Árbol", "cosas", "fútbol", "único"]
165166
*/
166167
```
167168

168-
### Sorting accented words by @text
169+
### Sorting ASC: accented words by @text
169170

170171
```javascript
171172
var arr = [
@@ -186,25 +187,25 @@ sortBy(arr, item => item.text);
186187
*/
187188
```
188189

189-
### Sorting DESC by @a, after ASC by @d (as Date)
190+
### Sorting DESC by @id, after ASC by @dob (as Date)
190191

191192
```javascript
192193
let arr = [
193-
{ a: 8, d: "1985/08/31" },
194-
{ a: 2, d: "1980/12/24" },
195-
{ a: 5, d: "1983/03/06" },
196-
{ a: 8, d: "1983/03/06" }
194+
{ id: 8, dob: '1985/08/31' },
195+
{ id: 2, dob: '1980/12/24' },
196+
{ id: 5, dob: '1983/03/06' },
197+
{ id: 8, dob: '1983/03/06' }
197198
];
198199

199-
sortBy(arr, (o) => [-o.a, new Date(o.d)]);
200+
sortBy(arr, (o) => [-o.id, new Date(o.dob)]);
200201

201202
/**
202203
* expected:
203204
* [
204-
* { a: 8, d: "1983/03/06" },
205-
* { a: 8, d: "1985/08/31" },
206-
* { a: 5, d: "1983/03/06" },
207-
* { a: 2, d: "1980/12/24" }
205+
* { id: 8, dob: "1983/03/06" },
206+
* { id: 8, dob: "1985/08/31" },
207+
* { id: 5, dob: "1983/03/06" },
208+
* { id: 2, dob: "1980/12/24" }
208209
* ]
209210
*/
210211
```
@@ -213,13 +214,13 @@ sortBy(arr, (o) => [-o.a, new Date(o.d)]);
213214

214215
```javascript
215216
let arr = [
216-
{ id: 4, name: "Pedro" },
217-
{ id: 6, name: "Lucía" },
218-
{ id: 7, name: "paco" },
219-
{ id: 3, name: "luis" }
217+
{ id: 4, name: 'Pedro' },
218+
{ id: 6, name: 'Lucía' },
219+
{ id: 7, name: 'paco' },
220+
{ id: 3, name: 'luis' }
220221
];
221222

222-
sortBy(arr, item => "DESC:" + item.name);
223+
sortBy(arr, item => `DESC:${item.name}`);
223224

224225
/**
225226
* expected:
@@ -232,25 +233,25 @@ sortBy(arr, item => "DESC:" + item.name);
232233
*/
233234
```
234235

235-
### Sorting ASC by @name, after DESC by @age, after ASC by @a
236+
### Sorting ASC by @name, after DESC by @age, after ASC by @id
236237

237238
```javascript
238239
let arr = [
239-
{ a: 9, age: 26, name: "pedro" },
240-
{ a: 6, age: 21, name: "Pedro" },
241-
{ a: 7, age: 26, name: "Maria" },
242-
{ a: 2, age: 26, name: "maría" }
240+
{ id: 9, age: 26, name: 'pedro' },
241+
{ id: 6, age: 21, name: 'Pedro' },
242+
{ id: 7, age: 26, name: 'Maria' },
243+
{ id: 2, age: 26, name: 'maría' }
243244
];
244245

245-
sortBy(arr, item => [item.name, -item.age, item.a]);
246+
sortBy(arr, item => [item.name, -item.age, item.id]);
246247

247248
/**
248249
* expected:
249250
* [
250-
* { a: 2, age: 26, name: "maría" },
251-
* { a: 7, age: 26, name: "Maria" },
252-
* { a: 9, age: 26, name: "pedro" },
253-
* { a: 6, age: 21, name: "Pedro" }
251+
* { id: 2, age: 26, name: "maría" },
252+
* { id: 7, age: 26, name: "Maria" },
253+
* { id: 9, age: 26, name: "pedro" },
254+
* { id: 6, age: 21, name: "Pedro" }
254255
* ]
255256
*/
256257
```

test/sorting-asc-by-text.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import test from 'ava';
2+
import sortBy from '../dist/sort-by.min';
3+
4+
test('Sorting ASC: accented words by @text', async (t) => {
5+
const arr = [
6+
{id: 10, text: 'Woche'},
7+
{id: 20, text: 'wöchentlich'},
8+
{id: 30, text: 'wäre'},
9+
];
10+
11+
const expected = [
12+
{id: 30, text: 'wäre'},
13+
{id: 10, text: 'Woche'},
14+
{id: 20, text: 'wöchentlich'},
15+
];
16+
17+
sortBy(arr, item => item.text);
18+
t.deepEqual(arr, expected);
19+
});
File renamed without changes.

test/sorting-asc-strings.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava';
2+
import sortBy from '../dist/sort-by.min';
3+
4+
test('Sorting ASC: strings', async (t) => {
5+
const arr = ['único', 'cosas', 'Árbol', 'fútbol', 'algo'];
6+
const expected = ['algo', 'Árbol', 'cosas', 'fútbol', 'único'];
7+
8+
sortBy(arr);
9+
t.deepEqual(arr, expected);
10+
});

test/sorting-by-id-dob.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import test from 'ava';
2+
import sortBy from '../dist/sort-by.min';
3+
4+
test('Sorting DESC by @id, after ASC by @dob (as Date)', async (t) => {
5+
const arr = [
6+
{id: 8, dob: '1985/08/31'},
7+
{id: 2, dob: '1980/12/24'},
8+
{id: 5, dob: '1983/03/06'},
9+
{id: 8, dob: '1983/03/06'},
10+
];
11+
12+
const expected = [
13+
{id: 8, dob: '1983/03/06'},
14+
{id: 8, dob: '1985/08/31'},
15+
{id: 5, dob: '1983/03/06'},
16+
{id: 2, dob: '1980/12/24'},
17+
];
18+
19+
sortBy(arr, o => [-o.id, new Date(o.dob)]);
20+
t.deepEqual(arr, expected);
21+
});

test/sorting-by-name-age-id.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import test from 'ava';
2+
import sortBy from '../dist/sort-by.min';
3+
4+
test('Sorting ASC by @name, after DESC by @age, after ASC by @id', async (t) => {
5+
const arr = [
6+
{id: 9, age: 26, name: 'pedro'},
7+
{id: 6, age: 21, name: 'Pedro'},
8+
{id: 7, age: 26, name: 'Maria'},
9+
{id: 2, age: 26, name: 'maría'},
10+
];
11+
12+
const expected = [
13+
{id: 2, age: 26, name: 'maría'},
14+
{id: 7, age: 26, name: 'Maria'},
15+
{id: 9, age: 26, name: 'pedro'},
16+
{id: 6, age: 21, name: 'Pedro'},
17+
];
18+
19+
sortBy(arr, item => [item.name, -item.age, item.id]);
20+
t.deepEqual(arr, expected);
21+
});

test/sorting-by-name.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import test from 'ava';
2+
import sortBy from '../dist/sort-by.min';
3+
4+
test('Sorting DESC by @name', async (t) => {
5+
const arr = [
6+
{id: 4, name: 'Pedro'},
7+
{id: 6, name: 'Lucía'},
8+
{id: 7, name: 'paco'},
9+
{id: 3, name: 'luis'},
10+
];
11+
12+
const expected = [
13+
{id: 4, name: 'Pedro'},
14+
{id: 7, name: 'paco'},
15+
{id: 3, name: 'luis'},
16+
{id: 6, name: 'Lucía'},
17+
];
18+
19+
sortBy(arr, item => `DESC:${item.name}`);
20+
t.deepEqual(arr, expected);
21+
});

test/sorting-desc-date.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava';
2+
import sortBy from '../dist/sort-by.min';
3+
4+
test('Sorting DESC: date-strings as Date', async (t) => {
5+
const arr = ['1983/03/06', '1980/12/24', '1985/08/31', '1983/03/05'];
6+
const expected = ['1985/08/31', '1983/03/06', '1983/03/05', '1980/12/24'];
7+
8+
sortBy(arr, s => -new Date(s));
9+
t.deepEqual(arr, expected);
10+
});

test/sorting-desc.js renamed to test/sorting-desc-numbers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import test from 'ava';
22
import sortBy from '../dist/sort-by.min';
33

4-
test('Sorting DESC', async (t) => {
4+
test('Sorting DESC: numbers', async (t) => {
55
const arr = [5, 1, 8, 0, 3, 7, 10, 4, 3, 8];
66
const expected = [10, 8, 8, 7, 5, 4, 3, 3, 1, 0];
77

test/sorting-desc-strings.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import test from 'ava';
2+
import sortBy from '../dist/sort-by.min';
3+
4+
/**
5+
* Because we use the minus (-) symbol to specify a descending order,
6+
* it would produce a `NaN` value if used with a `String` value.
7+
* So the flag `"desc:"` (not case sensitive) is prefixed
8+
* to the string value in the `parser` callback.
9+
*/
10+
test('Sorting DESC: strings', async (t) => {
11+
const arr = ['único', 'cosas', 'Árbol', 'fútbol', 'algo'];
12+
const expected = ['único', 'fútbol', 'cosas', 'Árbol', 'algo'];
13+
14+
sortBy(arr, item => `desc:${item}`);
15+
t.deepEqual(arr, expected);
16+
});

0 commit comments

Comments
 (0)