CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/npm-lightdash--common

tessl install tessl/npm-lightdash--common@0.2231.5

Shared TypeScript library for the Lightdash platform containing common types, utilities, and business logic for analytics workflows

Agent Success

Agent success rate when using this tile

72%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.09x

Baseline

Agent success rate without this tile

66%

task.mdevals/scenario-5/

Sales Report Formatter

Overview

Build a sales report formatting utility that takes raw sales data and formats it for presentation in reports and dashboards. The utility should handle date formatting, number formatting with various styles, and apply appropriate formatting based on data types.

Requirements

Create a TypeScript module that exports a formatSalesReport function that:

  1. Accepts an array of raw sales records containing:

    • date: ISO date string (e.g., "2024-03-15T10:30:00Z")
    • revenue: number representing dollars
    • quantity: integer count
    • growth_rate: decimal number representing percentage (e.g., 0.15 for 15%)
    • region: string identifier
  2. Returns formatted sales records where:

    • Dates are formatted as human-readable strings (e.g., "March 15, 2024")
    • Revenue values are formatted as currency with appropriate separators (e.g., "$1,234.56")
    • Quantities are formatted as plain numbers with thousand separators (e.g., "1,234")
    • Growth rates are formatted as percentages (e.g., "15%")
    • Region names remain unchanged
  3. The formatting should handle edge cases:

    • Null or undefined values should display as "-"
    • Very large numbers should be readable with proper separators
    • Zero values should still be formatted appropriately

Implementation Details

  • Create a file src/formatSalesReport.ts with the main function
  • Define appropriate TypeScript types for input and output data
  • Use a consistent formatting approach across all data types
  • Export both the function and any relevant types

Dependencies { .dependencies }

@lightdash/common { .dependency }

Provides data formatting utilities for Business Intelligence operations.

Test Cases

Test Case 1: Format complete sales record @test

// File: src/formatSalesReport.test.ts
import { formatSalesReport } from './formatSalesReport';

test('formats complete sales record correctly', () => {
  const rawData = [
    {
      date: '2024-03-15T10:30:00Z',
      revenue: 12345.67,
      quantity: 1234,
      growth_rate: 0.157,
      region: 'US-WEST'
    }
  ];

  const formatted = formatSalesReport(rawData);

  expect(formatted[0].date).toMatch(/March.*15.*2024/);
  expect(formatted[0].revenue).toContain('$');
  expect(formatted[0].revenue).toContain('12,345');
  expect(formatted[0].quantity).toBe('1,234');
  expect(formatted[0].growth_rate).toContain('15');
  expect(formatted[0].growth_rate).toContain('%');
  expect(formatted[0].region).toBe('US-WEST');
});

Test Case 2: Handle null values @test

// File: src/formatSalesReport.test.ts
import { formatSalesReport } from './formatSalesReport';

test('handles null and undefined values', () => {
  const rawData = [
    {
      date: '2024-03-15T10:30:00Z',
      revenue: null,
      quantity: undefined,
      growth_rate: 0.0,
      region: 'EU-CENTRAL'
    }
  ];

  const formatted = formatSalesReport(rawData);

  expect(formatted[0].revenue).toBe('-');
  expect(formatted[0].quantity).toBe('-');
  expect(formatted[0].growth_rate).toContain('0');
});

Test Case 3: Format large numbers @test

// File: src/formatSalesReport.test.ts
import { formatSalesReport } from './formatSalesReport';

test('formats large numbers with separators', () => {
  const rawData = [
    {
      date: '2024-01-01T00:00:00Z',
      revenue: 1000000.99,
      quantity: 50000,
      growth_rate: 0.333,
      region: 'ASIA-PAC'
    }
  ];

  const formatted = formatSalesReport(rawData);

  expect(formatted[0].revenue).toContain('1,000,000');
  expect(formatted[0].quantity).toBe('50,000');
});

Deliverables

  1. src/formatSalesReport.ts - Main implementation file
  2. src/formatSalesReport.test.ts - Test file with all test cases passing

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@lightdash/common@0.2231.x
tile.json