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.

33 lines
831 B

4 years ago
  1. 'use strict';
  2. var _require = require('../helpers/is'),
  3. isFunction = _require.isFunction;
  4. module.exports = function unique(key) {
  5. var collection = void 0;
  6. if (key === undefined) {
  7. collection = this.items.filter(function (element, index, self) {
  8. return self.indexOf(element) === index;
  9. });
  10. } else {
  11. collection = [];
  12. var usedKeys = [];
  13. for (var iterator = 0, length = this.items.length; iterator < length; iterator += 1) {
  14. var uniqueKey = void 0;
  15. if (isFunction(key)) {
  16. uniqueKey = key(this.items[iterator]);
  17. } else {
  18. uniqueKey = this.items[iterator][key];
  19. }
  20. if (usedKeys.indexOf(uniqueKey) === -1) {
  21. collection.push(this.items[iterator]);
  22. usedKeys.push(uniqueKey);
  23. }
  24. }
  25. }
  26. return new this.constructor(collection);
  27. };