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
Labels
Merge Requests
18
Merge Requests
18
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
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
Hide 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 @@
function
SimpleQuery
(
spec
,
key_schema
)
{
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
||
{};
/**
...
...
@@ -52,19 +59,33 @@ inherits(SimpleQuery, Query);
* #crossLink "Query/match:method"
*/
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
];
if
(
this
.
_key_schema
[
key
]
!==
undefined
)
{
key
=
this
.
_key_schema
[
key
];
if
(
this
.
_key_schema
.
keys
&&
this
.
_key_schema
.
keys
[
key
]
!==
undefined
)
{
key
=
this
.
_key_schema
.
keys
[
key
];
}
if
(
typeof
key
===
'
object
'
)
{
object_value
=
item
[
key
.
readFrom
];
// 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
if
(
this
.
_spec
&&
this
.
_spec
.
operator
)
{
...
...
@@ -72,9 +93,16 @@ SimpleQuery.prototype.match = function (item, wildcard_character) {
}
value
=
this
.
value
;
if
(
key
.
castTo
)
{
value
=
key
.
castTo
(
value
);
object_value
=
key
.
castTo
(
object_value
);
castTo
=
key
.
castTo
;
if
(
castTo
)
{
// 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
{
object_value
=
item
[
key
];
...
...
test/queries/key-schema.tests.js
View file @
7f4bdc7f
...
...
@@ -15,33 +15,6 @@
}([
'
complex_queries
'
,
'
qunit
'
],
function
(
complex_queries
)
{
"
use strict
"
;
var
dateType
=
function
(
obj
)
{
if
(
Object
.
prototype
.
toString
.
call
(
obj
)
===
'
[object Date]
'
)
{
// no need to clone
return
obj
;
}
return
new
Date
(
obj
);
};
var
sameDay
=
function
(
a
,
b
)
{
return
(
(
a
.
getFullYear
()
===
b
.
getFullYear
())
&&
(
a
.
getMonth
()
===
b
.
getMonth
())
&&
(
a
.
getDay
()
===
b
.
getDay
())
);
};
var
sameMonth
=
function
(
a
,
b
)
{
return
(
(
a
.
getFullYear
()
===
b
.
getFullYear
())
&&
(
a
.
getMonth
()
===
b
.
getMonth
())
);
};
var
sameYear
=
function
(
a
,
b
)
{
return
(
a
.
getFullYear
()
===
b
.
getFullYear
());
};
var
translationEqualityMatcher
=
function
(
data
)
{
return
function
(
object_value
,
value
)
{
value
=
data
[
value
];
...
...
@@ -49,36 +22,66 @@
};
};
var
equalState
=
translationEqualityMatcher
({
'
ouvert
'
:
'
open
'
});
/*jslint unparam: true*/
var
key_schema
=
{
case_insensitive_identifier
:
{
readFrom
:
'
identifier
'
,
defaultMatch
:
function
(
object_value
,
value
,
wildcard_character
)
{
return
(
object_value
.
toLowerCase
()
===
value
.
toLowerCase
());
types
:
{
dateType
:
function
(
obj
)
{
if
(
Object
.
prototype
.
toString
.
call
(
obj
)
===
'
[object Date]
'
)
{
// no need to clone
return
obj
;
}
return
new
Date
(
obj
);
}
},
date_day
:
{
readFrom
:
'
date
'
,
castTo
:
dateType
,
defaultMatch
:
sameDay
},
date_month
:
{
readFrom
:
'
date
'
,
castTo
:
dateType
,
defaultMatch
:
sameMonth
},
date_year
:
{
readFrom
:
'
date
'
,
castTo
:
dateType
,
defaultMatch
:
sameYear
comparators
:
{
sameDay
:
function
(
a
,
b
)
{
return
(
(
a
.
getFullYear
()
===
b
.
getFullYear
())
&&
(
a
.
getMonth
()
===
b
.
getMonth
())
&&
(
a
.
getDay
()
===
b
.
getDay
())
);
},
sameMonth
:
function
(
a
,
b
)
{
return
(
(
a
.
getFullYear
()
===
b
.
getFullYear
())
&&
(
a
.
getMonth
()
===
b
.
getMonth
())
);
},
sameYear
:
function
(
a
,
b
)
{
return
(
a
.
getFullYear
()
===
b
.
getFullYear
());
},
equalState
:
translationEqualityMatcher
({
'
ouvert
'
:
'
open
'
})
},
translated_state
:
{
readFrom
:
'
state
'
,
defaultMatch
:
equalState
keys
:
{
case_insensitive_identifier
:
{
readFrom
:
'
identifier
'
,
defaultMatch
:
function
(
object_value
,
value
,
wildcard_character
)
{
// XXX do this with a regexp and wildcard support
return
(
object_value
.
toLowerCase
()
===
value
.
toLowerCase
());
}
},
date_day
:
{
readFrom
:
'
date
'
,
castTo
:
'
dateType
'
,
defaultMatch
:
'
sameDay
'
},
date_month
:
{
readFrom
:
'
date
'
,
castTo
:
'
dateType
'
,
defaultMatch
:
'
sameMonth
'
},
date_year
:
{
readFrom
:
'
date
'
,
castTo
:
'
dateType
'
,
defaultMatch
:
'
sameYear
'
},
translated_state
:
{
readFrom
:
'
state
'
,
defaultMatch
:
'
equalState
'
}
}
};
/*jslint unparam: false*/
...
...
test/queries/localstorage-keys.tests.js
View file @
7f4bdc7f
...
...
@@ -69,19 +69,25 @@
}
var
dateType
=
function
(
obj
)
{
if
(
Object
.
prototype
.
toString
.
call
(
obj
)
===
'
[object Date]
'
)
{
// no need to clone
return
obj
;
}
return
new
Date
(
obj
);
},
key_schema
=
{
mydate
:
{
readFrom
:
'
date
'
,
castTo
:
dateType
var
key_schema
=
{
types
:
{
dateType
:
function
(
obj
)
{
if
(
Object
.
prototype
.
toString
.
call
(
obj
)
===
'
[object Date]
'
)
{
// no need to clone
return
obj
;
}
return
new
Date
(
obj
);
}
},
keys
:
{
mydate
:
{
readFrom
:
'
date
'
,
castTo
:
'
dateType
'
}
}
};
test
(
"
AllDocs
"
,
function
()
{
expect
(
3
);
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