
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
predict-data-types
Advanced tools
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.
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.
npm install predict-data-types
| Type | Description | Examples |
|---|---|---|
string | Plain text values | 'John', 'Hello World' |
number | Integers and decimals | 42, 3.14, -17, 1e10 |
boolean | Boolean representations | true, false, yes, no |
email | Valid email addresses | user@example.com, test+tag@domain.co.uk |
phone | Phone numbers | 555-555-5555, (555) 555-5555, +1 555-555-5555 |
url | Web URLs | https://example.com, http://subdomain.site.co.uk/path |
uuid | UUID v1-v5 | 550e8400-e29b-41d4-a716-446655440000 |
date | Various date formats | 2023-12-31, 31/12/2023, 2023-12-31T23:59:59Z |
ip | IPv4 and IPv6 addresses | 192.168.1.1, 2001:0db8::1 |
color | Hex color codes | #FF0000, #fff, #00ff00 |
percentage | Percentage values | 50%, 0.5%, -25% |
currency | Currency amounts | $100, €50.99, £25, ¥1000 |
array | JSON arrays | [1, 2, 3], ["apple", "banana"] |
object | JSON objects | {"name": "John", "age": 30} |
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'
// }
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'
// }
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'
// }
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'
// }
predictDataTypes(input, firstRowIsHeader)Parameters:
input (string): The comma-separated string to analyzefirstRowIsHeader (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 typesThrows:
Error: When input is null, undefined, or not a stringSupported Date Formats:
2023-12-31T23:59:59ZYYYY-MM-DD, DD/MM/YYYY, MM/DD/YYYYYYYY-MM-DD HH:mm:ssDD-MMM-YYYY, MMM-DD-YYYYThe 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' }
npm test # Run all tests
npm run lint # Check code quality
npm run lint:fix # Auto-fix lint issues
The package includes comprehensive tests covering:
Contributions are welcome! Please read our Contributing Guidelines for details on our code of conduct and development process.
git checkout -b feature/amazing-feature)npm test)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by Melih Birim
FAQs
A lightweight, zero-dependency 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.
The npm package predict-data-types receives a total of 20 weekly downloads. As such, predict-data-types popularity was classified as not popular.
We found that predict-data-types demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.