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

[#37] isCuid(id) validates cuids #38

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,41 @@ const ids = [
];
```

### Configuration

```js
import { init } from '@paralleldrive/cuid2';

// The init function returns a custom createId function with the specified
// configuration.
const createId = init({
// A custom random function with the same API as Math.random.
// You should use this to pass a cryptographically secure random function.
random: Math.random,
length: 10, // the length of the id
fingerprint: 'a-custom-host-fingerprint',
});

console.log(
createId(), // wjfazn7qnd
createId(), // cerhuy9499
createId(), // itp2u4ozr4
);
```


### Validation

```js
import { createId, isCuid } from '@paralleldrive/cuid2';


console.log(
isCuid(createId()), // true
isCuid('not a cuid'), // false
);
```


## Trusted By

Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ declare namespace cuid2 {
fingerprint?: string
}): () => string

export function isCuid(id: string): boolean

export function createId(): string
}

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { createId, init, getConstants } = require("./src/index");
const { createId, init, getConstants, isCuid } = require("./src/index");

module.exports.createId = createId;
module.exports.init = init;
module.exports.getConstants = getConstants;
module.exports.isCuid = isCuid;
39 changes: 39 additions & 0 deletions src/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
createCounter,
bufToBigInt,
createFingerprint,
isCuid,
} = require("./index");

const { info } = require("./test-utils.js");
Expand Down Expand Up @@ -136,3 +137,41 @@ describe("createFingerprint", async (assert) => {
});
}
});

describe("isCuid", async (assert) => {
{
const actual = isCuid(createId());
const expected = true;

assert({
given: "a valid cuid",
should: "return true",
actual,
expected,
});
}

{
const actual = isCuid(createId() + createId() + createId());
const expected = false;

assert({
given: "a cuid that is too long",
should: "return false",
actual,
expected,
});
}

{
const actual = isCuid("");
const expected = false;

assert({
given: "an empty string",
should: "return false",
actual,
expected,
});
}
});
19 changes: 19 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,28 @@ const init = ({

const createId = init();

const isCuid = (id, { minLength = 2, maxLength = bigLength } = {}) => {
const length = id.length;
const regex = /^[0-9a-z]+$/;
ericelliott marked this conversation as resolved.
Show resolved Hide resolved

try {
if (
typeof id === "string" &&
length >= minLength &&
length <= maxLength &&
regex.test(id)
)
return true;
} finally {
}

return false;
};

module.exports.getConstants = () => ({ defaultLength, bigLength });
module.exports.init = init;
module.exports.createId = createId;
module.exports.bufToBigInt = bufToBigInt;
module.exports.createCounter = createCounter;
module.exports.createFingerprint = createFingerprint;
module.exports.isCuid = isCuid;