Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated CUID contains 'undefined' #82

Open
phr3nzy opened this issue Nov 5, 2024 · 0 comments
Open

Generated CUID contains 'undefined' #82

phr3nzy opened this issue Nov 5, 2024 · 0 comments

Comments

@phr3nzy
Copy link

phr3nzy commented Nov 5, 2024

Hello, wonderful library!

When I generate a CUID, the result is undefined${cuid}. I'm unsure why this is happening.

Reproduction

  • install typescript, ts-node, and this library using pnpm
  • Create index.ts with file under
  • Run using pnpm ts-node index.ts
import { assert } from 'node:console';
import { randomInt } from 'node:crypto';
import { init, isCuid } from '@paralleldrive/cuid2';

const MAX_NUMBER = 2 ** 48 - 1;

function randomNumber(): number {
	return randomInt(MAX_NUMBER);
}

export const createCuid = init({
	length: 12,
	random: randomNumber,
	fingerprint: 'api',
});

export function cuid(): string {
	const cuid = createCuid();
	assert(isCuid(cuid), 'Generated CUID is not valid');
	assert(
		cuid.match(/undefined/) === null,
		'Generated CUID contains "undefined"',
	);

	return cuid.replace(/undefined/, '');
}

console.log(cuid());

Environment

  • node.js v20.15.0
  • pnpm 9.7.0
  • typescript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant