CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-safe-stable-stringify

Deterministic and safely JSON.stringify to quickly serialize JavaScript objects

90

1.08x

Evaluation90%

1.08x

Agent success when using this tile

Overview
Eval results
Files

task.mdevals/scenario-5/

Nested Object Logger

Build a utility that serializes deeply nested configuration objects for logging purposes, with the ability to control how deep the serialization goes to prevent overwhelming log output.

Requirements

Your task is to implement a configuration logger that can handle complex, deeply nested configuration objects. The logger should be able to limit the depth of serialization to make log output more readable while still providing useful information about the structure.

Core Functionality

Implement a function serializeConfig(config, options) that:

  1. Accepts a configuration object (which may be deeply nested)
  2. Accepts an options object with a maxDepth property
  3. Returns a JSON string representation of the config
  4. When objects or arrays exceed the specified depth, they should be replaced with placeholder strings indicating their type
  5. The output should have deterministic (alphabetically sorted) keys for consistency

Specific Behavior

  • Objects beyond the maximum depth should appear as "[Object]" in the output
  • Arrays beyond the maximum depth should appear as "[Array]" in the output
  • The serialization should handle circular references gracefully
  • The output should be deterministic (same input produces same output)

Example

const config = {
  server: {
    port: 3000,
    host: {
      primary: "localhost",
      fallback: {
        local: "127.0.0.1",
        remote: "0.0.0.0"
      }
    }
  },
  database: {
    connections: [
      { name: "primary", pool: { min: 2, max: 10 } },
      { name: "replica", pool: { min: 1, max: 5 } }
    ]
  }
};

serializeConfig(config, { maxDepth: 3 });
// Should limit nesting to 3 levels, showing "[Object]" and "[Array]" for deeper structures

Test Cases

  • When maxDepth is 1, only top-level properties are expanded, all nested objects/arrays become placeholders @test
  • When maxDepth is 2, two levels of nesting are shown, deeper structures become placeholders @test
  • When maxDepth is 3, three levels of nesting are shown @test
  • The function produces deterministic output with keys sorted alphabetically @test

Implementation

@generates

API

/**
 * Serializes a configuration object to a JSON string with depth limiting.
 *
 * @param {Object} config - The configuration object to serialize
 * @param {Object} options - Serialization options
 * @param {number} options.maxDepth - Maximum depth of nesting to serialize
 * @returns {string} JSON string representation with depth limiting applied
 */
function serializeConfig(config, options) {
  // Implementation here
}

module.exports = { serializeConfig };

Dependencies { .dependencies }

safe-stable-stringify { .dependency }

Provides safe, deterministic JSON stringification with depth limiting support.

Install with Tessl CLI

npx tessl i tessl/npm-safe-stable-stringify

tile.json