Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nicolaslabbe
committed
Oct 4, 2016
1 parent
bdfcbec
commit 91ed9cd
Showing
5 changed files
with
98 additions
and
44 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,5 @@ | ||
import * as sort from './sort' | ||
|
||
export { | ||
sort | ||
} |
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,40 @@ | ||
export function byDateDesc(a, b) { | ||
var dateA = new Date(a.date) | ||
var dateB = new Date(b.date) | ||
if(dateA < dateB) { | ||
return 1 | ||
}else if(dateA > dateB) { | ||
return -1 | ||
} | ||
return 0 | ||
} | ||
|
||
export function shuffle(array) { | ||
var currentIndex = array.length, temporaryValue, randomIndex | ||
|
||
// While there remain elements to shuffle... | ||
while (0 !== currentIndex) { | ||
|
||
// Pick a remaining element... | ||
randomIndex = Math.floor(Math.random() * currentIndex) | ||
currentIndex -= 1 | ||
|
||
// And swap it with the current element. | ||
temporaryValue = array[currentIndex] | ||
array[currentIndex] = array[randomIndex] | ||
array[randomIndex] = temporaryValue | ||
} | ||
|
||
return array | ||
} | ||
|
||
export function byDateAsc(a, b) { | ||
var dateA = new Date(a.date) | ||
var dateB = new Date(b.date) | ||
if(dateA > dateB) { | ||
return 1 | ||
}else if(dateA < dateB) { | ||
return -1 | ||
} | ||
return 0 | ||
} |
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,47 @@ | ||
var chai = require('chai'); | ||
var fse = require('fs-extra'); | ||
var config = require('../src/cli').config | ||
config.set({root: __dirname + '/fixtures'}) | ||
|
||
var Manager = require('../src/cli').Manager; | ||
var coreUtils = require('../src/cli').coreUtils | ||
|
||
describe('Sort', function() { | ||
before( function(done) { | ||
Manager.instance.init() | ||
.then(function () { | ||
done() | ||
|
||
}.bind(this)) | ||
}); | ||
|
||
/** | ||
* coreUtils.sort.byDateAsc | ||
* | ||
*/ | ||
it('coreUtils.sort.byDateAsc', function() { | ||
var list = Manager.instance.getList() | ||
list.sort(coreUtils.sort.byDateAsc) | ||
chai.expect(list[0].name).to.contain('homepage'); | ||
}); | ||
|
||
/** | ||
* coreUtils.sort.byDateDesc | ||
* | ||
*/ | ||
it('coreUtils.sort.byDateDesc', function() { | ||
var list = Manager.instance.getList() | ||
list.sort(coreUtils.sort.byDateDesc) | ||
chai.expect(list[0].name).to.contain('article'); | ||
}); | ||
|
||
/** | ||
* coreUtils.sort.shuffle | ||
* | ||
*/ | ||
it('coreUtils.sort.shuffle', function() { | ||
var list = Manager.instance.getList() | ||
var shuffled = coreUtils.sort.shuffle(list) | ||
chai.expect(shuffled[0].name).to.be.oneOf(['article-1.json', 'homepage-1.json']); | ||
}); | ||
}); |