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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | 9x 9x 9x 9x 9x 3x 1x 1x 17x 17x 17x 17x 1x 1x 4x 4x 4x 4x 4x 4x 8x 8x 8x 8x | /**
* Coherent.js I18n Formatters
*
* Locale-aware formatting for dates, numbers, and currencies
*
* @module i18n/formatters
*/
/**
* Date Formatter
* Formats dates according to locale
*/
export class DateFormatter {
constructor(locale = 'en') {
this.locale = locale;
}
/**
* Format a date
*
* @param {Date|string|number} date - Date to format
* @param {Object} [options] - Intl.DateTimeFormat options
* @returns {string} Formatted date
*/
format(date, options = {}) {
const dateObj = date instanceof Date ? date : new Date(date);
Eif (typeof Intl !== 'undefined' && Intl.DateTimeFormat) {
const formatter = new Intl.DateTimeFormat(this.locale, options);
return formatter.format(dateObj);
}
// Fallback
return dateObj.toLocaleDateString();
}
/**
* Format date as short (e.g., 1/1/2024)
*/
short(date) {
return this.format(date, {
year: 'numeric',
month: 'numeric',
day: 'numeric'
});
}
/**
* Format date as medium (e.g., Jan 1, 2024)
*/
medium(date) {
return this.format(date, {
year: 'numeric',
month: 'short',
day: 'numeric'
});
}
/**
* Format date as long (e.g., January 1, 2024)
*/
long(date) {
return this.format(date, {
year: 'numeric',
month: 'long',
day: 'numeric'
});
}
/**
* Format date as full (e.g., Monday, January 1, 2024)
*/
full(date) {
return this.format(date, {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
});
}
/**
* Format time
*/
time(date, options = {}) {
return this.format(date, {
hour: 'numeric',
minute: 'numeric',
...options
});
}
/**
* Format date and time
*/
dateTime(date, options = {}) {
return this.format(date, {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
...options
});
}
/**
* Format relative time (e.g., "2 days ago")
*/
relative(date) {
const dateObj = date instanceof Date ? date : new Date(date);
const now = new Date();
const diffMs = now - dateObj;
const diffSec = Math.floor(diffMs / 1000);
const diffMin = Math.floor(diffSec / 60);
const diffHour = Math.floor(diffMin / 60);
const diffDay = Math.floor(diffHour / 24);
if (typeof Intl !== 'undefined' && Intl.RelativeTimeFormat) {
const rtf = new Intl.RelativeTimeFormat(this.locale, { numeric: 'auto' });
if (diffDay > 0) {
return rtf.format(-diffDay, 'day');
} else if (diffHour > 0) {
return rtf.format(-diffHour, 'hour');
} else if (diffMin > 0) {
return rtf.format(-diffMin, 'minute');
} else {
return rtf.format(-diffSec, 'second');
}
}
// Fallback
if (diffDay > 0) {
return `${diffDay} day${diffDay > 1 ? 's' : ''} ago`;
} else if (diffHour > 0) {
return `${diffHour} hour${diffHour > 1 ? 's' : ''} ago`;
} else if (diffMin > 0) {
return `${diffMin} minute${diffMin > 1 ? 's' : ''} ago`;
} else {
return 'just now';
}
}
}
/**
* Number Formatter
* Formats numbers according to locale
*/
export class NumberFormatter {
constructor(locale = 'en') {
this.locale = locale;
}
/**
* Format a number
*
* @param {number} value - Number to format
* @param {Object} [options] - Intl.NumberFormat options
* @returns {string} Formatted number
*/
format(value, options = {}) {
Eif (typeof Intl !== 'undefined' && Intl.NumberFormat) {
const formatter = new Intl.NumberFormat(this.locale, options);
return formatter.format(value);
}
// Fallback
return value.toLocaleString();
}
/**
* Format as decimal
*/
decimal(value, decimals = 2) {
return this.format(value, {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals
});
}
/**
* Format as percentage
*/
percent(value, decimals = 0) {
return this.format(value, {
style: 'percent',
minimumFractionDigits: decimals,
maximumFractionDigits: decimals
});
}
/**
* Format as compact (e.g., 1.2K, 3.4M)
*/
compact(value) {
if (typeof Intl !== 'undefined' && Intl.NumberFormat) {
try {
const formatter = new Intl.NumberFormat(this.locale, {
notation: 'compact',
compactDisplay: 'short'
});
return formatter.format(value);
} catch {
// Fallback for older browsers
}
}
// Manual compact formatting
if (value >= 1e9) {
return `${(value / 1e9).toFixed(1) }B`;
} else if (value >= 1e6) {
return `${(value / 1e6).toFixed(1) }M`;
} else if (value >= 1e3) {
return `${(value / 1e3).toFixed(1) }K`;
}
return String(value);
}
/**
* Format with units
*/
unit(value, unit, options = {}) {
if (typeof Intl !== 'undefined' && Intl.NumberFormat) {
try {
const formatter = new Intl.NumberFormat(this.locale, {
style: 'unit',
unit,
...options
});
return formatter.format(value);
} catch {
// Fallback
}
}
return `${value} ${unit}`;
}
}
/**
* Currency Formatter
* Formats currency values according to locale
*/
export class CurrencyFormatter {
constructor(locale = 'en', defaultCurrency = 'USD') {
this.locale = locale;
this.defaultCurrency = defaultCurrency;
}
/**
* Format a currency value
*
* @param {number} value - Amount to format
* @param {string} [currency] - Currency code (e.g., 'USD', 'EUR')
* @param {Object} [options] - Additional options
* @returns {string} Formatted currency
*/
format(value, currency = null, options = {}) {
const currencyCode = currency || this.defaultCurrency;
Eif (typeof Intl !== 'undefined' && Intl.NumberFormat) {
const formatter = new Intl.NumberFormat(this.locale, {
style: 'currency',
currency: currencyCode,
...options
});
return formatter.format(value);
}
// Fallback
return `${currencyCode} ${value.toFixed(2)}`;
}
/**
* Format without decimal places
*/
whole(value, currency = null) {
return this.format(value, currency, {
minimumFractionDigits: 0,
maximumFractionDigits: 0
});
}
/**
* Format with symbol only (no code)
*/
symbol(value, currency = null) {
return this.format(value, currency, {
currencyDisplay: 'symbol'
});
}
/**
* Format with narrow symbol
*/
narrowSymbol(value, currency = null) {
return this.format(value, currency, {
currencyDisplay: 'narrowSymbol'
});
}
/**
* Format with code (e.g., USD 100.00)
*/
code(value, currency = null) {
return this.format(value, currency, {
currencyDisplay: 'code'
});
}
}
/**
* List Formatter
* Formats lists according to locale
*/
export class ListFormatter {
constructor(locale = 'en') {
this.locale = locale;
}
/**
* Format a list
*
* @param {Array} items - Items to format
* @param {Object} [options] - Formatting options
* @returns {string} Formatted list
*/
format(items, options = {}) {
Eif (typeof Intl !== 'undefined' && Intl.ListFormat) {
const formatter = new Intl.ListFormat(this.locale, options);
return formatter.format(items);
}
// Fallback
if (items.length === 0) return '';
if (items.length === 1) return items[0];
if (items.length === 2) return `${items[0]} and ${items[1]}`;
const last = items[items.length - 1];
const rest = items.slice(0, -1);
return `${rest.join(', ')}, and ${last}`;
}
/**
* Format as conjunction (and)
*/
and(items) {
return this.format(items, { type: 'conjunction' });
}
/**
* Format as disjunction (or)
*/
or(items) {
return this.format(items, { type: 'disjunction' });
}
/**
* Format as unit list
*/
unit(items) {
return this.format(items, { type: 'unit' });
}
}
/**
* Create formatters for a locale
*
* @param {string} locale - Locale code
* @param {Object} [options] - Formatter options
* @returns {Object} Formatter instances
*/
export function createFormatters(locale = 'en', options = {}) {
return {
date: new DateFormatter(locale),
number: new NumberFormatter(locale),
currency: new CurrencyFormatter(locale, options.defaultCurrency),
list: new ListFormatter(locale)
};
}
export default {
DateFormatter,
NumberFormatter,
CurrencyFormatter,
ListFormatter,
createFormatters
};
|