Dashboard sipadu mbip
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.

unique.js 761B

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