Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
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
Stefane Fermigier
jio
Commits
7cc44e36
Commit
7cc44e36
authored
Jul 03, 2013
by
Tristan Cavelier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
query.js filter static methods are now able to clone the given list or not
parent
1c36476d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
11 deletions
+72
-11
src/queries/query.js
src/queries/query.js
+44
-11
src/queries/tool.js
src/queries/tool.js
+28
-0
No files found.
src/queries/query.js
View file @
7cc44e36
/*jslint indent: 2, maxlen: 80, sloppy: true, nomen: true */
/*global newClass: true, sortFunction: true, parseStringToObject: true,
_export: true, stringEscapeRegexpCharacters: true */
_export: true, stringEscapeRegexpCharacters: true
, deepClone: true
*/
/**
* The query to use to filter a list of objects.
...
...
@@ -39,12 +39,9 @@ var Query = newClass(function () {
Query
.
sortOn
(
option
.
sort_on
,
item_list
);
}
if
(
option
.
limit
)
{
item_list
.
splice
(
0
,
option
.
limit
[
0
]);
if
(
option
.
limit
[
1
])
{
item_list
.
splice
(
option
.
limit
[
1
]);
Query
.
limit
(
option
.
limit
,
item_list
);
}
}
Query
.
filterListSelect
(
option
.
select_list
||
[],
item_list
);
Query
.
select
(
option
.
select_list
||
[],
item_list
);
};
/**
...
...
@@ -163,15 +160,21 @@ var Query = newClass(function () {
},
{
"
static_methods
"
:
{
/**
* Filter a list of items, modifying them to select only wanted keys.
* Filter a list of items, modifying them to select only wanted keys. If
* `clone` is true, then the method will act on a cloned list.
*
* @method
filterListS
elect
* @method
s
elect
* @static
* @param {Array} select_option Key list to keep
* @param {Array} list The item list to filter
* @param {Boolean} [clone=false] If true, modifies a clone of the list
* @return {Array} The filtered list
*/
"
filterListSelect
"
:
function
(
select_option
,
list
)
{
"
select
"
:
function
(
select_option
,
list
,
clone
)
{
var
i
,
j
,
new_item
;
if
(
clone
)
{
list
=
deepClone
(
list
);
}
for
(
i
=
0
;
i
<
list
.
length
;
i
+=
1
)
{
new_item
=
{};
for
(
j
=
0
;
j
<
select_option
.
length
;
j
+=
1
)
{
...
...
@@ -184,18 +187,25 @@ var Query = newClass(function () {
}
}
}
return
list
;
},
/**
* Sort a list of items, according to keys and directions.
* Sort a list of items, according to keys and directions. If `clone` is true,
* then the method will act on a cloned list.
*
* @method sortOn
* @static
* @param {Array} sort_on_option List of couples [key, direction]
* @param {Array} list The item list to sort
* @param {Boolean} [clone=false] If true, modifies a clone of the list
* @return {Array} The filtered list
*/
"
sortOn
"
:
function
(
sort_on_option
,
list
)
{
"
sortOn
"
:
function
(
sort_on_option
,
list
,
clone
)
{
var
sort_index
;
if
(
clone
)
{
list
=
deepClone
(
list
);
}
for
(
sort_index
=
sort_on_option
.
length
-
1
;
sort_index
>=
0
;
sort_index
-=
1
)
{
list
.
sort
(
sortFunction
(
...
...
@@ -203,6 +213,29 @@ var Query = newClass(function () {
sort_on_option
[
sort_index
][
1
]
));
}
return
list
;
},
/**
* Limit a list of items, according to index and length. If `clone` is true,
* then the method will act on a cloned list.
*
* @method limit
* @static
* @param {Array} limit_option A couple [from, length]
* @param {Array} list The item list to limit
* @param {Boolean} [clone=false] If true, modifies a clone of the list
* @return {Array} The filtered list
*/
"
limit
"
:
function
(
limit_option
,
list
,
clone
)
{
if
(
clone
)
{
list
=
deepClone
(
list
);
}
list
.
splice
(
0
,
limit_option
[
0
]);
if
(
limit_option
[
1
])
{
list
.
splice
(
limit_option
[
1
]);
}
return
list
;
},
/**
...
...
src/queries/tool.js
View file @
7cc44e36
...
...
@@ -128,3 +128,31 @@ function sortFunction(key, way) {
return
a
[
key
]
>
b
[
key
]
?
1
:
a
[
key
]
<
b
[
key
]
?
-
1
:
0
;
};
}
/**
* Clones all native object in deep. Managed types: Object, Array, String,
* Number, Boolean, null.
*
* @param {A} object The object to clone
* @return {A} The cloned object
*/
function
deepClone
(
object
)
{
var
i
,
cloned
;
if
(
Object
.
prototype
.
toString
.
call
(
object
)
===
"
[object Array]
"
)
{
cloned
=
[];
for
(
i
=
0
;
i
<
object
.
length
;
i
+=
1
)
{
cloned
[
i
]
=
deepClone
(
object
[
i
]);
}
return
cloned
;
}
if
(
typeof
object
===
"
object
"
)
{
cloned
=
{};
for
(
i
in
object
)
{
if
(
object
.
hasOwnProperty
(
i
))
{
cloned
[
i
]
=
deepClone
(
object
[
i
]);
}
}
return
cloned
;
}
return
object
;
}
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