A lightweight, customizable, and efficient code generation library written in JavaScript. This modernized version preserves all the original functionality while enhancing readability, maintainability, and compatibility with ES6+ standards.
- Generate random codes with custom lengths, charsets, and patterns.
- Supports prefixes, postfixes, and unique sequential code generation.
- Handles alphanumeric, numeric, and alphabetic charsets.
- Fully backward-compatible with legacy implementations.
- Detects infeasible configurations to avoid errors.
- Modernized with ES6+ features for cleaner and more efficient code.
npm install coupon_crafter
const coupon_crafter = require("coupon_crafter");
Include the script file:
<script src="path/to/coupon_crafter.js"></script>
<script>
const code = coupon_crafter.generate({ length: 6 });
</script>
const code = coupon_crafter.generate({
length: 8,
prefix: "promo-",
postfix: "-end",
})[0];
console.log(code); // Example: promo-Ab12Cd34-end
const codes = coupon_crafter.generate({
count: 5,
length: 6,
charset: coupon_crafter.charset("alphabetic"),
});
console.log(codes);
// Example: ['AbCdEf', 'GhIjKl', 'MnOpQr', 'StUvWx', 'YzAbCd']
const config = {
pattern: "A###Z",
charset: coupon_crafter.charset("numbers"),
};
console.log(coupon_crafter.generate(config, 0)); // ['A000Z']
console.log(coupon_crafter.generate(config, 1)); // ['A001Z']
console.log(coupon_crafter.generate(config, 10)); // ['A010Z']
The library has been upgraded to use:
- ES6+ Syntax: Arrow functions, template literals,
class
, andconst/let
. - Improved Efficiency: Leveraging modern features like
Set
for uniqueness checks. - Enhanced Readability: Cleaner and more modular code structure.
- Backward Compatibility: Fully supports legacy environments and usage patterns.
The library includes a comprehensive test suite. Run the tests using:
npm test
This project is licensed under the MIT License - see the LICENSE file for details.