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
Bryan Kaperick
jio
Commits
7f4bdc7f
Commit
7f4bdc7f
authored
Dec 17, 2013
by
Marco Mariani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support for key_schema.types (castTo) and comparators (defaultMatch)
parent
0dffe5c1
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
105 additions
and
68 deletions
+105
-68
src/queries/core/simplequery.js
src/queries/core/simplequery.js
+35
-7
test/queries/key-schema.tests.js
test/queries/key-schema.tests.js
+54
-51
test/queries/localstorage-keys.tests.js
test/queries/localstorage-keys.tests.js
+16
-10
No files found.
src/queries/core/simplequery.js
View file @
7f4bdc7f
...
@@ -15,6 +15,13 @@
...
@@ -15,6 +15,13 @@
function
SimpleQuery
(
spec
,
key_schema
)
{
function
SimpleQuery
(
spec
,
key_schema
)
{
Query
.
call
(
this
);
Query
.
call
(
this
);
// XXX check for correctness of key_schema:
// XXX 'keys' must exist
// XXX 'types' is optional
// XXX 'comparators' is optional
// XXX anything else is invalid
// XXX each key can have readFrom, castTo, defaultMatch
// (can be checked in the match function)
this
.
_key_schema
=
key_schema
||
{};
this
.
_key_schema
=
key_schema
||
{};
/**
/**
...
@@ -52,19 +59,33 @@ inherits(SimpleQuery, Query);
...
@@ -52,19 +59,33 @@ inherits(SimpleQuery, Query);
* #crossLink "Query/match:method"
* #crossLink "Query/match:method"
*/
*/
SimpleQuery
.
prototype
.
match
=
function
(
item
,
wildcard_character
)
{
SimpleQuery
.
prototype
.
match
=
function
(
item
,
wildcard_character
)
{
var
object_value
=
null
,
matchMethod
=
null
,
value
=
null
,
key
=
this
.
key
;
var
object_value
=
null
,
defaultMatch
=
null
,
castTo
=
null
,
matchMethod
=
null
,
value
=
null
,
key
=
this
.
key
;
matchMethod
=
this
[
this
.
operator
];
matchMethod
=
this
[
this
.
operator
];
if
(
this
.
_key_schema
[
key
]
!==
undefined
)
{
if
(
this
.
_key_schema
.
keys
&&
this
.
_key_schema
.
keys
[
key
]
!==
undefined
)
{
key
=
this
.
_key_schema
[
key
];
key
=
this
.
_key_schema
.
keys
[
key
];
}
}
if
(
typeof
key
===
'
object
'
)
{
if
(
typeof
key
===
'
object
'
)
{
object_value
=
item
[
key
.
readFrom
];
object_value
=
item
[
key
.
readFrom
];
// defaultMatch overrides the default '=' operator
// defaultMatch overrides the default '=' operator
matchMethod
=
(
key
.
defaultMatch
||
matchMethod
);
defaultMatch
=
key
.
defaultMatch
;
// defaultMatch can be a string
if
(
typeof
defaultMatch
===
'
string
'
)
{
// XXX raise error if defaultMatch not in comparators
defaultMatch
=
this
.
_key_schema
.
comparators
[
defaultMatch
];
}
// defaultMatch overrides the default '=' operator
matchMethod
=
(
defaultMatch
||
matchMethod
);
// but an explicit operator: key overrides DefaultMatch
// but an explicit operator: key overrides DefaultMatch
if
(
this
.
_spec
&&
this
.
_spec
.
operator
)
{
if
(
this
.
_spec
&&
this
.
_spec
.
operator
)
{
...
@@ -72,9 +93,16 @@ SimpleQuery.prototype.match = function (item, wildcard_character) {
...
@@ -72,9 +93,16 @@ SimpleQuery.prototype.match = function (item, wildcard_character) {
}
}
value
=
this
.
value
;
value
=
this
.
value
;
if
(
key
.
castTo
)
{
castTo
=
key
.
castTo
;
value
=
key
.
castTo
(
value
);
if
(
castTo
)
{
object_value
=
key
.
castTo
(
object_value
);
// castTo can be a string
if
(
typeof
castTo
===
'
string
'
)
{
// XXX raise error if castTo not in types
castTo
=
this
.
_key_schema
.
types
[
castTo
];
}
value
=
castTo
(
value
);
object_value
=
castTo
(
object_value
);
}
}
}
else
{
}
else
{
object_value
=
item
[
key
];
object_value
=
item
[
key
];
...
...
test/queries/key-schema.tests.js
View file @
7f4bdc7f
...
@@ -15,70 +15,73 @@
...
@@ -15,70 +15,73 @@
}([
'
complex_queries
'
,
'
qunit
'
],
function
(
complex_queries
)
{
}([
'
complex_queries
'
,
'
qunit
'
],
function
(
complex_queries
)
{
"
use strict
"
;
"
use strict
"
;
var
dateType
=
function
(
obj
)
{
var
translationEqualityMatcher
=
function
(
data
)
{
return
function
(
object_value
,
value
)
{
value
=
data
[
value
];
return
(
object_value
===
value
);
};
};
/*jslint unparam: true*/
var
key_schema
=
{
types
:
{
dateType
:
function
(
obj
)
{
if
(
Object
.
prototype
.
toString
.
call
(
obj
)
===
'
[object Date]
'
)
{
if
(
Object
.
prototype
.
toString
.
call
(
obj
)
===
'
[object Date]
'
)
{
// no need to clone
// no need to clone
return
obj
;
return
obj
;
}
}
return
new
Date
(
obj
);
return
new
Date
(
obj
);
};
}
},
var
sameDay
=
function
(
a
,
b
)
{
comparators
:
{
sameDay
:
function
(
a
,
b
)
{
return
(
return
(
(
a
.
getFullYear
()
===
b
.
getFullYear
())
&&
(
a
.
getFullYear
()
===
b
.
getFullYear
())
&&
(
a
.
getMonth
()
===
b
.
getMonth
())
&&
(
a
.
getMonth
()
===
b
.
getMonth
())
&&
(
a
.
getDay
()
===
b
.
getDay
())
(
a
.
getDay
()
===
b
.
getDay
())
);
);
};
},
sameMonth
:
function
(
a
,
b
)
{
var
sameMonth
=
function
(
a
,
b
)
{
return
(
return
(
(
a
.
getFullYear
()
===
b
.
getFullYear
())
&&
(
a
.
getFullYear
()
===
b
.
getFullYear
())
&&
(
a
.
getMonth
()
===
b
.
getMonth
())
(
a
.
getMonth
()
===
b
.
getMonth
())
);
);
};
},
sameYear
:
function
(
a
,
b
)
{
var
sameYear
=
function
(
a
,
b
)
{
return
(
a
.
getFullYear
()
===
b
.
getFullYear
());
return
(
a
.
getFullYear
()
===
b
.
getFullYear
());
};
},
equalState
:
translationEqualityMatcher
({
'
ouvert
'
:
'
open
'
})
var
translationEqualityMatcher
=
function
(
data
)
{
},
return
function
(
object_value
,
value
)
{
value
=
data
[
value
];
return
(
object_value
===
value
);
};
};
var
equalState
=
translationEqualityMatcher
({
'
ouvert
'
:
'
open
'
});
/*jslint unparam: true*/
keys
:
{
var
key_schema
=
{
case_insensitive_identifier
:
{
case_insensitive_identifier
:
{
readFrom
:
'
identifier
'
,
readFrom
:
'
identifier
'
,
defaultMatch
:
function
(
object_value
,
value
,
wildcard_character
)
{
defaultMatch
:
function
(
object_value
,
value
,
wildcard_character
)
{
// XXX do this with a regexp and wildcard support
return
(
object_value
.
toLowerCase
()
===
value
.
toLowerCase
());
return
(
object_value
.
toLowerCase
()
===
value
.
toLowerCase
());
}
}
},
},
date_day
:
{
date_day
:
{
readFrom
:
'
date
'
,
readFrom
:
'
date
'
,
castTo
:
dateType
,
castTo
:
'
dateType
'
,
defaultMatch
:
sameDay
defaultMatch
:
'
sameDay
'
},
},
date_month
:
{
date_month
:
{
readFrom
:
'
date
'
,
readFrom
:
'
date
'
,
castTo
:
dateType
,
castTo
:
'
dateType
'
,
defaultMatch
:
sameMonth
defaultMatch
:
'
sameMonth
'
},
},
date_year
:
{
date_year
:
{
readFrom
:
'
date
'
,
readFrom
:
'
date
'
,
castTo
:
dateType
,
castTo
:
'
dateType
'
,
defaultMatch
:
sameYear
defaultMatch
:
'
sameYear
'
},
},
translated_state
:
{
translated_state
:
{
readFrom
:
'
state
'
,
readFrom
:
'
state
'
,
defaultMatch
:
equalState
defaultMatch
:
'
equalState
'
}
}
}
};
};
/*jslint unparam: false*/
/*jslint unparam: false*/
...
...
test/queries/localstorage-keys.tests.js
View file @
7f4bdc7f
...
@@ -69,19 +69,25 @@
...
@@ -69,19 +69,25 @@
}
}
var
dateType
=
function
(
obj
)
{
var
key_schema
=
{
types
:
{
dateType
:
function
(
obj
)
{
if
(
Object
.
prototype
.
toString
.
call
(
obj
)
===
'
[object Date]
'
)
{
if
(
Object
.
prototype
.
toString
.
call
(
obj
)
===
'
[object Date]
'
)
{
// no need to clone
// no need to clone
return
obj
;
return
obj
;
}
}
return
new
Date
(
obj
);
return
new
Date
(
obj
);
},
key_schema
=
{
}
},
keys
:
{
mydate
:
{
mydate
:
{
readFrom
:
'
date
'
,
readFrom
:
'
date
'
,
castTo
:
dateType
castTo
:
'
dateType
'
}
}
}
};
};
test
(
"
AllDocs
"
,
function
()
{
test
(
"
AllDocs
"
,
function
()
{
expect
(
3
);
expect
(
3
);
var
o
=
{},
jio
=
jIO
.
createJIO
({
var
o
=
{},
jio
=
jIO
.
createJIO
({
...
...
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