@onehat/data 1.8.33 → 1.8.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -210,6 +210,19 @@ describe('MemoryRepository', function() {
210
210
  expect(result.value).to.be.eq('four');
211
211
  });
212
212
 
213
+ it.only('getIxById', function() {
214
+ this.repository.setPage(1);
215
+ this.repository.setPageSize(2);
216
+ let ix = this.repository.getIxById(1);
217
+ expect(ix).to.be.eq(0);
218
+
219
+ ix = this.repository.getIxById(2);
220
+ expect(ix).to.be.eq(1);
221
+
222
+ ix = this.repository.getIxById(3);
223
+ expect(ix).to.be.undefined;
224
+ });
225
+
213
226
  it('getBy', function() {
214
227
  const results = this.repository.getBy((entity) => {
215
228
  return entity.id > 2;
@@ -652,6 +652,14 @@ describe('Repository Base', function() {
652
652
  expect(result.value).to.be.eq('three');
653
653
  });
654
654
 
655
+ it('getIxById', function() {
656
+ let ix = this.repository.getIxById(1);
657
+ expect(ix).to.be.eq(0);
658
+
659
+ ix = this.repository.getIxById(3);
660
+ expect(ix).to.be.eq(2);
661
+ });
662
+
655
663
  it('getBy', function() {
656
664
  const result = this.repository.getBy(entity => entity.id === 2 || entity.id === 3);
657
665
  expect(result.length).to.be.eq(2);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.8.33",
3
+ "version": "1.8.34",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -416,6 +416,24 @@ class MemoryRepository extends Repository {
416
416
  return this._keyedEntities[id] ? this._keyedEntities[id] : null;
417
417
  }
418
418
 
419
+ /**
420
+ * Get a single Entity's index by its id.
421
+ * Overrides Repository method, to limite results to only the current page.
422
+ * @param {integer} id - id of record to retrieve
423
+ * @return {integer} The numerical index, or undefined
424
+ */
425
+ getIxById = (id) => {
426
+ if (this.isDestroyed) {
427
+ throw Error('this.getIxById is no longer valid. Repository has been destroyed.');
428
+ }
429
+
430
+ const ix = this.getEntitiesOnPage().findIndex((entity) => entity.id === id);
431
+ if (ix >= 0) {
432
+ return ix;
433
+ }
434
+ return undefined;
435
+ }
436
+
419
437
  /**
420
438
  * Override Repository.getEntities, so we can get an array of
421
439
  * all *active* Entities, with sorting and filtering applied.
@@ -1166,6 +1166,23 @@ export default class Repository extends EventEmitter {
1166
1166
  return this.getFirstBy(entity => entity.id === id);
1167
1167
  }
1168
1168
 
1169
+ /**
1170
+ * Get a single Entity's index by its id.
1171
+ * @param {integer} id - id of record to retrieve
1172
+ * @return {integer} The numerical index, or undefined
1173
+ */
1174
+ getIxById = (id) => {
1175
+ if (this.isDestroyed) {
1176
+ throw Error('this.getIxById is no longer valid. Repository has been destroyed.');
1177
+ }
1178
+
1179
+ const ix = this.entities.findIndex((entity) => entity.id === id);
1180
+ if (ix >= 0) {
1181
+ return ix;
1182
+ }
1183
+ return undefined;
1184
+ }
1185
+
1169
1186
  /**
1170
1187
  * Get an array of Entities by supplied filter function
1171
1188
  * @param {function} filter - Filter function to apply to all entities