data_manipulation.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. module.exports = {
  19. /**
  20. * Exclude objects from <code>collection</code> if its <code>key</code> exist in <code>valuesToReject</code>
  21. * Example:
  22. * <code>
  23. * var collection = [{n: 'v1'}, {n: 'v2'}, {n: 'v3'}, {n: 'v4'}],
  24. * key = 'n',
  25. * valuesToReject = ['v2', 'v3'];
  26. * var result = rejectPropertyValues(collection, key, valuesToReject); // [{n: 'v1'}, {n: 'v4'}]
  27. * </code>
  28. * @param {Object[]} collection array of objects
  29. * @param {String} key property name of each object to be checked
  30. * @param {String[]} valuesToReject list of <code>key</code> values. Objects with <code>key</code>,
  31. * equal to one of them, will be excluded
  32. * @returns {Object[]} Rejected array
  33. */
  34. rejectPropertyValues: function(collection, key, valuesToReject) {
  35. return collection.filter(function (item) {
  36. var propertyValue = Em.get(item, key);
  37. return valuesToReject.contains(propertyValue);
  38. });
  39. },
  40. /**
  41. * Wrapper to <code>filterProperty</code>-method that allows using list of values to filter
  42. * Example:
  43. * <code>
  44. * var collection = [{n: 'v1'}, {n: 'v2'}, {n: 'v3'}, {n: 'v4'}],
  45. * key = 'n',
  46. * valuesToFilter = ['v2', 'v3'];
  47. * var result = filterPropertyValues(collection, key, valuesToFilter); // [{n: 'v2'}, {n: 'v3'}]
  48. * </code>
  49. * @param {Object[]} collection array of objects
  50. * @param {String} key property name of each object to be filtered
  51. * @param {String|String[]} valuesToFilter array of values (or one value-string) to filter
  52. * @returns {Object[]} Filtered array
  53. */
  54. filterPropertyValues: function(collection, key, valuesToFilter) {
  55. var type = Em.typeOf(valuesToFilter);
  56. if ('string' === type)
  57. return collection.filterProperty(key, valuesToFilter);
  58. var ret = [];
  59. if ('array' === type) {
  60. valuesToFilter.forEach(function (value) {
  61. ret = ret.concat(collection.filterProperty(key, value));
  62. });
  63. }
  64. return ret;
  65. },
  66. /**
  67. * Group <code>collection</code> items by <code>key</code> value
  68. * Example:
  69. * <code>
  70. * var collection = [{n: 'v1'}, {n: 'v2'}, {n: 'v2'}, {n: 'v4'}],
  71. * key = 'n';
  72. * var result = groupPropertyValues(collection, key); // {v1: [{n: 'v1'}], v2: [{n: 'v2'}, {n: 'v2'}], v4: [{n: 'v4'}]}
  73. * </code>
  74. * @param {Object[]} collection array of objects
  75. * @param {String} key property name of each object to be grouped
  76. * @returns {*}
  77. */
  78. groupPropertyValues: function(collection, key) {
  79. var group = {};
  80. collection.forEach(function(item) {
  81. var value = Em.get(item, key);
  82. if (Em.isNone(group[value])) {
  83. group[value] = [item];
  84. }
  85. else {
  86. group[value].pushObject(item);
  87. }
  88. });
  89. return group;
  90. }
  91. };