Skip to content

Commit b52bdaa

Browse files
committed
Refactor _setOrderbyOption()
1 parent f931ada commit b52bdaa

File tree

9 files changed

+89
-23
lines changed

9 files changed

+89
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG: jquery.ajax-combobox
22

3+
## v7.5.3
4+
#### Deprecated
5+
- Lowercase "asc" or "desc" in `order_by` are **deprecated**.
6+
(e.g. `order_by: ['field1 asc', 'field2 desc']`)
7+
In the future, lowercase "asc" or "desc" are judged to be field names.
8+
39
## v7.5.0
410
- Extend `button_img` option to accept HTML element such as `<img>` or `<svg>`.
511
See [Document](http://www.usamimi.info/~sutara/ajax-combobox/sample/others.html#button-image).

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ new AjaxComboBox($sqlite);
7070
|[db_table](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_01)|string|'tbl'|Table of database to query|
7171
|field|string|'name'|Field of table to display on result|
7272
|[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'`)|
73-
|[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']`)|
73+
|[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.|
7474
|[and_or](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_05)|string|'AND'|Boolean searching ('AND', 'OR')|
7575
|[per_page](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_02)|number|10|Amount of items per page|
7676
|[navi_num](https://sutara79-php.herokuapp.com/demo/jquery.ajax-combobox/sample/basic.html#sample01_02)|number|5|Amount of page-link on navi|

dist/js/jquery.ajax-combobox.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "jquery.ajax-combobox",
33
"description": "jQuery plugin to create a text box which can auto-complete and pull-down-select.",
44
"license": "MIT",
5-
"version": "7.5.2",
5+
"version": "7.5.3",
66
"author": "Yuusaku Miyazaki <toumin.m7@gmail.com>",
77
"homepage": "https://github.com/sutara79/jquery.ajax-combobox",
88
"main": "dist/jquery.ajax-combobox.min.js",

sample/basic.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ <h5 class="card-header">
233233
'jquery.ajax-combobox.php',
234234
{
235235
<span class="green">order_by</span>: [
236-
'name DESC', // ASC or DESC
236+
'name DESC', // ASC or DESC should be UPPERCASE.
237237
'created'
238238
]
239239
}

sample/text-area.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ <h5 class="card-header">
290290
{
291291
pattern: ['@', ''],
292292
db_table: 'tag',
293-
<span class="green">order_by</span>: 'name DESC'
293+
<span class="green">order_by</span>: 'name DESC' // ASC or DESC should be UPPERCASE.
294294
}
295295
]
296296
}

src/method/03-_setOption.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,26 +245,25 @@ export default {
245245
* Adjust an array of "ORDER BY" to use it in the code.
246246
*
247247
* @private
248-
* @arg {Array} arg_order - Array of "ORDER BY" (not have processed).
249-
* @arg {string} arg_field - Field to search.
248+
* @arg {Array|string} orders - Array of "ORDER BY" (not have processed).
249+
* @arg {string} field - Field to search.
250250
* @return {Array} - Array of "ORDER BY" (have processed).
251251
*/
252-
_setOrderbyOption: function(arg_order, arg_field) {
253-
var arr = [];
254-
var orders = [];
255-
if (typeof arg_order == 'object') {
256-
for (var i = 0; i < arg_order.length; i++) {
257-
orders = $.trim(arg_order[i]).split(' ');
258-
arr[i] = (orders.length == 2) ? orders : [orders[0], 'ASC'];
259-
}
260-
} else {
261-
orders = $.trim(arg_order).split(' ');
262-
arr[0] = (orders.length == 2) ?
263-
orders :
264-
(orders[0].match(/^(ASC|DESC)$/i)) ?
265-
[arg_field, orders[0]] :
266-
[orders[0], 'ASC'];
252+
_setOrderbyOption: function(orders, field) {
253+
if (typeof orders == 'string') orders = new Array(orders);
254+
255+
var result = [];
256+
var arrSplit = [];
257+
258+
for (var i = 0; i < orders.length; i++) {
259+
arrSplit = $.trim(orders[i]).split(/ +/);
260+
result[i] = (arrSplit.length == 2) ?
261+
arrSplit :
262+
(arrSplit[0].match(/^(ASC|DESC)$/i)) ? // In the future, lowercase "asc" or "desc" are judged to be field names.
263+
[field, arrSplit[0]] :
264+
[arrSplit[0], 'ASC'];
267265
}
268-
return arr;
266+
267+
return result;
269268
}
270269
};

test/js/unit/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<script src="jQuery.fn/ajaxComboBox.js"></script>
2626
<script src="jQuery.ajaxComboBox/setOption.js"></script>
2727
<script src="jQuery.ajaxComboBox/strToArray.js"></script>
28+
<script src="jQuery.ajaxComboBox/setOrderbyOption.js"></script>
2829

2930
<script>
3031
mocha.run(function (err) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @file Unit Testing
3+
*/
4+
describe('$.ajaxComboBox._setOrderbyOption', () => {
5+
it('should convert strings to array', () => {
6+
const orders = 'field1 ASC';
7+
const field = 'field999';
8+
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
9+
assert.equal(result[0][0], 'field1');
10+
assert.equal(result[0][1], 'ASC');
11+
});
12+
13+
it('should accept multiple space', () => {
14+
const orders = 'field1 DESC';
15+
const field = 'field999';
16+
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
17+
assert.equal(result[0][0], 'field1');
18+
assert.equal(result[0][1], 'DESC');
19+
});
20+
21+
it('should complement a field name', () => {
22+
const orders = 'ASC';
23+
const field = 'field999';
24+
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
25+
assert.equal(result[0][0], 'field999');
26+
assert.equal(result[0][1], 'ASC');
27+
});
28+
29+
it('should accept lowercase "asc" or "desc"', () => {
30+
// In the future, lowercase "asc" or "desc" are judged to be field names.
31+
32+
const orders = 'desc';
33+
const field = 'field999';
34+
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
35+
assert.equal(result[0][0], 'field999');
36+
assert.equal(result[0][1], 'desc');
37+
});
38+
39+
it('should accept an array', () => {
40+
const orders = [
41+
'field1',
42+
'field2 DESC',
43+
'field3 ASC',
44+
'DESC'
45+
];
46+
const field = 'field999';
47+
const result = $.ajaxComboBox.prototype._setOrderbyOption(orders, field);
48+
assert.equal(result[0][0], 'field1');
49+
assert.equal(result[0][1], 'ASC');
50+
51+
assert.equal(result[1][0], 'field2');
52+
assert.equal(result[1][1], 'DESC');
53+
54+
assert.equal(result[2][0], 'field3');
55+
assert.equal(result[2][1], 'ASC');
56+
57+
assert.equal(result[3][0], 'field999');
58+
assert.equal(result[3][1], 'DESC');
59+
});
60+
});

0 commit comments

Comments
 (0)