npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sumit-paul/universal-method

v1.0.3

Published

A package that converts natural language queries into typed responses using AI

Readme

Universal Method

A powerful package that converts natural language queries into typed responses using AI. Supports both OpenAI and Google's Gemini models.

Features

  • Convert natural language queries to typed responses
  • Support for multiple AI models (OpenAI and Gemini)
  • Type-safe responses (String, Number, Boolean, Object, Array, or custom types)
  • Automatic type conversion and validation
  • Configurable model options and temperature
  • Error handling with appropriate default values
  • Automatic retries with error feedback (up to 2 retries)
  • No manual logic needed - just ask in natural language!

Installation

npm install @sumit-paul/universal-method

Quick Start

import { universalMethod } from '@sumit-paul/universal-method';

// Get a typed response for a data transformation
const result = await universalMethod(
  'Convert this date string "2024-03-19" to a more readable format',
  String
);

// Parse and validate JSON data
const data = await universalMethod(
  'Parse this JSON string: {"name": "John", "age": 30}',
  Object
);

// Extract specific information
const numbers = await universalMethod(
  'Extract all numbers from this text: "The price is $99.99 and quantity is 5"',
  Array
);

// Validate boolean conditions
const isValid = await universalMethod(
  'Check if "[email protected]" is a valid email address',
  Boolean
);

// Custom options
const custom = await universalMethod('Complex query', String, {
  model: 'gpt-4',
  temperature: 0.7
});

Error Handling and Retries

Universal Method includes automatic retry functionality when type conversion fails:

// If the AI response can't be converted to the expected type,
// the system will automatically retry up to 2 times with error feedback
const result = await universalMethod(
  'Give me a number between 1 and 10',
  Number
);

// If all retries fail, it returns the appropriate default value:
// - 0 for Number
// - false for Boolean
// - [] for Array
// - {} for Object
// - '' for String
// - null for custom types

The retry system:

  1. Logs the conversion error to stderr
  2. Creates a new prompt that includes:
    • The original query
    • The error message
    • The expected return type
  3. Makes up to 2 retry attempts
  4. Returns the default value if all retries fail

Examples

1. Data Parsing and Validation

// Parse complex JSON structures
const parsedData = await universalMethod(
  'Parse this nested JSON: {"user": {"name": "John", "settings": {"theme": "dark"}}}',
  Object
);

// Validate data format
const isValidFormat = await universalMethod(
  'Check if "2024-03-19T10:00:00Z" is a valid ISO date string',
  Boolean
);

2. Text Processing

// Extract structured data from text
const extractedData = await universalMethod(
  'Extract name, email, and phone from: "Contact John at [email protected] or 123-456-7890"',
  Object
);

// Format text according to rules
const formattedText = await universalMethod(
  'Format this text as a proper title: "the quick brown fox jumps over the lazy dog"',
  String
);

3. Data Transformation

// Convert between data formats
const convertedData = await universalMethod(
  'Convert this CSV row to JSON: "name,age,city\nJohn,30,New York"',
  Object
);

// Transform data structure
const transformedData = await universalMethod(
  'Transform this array of objects to group by category: [{"name": "Item1", "category": "A"}, {"name": "Item2", "category": "A"}]',
  Object
);

4. Data Validation

// Validate data against rules
const validationResult = await universalMethod(
  'Check if this password meets security requirements: "Password123!"',
  Object
);

// Validate business rules
const businessValidation = await universalMethod(
  'Check if order total $150 with 10% discount is valid for free shipping (threshold: $100)',
  Boolean
);

5. Data Extraction

// Extract specific information
const extractedInfo = await universalMethod(
  'Extract all URLs from this text: "Visit https://example.com or http://test.com"',
  Array
);

// Parse complex strings
const parsedInfo = await universalMethod(
  'Parse this log entry: "[2024-03-19 10:00:00] ERROR: Connection failed (code: 404)"',
  Object
);

Why Universal Method?

Universal Method eliminates the need for writing complex manual logic. Instead of:

  • Writing regex patterns for text manipulation
  • Implementing complex data parsing logic
  • Creating validation functions
  • Building data transformation utilities
  • Writing string manipulation code

Just ask in natural language and get typed responses!

Configuration

Set the following environment variables:

UNIVERSAL_METHOD_KEY=your_api_key_here
UNIVERSAL_METHOD_MODEL=openai  # or 'gemini'

API

universalMethod(query: string, returnType?: ReturnType, options?: UniversalMethodOptions): Promise<unknown>

  • query: The natural language query to process
  • returnType: The expected return type (String, Number, Boolean, Object, Array, or custom constructor)
  • options: Optional configuration
    • model: Override the default AI model
    • temperature: Control response randomness (0.0 to 1.0)

The function will automatically retry up to 2 times if type conversion fails, providing error feedback to the AI model.

Best Practices

  1. Type Safety: Always specify the return type for better type inference and validation
  2. Error Handling: Wrap calls in try-catch blocks for graceful error handling
  3. Temperature: Adjust temperature based on your needs:
    • Lower (0.1-0.3): More consistent, factual responses
    • Higher (0.7-0.9): More creative, varied responses
  4. Model Selection: Choose the appropriate model based on your use case:
    • GPT-4: Complex reasoning, detailed responses
    • GPT-3.5: General purpose, faster responses
    • Gemini: Alternative option with good performance

Development

# Install dependencies
npm install

# Build the package
npm run build

# Run tests
npm test

# Type checking
npm run typecheck

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT