🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

predict-data-types

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

predict-data-types

A simple npm package that predicts data types for comma-separated values, including JSON objects, and validates URLs, phone numbers, email addresses, IP addresses, colors, percentages, and currency within string values.

Source
npmnpm
Version
1.3.0
Version published
Weekly downloads
20
150%
Maintainers
1
Weekly downloads
 
Created
Source

Predict Data Types

npm version License: MIT

A lightweight and robust npm package that automatically predicts data types for comma-separated values, including JSON objects, and validates URLs, phone numbers, email addresses, UUIDs, dates, and more within string values.

✨ Features

  • 🎯 Automatic Type Detection: Intelligently identifies 14 data types
  • 🔒 Input Validation: Robust error handling and input validation
  • 📊 CSV Support: Parse CSV-like data with optional headers
  • 🚀 Lightweight: Minimal dependencies (only dayjs)
  • 📝 Well Tested: Comprehensive test suite with edge cases
  • 🔧 TypeScript Ready: Type definitions included
  • Fast: Optimized tokenization and regex patterns

📦 Installation

npm install predict-data-types

🔧 Supported Data Types

TypeDescriptionExamples
stringPlain text values'John', 'Hello World'
numberIntegers and decimals42, 3.14, -17, 1e10
booleanBoolean representationstrue, false, yes, no
emailValid email addressesuser@example.com, test+tag@domain.co.uk
phonePhone numbers555-555-5555, (555) 555-5555, +1 555-555-5555
urlWeb URLshttps://example.com, http://subdomain.site.co.uk/path
uuidUUID v1-v5550e8400-e29b-41d4-a716-446655440000
dateVarious date formats2023-12-31, 31/12/2023, 2023-12-31T23:59:59Z
ipIPv4 and IPv6 addresses192.168.1.1, 2001:0db8::1
colorHex color codes#FF0000, #fff, #00ff00
percentagePercentage values50%, 0.5%, -25%
currencyCurrency amounts$100, €50.99, £25, ¥1000
arrayJSON arrays[1, 2, 3], ["apple", "banana"]
objectJSON objects{"name": "John", "age": 30}

🚀 Usage

Basic Usage

const predictDataTypes = require("predict-data-types");

const text = "John, 30, true, john@example.com, 2023-01-01";
const types = predictDataTypes(text);

console.log(types);
// Output:
// {
//   'John': 'string',
//   '30': 'number',
//   'true': 'boolean',
//   'john@example.com': 'email',
//   '2023-01-01': 'date'
// }

Advanced Examples

CSV-like Data with Headers

const csvData = `name,age,active,email,signup_date
John,30,true,john@example.com,2023-01-01
Jane,25,false,jane@example.com,2023-02-15`;

const types = predictDataTypes(csvData, true); // true = first row is header
console.log(types);
// Output:
// {
//   'name': 'string',
//   'age': 'number',
//   'active': 'boolean',
//   'email': 'email',
//   'signup_date': 'date'
// }

Mixed Complex Data

const complexData = `
  user@test.com,
  555-123-4567,
  https://github.com/user/repo,
  550e8400-e29b-41d4-a716-446655440000,
  {"settings": {"theme": "dark"}},
  [1, 2, 3, 4, 5]
`;

const types = predictDataTypes(complexData);
console.log(types);
// Output:
// {
//   'user@test.com': 'email',
//   '555-123-4567': 'phone',
//   'https://github.com/user/repo': 'url',
//   '550e8400-e29b-41d4-a716-446655440000': 'uuid',
//   '{"settings": {"theme": "dark"}}': 'object',
//   '[1, 2, 3, 4, 5]': 'array'
// }

Date Format Detection

const dates = "2023-12-31, 31/12/2023, 2023-12-31T23:59:59Z, Dec-31-2023";
const types = predictDataTypes(dates);

console.log(types);
// Output:
// {
//   '2023-12-31': 'date',
//   '31/12/2023': 'date',
//   '2023-12-31T23:59:59Z': 'date',
//   'Dec-31-2023': 'date'
// }

📚 API Reference

predictDataTypes(input, firstRowIsHeader)

Parameters:

  • input (string): The comma-separated string to analyze
  • firstRowIsHeader (boolean, optional): Whether to treat the first row as column headers (default: false)

Returns:

  • Object<string, string>: Mapping of field names/values to their predicted data types

Throws:

  • Error: When input is null, undefined, or not a string

Supported Date Formats:

  • ISO 8601: 2023-12-31T23:59:59Z
  • Standard: YYYY-MM-DD, DD/MM/YYYY, MM/DD/YYYY
  • With time: YYYY-MM-DD HH:mm:ss
  • Month names: DD-MMM-YYYY, MMM-DD-YYYY

⚠️ Error Handling

The package includes robust error handling:

// These will throw errors
try {
  predictDataTypes(null); // Error: Input cannot be null or undefined
  predictDataTypes(123); // Error: Input must be a string
  predictDataTypes([1, 2, 3]); // Error: Input must be a string
} catch (error) {
  console.error(error.message);
}

// These will return empty object or appropriate results
predictDataTypes(""); // Returns: {}
predictDataTypes("   "); // Returns: { '': 'string' }

🧪 Development

Running Tests

npm test        # Run all tests
npm run lint    # Check code quality
npm run lint:fix # Auto-fix lint issues

Test Coverage

The package includes comprehensive tests covering:

  • ✅ All supported data types
  • ✅ Edge cases and error conditions
  • ✅ Input validation
  • ✅ Complex nested structures
  • ✅ Various date formats
  • ✅ Header mode functionality

📝 Changelog

v1.1.0

  • ✅ Fixed UUID pattern variable name bug
  • ✅ Replaced deprecated moment.js with dayjs
  • ✅ Added comprehensive input validation
  • ✅ Fixed security vulnerabilities
  • ✅ Added ESLint configuration
  • ✅ Enhanced test coverage
  • ✅ Added JSDoc documentation
  • ✅ Improved README documentation

🤝 Contributing

Contributions are welcome! Please read our Contributing Guidelines for details on our code of conduct and development process.

  • Fork the repository
  • Create your feature branch (git checkout -b feature/amazing-feature)
  • Run tests (npm test)
  • Commit your changes (git commit -m 'Add amazing feature')
  • Push to the branch (git push origin feature/amazing-feature)
  • Open a Pull Request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🆘 Support

Made with ❤️ by Melih Birim

Keywords

predict

FAQs

Package last updated on 14 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts