techtools
Головна / research / lab / stock-lab / src / domain / location.ts

location.ts

TypeScript · 18 рядків

1// Адреса комірки: у базі — число (location_id), людям — код «A-01-03» (зона, ряд, ярус).
2// id = зона × 10 000 + ряд × 100 + ярус; зона A = 1 … Z = 26.
3import { DomainError } from './errors.ts';
4
5export function formatLocation(id: number): string {
6  const zone = Math.floor(id / 10_000);
7  const row = Math.floor((id % 10_000) / 100);
8  const level = id % 100;
9  if (zone < 1 || zone > 26) return `#${id}`;
10  return `${String.fromCharCode(64 + zone)}-${String(row).padStart(2, '0')}-${String(level).padStart(2, '0')}`;
11}
12
13export function parseLocation(code: string): number {
14  const m = /^([A-Z])-(\d{2})-(\d{2})$/.exec(code.trim().toUpperCase());
15  if (!m) throw new DomainError('LOCATION_FORMAT', `Адреса «${code}» не у форматі A-01-03`);
16  return (m[1].charCodeAt(0) - 64) * 10_000 + Number(m[2]) * 100 + Number(m[3]);
17}
18