index.js 664 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import cuid from '../../source/server/index.js';
  2. import test from 'tape';
  3. const { slug } = cuid;
  4. const MAX = 1200000;
  5. const collisionTest = (fn) => {
  6. let i = 0;
  7. let ids = {};
  8. let pass = true;
  9. while (i < MAX) {
  10. let id = fn();
  11. if (!ids[id]) {
  12. ids[id] = id;
  13. } else {
  14. pass = false;
  15. console.log('Failed at ' + i + ' iterations.');
  16. break;
  17. }
  18. i++;
  19. }
  20. return pass;
  21. };
  22. test('cuid()', (t) => {
  23. t.plan(3);
  24. t.ok(typeof cuid() === 'string',
  25. '.cuid() should return a string.');
  26. t.ok(collisionTest(cuid),
  27. 'cuids should not collide.');
  28. t.ok(collisionTest(slug),
  29. 'slugs should not collide.');
  30. });