Skip to content

Commit

Permalink
split sql and sort (+ UT for sort)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Oct 4, 2016
1 parent bdfcbec commit 91ed9cd
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 44 deletions.
48 changes: 4 additions & 44 deletions src/cli/cms/data/sql.js
Expand Up @@ -2,6 +2,7 @@ import {parse} from 'node-sqlparser'
import {Promise} from 'es6-promise'
import path from 'path'
import {
coreUtils,
config,
Manager,
FileParser,
Expand Down Expand Up @@ -127,47 +128,6 @@ export function handleSqlRequest(str, jsonPage) {
}
}

export function sortByDateDesc(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 sortByDateAsc(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 getDataSource(str) {
var res = str.substring(str.indexOf('source=') + 8, str.length)

Expand Down Expand Up @@ -229,12 +189,12 @@ export function getFromDirectory(statement, tplPath){
export function executeOrderByClause(files, orderby){
if(typeof orderby !== 'undefined' && orderby !== null) {
if(orderby.column.toLowerCase() === 'random') {
shuffle(files)
files = coreUtils.sort.shuffle(files)
}else if(orderby.column.toLowerCase() === 'date') {
if(orderby.type === 'ASC') {
files.sort(sortByDateAsc)
files.sort(coreUtils.sort.byDateAsc)
}else if(orderby.type === 'DESC') {
files.sort(sortByDateDesc)
files.sort(coreUtils.sort.byDateDesc)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/cli/core/utils/index.js
@@ -0,0 +1,5 @@
import * as sort from './sort'

export {
sort
}
40 changes: 40 additions & 0 deletions src/cli/core/utils/sort.js
@@ -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
}
2 changes: 2 additions & 0 deletions src/cli/index.js
Expand Up @@ -61,10 +61,12 @@ import Plugins from './extend/abe-plugins'

import * as cmsData from './cms/data'
import * as cmsTemplate from './cms/templates'
import * as coreUtils from './core/utils'

export {
cmsData
,cmsTemplate
,coreUtils

,fileAttr
,moment
Expand Down
47 changes: 47 additions & 0 deletions test/sort.js
@@ -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']);
});
});

0 comments on commit 91ed9cd

Please sign in to comment.