devmandan's picture
Fix styled-file hints, and add inches, copies and the many-codes rule
a59c3c4 verified
Raw History Blame Contribute Delete
6.38 kB
// Lengths as the page shows them: centimetres, millimetres or metres, each with its inch figure (feet and inches for a
// distance from 1 m). Both come from the same exact length and are rounded the same way: half up for a code's width,
// up for a picture's width, down for how far a code scans and for the four distances, and up for the near limit. A
// margin's inch figure alone is rounded up, so it never falls short of the clear space asked for; its metric figure
// rounds half up, as a code's width does, so it can be a little under. The words come from copy.json. plan.js,
// made.js, checks.js and plan_view.js use these.
import { count } from './util.js';
import { t } from './copy.js';
export const clean = (x) => Number(x.toFixed(9)); // float noise off before rounding, as checks.js does
const halfUp = (x) => Math.floor(clean(x) + 0.5);
const ROUND = { near: halfUp, up: (x) => Math.ceil(clean(x)), down: (x) => Math.floor(clean(x)) };
export const INCH = 2.54; // cm
const EIGHTHS = ['', '⅛', '¼', '⅜', '½', '⅝', '¾', '⅞'];
/** A width rounded as shown: to the mm under 20 cm and to the whole cm from 20; halves up for a code's width, or up
* for a picture's (so printing at it never makes the code smaller than its need). */
export function rounded(cm, up = false) {
const r = up ? (x) => Math.ceil(clean(x)) : halfUp;
const mm = r(cm * 10);
return mm < 200 ? mm / 10 : r(cm);
}
/** A width in cm or mm alone: a formula's input behind "+", and what made.js compares two widths by. */
export function cmText(cm, up = false) {
const x = rounded(cm, up);
if (x >= 1) return t('plan.unit.cm', { n: x < 20 ? x.toFixed(1) : count(x) });
return x > 0 ? t('plan.unit.mm', { n: Math.round(x * 10) }) : t('plan.unit.under_mm'); // under 1 cm, in mm
}
/** A length in inches as shown, rounded by mode ('near' half up, 'up' or 'down'): to the eighth under 10 in, and to
* the whole inch from 10. */
export function inches(cm, mode = 'near') {
const e = ROUND[mode]((cm / INCH) * 8);
return e >= 80 ? ROUND[mode](cm / INCH) : e / 8;
}
/** The same written with the fraction signs ("1¼ in", "⅝ in", "12 in"); '' when it rounds to nothing. */
export function inchText(cm, mode = 'near') {
const x = inches(cm, mode);
return x > 0 ? t('plan.unit.in', { n: x >= 10 ? count(x) : eighthsText(x) }) : '';
}
/** A distance in inches, rounded by mode: whole inches under 1 m, feet and inches from 1 m ("3 ft 3 in", "4 ft"), 1 m
* as its metric figure shows it (a least distance rounded up to 1 m is in feet); '' when it rounds to nothing. */
export function feetText(cm, mode = 'down') {
const n = cm < 1 ? 0 : ROUND[mode](cm / INCH);
if (n <= 0) return '';
if ((mode === 'up' ? upCm(cm) : cm) < 100) return t('plan.unit.in', { n });
const ft = Math.floor(n / 12);
return n % 12 ? t('plan.unit.ft_in', { ft, n: n % 12 }) : t('plan.unit.ft', { ft });
}
/** A metric figure with its inch figure ("3.2 cm (1¼ in)"), or alone when that rounds to nothing. */
export const both = (metric, imperial) => (imperial ? t('plan.unit.both', { metric, imperial }) : metric);
/** A code's width (half up), or a picture's (up), in both units. */
export const widthText = (cm, up = false) => both(cmText(cm, up), inchText(cm, up ? 'up' : 'near'));
/** A distance in cm or m alone, rounded down: to the cm under 1 m, to a tenth of a metre from 1 m. */
export function distCmText(cm) {
if (cm < 1) return t('plan.unit.under_cm');
if (cm < 100) return t('plan.unit.cm', { n: Math.floor(clean(cm)) });
return t('plan.unit.m', { n: String(Math.floor(clean(cm / 10)) / 10) });
}
/** A distance in both units: rounded down (how far a code scans, and the four distances), or up (mode 'up': a
* distance a code falls short of, the nearest planned for). */
export const distText = (cm, mode = 'down') => both(mode === 'up' ? upText(cm) : distCmText(cm), feetText(cm, mode));
/** A distance as shown, in cm: rounded down as distText rounds it, or up for a least distance (the near limit). */
export const downCm = (cm) => (cm < 100 ? Math.floor(clean(cm)) : 10 * Math.floor(clean(cm / 10)));
export const upCm = (cm) => (Math.ceil(clean(cm)) < 100 ? Math.ceil(clean(cm)) : 10 * Math.ceil(clean(cm / 10)));
const upText = (cm) => (upCm(cm) < 100 ? t('plan.unit.cm', { n: upCm(cm) }) : t('plan.unit.m', { n: String(upCm(cm) / 100) }));
/** The near limit, a least distance: rounded up in both units. */
export const distUpText = (cm) => distText(cm, 'up');
/** Whether a least distance, rounded up, comes out further than a distance rounded down, in cm or in inches: then the
* two can't be told apart, and the least one isn't shown. */
export const passes = (lo, hi) => upCm(lo) > downCm(hi) || Math.ceil(clean(lo / INCH)) > Math.floor(clean(hi / INCH));
/** A number as the page writes one: thousands separated, at most two decimals. */
export const numText = (x) => count(Number(x.toFixed(2)));
/** A typed number as the page read it: thousands separated, up to three decimals. */
export const typedText = (x) => count(Number(x.toFixed(3)));
/** A whole number of eighths of an inch as written: "1¼", "⅝", "3". */
export function eighthsText(x) {
const e = Math.round(x * 8);
return `${Math.floor(e / 8) || (e % 8 ? '' : '0')}${EIGHTHS[e % 8]}`;
}
/** Typed inches as a fraction, { x, n }: the inches and how the answer repeats them; null when the text isn't a
* fraction (a decimal is read as for cm). In eighths with a space or none ("5/8", "1 1/4", "1¼", "1 ¼") they are
* repeated with the fraction signs ("1¼"); a whole number and a fraction 1 or more ("5/4"), sixteenths ("9/16") and
* a whole number joined by a hyphen ("1-1/4") are repeated as typed. Another fraction ("1/3", "1 5/4") is NaN. */
export function parseInches(s) {
const v = s.trim().replace(/\s+/g, ' ');
const sign = /^(?:(\d+) ?-? ?)?([⅛¼⅜½⅝¾⅞])$/.exec(v);
if (sign) {
const x = Number(sign[1] || 0) + EIGHTHS.indexOf(sign[2]) / 8;
return { x, n: /-/.test(v) ? v : eighthsText(x) };
}
const m = /^(?:(\d+)( | ?- ?))?(\d+)\/(\d+)$/.exec(v);
if (!m) return null;
const [whole, a, b] = [Number(m[1] || 0), Number(m[3]), Number(m[4])];
if (![2, 4, 8, 16].includes(b) || !a || (m[1] && a >= b)) return { x: NaN, n: v };
const x = whole + a / b;
return { x, n: b <= 8 && a < b && !m[2]?.includes('-') ? eighthsText(x) : v };
}