unique.js 529 B

12345678910111213141516171819202122232425
  1. var filter = require('./filter');
  2. /**
  3. * @return {array} Array of unique items
  4. */
  5. function unique(arr, compare){
  6. compare = compare || isEqual;
  7. return filter(arr, function(item, i, arr){
  8. var n = arr.length;
  9. while (++i < n) {
  10. if ( compare(item, arr[i]) ) {
  11. return false;
  12. }
  13. }
  14. return true;
  15. });
  16. }
  17. function isEqual(a, b){
  18. return a === b;
  19. }
  20. module.exports = unique;