@lightdash/common 2.15.0 → 2.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ const moment_1 = tslib_1.__importDefault(require("moment"));
6
6
  const moment_timezone_1 = tslib_1.__importDefault(require("moment-timezone"));
7
7
  const field_1 = require("../types/field");
8
8
  const timeFrames_1 = require("../types/timeFrames");
9
+ const convertCustomMetricsToYaml_1 = require("./convertCustomMetricsToYaml");
9
10
  const formatting_1 = require("./formatting");
10
11
  const formatting_mock_1 = require("./formatting.mock");
11
12
  describe('Formatting', () => {
@@ -1547,15 +1548,18 @@ describe('Formatting', () => {
1547
1548
  type: field_1.CustomFormatType.BYTES_IEC,
1548
1549
  compact: field_1.Compact.KIBIBYTES,
1549
1550
  });
1550
- expect(kibibyteExpression).toEqual('#,##0.00"KiB"');
1551
- expect((0, formatting_1.formatValueWithExpression)(kibibyteExpression, 2048)).toEqual('2.00KiB');
1551
+ // No round → up to 3 trailing-dropped decimals, matching the
1552
+ // structured applyCustomFormat path (G4 alignment).
1553
+ expect(kibibyteExpression).toEqual('#,##0.###"KiB"');
1554
+ expect((0, formatting_1.formatValueWithExpression)(kibibyteExpression, 2048)).toEqual('2KiB');
1555
+ expect((0, formatting_1.formatValueWithExpression)(kibibyteExpression, 1536)).toEqual('1.5KiB');
1552
1556
  // Test MEBIBYTES (should divide by 1048576)
1553
1557
  const mebibyteExpression = (0, formatting_1.convertCustomFormatToFormatExpression)({
1554
1558
  type: field_1.CustomFormatType.BYTES_IEC,
1555
1559
  compact: field_1.Compact.MEBIBYTES,
1556
1560
  });
1557
- expect(mebibyteExpression).toEqual('#,##0.00"MiB"');
1558
- expect((0, formatting_1.formatValueWithExpression)(mebibyteExpression, 2097152)).toEqual('2.00MiB');
1561
+ expect(mebibyteExpression).toEqual('#,##0.###"MiB"');
1562
+ expect((0, formatting_1.formatValueWithExpression)(mebibyteExpression, 2097152)).toEqual('2MiB');
1559
1563
  // Test with rounding
1560
1564
  const roundedExpression = (0, formatting_1.convertCustomFormatToFormatExpression)({
1561
1565
  type: field_1.CustomFormatType.BYTES_IEC,
@@ -1574,7 +1578,7 @@ describe('Formatting', () => {
1574
1578
  };
1575
1579
  // Backend calls convertCustomFormatToFormatExpression
1576
1580
  const formatExpression = (0, formatting_1.convertCustomFormatToFormatExpression)(formatOptions);
1577
- expect(formatExpression).toEqual('#,##0.00"KiB"');
1581
+ expect(formatExpression).toEqual('#,##0.###"KiB"');
1578
1582
  // Mock metric with format expression set by backend
1579
1583
  const mockMetric = {
1580
1584
  fieldType: field_1.FieldType.METRIC,
@@ -1590,10 +1594,10 @@ describe('Formatting', () => {
1590
1594
  };
1591
1595
  // Frontend uses formatItemValue which should call formatValueWithExpression
1592
1596
  const result = (0, formatting_1.formatItemValue)(mockMetric, 2048);
1593
- expect(result).toEqual('2.00KiB'); // Should be correctly divided by 1024
1597
+ expect(result).toEqual('2KiB'); // Should be correctly divided by 1024
1594
1598
  // Test larger values
1595
- expect((0, formatting_1.formatItemValue)(mockMetric, 1024)).toEqual('1.00KiB');
1596
- expect((0, formatting_1.formatItemValue)(mockMetric, 3072)).toEqual('3.00KiB');
1599
+ expect((0, formatting_1.formatItemValue)(mockMetric, 1024)).toEqual('1KiB');
1600
+ expect((0, formatting_1.formatItemValue)(mockMetric, 3072)).toEqual('3KiB');
1597
1601
  });
1598
1602
  test('formatValueWithExpression ignores the incidental runtime timezone for date expressions (#19759)', () => {
1599
1603
  const previousTz = process.env.TZ;
@@ -2325,4 +2329,291 @@ describe('isUnambiguousTemporalString', () => {
2325
2329
  expect((0, formatting_1.isUnambiguousTemporalString)(value)).toBe(false);
2326
2330
  });
2327
2331
  });
2332
+ // Converter gap fixes (docs/composer-viz-plan/01-design.md §2): the format
2333
+ // expression produced for a CustomFormat must render identically to the
2334
+ // structured applyCustomFormat path, or return null when no ECMA-376 form
2335
+ // exists (the formatOptions escape hatch).
2336
+ describe('convertCustomFormatToFormatExpression gap fixes', () => {
2337
+ const gridValues = [-9876543.21, 0, 0.1234, 1234567, 12345.1235];
2338
+ const gridRounds = [undefined, 0, 2, -2];
2339
+ const gridSeparators = [undefined, ...Object.values(field_1.NumberSeparator)];
2340
+ const gridTypes = [
2341
+ { type: field_1.CustomFormatType.DEFAULT },
2342
+ { type: field_1.CustomFormatType.ID },
2343
+ { type: field_1.CustomFormatType.PERCENT },
2344
+ { type: field_1.CustomFormatType.NUMBER },
2345
+ { type: field_1.CustomFormatType.CURRENCY, currency: field_1.Format.USD },
2346
+ { type: field_1.CustomFormatType.BYTES_SI },
2347
+ { type: field_1.CustomFormatType.BYTES_IEC },
2348
+ ];
2349
+ const magnitudeRoundTypes = [
2350
+ field_1.CustomFormatType.PERCENT,
2351
+ field_1.CustomFormatType.NUMBER,
2352
+ field_1.CustomFormatType.CURRENCY,
2353
+ field_1.CustomFormatType.BYTES_SI,
2354
+ field_1.CustomFormatType.BYTES_IEC,
2355
+ ];
2356
+ // Documented G10 divergences we do not attempt to close:
2357
+ // - DEFAULT ignores the separator in the structured path (host locale),
2358
+ // while the expression render localises it.
2359
+ // - PERIOD_COMMA currency moves the symbol after the number in de-DE.
2360
+ const isDocumentedDivergence = (type, separator) => (type === field_1.CustomFormatType.DEFAULT &&
2361
+ separator !== undefined &&
2362
+ separator !== field_1.NumberSeparator.DEFAULT &&
2363
+ separator !== field_1.NumberSeparator.COMMA_PERIOD) ||
2364
+ (type === field_1.CustomFormatType.CURRENCY &&
2365
+ separator === field_1.NumberSeparator.PERIOD_COMMA);
2366
+ test('round-trip grid: expression render matches applyCustomFormat', () => {
2367
+ gridTypes.forEach((baseFormat) => {
2368
+ gridRounds.forEach((round) => {
2369
+ gridSeparators.forEach((separator) => {
2370
+ const customFormat = {
2371
+ ...baseFormat,
2372
+ round,
2373
+ separator,
2374
+ };
2375
+ const expression = (0, formatting_1.convertCustomFormatToFormatExpression)(customFormat);
2376
+ if (expression === null) {
2377
+ // The only legitimate null in this grid is magnitude
2378
+ // rounding (negative round), which has no ECMA-376 form.
2379
+ expect({ customFormat, expression }).toEqual({
2380
+ customFormat,
2381
+ expression: round !== undefined &&
2382
+ round < 0 &&
2383
+ magnitudeRoundTypes.includes(baseFormat.type)
2384
+ ? null
2385
+ : expect.anything(),
2386
+ });
2387
+ return;
2388
+ }
2389
+ if (isDocumentedDivergence(baseFormat.type, separator)) {
2390
+ return;
2391
+ }
2392
+ gridValues.forEach((value) => {
2393
+ const structured = (0, formatting_1.applyCustomFormat)(value, customFormat);
2394
+ const viaExpression = (0, formatting_1.formatValueWithExpression)(expression, value, (0, formatting_1.separatorToNumfmtLocale)(separator));
2395
+ expect({
2396
+ customFormat,
2397
+ value,
2398
+ formatted: viaExpression,
2399
+ }).toEqual({
2400
+ customFormat,
2401
+ value,
2402
+ formatted: structured,
2403
+ });
2404
+ });
2405
+ });
2406
+ });
2407
+ });
2408
+ });
2409
+ test('magnitude rounding (negative round) returns null — formatOptions escape hatch', () => {
2410
+ magnitudeRoundTypes.forEach((type) => {
2411
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2412
+ type,
2413
+ round: -2,
2414
+ ...(type === field_1.CustomFormatType.CURRENCY && {
2415
+ currency: field_1.Format.USD,
2416
+ }),
2417
+ })).toBeNull();
2418
+ });
2419
+ });
2420
+ test('dynamic AUTO compact returns null for bytes too', () => {
2421
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2422
+ type: field_1.CustomFormatType.BYTES_SI,
2423
+ compact: field_1.Compact.AUTO,
2424
+ })).toBeNull();
2425
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2426
+ type: field_1.CustomFormatType.BYTES_IEC,
2427
+ compact: field_1.Compact.AUTO,
2428
+ })).toBeNull();
2429
+ });
2430
+ test('DEFAULT emits the applyDefaultFormat equivalent', () => {
2431
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2432
+ type: field_1.CustomFormatType.DEFAULT,
2433
+ })).toBe('#,##0.###');
2434
+ // round and separator are ignored by applyDefaultFormat, so the
2435
+ // expression must not encode them either.
2436
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2437
+ type: field_1.CustomFormatType.DEFAULT,
2438
+ round: 2,
2439
+ separator: field_1.NumberSeparator.NO_SEPARATOR_PERIOD,
2440
+ })).toBe('#,##0.###');
2441
+ });
2442
+ test('ID emits the verbatim text format', () => {
2443
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2444
+ type: field_1.CustomFormatType.ID,
2445
+ })).toBe('@');
2446
+ // Large numeric IDs must render verbatim, not in scientific notation.
2447
+ expect((0, formatting_1.formatValueWithExpression)('@', 1234567890123456)).toBe('1234567890123456');
2448
+ expect((0, formatting_1.formatValueWithExpression)('@', 'ID-123')).toBe('ID-123');
2449
+ expect((0, formatting_1.formatValueWithExpression)('@', 12345)).toBe((0, formatting_1.applyCustomFormat)(12345, { type: field_1.CustomFormatType.ID }));
2450
+ });
2451
+ test('DATE emits per-grain expressions; QUARTER stays renderer-side', () => {
2452
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2453
+ type: field_1.CustomFormatType.DATE,
2454
+ timeInterval: timeFrames_1.TimeFrames.YEAR,
2455
+ })).toBe('yyyy');
2456
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2457
+ type: field_1.CustomFormatType.DATE,
2458
+ timeInterval: timeFrames_1.TimeFrames.MONTH,
2459
+ })).toBe('yyyy-mm');
2460
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2461
+ type: field_1.CustomFormatType.DATE,
2462
+ timeInterval: timeFrames_1.TimeFrames.DAY,
2463
+ })).toBe('yyyy-mm-dd');
2464
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2465
+ type: field_1.CustomFormatType.DATE,
2466
+ timeInterval: timeFrames_1.TimeFrames.WEEK,
2467
+ })).toBe('yyyy-mm-dd');
2468
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2469
+ type: field_1.CustomFormatType.DATE,
2470
+ })).toBe('yyyy-mm-dd');
2471
+ // No ECMA-376 quarter token; renderer resolves it via timeInterval.
2472
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2473
+ type: field_1.CustomFormatType.DATE,
2474
+ timeInterval: timeFrames_1.TimeFrames.QUARTER,
2475
+ })).toBeNull();
2476
+ });
2477
+ test('DATE expressions render like the structured path', () => {
2478
+ const date = new Date(Date.UTC(2024, 0, 2, 3, 4, 5));
2479
+ [
2480
+ [timeFrames_1.TimeFrames.YEAR, '2024'],
2481
+ [timeFrames_1.TimeFrames.MONTH, '2024-01'],
2482
+ [timeFrames_1.TimeFrames.DAY, '2024-01-02'],
2483
+ ].forEach(([timeInterval, expected]) => {
2484
+ const customFormat = {
2485
+ type: field_1.CustomFormatType.DATE,
2486
+ timeInterval,
2487
+ };
2488
+ const expression = (0, formatting_1.convertCustomFormatToFormatExpression)(customFormat);
2489
+ expect((0, formatting_1.formatValueWithExpression)(expression, date, undefined, 'UTC')).toBe(expected);
2490
+ expect((0, formatting_1.applyCustomFormat)(date, customFormat, 'UTC')).toBe(expected);
2491
+ });
2492
+ });
2493
+ test('TIMESTAMP emits sub-day expressions to the second; the (Z) offset suffix stays renderer-side', () => {
2494
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2495
+ type: field_1.CustomFormatType.TIMESTAMP,
2496
+ timeInterval: timeFrames_1.TimeFrames.HOUR,
2497
+ })).toBe('yyyy-mm-dd, hh');
2498
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2499
+ type: field_1.CustomFormatType.TIMESTAMP,
2500
+ timeInterval: timeFrames_1.TimeFrames.MINUTE,
2501
+ })).toBe('yyyy-mm-dd, hh:mm');
2502
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2503
+ type: field_1.CustomFormatType.TIMESTAMP,
2504
+ timeInterval: timeFrames_1.TimeFrames.SECOND,
2505
+ })).toBe('yyyy-mm-dd, hh:mm:ss');
2506
+ // Milliseconds (the default grain) render as `:SSS (Z)` in the
2507
+ // structured path — no ECMA-376 form, stays renderer-side.
2508
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2509
+ type: field_1.CustomFormatType.TIMESTAMP,
2510
+ })).toBeNull();
2511
+ expect((0, formatting_1.convertCustomFormatToFormatExpression)({
2512
+ type: field_1.CustomFormatType.TIMESTAMP,
2513
+ timeInterval: timeFrames_1.TimeFrames.MILLISECOND,
2514
+ })).toBeNull();
2515
+ const date = new Date(Date.UTC(2024, 0, 2, 3, 4, 5));
2516
+ // The structured equivalent is '2024-01-02, 03:04:05 (+00:00)'; the
2517
+ // expression carries everything except the offset suffix.
2518
+ expect((0, formatting_1.formatValueWithExpression)('yyyy-mm-dd, hh:mm:ss', date, undefined, 'UTC')).toBe('2024-01-02, 03:04:05');
2519
+ });
2520
+ test('quotes in prefix/suffix are escaped', () => {
2521
+ const customFormat = {
2522
+ type: field_1.CustomFormatType.NUMBER,
2523
+ prefix: 'ab"cd',
2524
+ suffix: ' un"it',
2525
+ round: 0,
2526
+ };
2527
+ const expression = (0, formatting_1.convertCustomFormatToFormatExpression)(customFormat);
2528
+ expect(expression).toBe('"ab"\\""cd"#,##0" un"\\""it"');
2529
+ expect((0, formatting_1.formatValueWithExpression)(expression, 5)).toBe((0, formatting_1.applyCustomFormat)(5, customFormat));
2530
+ expect((0, formatting_1.formatValueWithExpression)(expression, 5)).toBe('ab"cd5 un"it');
2531
+ });
2532
+ test('all-optional decimals never leave a dangling separator', () => {
2533
+ expect((0, formatting_1.formatValueWithExpression)('#,##0.###', 1234567)).toBe('1,234,567');
2534
+ expect((0, formatting_1.formatValueWithExpression)('#,##0.###', 0)).toBe('0');
2535
+ expect((0, formatting_1.formatValueWithExpression)('#,##0.###,"K"', 1000000)).toBe('1,000K');
2536
+ expect((0, formatting_1.formatValueWithExpression)('#,##0.###', 1234567, (0, formatting_1.separatorToNumfmtLocale)(field_1.NumberSeparator.PERIOD_COMMA))).toBe('1.234.567');
2537
+ // Fixed decimals are untouched.
2538
+ expect((0, formatting_1.formatValueWithExpression)('0.00', 5)).toBe('5.00');
2539
+ });
2540
+ });
2541
+ describe('getFormatExpression year-number guard', () => {
2542
+ const yearNumDimension = {
2543
+ ...formatting_mock_1.dimension,
2544
+ type: field_1.DimensionType.NUMBER,
2545
+ timeInterval: timeFrames_1.TimeFrames.YEAR_NUM,
2546
+ };
2547
+ test('emits no expression for YEAR_NUM dimensions', () => {
2548
+ // formatItemValue renders year numbers as plain unseparated values,
2549
+ // short-circuiting before applyCustomFormat — the expression must not
2550
+ // reintroduce digit grouping (2021, never 2,021).
2551
+ expect((0, formatting_1.getFormatExpression)(yearNumDimension)).toBeUndefined();
2552
+ expect((0, formatting_1.getFormatExpression)({ ...yearNumDimension, round: 0 })).toBeUndefined();
2553
+ expect((0, formatting_1.formatItemValue)(yearNumDimension, 2021)).toBe('2021');
2554
+ });
2555
+ });
2556
+ // Blast-radius pins (docs/composer-viz-plan/01-design.md §2): existing
2557
+ // consumers of the converter whose output changes with the gap fixes.
2558
+ describe('converter gap fixes blast radius', () => {
2559
+ describe('getFieldFormatOverrideProps', () => {
2560
+ test('DEFAULT/ID/DATE overrides now encode as expressions', () => {
2561
+ expect((0, formatting_1.getFieldFormatOverrideProps)({
2562
+ type: field_1.CustomFormatType.DEFAULT,
2563
+ })).toEqual({ format: '#,##0.###', separator: undefined });
2564
+ expect((0, formatting_1.getFieldFormatOverrideProps)({ type: field_1.CustomFormatType.ID })).toEqual({ format: '@', separator: undefined });
2565
+ expect((0, formatting_1.getFieldFormatOverrideProps)({
2566
+ type: field_1.CustomFormatType.DATE,
2567
+ timeInterval: timeFrames_1.TimeFrames.MONTH,
2568
+ })).toEqual({ format: 'yyyy-mm', separator: undefined });
2569
+ });
2570
+ test('non-expressible overrides keep structured formatOptions', () => {
2571
+ const quarterDate = {
2572
+ type: field_1.CustomFormatType.DATE,
2573
+ timeInterval: timeFrames_1.TimeFrames.QUARTER,
2574
+ };
2575
+ expect((0, formatting_1.getFieldFormatOverrideProps)(quarterDate)).toEqual({
2576
+ format: undefined,
2577
+ formatOptions: quarterDate,
2578
+ separator: undefined,
2579
+ });
2580
+ const millisecondTimestamp = {
2581
+ type: field_1.CustomFormatType.TIMESTAMP,
2582
+ };
2583
+ expect((0, formatting_1.getFieldFormatOverrideProps)(millisecondTimestamp)).toEqual({
2584
+ format: undefined,
2585
+ formatOptions: millisecondTimestamp,
2586
+ separator: undefined,
2587
+ });
2588
+ const magnitudeRound = {
2589
+ type: field_1.CustomFormatType.NUMBER,
2590
+ round: -2,
2591
+ };
2592
+ expect((0, formatting_1.getFieldFormatOverrideProps)(magnitudeRound)).toEqual({
2593
+ format: undefined,
2594
+ formatOptions: magnitudeRound,
2595
+ separator: undefined,
2596
+ });
2597
+ });
2598
+ });
2599
+ describe('convertCustomMetricToDbt writeback output', () => {
2600
+ test('unformatted numeric metrics keep the pre-existing default expression', () => {
2601
+ expect((0, convertCustomMetricsToYaml_1.convertCustomMetricToDbt)(formatting_mock_1.additionalMetric).format).toBe('#,##0.###');
2602
+ });
2603
+ test('legacy percent metrics emit trailing-dropped decimals (G4 alignment)', () => {
2604
+ // Was '#,##0.00%' before the round-default fix.
2605
+ expect((0, convertCustomMetricsToYaml_1.convertCustomMetricToDbt)({
2606
+ ...formatting_mock_1.additionalMetric,
2607
+ format: field_1.Format.PERCENT,
2608
+ }).format).toBe('#,##0.###%');
2609
+ });
2610
+ test('legacy id metrics now emit the text format', () => {
2611
+ // Was undefined before the ID fix.
2612
+ expect((0, convertCustomMetricsToYaml_1.convertCustomMetricToDbt)({
2613
+ ...formatting_mock_1.additionalMetric,
2614
+ format: field_1.Format.ID,
2615
+ }).format).toBe('@');
2616
+ });
2617
+ });
2618
+ });
2328
2619
  //# sourceMappingURL=formatting.test.js.map