| 353 | |
| 354 | // Build a chainable collection over an array of Realm objects. |
| 355 | const makeCollection = (type, items) => { |
| 356 | const arr = Array.isArray(items) ? items : [...items]; |
| 357 | return { |
| 358 | filtered: (query, ...args) => |
| 359 | makeCollection( |
| 360 | type, |
| 361 | arr.filter(o => evalExpr(o, query, args)), |
| 362 | ), |
| 363 | sorted: (field, reverse) => { |
| 364 | const sorted = [...arr].sort((a, b) => { |
| 365 | if (a[field] < b[field]) return reverse ? 1 : -1; |
| 366 | if (a[field] > b[field]) return reverse ? -1 : 1; |
| 367 | return 0; |
| 368 | }); |
| 369 | return makeCollection(type, sorted); |
| 370 | }, |
| 371 | get length() { |
| 372 | return arr.length; |
| 373 | }, |
| 374 | [Symbol.iterator]: function* () { |
| 375 | yield* arr; |
| 376 | }, |
| 377 | // Internal: used by delete() to identify the backing type and items. |
| 378 | _type: type, |
| 379 | _items: arr, |
| 380 | }; |
| 381 | }; |
| 382 | |
| 383 | const makeRealmInstance = path => { |
| 384 | let isClosed = false; |