Skip to content

Commit 24445c0

Browse files
author
pasit.r
committed
add sample js
1 parent 0e781ba commit 24445c0

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/sorter.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const sort = (list = [], key = '') => list.sort(compare(key))
2+
3+
const compare = key => (a, b) => a[key].localeCompare(b[key])

test/sorter.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { sort } from '../src/sorter'
2+
import { expect } from 'chai'
3+
4+
describe('sort', () => {
5+
6+
const data = [{
7+
name: "Foo"
8+
}, {
9+
name: "Bar"
10+
}, {
11+
name: "Zab"
12+
}]
13+
14+
it('should sort string property', () => {
15+
const sorted = sort(data, 'name')
16+
expect(sorted[0].name).to.eq('Bar')
17+
expect(sorted[1].name).to.eq('Foo')
18+
expect(sorted[2].name).to.eq('Zab')
19+
})
20+
})

0 commit comments

Comments
 (0)