Functional Weave
Code in TypeScript

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

  • generateSku(TSH, Navy Blue, Large, Cotton, 3) → TSH-NAV-LAR-COT prefix kept whole, each attribute cut to three characters
  • generateSku(TSH, Navy Blue, XL, 0) → TSH-NAVYBLUE-XL segment length 0 keeps attributes whole, spaces dropped
  • generateSku(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.

export function generateSku(prefix: string, attributes: readonly string[], segmentLength: number): string
prefixstringproduct family code, kept whole; "" for none
attributesstring[]variant attributes in a fixed order: colour, size, material
segmentLengthintcharacters kept from each attribute; 0 keeps them whole
returnsstringuppercase 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

import { generateSku } from "#fune/retail.sku-generate@^1";
impl/typescript.ts · 33 lines · open · raw
// Keep ASCII letters and digits only, then uppercase. Filtering first matters:
// "ß".toUpperCase() is "SS", which would make the SKU depend on how each
// language's Unicode tables treat one letter.
function clean(part: string): string {
  return part.replace(/[^A-Za-z0-9]/g, "").toUpperCase();
}

/**
 * A deterministic SKU: the cleaned prefix and attributes, joined by hyphens.
 * Each attribute is cut to `segmentLength` characters (0 keeps it whole).
 */
export function generateSku(prefix: string, attributes: readonly string[], segmentLength: number): string {
  if (!Number.isInteger(segmentLength) || segmentLength < 0) {
    throw new RangeError(`segmentLength must be 0 or more, received ${segmentLength}`);
  }
  if (attributes.length === 0) {
    throw new RangeError("a SKU needs at least one attribute");
  }
  const parts: string[] = [];
  if (prefix !== "") {
    const cleaned = clean(prefix);
    if (cleaned === "") throw new RangeError(`prefix ("${prefix}") has no letters or digits`);
    parts.push(cleaned);
  }
  attributes.forEach((attribute, index) => {
    const cleaned = clean(attribute);
    if (cleaned === "") {
      throw new RangeError(`attribute ${index + 1} ("${attribute}") has no letters or digits`);
    }
    parts.push(segmentLength === 0 ? cleaned : cleaned.slice(0, segmentLength));
  });
  return parts.join("-");
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the TypeScript 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. Or pin a range in fune.project and build in one step:

fune add retail.sku-generate
Download for TypeScript retail.sku-generate-1.0.0-typescript.fune · 7,100 bytes sha256 d9b1b0cc947846d84c92cdac7fe108f31bce532c1a8a42b9dabec7295427bd93

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./retail.sku-generate-1.0.0-typescript.fune, or fetch it from a terminal with fune pull retail.sku-generate@1.0.0:typescript.

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.

CaseArgumentsExpected
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
CaseArgumentsExpected
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

PathBytes
README.md1,729
impl/python.py1,447
impl/rust.rs2,082
impl/typescript.ts1,365
vectors.json2,019