unique.js 792 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * Copyright (c) 2015-present, Parse, LLC.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. * @flow
  10. */
  11. import arrayContainsObject from './arrayContainsObject';
  12. import ParseObject from './ParseObject';
  13. export default function unique
  14. /*:: <T>*/
  15. (arr
  16. /*: Array<T>*/
  17. )
  18. /*: Array<T>*/
  19. {
  20. const uniques = [];
  21. arr.forEach(value => {
  22. if (value instanceof ParseObject) {
  23. if (!arrayContainsObject(uniques, value)) {
  24. uniques.push(value);
  25. }
  26. } else {
  27. if (uniques.indexOf(value) < 0) {
  28. uniques.push(value);
  29. }
  30. }
  31. });
  32. return uniques;
  33. }