@zenuinebit/sku-generator
v0.1.0
Published
Readable SKU codes from product attributes, with variant matrices and optional check characters.
Readme
@zenuinebit/sku-generator
Readable, repeatable SKU codes from product attributes, with variant matrices and an optional check character that catches typos. No dependencies.
import { generateSku, variants, verifyCheckCharacter } from "@zenuinebit/sku-generator";
generateSku(["T-Shirt", "Blue", "M"]); // "TSH-BLU-M"
generateSku(["T-Shirt", "Blue", "M"], { seq: 42 }); // "TSH-BLU-M-0042"
generateSku(["Midnight Blue"], { abbreviations: { "Midnight Blue": "NVY" } }); // "NVY"
variants(["Tee"], { color: ["Blue", "Red"], size: ["S", "M"] });
// [{ sku: "TEE-BLU-S", attributes: { color: "Blue", size: "S" } }, ... 4 in total]
const sku = generateSku(["Tea", "250g"], { seq: 9, checkChar: true });
verifyCheckCharacter(sku); // true; false if one character is mistypedHow it works
- Each part is cleaned (accents removed, upper case, letters and digits only) and cut to 3 characters: the first letter, then consonants, then whatever is left.
BluebecomesBLU,ShirtbecomesSHR. Passabbreviationsfor words where you want a specific code, andpartLengthto change the length. - The same input always gives the same SKU. Add
seq(your product's row number) when two products could share all their parts. variantsthrows if two variants end up with the same SKU, so you find out when generating, not when scanning.- The check character uses Luhn mod 36. It catches any single wrong character and most swapped neighbours.
Not included: storage or uniqueness across your catalogue (keep seq in your database), and barcodes (see @zenuinebit/barcode).
