0

A fit check for one-size stretch clothing in JavaScript

Most clothes come in sizes, and a size chart maps a body measurement to S, M or L. A one-size stretch knit has nothing to pick from. Its chart gives a range for each measurement, and the customer's question changes from "which size?" to "will this one fit me?".

I look after the online side of a small kawaii clothing shop. The numbers in this post come from the chart of a one-size backless turtleneck knit, a virgin killer sweater dress at the Kawaiiki store. The knit stretches to a bust of 72–94 cm and a waist of 58–82 cm. Measurements arrive in different forms: centimetres, inches, a comma as the decimal mark, or a bare number. Below is the small JavaScript check that turns them into an answer. I ran every block in Node 24, and none of them needs a dependency.

Parse the measurement first

The input is a string, so the first job is to turn it into centimetres. A bare number is read as centimetres, because the chart is in centimetres. A decimal comma ("31,5") is common in Europe, so it gets the same treatment as a dot.

const CM_PER_INCH = 2.54;

function toCm(input) {
  const m = String(input).trim().toLowerCase()
    .match(/^(\d+(?:[.,]\d+)?)\s*(cm|in|inch|inches|")?$/);
  if (!m) throw new RangeError(`Not a measurement: ${input}`);
  const value = Number(m[1].replace(",", "."));
  return m[2] && m[2] !== "cm" ? value * CM_PER_INCH : value;
}

toCm("86");      // 86
toCm('34"');     // 86.36
toCm("31,5 in"); // 80.01
toCm("M");       // throws RangeError

toCm("M") throws on purpose. A letter size says nothing about a one-size chart, and a silent guess is worse than asking the customer again.

Where in the range the body falls

Being inside the range is only half the answer. A bust of 73 cm and a bust of 93 cm are both inside 72–94 cm, but the knit hangs loose on the first and is stretched almost to its limit on the second. So the check returns a position: 0 at the bottom of the range, 1 at the top.

const chart = {
  bust:  { min: 72, max: 94 },
  waist: { min: 58, max: 82 },
};

function position(cm, { min, max }) {
  return (cm - min) / (max - min);
}

function describe(p) {
  if (p < 0) return "loose";
  if (p > 1) return "too small";
  if (p > 0.8) return "very snug";
  return "fits";
}

position(83, chart.bust);           // 0.5
describe(position(92, chart.bust)); // "very snug"

The 0.8 cut-off is my own choice. Above it the fabric is close to its maximum stretch. I would rather warn a customer that the dress will be tight than wait for a return.

One verdict for several measurements

A garment fits only if every measurement fits, so the worst result decides. The function keeps the per-measurement rows too. "The waist is fine, the bust is near the top of the range" is a more useful reply than a single word.

const SEVERITY = ["fits", "very snug", "loose", "too small"];

function checkFit(measurements, chart) {
  const rows = Object.entries(measurements).map(([name, raw]) => {
    const cm = toCm(raw);
    const p = position(cm, chart[name]);
    return {
      name,
      cm: Math.round(cm * 10) / 10,
      position: Math.round(p * 100) / 100,
      verdict: describe(p),
    };
  });
  const worst = rows.reduce((a, b) =>
    SEVERITY.indexOf(b.verdict) > SEVERITY.indexOf(a.verdict) ? b : a);
  return { verdict: worst.verdict, decidedBy: worst.name, rows };
}

checkFit({ bust: '34"', waist: "66 cm" }, chart).verdict;
// "fits": bust 86.4 cm at 0.65, waist 66 cm at 0.33

const r = checkFit({ bust: "37 in", waist: "70" }, chart);
r.verdict;   // "very snug"
r.decidedBy; // "bust"

37 inches is 93.98 cm, just under the 94 cm limit. The waist sits in the middle of its range, so the bust alone sets the answer.

Height stays out of the check. The chart gives it only as a recommendation, 150–170 cm, for a dress with a fixed length of 64 cm. A taller customer still gets into the dress; it just sits shorter.

Showing the range in inches

The same chart has to be shown to customers who measure in inches, and rounding can promise more than the centimetre range does. 72 cm is 28.35 inches. Rounded to 28", it suggests that a 28-inch bust is inside the range, but 28 inches is 71.1 cm. The safe rule is to round the minimum up and the maximum down:

function rangeInInches({ min, max }) {
  const up = (cm) => Math.ceil((cm / CM_PER_INCH) * 10) / 10;
  const down = (cm) => Math.floor((cm / CM_PER_INCH) * 10) / 10;
  return `${up(min)}–${down(max)}"`;
}

rangeInInches(chart.bust);  // '28.4–37"'
rangeInInches(chart.waist); // '22.9–32.2"'

The inch range comes out slightly narrower than the centimetre one, and every value inside it is inside the chart.

What the check cannot know

The script knows the two numbers per measurement that the chart gives and nothing else about the fabric. Two knits with the same range can feel different near the top of it, and a ribbed waist stretches differently from the body of a dress. A chart records neither, so a "very snug" result still needs a person who has handled the garment to say how snug.


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí