Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jio
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Nicolas Wavrant
jio
Commits
fdcb654c
Commit
fdcb654c
authored
Oct 09, 2017
by
Vincent Bechu
Committed by
Vincent Bechu
Oct 11, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[query] use key_schema to sort
parent
167105f4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
6 deletions
+103
-6
src/queries/query.js
src/queries/query.js
+31
-6
test/queries/tests.js
test/queries/tests.js
+72
-0
No files found.
src/queries/query.js
View file @
fdcb654c
...
...
@@ -44,8 +44,8 @@
* @param {String} [way="ascending"] 'ascending' or 'descending'
* @return {Function} The sort function
*/
function
sortFunction
(
key
,
way
)
{
var
result
;
function
sortFunction
(
key
,
way
,
key_schema
)
{
var
result
,
cast_to
;
if
(
way
===
'
descending
'
)
{
result
=
1
;
}
else
if
(
way
===
'
ascending
'
)
{
...
...
@@ -54,6 +54,29 @@
throw
new
TypeError
(
"
Query.sortFunction():
"
+
"
Argument 2 must be 'ascending' or 'descending'
"
);
}
if
(
key_schema
!==
undefined
&&
key_schema
.
key_set
!==
undefined
&&
key_schema
.
key_set
[
key
]
!==
undefined
&&
key_schema
.
key_set
[
key
].
cast_to
!==
undefined
)
{
if
(
typeof
key_schema
.
key_set
[
key
].
cast_to
===
"
string
"
)
{
cast_to
=
key_schema
.
cast_lookup
[
key_schema
.
key_set
[
key
].
cast_to
];
}
else
{
cast_to
=
key_schema
.
key_set
[
key
].
cast_to
;
}
return
function
(
a
,
b
)
{
var
f_a
=
cast_to
(
a
[
key
]),
f_b
=
cast_to
(
b
[
key
]);
if
(
typeof
f_b
.
cmp
===
'
function
'
)
{
return
result
*
f_b
.
cmp
(
f_a
);
}
if
(
f_a
>
f_b
)
{
return
-
result
;
}
if
(
f_a
<
f_b
)
{
return
result
;
}
return
0
;
};
}
return
function
(
a
,
b
)
{
// this comparison is 5 times faster than json comparison
var
i
,
l
;
...
...
@@ -78,6 +101,7 @@
};
}
/**
* Sort a list of items, according to keys and directions.
*
...
...
@@ -85,7 +109,7 @@
* @param {Array} list The item list to sort
* @return {Array} The filtered list
*/
function
sortOn
(
sort_on_option
,
list
)
{
function
sortOn
(
sort_on_option
,
list
,
key_schema
)
{
var
sort_index
;
if
(
!
Array
.
isArray
(
sort_on_option
))
{
throw
new
TypeError
(
"
jioquery.sortOn():
"
+
...
...
@@ -95,7 +119,8 @@
sort_index
-=
1
)
{
list
.
sort
(
sortFunction
(
sort_on_option
[
sort_index
][
0
],
sort_on_option
[
sort_index
][
1
]
sort_on_option
[
sort_index
][
1
],
key_schema
));
}
return
list
;
...
...
@@ -270,7 +295,7 @@
}
if
(
option
.
sort_on
)
{
sortOn
(
option
.
sort_on
,
item_list
);
sortOn
(
option
.
sort_on
,
item_list
,
this
.
_key_schema
);
}
if
(
option
.
limit
)
{
...
...
@@ -609,7 +634,7 @@
*/
QueryFactory
.
create
=
function
(
object
,
key_schema
)
{
if
(
object
===
""
)
{
return
new
Query
();
return
new
Query
(
key_schema
);
}
if
(
typeof
object
===
"
string
"
)
{
object
=
parseStringToObject
(
object
);
...
...
test/queries/tests.js
View file @
fdcb654c
...
...
@@ -505,6 +505,78 @@
});
});
test
(
'
Query & sort_on option
'
,
function
()
{
var
doc_list
=
[
{
idendifier
:
'
a
'
,
date
:
"
Fri, 08 Sep 2017 07:46:27 +0000
"
},
{
identifier
:
'
c
'
,
date
:
"
Wed, 06 Sep 2017 00:27:13 +0000
"
},
{
identifier
:
'
b
'
,
date
:
"
Thu, 07 Sep 2017 18:59:23 +0000
"
}
];
stop
();
expect
(
2
);
jIO
.
QueryFactory
.
create
(
""
).
exec
(
doc_list
,
{
sort_on
:
[[
'
date
'
,
'
descending
'
]]}
).
then
(
function
(
list
)
{
var
key_schema
=
{
key_set
:
{
date
:
{
read_from
:
'
date
'
,
cast_to
:
'
dateType
'
}
},
cast_lookup
:
{
dateType
:
function
(
str
)
{
return
window
.
jiodate
.
JIODate
(
new
Date
(
str
).
toISOString
());
}
}
};
deepEqual
(
list
,
[
{
identifier
:
'
c
'
,
date
:
"
Wed, 06 Sep 2017 00:27:13 +0000
"
},
{
identifier
:
'
b
'
,
date
:
"
Thu, 07 Sep 2017 18:59:23 +0000
"
},
{
idendifier
:
'
a
'
,
date
:
"
Fri, 08 Sep 2017 07:46:27 +0000
"
}
],
'
Document list is sorted
'
);
return
jIO
.
QueryFactory
.
create
(
""
,
key_schema
).
exec
(
doc_list
,
{
sort_on
:
[[
'
date
'
,
'
ascending
'
]]}
);
})
.
then
(
function
(
list
)
{
deepEqual
(
list
,
[
{
identifier
:
'
c
'
,
date
:
"
Wed, 06 Sep 2017 00:27:13 +0000
"
},
{
identifier
:
'
b
'
,
date
:
"
Thu, 07 Sep 2017 18:59:23 +0000
"
},
{
idendifier
:
'
a
'
,
date
:
"
Fri, 08 Sep 2017 07:46:27 +0000
"
}
],
'
Document list is sorted with key_schema
'
);
}).
always
(
start
);
});
// Asterisk wildcard is not supported yet.
/* test('Full text query with asterisk', function () {
var doc_list = [
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment