Generating Licence Keys with TypeScript

I’m always looking for fun little coding challenges that are not full projects, and I thought I would do a fun little licence key generator using Typescript.

Step 1: Setting Up the Project

First, create a new directory for your project and navigate into it using your terminal. Then, initialise a new TypeScript project by running the following command:

npm init -y
npm install --save-dev typescript

Step 2: Configuring TypeScript

To configure TypeScript, create a tsconfig.json file in your project’s root directory and add the following configuration:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true
  },
  "include": ["src"]
}

This configuration sets the target ECMAScript version to ES6, uses CommonJS modules, and specifies the output directory as dist, enables strict type checking, and includes the src directory for TypeScript files.

Step 3: Implementing the Licence Key Generator

Now, let’s write the code for the license key generator. Create a new file named licence-key-generator.ts inside the src directory and add the following code:

export const generateLicenceKey = (format: string): string => {
  const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  const getRandomChar = (placeholder: string): string => {
    switch (placeholder) {
      case "X":
        return chars[Math.floor(Math.random() \* chars.length)];
      case "N":
        return String(Math.floor(Math.random() \* 10));
      default:
        return placeholder;
    }
  };
  return Array.from(format, getRandomChar).join("");
};

In this code, we define a generateLicenceKey a function that takes a format parameter representing the format placeholder for the license key. It uses a getRandomChar function to generate a random character based on the placeholder. The Array.from method is used to create an array of characters based on the format, and the join method is used to concatenate the characters into a single string.

Step 4: Using the Licence Key Generator

To use the licence key generator, create a new file named index.ts in the src directory and add the following code:

import { generateLicenceKey } from './licence-key-generator';

const formatPlaceholder1 = "XXXNXN-NXXNN-XXXXX-XXXNN-XXXNN";
const formatPlaceholder2 = "NNNNNN-NNNNN-NNNN-XXXNN";

const licenseKey1 = generateLicenceKey(formatPlaceholder1);
const licenseKey2 = generateLicenceKey(formatPlaceholder2);

console.log(\`Licence Key 1: ${licenceKey1}\`);
console.log(\`Licence Key 2: ${licenceKey2}\`);

In this code, we define two format placeholders and generate licence keys based on those formats. Finally, we log the generated licence keys to the console.

Step 5: Compiling and Running the Code

To compile the TypeScript code, run the following command in your terminal:

npx tsc

This command compiles the TypeScript files in the src directory and outputs the JavaScript files in the dist directory.

To run the code, use the following command:

node dist/index.js

You should see the generated license keys printed in the console.

And there you have it! You’ve successfully built a licence key generator using TypeScript. Feel free to customise the format placeholders and experiment with variations to suit your needs.

The code in action

You can see the above code in action below.