Skip to content

Instantly share code, notes, and snippets.

@arunrreddy
Created September 16, 2024 05:06
Show Gist options
  • Select an option

  • Save arunrreddy/92021cd7fbdf32e64bcfb1266685b1ca to your computer and use it in GitHub Desktop.

Select an option

Save arunrreddy/92021cd7fbdf32e64bcfb1266685b1ca to your computer and use it in GitHub Desktop.
Coupon code generator
const fs = require('fs');
const pattern = /^MT[A-Z0-9]{7}$/;
const codeCount = 1000000;
const outputFile = 'codes_5.csv';
const codes = [];
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const charsetLength = charset.length;
while (codes.length < codeCount) {
let code = 'MT';
for (let i = 0; i < 7; i++) {
code += charset.charAt(Math.floor(Math.random() * charsetLength));
}
if (pattern.test(code)) {
codes.push(code);
}
}
// Convert the codes to a CSV string
const csvContent = 'Code\n' + codes.join('\n');
// Save the generated codes to a CSV file
fs.writeFileSync(outputFile, csvContent, 'utf8');
console.log(`Generated ${codes.length} unique codes and saved them to '${outputFile}'.`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment