@onehat/data 1.6.7 → 1.6.8

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.
@@ -111,6 +111,33 @@ describe('OneBuildRepository', function() {
111
111
  await this.repository.add({ key: 6, value: 'six' });
112
112
  expect(_.size(this.repository.entities)).to.be.eq(1);
113
113
  });
114
+
115
+ it.only('sortInMemory', function() {
116
+
117
+ const repository = this.repository;
118
+
119
+ // Create two phantom records, out of order
120
+ repository.setAutoSave(false);
121
+ repository.add({ key: 5, value: 'Five', });
122
+ repository.add({ key: 4, value: 'One', });
123
+ repository.add({ key: 2, value: 'Two', });
124
+ repository.add({ key: 3, value: 'Three', });
125
+ repository.add({ key: 1, value: 'One', });
126
+
127
+ repository.sorters = [
128
+ { name: 'value', direction: 'DESC', }, // 2,3,4,1,5
129
+ { name: 'key', direction: 'ASC', }, // 2,3,1,4,5
130
+ ];
131
+ repository.sortInMemory();
132
+
133
+ // Check that they are correct order
134
+ const entities = repository.entities;
135
+ expect(entities[0].key).to.be.eq(2);
136
+ expect(entities[1].key).to.be.eq(3);
137
+ expect(entities[2].key).to.be.eq(1);
138
+ expect(entities[3].key).to.be.eq(4);
139
+ expect(entities[4].key).to.be.eq(5);
140
+ });
114
141
 
115
142
  });
116
143
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.6.7",
3
+ "version": "1.6.8",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -653,6 +653,24 @@ class AjaxRepository extends Repository {
653
653
  return this.reader.read(result);
654
654
  }
655
655
 
656
+ /**
657
+ * Sorts the items in the current page of memory
658
+ * This is mainly used to sort isPhantom entities,
659
+ * since the server normally sorts, and they haven't yet gone to server.
660
+ */
661
+ sortInMemory = () => {
662
+ const sorters = this.sorters;
663
+ let sortNames = [],
664
+ sortDirections = [];
665
+ _.each(sorters, (sorter) => {
666
+ sortNames.push(sorter.name);
667
+ sortDirections.push(sorter.direction.toLowerCase());
668
+ });
669
+ let entities = this.entities;
670
+ entities = _.orderBy(entities, sortNames, sortDirections);
671
+ this.entities = entities;
672
+ }
673
+
656
674
  /**
657
675
  * Helper for save.
658
676
  * Takes an array of Promises from axios. When they are all resolved,