Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic-test.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import * as assert from 'assert';
  2. import sift, {indexOf as siftIndexOf} from '..';
  3. describe(__filename + '#', function() {
  4. it('doesn\'t sort arrays', function () {
  5. var values = sift({
  6. $or: [3, 2, 1]
  7. }, [9,8,7,6,5,4,3,2,1]);
  8. assert.equal(values.length, 3);
  9. assert.equal(values[0], 3);
  10. assert.equal(values[1], 2);
  11. assert.equal(values[2], 1);
  12. });
  13. it('can create a custom selector, and use it', function () {
  14. var sifter = sift({ age: { $gt: 5}}, function (item) {
  15. return item.person;
  16. });
  17. var people = [{ person: { age: 6 }}],
  18. filtered = people.filter(sifter);
  19. assert.equal(filtered.length, 1);
  20. assert.equal(filtered[0], people[0]);
  21. });
  22. it('throws an error if the operation is invalid', function () {
  23. var err;
  24. try {
  25. sift({$aaa:1})('b');
  26. } catch (e) {
  27. err = e;
  28. }
  29. assert.equal(err.message, 'Unknown operation $aaa');
  30. });
  31. it('can use a custom selector as the 3rd param', function () {
  32. var people = [{ person: { age: 6 }}];
  33. var filtered = sift({ age: { $gt: 5}}, people, function (item) {
  34. return item.person;
  35. });
  36. assert.equal(filtered.length, 1);
  37. assert.equal(filtered[0], people[0]);
  38. });
  39. it('can get the first index of a matching element', function () {
  40. var index = siftIndexOf({ val: { $gt: 5}}, [{val: 4}, {val: 3}, {val: 6}, {val: 7}]);
  41. assert.equal(index, 2);
  42. });
  43. it('returns -1 as index if no matching element is found', function () {
  44. var index = siftIndexOf({ val: { $gt: 7}}, [{val: 4}, {val: 3}, {val: 6}, {val: 7}]);
  45. assert.equal(index, -1);
  46. });
  47. it('can match empty arrays', function () {
  48. var statusQuery = {$or: [{status: {$exists: false}},
  49. {status: []},
  50. {status: {$in: ['urgent', 'completed', 'today']}}
  51. ]};
  52. var filtered = sift(statusQuery, [{ status: [] },
  53. { status: ['urgent'] },
  54. { status: ['nope'] }
  55. ]);
  56. assert.equal(filtered.length, 2);
  57. });
  58. it('$ne: null does not hit when field is present', function(){
  59. var sifter = sift({age: {$ne: null}});
  60. var people = [
  61. {age: 'matched'},
  62. {missed: 1}
  63. ];
  64. var filtered = people.filter(sifter);
  65. assert.equal(filtered.length, 1);
  66. assert.equal(filtered[0].age, 'matched');
  67. });
  68. it('$ne does not hit when field is different', function () {
  69. var sifter = sift({ age: { $ne: 5 }});
  70. var people = [{ age: 5 }],
  71. filtered = people.filter(sifter);
  72. assert.equal(filtered.length, 0);
  73. });
  74. it('$ne does hit when field exists with different value', function () {
  75. var sifter = sift({ age: { $ne: 4 }});
  76. var people = [{ age: 5 }],
  77. filtered = people.filter(sifter);
  78. assert.equal(filtered.length, 1);
  79. });
  80. it('$ne does hit when field does not exist', function(){
  81. var sifter = sift({ age: { $ne: 5 }});
  82. var people = [{}],
  83. filtered = people.filter(sifter);
  84. assert.equal(filtered.length, 1);
  85. });
  86. it('$eq matches objects that serialize to the same value', function() {
  87. var counter = 0;
  88. function Book(name) {
  89. this.name = name;
  90. this.copyNumber = counter;
  91. this.toJSON = function() {
  92. return this.name; // discard the copy when serializing.
  93. };
  94. counter += 1;
  95. }
  96. var warAndPeace = new Book('War and Peace');
  97. var sifter = sift({ $eq: warAndPeace});
  98. var books = [ new Book('War and Peace')];
  99. var filtered = books.filter(sifter);
  100. assert.equal(filtered.length, 1);
  101. });
  102. it('$neq does not match objects that serialize to the same value', function() {
  103. var counter = 0;
  104. function Book(name) {
  105. this.name = name;
  106. this.copyNumber = counter;
  107. this.toJSON = function() {
  108. return this.name; // discard the copy when serializing.
  109. };
  110. counter += 1;
  111. }
  112. var warAndPeace = new Book('War and Peace');
  113. var sifter = sift({ $ne: warAndPeace});
  114. var books = [ new Book('War and Peace')];
  115. var filtered = books.filter(sifter);
  116. assert.equal(filtered.length, 0);
  117. });
  118. // https://gist.github.com/jdnichollsc/00ea8cf1204b17d9fb9a991fbd1dfee6
  119. it('returns a period between start and end dates', function() {
  120. var product = {
  121. 'productTypeCode': 'productTypeEnergy',
  122. 'quantities': [
  123. {
  124. 'period': {
  125. 'startDate': new Date('2017-01-13T05:00:00.000Z'),
  126. 'endDate': new Date('2017-01-31T05:00:00.000Z'),
  127. 'dayType': {
  128. 'normal': true,
  129. 'holiday': true
  130. },
  131. 'specificDays': [
  132. 'monday',
  133. 'wednesday',
  134. 'friday'
  135. ],
  136. 'loadType': {
  137. 'high': true,
  138. 'medium': false,
  139. 'low': false
  140. }
  141. },
  142. 'type': 'DemandPercentage',
  143. 'quantityValue': '44'
  144. },
  145. {
  146. 'period': {
  147. 'startDate': new Date('2017-01-13T05:00:00.000Z'),
  148. 'endDate': new Date('2017-01-31T05:00:00.000Z'),
  149. 'dayType': {
  150. 'normal': true,
  151. 'holiday': true
  152. },
  153. 'loadType': {
  154. 'high': false,
  155. 'medium': true,
  156. 'low': false
  157. }
  158. },
  159. 'type': 'Value',
  160. 'quantityValue': '22'
  161. }
  162. ]
  163. };
  164. var period = {
  165. 'startDate': new Date('2017-01-08T05:00:00.000Z'),
  166. 'endDate': new Date('2017-01-29T05:00:00.000Z'),
  167. 'dayType': {
  168. 'normal': true,
  169. 'holiday': true
  170. },
  171. 'loadType': {
  172. 'high': true,
  173. 'medium': false,
  174. 'low': true
  175. },
  176. specificPeriods : ['3', '4', '5-10']
  177. };
  178. var results = sift({
  179. $and: [
  180. { 'period.startDate': { $lte : period.endDate } },
  181. { 'period.endDate': { $gte : period.startDate } }
  182. ]
  183. }, product.quantities);
  184. assert.equal(results.length, 2);
  185. });
  186. });