Skip to content

Commit

Permalink
Refactor _setOrderbyOption()
Browse files Browse the repository at this point in the history
  • Loading branch information
sutara79 committed Sep 4, 2018
1 parent f931ada commit b52bdaa
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG: jquery.ajax-combobox

## v7.5.3
#### Deprecated
- Lowercase "asc" or "desc" in `order_by` are **deprecated**.
(e.g. `order_by: ['field1 asc', 'field2 desc']`)
In the future, lowercase "asc" or "desc" are judged to be field names.

## v7.5.0
- Extend `button_img` option to accept HTML element such as `<img>` or `<svg>`.
See [Document](http://www.usamimi.info/~sutara/ajax-combobox/sample/others.html#button-image).
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ new AjaxComboBox($sqlite);
|[db_table](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_01)|string|'tbl'|Table of database to query|
|field|string|'name'|Field of table to display on result|
|[search_field](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_04)|string|=field|Field of table to search. Accept comma separated string. (e.g.: `'id, name, job'`)|
|[order_by](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_06)|mixed|=search_field|Field for sorting (e.g.: `'name DESC'`, `['name ASC', 'age DESC']`)|
|[order_by](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_06)|mixed|=search_field|Field for sorting (e.g. `'name DESC'`, `['name ASC', 'age DESC']`).<br>`ASC` or `DESC` should be UPPERCASE.|
|[and_or](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_05)|string|'AND'|Boolean searching ('AND', 'OR')|
|[per_page](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_02)|number|10|Amount of items per page|
|[navi_num](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_02)|number|5|Amount of page-link on navi|
Expand Down
2 changes: 1 addition & 1 deletion dist/js/jquery.ajax-combobox.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jquery.ajax-combobox",
"description": "jQuery plugin to create a text box which can auto-complete and pull-down-select.",
"license": "MIT",
"version": "7.5.2",
"version": "7.5.3",
"author": "Yuusaku Miyazaki <[email protected]>",
"homepage": "https://github.com/sutara79/jquery.ajax-combobox",
"main": "dist/jquery.ajax-combobox.min.js",
Expand Down
2 changes: 1 addition & 1 deletion sample/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ <h5 class="card-header">
'jquery.ajax-combobox.php',
{
<span class="green">order_by</span>: [
'name DESC', // ASC or DESC
'name DESC', // ASC or DESC should be UPPERCASE.
'created'
]
}
Expand Down
2 changes: 1 addition & 1 deletion sample/text-area.html
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ <h5 class="card-header">
{
pattern: ['@', ''],
db_table: 'tag',
<span class="green">order_by</span>: 'name DESC'
<span class="green">order_by</span>: 'name DESC' // ASC or DESC should be UPPERCASE.
}
]
}
Expand Down
35 changes: 17 additions & 18 deletions src/method/03-_setOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,26 +245,25 @@ export default {
* Adjust an array of "ORDER BY" to use it in the code.
*
* @private
* @arg {Array} arg_order - Array of "ORDER BY" (not have processed).
* @arg {string} arg_field - Field to search.
* @arg {Array|string} orders - Array of "ORDER BY" (not have processed).
* @arg {string} field - Field to search.
* @return {Array} - Array of "ORDER BY" (have processed).
*/
_setOrderbyOption: function(arg_order, arg_field) {
var arr = [];
var orders = [];
if (typeof arg_order == 'object') {
for (var i = 0; i < arg_order.length; i++) {
orders = $.trim(arg_order[i]).split(' ');
arr[i] = (orders.length == 2) ? orders : [orders[0], 'ASC'];
}
} else {
orders = $.trim(arg_order).split(' ');
arr[0] = (orders.length == 2) ?
orders :
(orders[0].match(/^(ASC|DESC)$/i)) ?
[arg_field, orders[0]] :
[orders[0], 'ASC'];
_setOrderbyOption: function(orders, field) {
if (typeof orders == 'string') orders = new Array(orders);

var result = [];
var arrSplit = [];

for (var i = 0; i < orders.length; i++) {
arrSplit = $.trim(orders[i]).split(/ +/);
result[i] = (arrSplit.length == 2) ?
arrSplit :
(arrSplit[0].match(/^(ASC|DESC)$/i)) ? // In the future, lowercase "asc" or "desc" are judged to be field names.
[field, arrSplit[0]] :
[arrSplit[0], 'ASC'];
}
return arr;

return result;
}
};
1 change: 1 addition & 0 deletions test/js/unit/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<script src="jQuery.fn/ajaxComboBox.js"></script>
<script src="jQuery.ajaxComboBox/setOption.js"></script>
<script src="jQuery.ajaxComboBox/strToArray.js"></script>
<script src="jQuery.ajaxComboBox/setOrderbyOption.js"></script>

<script>
mocha.run(function (err) {
Expand Down
60 changes: 60 additions & 0 deletions test/js/unit/jQuery.ajaxComboBox/setOrderbyOption.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @file Unit Testing
*/
describe('$.ajaxComboBox._setOrderbyOption', () => {
it('should convert strings to array', () => {
const orders = 'field1 ASC';
const field = 'field999';
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
assert.equal(result[0][0], 'field1');
assert.equal(result[0][1], 'ASC');
});

it('should accept multiple space', () => {
const orders = 'field1 DESC';
const field = 'field999';
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
assert.equal(result[0][0], 'field1');
assert.equal(result[0][1], 'DESC');
});

it('should complement a field name', () => {
const orders = 'ASC';
const field = 'field999';
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
assert.equal(result[0][0], 'field999');
assert.equal(result[0][1], 'ASC');
});

it('should accept lowercase "asc" or "desc"', () => {
// In the future, lowercase "asc" or "desc" are judged to be field names.

const orders = 'desc';
const field = 'field999';
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
assert.equal(result[0][0], 'field999');
assert.equal(result[0][1], 'desc');
});

it('should accept an array', () => {
const orders = [
'field1',
'field2 DESC',
'field3 ASC',
'DESC'
];
const field = 'field999';
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
assert.equal(result[0][0], 'field1');
assert.equal(result[0][1], 'ASC');

assert.equal(result[1][0], 'field2');
assert.equal(result[1][1], 'DESC');

assert.equal(result[2][0], 'field3');
assert.equal(result[2][1], 'ASC');

assert.equal(result[3][0], 'field999');
assert.equal(result[3][1], 'DESC');
});
});

0 comments on commit b52bdaa

Please sign in to comment.