Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | 25x 10125x | import { type } from 'arktype';
import { conversions } from 'convert/conversions';
export const NumericUnit = type.enumerated(
// TODO turn ...[...x] into just ...x once Vite build supports Iterator#flatMap
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/flatMap
...[...conversions.values()].flatMap(
({ units }) =>
/** @type {import('convert').Unit[]} */
(units.flatMap((unit) => [...unit.names, ...('symbols' in unit ? unit.symbols : [])]))
)
);
/**
*
* @param {string} nameOrSymbol
*/
export function findUnit(nameOrSymbol) {
// TODO turn [...x] into just x once Vite build supports Iterator#flatMap
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/flatMap
return /** @type {import('convert').Unit | undefined} */ (
[...conversions.values()]
.flatMap((kind) => kind.units)
.find(
(u) =>
u.names.includes(nameOrSymbol) ||
('symbols' in u && u.symbols.includes(nameOrSymbol))
)
);
}
/**
*
* @param {import('convert').Unit} unit
* @returns {import('convert').Unit[]}
*/
export function availableUnitsFor(unit) {
// TODO turn [...x] into just x once Vite build supports Iterator#flatMap
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/flatMap
const kind = [...conversions.values()].find((kind) =>
kind.units.some(
(u) =>
/** @type {readonly string[]} */ (u.names).includes(unit) ||
('symbols' in u && /** @type {readonly string[]} */ (u.symbols).includes(unit))
)
);
return kind?.units.map(({ names }) => names[0]) ?? [];
}
/**
*
* @param {import('convert').Unit} unit
* @returns {{symbol: string, name: string }}
*/
export function displayUnit(unit) {
const u = [...conversions.values()]
.flatMap((kind) => kind.units)
.find((u) => [...u.names, ...u.symbols].includes(unit));
if (!u) return { symbol: '', name: '' };
const { names, symbols } = u;
const name = names.find(Boolean) ?? '';
let symbol = symbols.find(Boolean) ?? '';
if (symbol === 'C' || symbol === 'F') {
symbol = `°${symbol}`;
}
return { name, symbol };
}
/**
*
* @param {import('convert').Unit} unit
*/
export function unitKind(unit) {
const found = [...conversions.entries()].find(([, { units }]) =>
units.some((u) => [...u.names, ...u.symbols].includes(unit))
);
if (!found) return;
return found[0];
}
|