* Determines whether an object matches a single key's constraints
(object, key, constraints)
| 201 | * Determines whether an object matches a single key's constraints |
| 202 | */ |
| 203 | function matchesKeyConstraints(object, key, constraints) { |
| 204 | if (constraints === null) { |
| 205 | return false; |
| 206 | } |
| 207 | if (key.indexOf('.') >= 0) { |
| 208 | // Key references a subobject |
| 209 | var keyComponents = key.split('.'); |
| 210 | var subObjectKey = keyComponents[0]; |
| 211 | var keyRemainder = keyComponents.slice(1).join('.'); |
| 212 | return matchesKeyConstraints(object[subObjectKey] || {}, keyRemainder, constraints); |
| 213 | } |
| 214 | var i; |
| 215 | if (key === '$or') { |
| 216 | if (!Array.isArray(constraints)) { |
| 217 | return false; |
| 218 | } |
| 219 | for (i = 0; i < constraints.length; i++) { |
| 220 | if (matchesQuery(object, constraints[i])) { |
| 221 | return true; |
| 222 | } |
| 223 | } |
| 224 | return false; |
| 225 | } |
| 226 | if (key === '$and') { |
| 227 | if (!Array.isArray(constraints)) { |
| 228 | return false; |
| 229 | } |
| 230 | for (i = 0; i < constraints.length; i++) { |
| 231 | if (!matchesQuery(object, constraints[i])) { |
| 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | if (key === '$nor') { |
| 238 | if (!Array.isArray(constraints)) { |
| 239 | return false; |
| 240 | } |
| 241 | for (i = 0; i < constraints.length; i++) { |
| 242 | if (matchesQuery(object, constraints[i])) { |
| 243 | return false; |
| 244 | } |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | if (key === '$relatedTo') { |
| 249 | // Bail! We can't handle relational queries locally |
| 250 | return false; |
| 251 | } |
| 252 | // Decode Date JSON value |
| 253 | if (object[key] && object[key].__type == 'Date') { |
| 254 | object[key] = new Date(object[key].iso); |
| 255 | } |
| 256 | // Equality (or Array contains) cases |
| 257 | if (typeof constraints !== 'object') { |
| 258 | if (Array.isArray(object[key])) { |
| 259 | return object[key].indexOf(constraints) > -1; |
| 260 | } |
no test coverage detected