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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 10x 10x 5x 5x 10x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 2x 2x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 5x 5x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 5x 5x 5x 5x 1x 1x 5x 8x 8x 6x 6x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 14x 14x | /**
* @typedef {import('$lib/database').Settings['language']} Language
*/
/**
* Return a ", "-separated list of "{count} {thing}" strings, with thing set to plural.
* If thing is found in plurals, use that, otherwise use "{thing}s".
* @param {Record<string, number>} things
* @param {Record<string, string>} [plurals]
*/
export function countThings(things, plurals) {
return Object.entries(things)
.filter(([, count]) => count > 0)
.map(([thing, count]) => {
let counted = thing;
if (count > 1) {
counted = plurals?.[thing] ?? `${thing}s`;
}
return `${count} ${counted}`;
})
.join(', ');
}
if (import.meta.vitest) {
const { test, expect } = import.meta.vitest;
test('countThings', () => {
expect(countThings({ a: 1, b: 2, c: 0 })).toBe('1 a, 2 bs');
expect(countThings({ a: 1, b: 2, c: 0 }, { b: 'foo' })).toBe('1 a, 2 foo');
expect(countThings({ a: 1, b: 2, c: 0 }, { a: 'a', b: 'b' })).toBe('1 a, 2 b');
});
}
/**
*
* @param {string} name
* @param {number} count
* @param {Record<string, string>} [plurals]
*/
export function countThing(name, count, plurals) {
return countThings({ [name]: count }, plurals);
}
if (import.meta.vitest) {
const { test, expect } = import.meta.vitest;
test('countThing', () => {
expect(countThing('item', 1)).toBe('1 item');
expect(countThing('item', 2)).toBe('2 items');
expect(countThing('item', 0)).toBe('');
expect(countThing('child', 3, { child: 'children' })).toBe('3 children');
expect(countThing('person', 1, { person: 'people' })).toBe('1 person');
});
}
/**
* Pluralizes a string based on a number and a list of candidate strings.
* @see https://wuchale.dev/guides/plurals/#usage
* @param {number} num
* @param {string[]} candidates
* @param {(n: number) => number} [rule]
* @returns {string}
*/
export function plural(num, candidates, rule = (n) => (n === 1 ? 0 : 1)) {
const index = rule(num);
return candidates[index].replace('#', Intl.NumberFormat().format(num));
}
/**
* Converts a number between 0 and 1 to a percentage string.
* @param {number} value Number between 0 and 1
* @param {number} [decimals=0] Number of decimal places to include in the output
* @param {object} [options] Additional options
* @param {'none'|'nbsp'|'zeros'} [options.pad=none] Whether to pad the percentage with leading non-breaking spaces
* @returns {`${number}%`} Percentage string
*/
export function percent(value, decimals = 0, { pad = 'none' } = {}) {
let result = (value * 100).toFixed(decimals);
// Remove trailing zeros and decimal point if not needed
if (decimals > 0) result = result.replace(/\.?0+$/, '');
if (pad !== 'none') {
result = result.padStart(2 + decimals, pad === 'nbsp' ? '\u00A0' : '0');
}
// @ts-expect-error
return result + '%';
}
if (import.meta.vitest) {
const { test, expect } = import.meta.vitest;
test('percent', () => {
expect(percent(0.5)).toBe('50%');
expect(percent(0.123)).toBe('12%');
expect(percent(0.123, 1)).toBe('12.3%');
expect(percent(0.123, 2)).toBe('12.3%'); // trailing zeros removed
expect(percent(0.1234, 2)).toBe('12.34%');
expect(percent(1)).toBe('100%');
expect(percent(0)).toBe('0%');
expect(percent(0.05, 1, { pad: 'zeros' })).toBe('005%'); // 5.0 -> 5 -> 005
expect(percent(0.05, 0, { pad: 'nbsp' })).toBe('\u00A05%');
});
}
/**
* Returns a human-readable name for a content type.
* @param {string} contentType Content type, of the form type/subtype
*/
export function humanFormatName(contentType) {
const [supertype, subtype] = contentType.split('/', 2);
let result = subtype.replace(/^x-/, '');
if (['image', 'video', 'audio'].includes(supertype)) {
result = result.toUpperCase();
}
return result;
}
if (import.meta.vitest) {
const { test, expect } = import.meta.vitest;
test('humanFormatName', () => {
expect(humanFormatName('image/jpeg')).toBe('JPEG');
expect(humanFormatName('image/png')).toBe('PNG');
expect(humanFormatName('video/mp4')).toBe('MP4');
expect(humanFormatName('audio/mpeg')).toBe('MPEG');
expect(humanFormatName('application/json')).toBe('json');
expect(humanFormatName('text/plain')).toBe('plain');
expect(humanFormatName('image/x-icon')).toBe('ICON');
expect(humanFormatName('application/x-zip-compressed')).toBe('zip-compressed');
});
}
/**
*
* @param {unknown} error
* @param {string} [prefix]
* @returns {string}
*/
export function errorMessage(error, prefix = '') {
const defaultMessage = 'Erreur inattendue';
let result = defaultMessage;
if (error instanceof Error) {
if ('message' in error && error.message) {
result = error.message || defaultMessage;
}
if ('cause' in error && error.cause) {
result = errorMessage(error.cause);
}
}
result = error?.toString() || defaultMessage;
while (result.startsWith('Error: ')) {
result = result.slice('Error: '.length);
}
result ||= defaultMessage;
return prefix ? `${prefix}: ${result}` : result;
}
if (import.meta.vitest) {
const { test, expect } = import.meta.vitest;
test('errorMessage', async () => {
expect(errorMessage(new Error('test error'))).toBe('test error');
expect(errorMessage(new Error(/* @wc-ignore */ 'Error: test error'))).toBe('test error');
expect(errorMessage('string error')).toBe('string error');
expect(errorMessage(null)).toBe('Erreur inattendue');
expect(errorMessage(undefined)).toBe('Erreur inattendue');
expect(errorMessage(new Error('test'), 'prefix')).toBe('prefix: test');
// The current implementation overwrites with toString(), so cause isn't used
const errorWithCause = new Error('main error');
errorWithCause.cause = new Error('cause error');
expect(errorMessage(errorWithCause)).toBe('main error');
});
}
/**
*
* @returns {Language}
*/
export function localeFromNavigator() {
const locale = navigator.language.split('-')[0];
return locale === 'fr' ? 'fr' : 'en';
}
|