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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | 124x 79x 91x 10x 18x 15x 18x 12x 6x 6x | import * as dates from 'date-fns';
import type * as DB from '$lib/database.js';
import { type Language } from '$lib/i18n';
import { type RuntimeValue } from '$lib/schemas/metadata.js';
import { fromEntries, groupBy, mapValues, round } from '$lib/utils.js';
import { metadataPrettyValue } from './display.js';
import { switchOnMetadataType } from './types.js';
/**
*
* @param args
* @param args.language the language to use for pretty-printing metadata values
* @param args.options the metadata options, if applicable (for enums)
* @param args.type the metadata type of the given values to group with
* @param [args.tolerances] tolerances to apply when comparing certain metadata types.
* @param args.tolerances.date the granularity to consider when comparing date values
* @param args.tolerances.float number of decimal places to consider when comparing number values (ints or floats). If negative, rounds to powers of ten. Also used for location latitude/longitude values and bounding box coordinates.
* @returns a function to put a given value into a group, returning the serialized group key
*/
export function metadataValueGrouper({
language,
type,
options = [],
tolerances = {
date: 'day',
float: 1
}
}: {
language: Language;
type: DB.MetadataType;
options?: DB.MetadataEnumVariant[];
tolerances?:
| undefined
| {
date: 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second';
float: number;
};
}) {
const rnd = (v: number) => round(v, tolerances.float);
const display = (value: RuntimeValue) =>
metadataPrettyValue(language, { type }, value, options.find((o) => o.key === value)?.label);
return (value: RuntimeValue) =>
switchOnMetadataType<string>(type, value, {
boolean: display,
string: display,
enum: display,
integer: (v) => display(rnd(v)),
float: (v) => display(rnd(v)),
boundingbox: (coords) => display(mapValues(coords, rnd)),
location: (coords) => display(mapValues(coords, rnd)),
date: (v) => {
switch (tolerances.date) {
case 'year':
return `Année ${dates.getYear(v)}`;
case 'month':
return dates.format(v, 'MMMM yyyy');
case 'day':
return new Intl.DateTimeFormat(language, {
dateStyle: 'long'
}).format(v);
case 'hour':
return new Intl.DateTimeFormat(language, {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric'
}).format(v);
case 'minute':
return new Intl.DateTimeFormat(language, {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric'
}).format(v);
case 'second':
return new Intl.DateTimeFormat(language, {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric'
}).format(v);
default:
throw new Error(
`Invalid date tolerance: ${tolerances.date} (expected year, month, day, hour, minute or second)`
);
}
}
});
}
if (import.meta.vitest) {
const { describe, test, expect } = import.meta.vitest;
const { metadatas, values } = await import('./_testdata.js');
function groupMetadataValues(
vals: (typeof values)[keyof typeof values],
settings: Omit<Parameters<typeof metadataValueGrouper>[0], 'language'>
) {
const grouper = metadataValueGrouper({
language: 'fr',
...settings
});
return mapValues(
fromEntries([...groupBy(vals, ([, value]) => grouper(value)).entries()]),
(items) => items.map(([id]) => id)
);
}
describe('groupMetadataValues', () => {
describe('integers', () => {
test('with default tolerances', () => {
expect(
groupMetadataValues(values.integer, {
type: metadatas.integer.type
})
).toMatchObject({
'10': ['item1', 'item2'],
'15': ['item3'],
'20': ['item4'],
'30': ['item5']
});
});
test('with tens tolerances', () => {
expect(
groupMetadataValues(values.integer, {
type: metadatas.integer.type,
tolerances: { float: -1, date: 'second' }
})
).toMatchObject({
'10': ['item1', 'item2'],
'20': ['item3', 'item4'],
'30': ['item5']
});
});
});
describe('floats', () => {
test('with default tolerances', () => {
expect(
groupMetadataValues(values.float, {
type: metadatas.float.type
})
).toMatchObject({
'10,1': ['item1', 'item2'],
'15,6': ['item3'],
'20': ['item4', 'item5'],
'25': ['item6']
});
});
test('with decimal tolerances', () => {
expect(
groupMetadataValues(values.float, {
type: metadatas.float.type,
tolerances: { float: 2, date: 'second' }
})
).toMatchObject({
'10,12': ['item1'],
'10,13': ['item2'],
'15,57': ['item3'],
'20': ['item4', 'item5'],
'25': ['item6']
});
});
test('with hundreds tolerances', () => {
expect(
groupMetadataValues(values.float, {
type: metadatas.float.type,
tolerances: { float: -2, date: 'second' }
})
).toMatchObject({
'0': ['item1', 'item2', 'item3', 'item4', 'item5', 'item6']
});
});
});
describe('dates', () => {
test('with default tolerances', () => {
expect(
groupMetadataValues(values.date, {
type: metadatas.date.type
})
).toMatchObject({
'1 janvier 2023': ['item1', 'item2'],
'2 janvier 2023': ['item3'],
'3 janvier 2023': ['item4', 'item5'],
'4 janvier 2023': ['item6']
});
});
test('with minute tolerances', () => {
expect(
groupMetadataValues(values.date, {
type: metadatas.date.type,
tolerances: { float: 2, date: 'minute' }
})
).toMatchObject({
'1 janvier 2023 à 12:00': ['item1', 'item2'],
'2 janvier 2023 à 12:00': ['item3'],
'3 janvier 2023 à 12:00': ['item4'],
'3 janvier 2023 à 12:05': ['item5'],
'4 janvier 2023 à 12:00': ['item6']
});
});
});
test('strings', () => {
expect(
groupMetadataValues(values.string, {
type: metadatas.string.type
})
).toMatchObject({
apple: ['item1', 'item3'],
banana: ['item2', 'item5'],
grape: ['item6'],
orange: ['item4']
});
});
test('booleans', () => {
expect(
groupMetadataValues(values.boolean, {
type: metadatas.boolean.type
})
).toMatchObject({
Non: ['item2', 'item4', 'item5'],
Oui: ['item1', 'item3', 'item6']
});
});
describe('locations', () => {
test('with default tolerances', () => {
expect(
groupMetadataValues(values.location, {
type: metadatas.location.type
})
).toMatchObject({
'10.1, 20.1': ['item1', 'item2', 'item4', 'item5'],
'15.6, 25.6': ['item3'],
'30, 40': ['item6']
});
});
test('with unit decimal tolerances', () => {
expect(
groupMetadataValues(values.location, {
type: metadatas.location.type,
tolerances: { float: 0, date: 'second' }
})
).toMatchObject({
'10, 20': ['item1', 'item2', 'item4', 'item5'],
'16, 26': ['item3'],
'30, 40': ['item6']
});
});
test('with tens tolerances', () => {
expect(
groupMetadataValues(values.location, {
type: metadatas.location.type,
tolerances: { float: -1, date: 'second' }
})
).toMatchObject({
'10, 20': ['item1', 'item2', 'item4', 'item5'],
'20, 30': ['item3'],
'30, 40': ['item6']
});
});
});
test('enums', () => {
expect(
groupMetadataValues(values.enum, {
type: metadatas.enum.type
})
).toMatchObject({
A: ['item1', 'item3', 'item6'],
B: ['item2', 'item5'],
C: ['item4']
});
});
describe('bounding boxes', () => {
test('with default tolerances', () => {
expect(
groupMetadataValues(values.boundingbox, {
type: metadatas.boundingbox.type
})
).toMatchObject({
'Boîte de (10.00, 10.00) à (60.00, 60.00)': ['item1'],
'Boîte de (11.00, 11.00) à (61.00, 61.00)': ['item4', 'item5'],
'Boîte de (12.00, 12.00) à (62.00, 62.00)': ['item2'],
'Boîte de (15.00, 15.00) à (65.00, 65.00)': ['item6']
});
});
test('with unit decimal tolerances', () => {
expect(
groupMetadataValues(values.boundingbox, {
type: metadatas.boundingbox.type,
tolerances: { float: 0, date: 'second' }
})
).toMatchObject({
'Boîte de (10.00, 10.00) à (60.00, 60.00)': ['item1'],
'Boîte de (11.00, 11.00) à (61.00, 61.00)': ['item4', 'item5'],
'Boîte de (12.00, 12.00) à (62.00, 62.00)': ['item2'],
'Boîte de (15.00, 15.00) à (65.00, 65.00)': ['item6']
});
});
test('with tens tolerances', () => {
expect(
groupMetadataValues(values.boundingbox, {
type: metadatas.boundingbox.type,
tolerances: { float: -1, date: 'second' }
})
).toMatchObject({
'Boîte de (10.00, 10.00) à (60.00, 60.00)': [
'item1',
'item2',
'item4',
'item5'
],
'Boîte de (20.00, 20.00) à (70.00, 70.00)': ['item6']
});
});
});
});
}
|