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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | 99x 99x 10x 8x 2x 2x 10x 20x 20x 19x 19x 76x 38x 19x 19x 19x 17x 2x 32x 6x | import { type } from 'arktype';
import * as dates from 'date-fns';
import * as DB from '$lib/database';
import type { RuntimeValue } from '$lib/schemas/metadata';
import { round } from '$lib/utils';
/**
* Adds valueLabel to each metadata value object when the metadata is an enum.
* @param values
* @param metadataOptions
* @returns
*/
export function addValueLabels(
values: DB.MetadataValues,
metadataOptions: Record<string, Record<string, Pick<DB.MetadataEnumVariant, 'key' | 'label'>>>
): Record<string, DB.MetadataValue & { valueLabel?: string }> {
return Object.fromEntries(
Object.entries(values).map(([key, value]) => {
const opts = metadataOptions[key];
if (!opts) return [key, value];
const opt = opts[value.value.toString()];
return [key, { ...value, valueLabel: opt?.label }];
})
);
}
/**
* Returns a human-friendly string for a metadata value.
* Used for e.g. CSV exports.
* @param language
* @param metadata the metadata definition
* @param value the value of the metadata
* @param valueLabel the label of the value, if applicable (e.g. for enums)
* @param precision number of decimal places to use for float values of bounding box. set to undefined to not limit decimal places.
*/
export function metadataPrettyValue<Type extends DB.MetadataType>(
value: RuntimeValue<Type> | null,
{
language,
type: metadataType,
valueLabel,
boundingBoxPrecision = 2
}: {
language: import('$lib/i18n.js').Language;
type: Type;
valueLabel?: string | undefined;
boundingBoxPrecision?: number | 'unbounded' | undefined;
}
) {
Iif (value === null) return '';
switch (metadataType) {
case 'boolean':
switch (language) {
case 'fr':
return value ? 'Oui' : 'Non';
default:
return value ? 'Yes' : 'No';
}
case 'date':
return value instanceof Date ? dates.format(value, 'Ppp') : value.toString();
case 'enum':
return valueLabel || value.toString();
case 'location': {
const { latitude, longitude } = type({
latitude: 'number',
longitude: 'number'
}).assert(value);
return `${latitude}, ${longitude}`;
}
case 'boundingbox': {
const { x, y, w, h } = type({
x: 'number',
y: 'number',
h: 'number',
w: 'number'
}).assert(value);
const coord = (v: number) =>
boundingBoxPrecision === 'unbounded'
? v.toString()
: round(v, boundingBoxPrecision);
const point = (x: number, y: number) => `(${coord(x)}, ${coord(y)})`;
const start = point(x, y);
const end = point(x + w, y + h);
switch (language) {
case 'fr':
return `Boîte de ${start} à ${end}`;
default:
return `Box from ${start} to ${end}`;
}
}
case 'float':
case 'integer':
return Intl.NumberFormat(language).format(type('number').assert(value));
default:
return value.toString();
}
}
if (import.meta.vitest) {
const { expect, test, describe, beforeEach } = import.meta.vitest;
describe('metadataPrettyValue', () => {
describe('in french', () => {
beforeEach(async () => {
const { fr } = await import('date-fns/locale');
dates.setDefaultOptions({ locale: fr });
});
test('booleans', () => {
expect(metadataPrettyValue(true, { type: 'boolean', language: 'fr' })).toBe('Oui');
expect(metadataPrettyValue(false, { type: 'boolean', language: 'fr' })).toBe('Non');
});
test('dates', () => {
expect(
metadataPrettyValue(new Date('2023-02-01T15:04:05Z'), {
type: 'date',
language: 'fr'
})
).toBe('01/02/2023, 15:04:05');
});
test('floats', () => {
expect(metadataPrettyValue(12012.34, { type: 'float', language: 'fr' })).toBe(
'12 012,34'
);
});
describe('bounding boxes', () => {
const box = { x: 1, y: 2.005, w: 3, h: 4 };
test('with default precision', () => {
expect(metadataPrettyValue(box, { type: 'boundingbox', language: 'fr' })).toBe(
'Boîte de (1, 2.01) à (4, 6.01)'
);
});
test('with unbounded precision', () => {
expect(
metadataPrettyValue(box, {
type: 'boundingbox',
language: 'fr',
boundingBoxPrecision: 'unbounded'
})
).toBe('Boîte de (1, 2.005) à (4, 6.005)');
});
});
test('integers', () => {
expect(metadataPrettyValue(12012, { type: 'integer', language: 'fr' })).toBe(
'12\u202F012'
);
});
});
describe('in english', () => {
beforeEach(async () => {
const { enUS } = await import('date-fns/locale');
dates.setDefaultOptions({ locale: enUS });
});
test('booleans', () => {
expect(metadataPrettyValue(true, { type: 'boolean', language: 'en' })).toBe('Yes');
expect(metadataPrettyValue(false, { type: 'boolean', language: 'en' })).toBe('No');
});
test('dates', () => {
expect(
metadataPrettyValue(new Date('2023-02-01T15:04:05Z'), {
type: 'date',
language: 'en'
})
).toBe('02/01/2023, 3:04:05 PM');
});
describe('bounding boxes', () => {
const box = { x: 1, y: 2.005, w: 3, h: 4 };
test('with default precision', () => {
expect(metadataPrettyValue(box, { type: 'boundingbox', language: 'en' })).toBe(
'Box from (1, 2.01) to (4, 6.01)'
);
});
test('with unbounded precision', () => {
expect(
metadataPrettyValue(box, {
type: 'boundingbox',
language: 'en',
boundingBoxPrecision: 'unbounded'
})
).toBe('Box from (1, 2.005) to (4, 6.005)');
});
});
test('floats', () => {
expect(metadataPrettyValue(12012.34, { type: 'float', language: 'en' })).toBe(
'12,012.34'
);
});
test('integers', () => {
expect(metadataPrettyValue(12012, { type: 'integer', language: 'en' })).toBe(
'12,012'
);
});
});
test('locations', () => {
expect(
metadataPrettyValue(
{ latitude: 12.34, longitude: 56.78 },
{ type: 'location', language: 'fr' }
)
).toBe('12.34, 56.78');
expect(
metadataPrettyValue(
{ latitude: 12.34, longitude: 56.78 },
{ type: 'location', language: 'en' }
)
).toBe('12.34, 56.78');
});
test('enums', () => {
expect(
metadataPrettyValue('value1', {
language: 'en',
type: 'enum',
valueLabel: 'Label 1'
})
).toBe('Label 1');
expect(
metadataPrettyValue('value1', {
language: 'fr',
type: 'enum',
valueLabel: 'Label 1'
})
).toBe('Label 1');
expect(metadataPrettyValue('value2', { language: 'en', type: 'enum' })).toBe('value2');
expect(metadataPrettyValue('value2', { language: 'fr', type: 'enum' })).toBe('value2');
});
});
}
/**
* Returns a human-friendly string for a metadata key. Uses the label, and adds useful info about the data format if applicable.
* To be used with `metadataPrettyValue`.
* @param {DB.Metadata} metadata
* @returns
*/
export function metadataPrettyKey(metadata: DB.Metadata) {
let out = metadata.label;
switch (metadata.type) {
case 'location':
out += ' (latitude, longitude)';
}
return out;
}
|