This JavaScript function generates valid EAN-13 codes with customizable country codes and company parts. It ensures that the generated codes are unique among previously generated ones.
This function can be directly included in your JavaScript project or used in any JavaScript environment.
After including the function in your code you can use it as follows:
const previousCodes = ["0123456789123", "1234567890128"]; // Array of previously generated codes
const countryCode = "621"; // Example country code (Syria)
const companyPart = "12345"; // Example company part (variable length)
const generatedCode = generateEAN13(countryCode, companyPart, previousCodes);
console.log("Generated EAN-13 code:", generatedCode);
countryCode: string | number
: The country code part of the EAN-13 code. You can find a list of country codes in the following link.companyPart: string | number
: The company part of the EAN-13 code. Its length should not exceed9 digits
the remaining length available after the country code3 digits
and check digit1 digit
.previousCodes: string[]
: An array containing previously generated EAN-13 codes. This function ensures that the generated code is unique among these codes.
If the company part exceeds the remaining length, an error will be thrown.
This EAN-13 code generator function is provided for demonstration and educational purposes only. It generates EAN-13-like codes but does not provide official GS1-issued codes. It is not endorsed or affiliated with GS1.
Using the generated codes for commercial purposes may not comply with GS1 standards and could lead to legal or compatibility issues. It is strongly recommended to obtain official GS1-issued codes through the GS1 organization or authorized resellers for use in commercial products.
The author and contributors of this function are not liable for any misuse or consequences arising from the use of the generated codes. By using this function, you agree to use it responsibly and in accordance with applicable laws and regulations.
This function is licensed under the MIT License.