Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samipshah100/b4cc85082e77c413ffc76165fbffcf0c to your computer and use it in GitHub Desktop.
Save samipshah100/b4cc85082e77c413ffc76165fbffcf0c to your computer and use it in GitHub Desktop.
const crypto = require('crypto');
// this generates a random 20 char string using upto the first 10 chars from the inputString.
// this is used to create a workspace id from workspace name so that the workspace namespace can be easily searchable amongst the pinecone vector database namespaces
function createRandomWorkspaceIdFromName(inputString) {
const alphanumericInput = inputString
.replace(/[^a-zA-Z0-9]/g, '')
.slice(0, 10);
const remainingLength = 20 - alphanumericInput.length;
const randomBytes = crypto.randomBytes(remainingLength * 2);
const randomAlphanumeric = randomBytes
.toString('hex')
.slice(0, remainingLength);
const id = alphanumericInput + randomAlphanumeric;
return id;
}
export default createRandomWorkspaceIdFromName;
@samipshah100
Copy link
Author

re

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment