index.js 779 B

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