retail.sku-generate
Build a deterministic stock-keeping unit code from a prefix and product attributes.
1.0.0 · published 2026-10-03 by charlie · Anterra
Pinned by 15 tests, run in TypeScript, Python and Rust.
What it does
Builds a stock-keeping unit code from a product family prefix and the variant's attributes, so the same product and options always give the same SKU: `generateSku("TSH", ["Navy Blue", "Large", "Cotton"], 3)` is `"TSH-NAV-LAR-COT"`.
## The scheme
For example
generate_sku(TSH, Navy Blue, Large, Cotton, 3)→ TSH-NAV-LAR-COT prefix kept whole, each attribute cut to three charactersgenerate_sku(TSH, Navy Blue, XL, 0)→ TSH-NAVYBLUE-XL segment length 0 keeps attributes whole, spaces droppedgenerate_sku(MUG, Red, M, 4)→ MUG-RED-M an attribute shorter than the segment length is not padded
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
pub fn generate_sku(prefix: &str, attributes: &[String], segment_length: i64) -> String
| prefix | string | product family code, kept whole; "" for none |
| attributes | string[] | variant attributes in a fixed order: colour, size, material |
| segment_length | int | characters kept from each attribute; 0 keeps them whole |
| returns | string | uppercase A-Z and 0-9 segments joined by hyphens: "TSH-NAV-LAR" |
Your code names it in one line, in the file that uses it
fune!(retail.sku-generate@^1); // then call generate_sku(…)
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
use super::funejson::Value; ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
// Keep ASCII letters and digits only, then uppercase. Filtering first matters:
// "ß".to_uppercase() is "SS", which would make the SKU depend on how each
// language's Unicode tables treat one letter.
fn clean(part: &str) -> String {
part.chars()
.filter(|c| c.is_ascii_alphanumeric())
.map(|c| c.to_ascii_uppercase())
.collect()
}
/// A deterministic SKU: the cleaned prefix and attributes, joined by hyphens.
/// Each attribute is cut to `segment_length` characters (0 keeps it whole).
///
/// # Panics
/// Panics on a negative segment length, no attributes, or a part with no
/// letters or digits.
pub fn generate_sku(prefix: &str, attributes: &[String], segment_length: i64) -> String {
if segment_length < 0 {
panic!("segmentLength must be 0 or more, received {}", segment_length);
}
if attributes.is_empty() {
panic!("a SKU needs at least one attribute");
}
let mut parts: Vec<String> = Vec::new();
if !prefix.is_empty() {
let cleaned = clean(prefix);
if cleaned.is_empty() {
panic!("prefix (\"{}\") has no letters or digits", prefix);
}
parts.push(cleaned);
}
for (index, attribute) in attributes.iter().enumerate() {
let cleaned = clean(attribute);
if cleaned.is_empty() {
panic!(
"attribute {} (\"{}\") has no letters or digits",
index + 1,
attribute
);
}
// Cleaned text is ASCII, so byte length is character count.
if segment_length > 0 && cleaned.len() > segment_length as usize {
parts.push(cleaned[..segment_length as usize].to_string());
} else {
parts.push(cleaned);
}
}
parts.join("-")
}
pub fn fune_vector(args: &[Value]) -> Value {
let attributes: Vec<String> = args[1]
.as_arr()
.iter()
.map(|v| v.as_str().to_string())
.collect();
Value::Str(generate_sku(args[0].as_str(), &attributes, args[2].as_i64()))
}Install
fune build
With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Rust package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:
fune add retail.sku-generate
The manifest, vectors and README with only the Rust implementation. Install it without the registry with fune add ./retail.sku-generate-1.0.0-rust.fune, or fetch it from a terminal with fune pull retail.sku-generate@1.0.0:rust.
The whole function, every language, is one file too: retail.sku-generate-1.0.0.fune, 10,804 bytes, sha256 7ef658222abadfe67f4a35e4484720e326cfec7c0c1a48b09861c4ff9050d9db. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
// fune: before retail.sku-generate
after — your function gets the result and the arguments, and returns the final result.
// fune: after retail.sku-generate
replace — it requires no other capability, so there is no dependency to replace.
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show retail.sku-generate --steps.
// fune: step retail.sku-generate after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| prefix kept whole, each attribute cut to three characters | TSH, Navy Blue, Large, Cotton, 3 | → | TSH-NAV-LAR-COT |
| segment length 0 keeps attributes whole, spaces dropped | TSH, Navy Blue, XL, 0 | → | TSH-NAVYBLUE-XL |
| an attribute shorter than the segment length is not padded | MUG, Red, M, 4 | → | MUG-RED-M |
| lowercase and punctuation in prefix and attributes | ab-1, x/l, 50 cm , 0 | → | AB1-XL-50CM |
| an empty prefix is left out, no leading hyphen | , Blue, S, 3 | → | BLU-S |
| sharp s is dropped, not uppercased to SS as a naive toUpperCase first would | , Straße, 0 | → | STRAE |
| accented letters are dropped, not transliterated | DES, Crème, Brûlée, 0 | → | DES-CRME-BRLE |
| a decimal size keeps its digits and loses the point | SH, 10.5, UK, 0 | → | SH-105-UK |
| truncation counts characters after cleaning | P, N-a-v-y, 3 | → | P-NAV |
| segment length 1 keeps initials | SOCK, Black, Wool, Medium, 1 | → | SOCK-B-W-M |
Show the other 5 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| the prefix is never truncated | LONGPREFIX, Green, 2 | → | LONGPREFIX-GR |
| no attributes is an error | TSH, , 3 | → | error: a SKU needs at least one attribute |
| an attribute with no letters or digits is an error | TSH, Blue, ***, 3 | → | error: attribute 2 ("***") has no letters or digits |
| a prefix with no letters or digits is an error | !!!, Blue, 3 | → | error: prefix ("!!!") has no letters or digits |
| a negative segment length is an error | TSH, Blue, -1 | → | error: segmentLength must be 0 or more |
More from the author
1. Each part (the prefix, then each attribute in the order given) keeps only the ASCII letters and digits `A-Z a-z 0-9`, in order. Spaces, punctuation and every non-ASCII character are dropped. 2. What is left is uppercased. 3. Each attribute, not the prefix, is cut to its first `segmentLength` characters. `0` keeps it whole. 4. The parts are joined with `-`. An empty prefix is left out.
Filtering happens before uppercasing on purpose: German sharp s uppercases to "SS" in all three languages, so uppercasing first would turn "Straße" into `STRASSE` in some places and `STRAE` in others. Here it is always `STRAE`. Accented letters are dropped, not transliterated ("Crème" is `CRME`); run the attribute through `text.slugify` first if you want `CREME`.
## What it does not do
It does not guarantee uniqueness. Two attributes that agree in their first `segmentLength` characters collide ("Brown" and "Bronze" are both `BRO` at 3), and so do "10.5" and "105" (both `105`). A SKU scheme with a fixed segment length needs a catalogue-level check, or a longer segment, and that belongs to whoever owns the catalogue. The order of the attributes is part of the SKU, so fix it per product family (colour, then size, then material).
## Errors
No attributes, a negative `segmentLength`, or a prefix or attribute with no letters or digits at all (`"***"`) are errors, because a SKU with an empty segment would silently collide with its neighbours.
Files
| Path | Bytes |
|---|---|
| README.md | 1,729 |
| impl/python.py | 1,447 |
| impl/rust.rs | 2,082 |
| impl/typescript.ts | 1,365 |
| vectors.json | 2,019 |