or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-4/

Product Data Stream Processor

Build a JSON stream processor that extracts and processes product information from a large streaming JSON dataset. The system should handle product data as it arrives without waiting for the complete dataset.

Requirements

Input Format

The streaming JSON data has the following structure:

{
  "metadata": {
    "source": "inventory-system",
    "timestamp": "2025-01-15T10:00:00Z"
  },
  "products": [
    {
      "id": "P001",
      "name": "Laptop",
      "price": 999.99,
      "category": "Electronics"
    },
    {
      "id": "P002",
      "name": "Mouse",
      "price": 29.99,
      "category": "Electronics"
    }
  ]
}

Core Functionality

Implement a module that:

  1. Processes streaming product data from a URL or file stream
  2. Extracts specific data elements using pattern matching:
    • Retrieves the entire root object
    • Accesses the metadata source field
    • Processes each individual product in the array
    • Extracts product names
    • Gets the price of the first product
  3. Stores collected data in appropriate data structures for later retrieval
  4. Provides access methods to retrieve the collected information

The solution should process data progressively as it streams in, not after the entire response arrives.

API Design

Your module should export functions or provide methods to:

  • Initialize streaming from a URL or file path
  • Register handlers for different data patterns
  • Retrieve collected results after processing completes

Error Handling

The processor should gracefully handle:

  • Network errors
  • Malformed JSON data
  • Missing or unexpected fields

Test Cases { .test-cases }

Test 1: Extract Root Object { .test-case }

@test

Input: JSON stream with root object containing metadata and products array

Expected Output: Complete root object is captured

Test 2: Extract Metadata Source { .test-case }

@test

Input: JSON stream with metadata.source = "inventory-system"

Expected Output: String value "inventory-system" is extracted

Test 3: Process All Products { .test-case }

@test

Input: JSON stream with 3 products in the products array

Expected Output: All 3 product objects are collected in an array

Test 4: Extract Product Names { .test-case }

@test

Input: JSON stream with products having names "Laptop", "Mouse", "Keyboard"

Expected Output: Array containing ["Laptop", "Mouse", "Keyboard"]

Test 5: Get First Product Price { .test-case }

@test

Input: JSON stream where first product has price 999.99

Expected Output: Number value 999.99

Implementation Files { .implementation-files }

processor.js { .implementation-file }

Main module implementing the stream processor.

processor.test.js { .implementation-file }

Test suite validating all functionality.

Dependencies { .dependencies }

oboe { .dependency }

Provides streaming JSON parsing and pattern matching capabilities.