predict-data-types
Advanced tools
+1
-1
| { | ||
| "name": "predict-data-types", | ||
| "version": "1.6.0", | ||
| "version": "1.7.0", | ||
| "description": "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.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+234
-101
@@ -6,23 +6,37 @@ # Predict Data Types | ||
| ## The Problem | ||
| **When users upload CSV or JSON files, everything arrives as strings.** | ||
| This library infers the actual data types automatically. | ||
| TypeScript and JavaScript can't help you here: | ||
| ```typescript | ||
| // ❌ TypeScript only knows static types | ||
| const userInput = "test@example.com"; // TypeScript thinks: string | ||
| const csvValue = "2024-01-01"; // TypeScript thinks: string | ||
| const formData = "42"; // TypeScript thinks: string | ||
| // TypeScript CANNOT detect these are email, date, and number at runtime | ||
| ``` | ||
| **This library solves that problem with runtime type detection:** | ||
| ```javascript | ||
| const { infer } = require("predict-data-types"); | ||
| infer("42") | ||
| // → 'number' | ||
| infer("test@example.com"); // → 'email' ✅ | ||
| infer("2024-01-01"); // → 'date' ✅ | ||
| infer("42"); // → 'number' ✅ | ||
| infer(["true", "false", "true"]) | ||
| // → 'boolean' | ||
| infer(["true", "false", "true"]); | ||
| // → 'boolean' ✅ | ||
| infer({ name: "Alice", age: "25", email: "alice@example.com" }) | ||
| // → { name: 'string', age: 'number', email: 'email' } | ||
| infer({ name: "Alice", age: "25", email: "alice@example.com" }); | ||
| // → { name: 'string', age: 'number', email: 'email' } ✅ | ||
| infer([ | ||
| { name: "Alice", age: "25" }, | ||
| { name: "Bob", age: "30" } | ||
| ]) | ||
| // → { name: 'string', age: 'number' } | ||
| { name: "Bob", age: "30" }, | ||
| ]); | ||
| // → { name: 'string', age: 'number' } ✅ | ||
| ``` | ||
@@ -36,2 +50,4 @@ | ||
| > **💡 Important:** This library performs **runtime type detection** on string values, not static type checking. TypeScript is a compile-time type system for your code structure - this library analyzes actual data content at runtime. They solve completely different problems! | ||
| ## Features | ||
@@ -47,3 +63,3 @@ | ||
| - **45+ Date Formats**: Comprehensive date parsing including month names and timezones | ||
| - **Battle-Tested**: 60 comprehensive test cases | ||
| - **Battle-Tested**: 61 comprehensive test cases | ||
@@ -56,20 +72,59 @@ ## Installation | ||
| ## Quick Examples | ||
| Real-world use cases showing what you can build: | ||
| **📊 CSV Import Tool** | ||
| ```javascript | ||
| // Auto-detect column types and transform data | ||
| const employees = parseCSV(file); // All values are strings | ||
| const schema = infer(employees); | ||
| // → { name: 'string', email: 'email', salary: 'currency', hire_date: 'date' } | ||
| ``` | ||
| **🎨 Form Builder** | ||
| ```javascript | ||
| // Auto-generate form fields with correct input types | ||
| const userData = { email: 'alice@example.com', age: '25', website: 'https://alice.dev' }; | ||
| const types = infer(userData); | ||
| // → { email: 'email', age: 'number', website: 'url' } | ||
| // Generate: <input type="email">, <input type="number">, <input type="url"> | ||
| ``` | ||
| **🌐 API Analyzer** | ||
| ```javascript | ||
| // Generate JSON Schema and TypeScript interfaces from API responses | ||
| const response = await fetch('/api/users').then(r => r.json()); | ||
| const jsonSchema = infer(response, Formats.JSONSCHEMA); | ||
| // Use with Ajv, joi, or generate TypeScript types | ||
| ``` | ||
| **✅ Data Validator** | ||
| ```javascript | ||
| // Validate imported data quality | ||
| const expected = { email: DataTypes.EMAIL, age: DataTypes.NUMBER }; | ||
| const actual = infer(importedData); | ||
| // Detect mismatches, missing fields, wrong types | ||
| ``` | ||
| 👉 **See full runnable examples in [`examples/`](./examples) directory** | ||
| ## Supported Data Types | ||
| | Type | Examples | | ||
| |------|----------| | ||
| | `string` | `'John'`, `'Hello World'` | | ||
| | `number` | `42`, `3.14`, `-17`, `1e10` | | ||
| | `boolean` | `true`, `false`, `yes`, `no` | | ||
| | `email` | `user@example.com` | | ||
| | `phone` | `555-555-5555`, `(555) 555-5555` | | ||
| | `url` | `https://example.com` | | ||
| | `uuid` | `550e8400-e29b-41d4-a716-446655440000` | | ||
| | `date` | `2023-12-31`, `31/12/2023` | | ||
| | `ip` | `192.168.1.1`, `2001:0db8::1` | | ||
| | `color` | `#FF0000`, `#fff` | | ||
| | `percentage` | `50%`, `-25%` | | ||
| | `currency` | `$100`, `€50.99` | | ||
| | `array` | `[1, 2, 3]` | | ||
| | `object` | `{"name": "John"}` | | ||
| | Type | Examples | | ||
| | ------------ | -------------------------------------- | | ||
| | `string` | `'John'`, `'Hello World'` | | ||
| | `number` | `42`, `3.14`, `-17`, `1e10` | | ||
| | `boolean` | `true`, `false`, `yes`, `no` | | ||
| | `email` | `user@example.com` | | ||
| | `phone` | `555-555-5555`, `(555) 555-5555` | | ||
| | `url` | `https://example.com` | | ||
| | `uuid` | `550e8400-e29b-41d4-a716-446655440000` | | ||
| | `date` | `2023-12-31`, `31/12/2023` | | ||
| | `ip` | `192.168.1.1`, `2001:0db8::1` | | ||
| | `color` | `#FF0000`, `#fff` | | ||
| | `percentage` | `50%`, `-25%` | | ||
| | `currency` | `$100`, `€50.99` | | ||
| | `array` | `[1, 2, 3]` | | ||
| | `object` | `{"name": "John"}` | | ||
@@ -138,16 +193,16 @@ ## Usage | ||
| // Single value → DataType | ||
| infer("2024-01-01") // → 'date' | ||
| infer("test@example.com") // → 'email' | ||
| infer("42") // → 'number' | ||
| infer("2024-01-01"); // → 'date' | ||
| infer("test@example.com"); // → 'email' | ||
| infer("42"); // → 'number' | ||
| // Array of values → Common DataType | ||
| infer(["1", "2", "3"]) // → 'number' | ||
| infer(["true", "false", "yes"]) // → 'boolean' | ||
| infer(["1", "2", "3"]); // → 'number' | ||
| infer(["true", "false", "yes"]); // → 'boolean' | ||
| // Object → Schema | ||
| infer({ | ||
| name: "Alice", | ||
| age: "25", | ||
| active: "true" | ||
| }) | ||
| infer({ | ||
| name: "Alice", | ||
| age: "25", | ||
| active: "true", | ||
| }); | ||
| // → { name: 'string', age: 'number', active: 'boolean' } | ||
@@ -158,4 +213,4 @@ | ||
| { name: "Alice", age: "25", email: "alice@example.com" }, | ||
| { name: "Bob", age: "30", email: "bob@example.com" } | ||
| ]) | ||
| { name: "Bob", age: "30", email: "bob@example.com" }, | ||
| ]); | ||
| // → { name: 'string', age: 'number', email: 'email' } | ||
@@ -175,11 +230,11 @@ ``` | ||
| email: "alice@example.com", | ||
| website: "https://example.com" | ||
| website: "https://example.com", | ||
| }; | ||
| // Simple format (default) | ||
| infer(data) | ||
| infer(data); | ||
| // → { name: 'string', age: 'number', email: 'email', website: 'url' } | ||
| // JSON Schema format | ||
| infer(data, Formats.JSONSCHEMA) | ||
| infer(data, Formats.JSONSCHEMA); | ||
| // → { | ||
@@ -197,3 +252,3 @@ // type: 'object', | ||
| // Use with validation libraries | ||
| const Ajv = require('ajv'); | ||
| const Ajv = require("ajv"); | ||
| const ajv = new Ajv(); | ||
@@ -203,3 +258,8 @@ | ||
| const validate = ajv.compile(schema); | ||
| const valid = validate({ name: "Bob", age: 30, email: "bob@example.com", website: "https://bob.dev" }); | ||
| const valid = validate({ | ||
| name: "Bob", | ||
| age: 30, | ||
| email: "bob@example.com", | ||
| website: "https://bob.dev", | ||
| }); | ||
| ``` | ||
@@ -222,65 +282,113 @@ | ||
| ### Real-World Use Cases | ||
| ## 📚 Complete Examples | ||
| **Form Validation** | ||
| ```javascript | ||
| const { infer, DataTypes } = require("predict-data-types"); | ||
| The [`examples/`](./examples) directory contains full, runnable code for real-world scenarios: | ||
| const formData = { | ||
| email: "user@example.com", | ||
| age: "25", | ||
| website: "https://example.com" | ||
| }; | ||
| - **[CSV Import](./examples/csv-import)** - Parse CSV files, auto-detect types, transform data to proper JavaScript types | ||
| - **[Form Builder](./examples/form-builder)** - Dynamically generate HTML forms with correct input types and validation | ||
| - **[API Analyzer](./examples/api-analyzer)** - Generate JSON Schemas, TypeScript interfaces, and API documentation | ||
| - **[Data Validation](./examples/data-validation)** - Validate imported data quality and detect type mismatches | ||
| const schema = infer(formData); | ||
| // { email: 'email', age: 'number', website: 'url' } | ||
| Each example includes: | ||
| // Type-safe validation | ||
| if (schema.email !== DataTypes.EMAIL) { | ||
| throw new Error("Invalid email format"); | ||
| } | ||
| - ✅ Complete runnable code with detailed comments | ||
| - ✅ Real-world use cases and scenarios | ||
| - ✅ Sample data files where applicable | ||
| const schema = infer(formData); | ||
| // Automatically validates field types | ||
| **Run any example:** | ||
| ```bash | ||
| cd examples/csv-import | ||
| node example.js | ||
| ``` | ||
| **API Response Analysis** | ||
| ### Complex Data | ||
| - ✅ Sample data files | ||
| ### Complex Data | ||
| ```javascript | ||
| const apiResponse = [ | ||
| { id: "1", created: "2024-01-01", status: "true" }, | ||
| { id: "2", created: "2024-01-02", status: "false" } | ||
| ]; | ||
| const { infer } = require('predict-data-types'); | ||
| const schema = infer(apiResponse); | ||
| // Generate schema for documentation | ||
| const complexString = "192.168.1.1, #FF0000, 50%, $100, 2023-12-31"; | ||
| const types = infer(complexString.split(', ').map(v => ({ value: v }))); | ||
| // { value: 'ip' } // Takes the most specific type found | ||
| // Or analyze each value separately: | ||
| const values = "192.168.1.1, #FF0000, 50%, $100, 2023-12-31".split(', '); | ||
| values.forEach(val => { | ||
| console.log(`${val}: ${infer(val)}`); | ||
| }); | ||
| // 192.168.1.1: ip | ||
| // #FF0000: color | ||
| // 50%: percentage | ||
| // $100: currency | ||
| // 2023-12-31: date | ||
| ``` | ||
| **CSV Import** | ||
| ## API | ||
| ### `infer(input, format?)` | ||
| **The main function - handles any input type:** | ||
| **Parameters:** | ||
| - `input` (string | string[] | Object | Object[]): Value(s) to analyze | ||
| - `format` (optional): Output format - `Formats.NONE` (default) or `Formats.JSONSCHEMA` | ||
| **Returns:** | ||
| - `DataType` (string) - for single values and arrays of values | ||
| - `Schema` (Object) - for objects and arrays of objects | ||
| - `JSONSchema` (Object) - when `format` is `Formats.JSONSCHEMA` | ||
| **Examples:** | ||
| ```javascript | ||
| const csvImport = `id,email,signup_date | ||
| 1,alice@example.com,2024-01-01 | ||
| 2,bob@example.com,2024-01-02`; | ||
| const { infer, Formats, DataTypes } = require('predict-data-types'); | ||
| const schema = predictDataTypes(csvImport, true); | ||
| // Auto-detect column types for database import | ||
| // Single values | ||
| infer("42"); // → 'number' | ||
| infer("test@example.com"); // → 'email' | ||
| // Arrays | ||
| infer(["1", "2", "3"]); // → 'number' | ||
| // Objects | ||
| infer({ age: "25", email: "test@example.com" }); | ||
| // → { age: 'number', email: 'email' } | ||
| // Arrays of objects | ||
| infer([{ age: "25" }, { age: "30" }]); | ||
| // → { age: 'number' } | ||
| // JSON Schema format | ||
| infer({ name: "Alice", age: "25" }, Formats.JSONSCHEMA); | ||
| // → { type: 'object', properties: {...}, required: [...] } | ||
| ``` | ||
| ### Complex Data | ||
| ### Constants | ||
| **`DataTypes`** - Type-safe constants for comparisons: | ||
| ```javascript | ||
| const data = "192.168.1.1, #FF0000, 50%, $100, 2023-12-31"; | ||
| const types = predictDataTypes(data); | ||
| // { | ||
| // '192.168.1.1': 'ip', | ||
| // '#FF0000': 'color', | ||
| // '50%': 'percentage', | ||
| // '$100': 'currency', | ||
| // '2023-12-31': 'date' | ||
| // } | ||
| DataTypes.STRING, DataTypes.NUMBER, DataTypes.BOOLEAN, DataTypes.EMAIL, | ||
| DataTypes.PHONE, DataTypes.URL, DataTypes.UUID, DataTypes.DATE, | ||
| DataTypes.IP, DataTypes.COLOR, DataTypes.PERCENTAGE, DataTypes.CURRENCY, | ||
| DataTypes.ARRAY, DataTypes.OBJECT | ||
| ``` | ||
| ## API | ||
| **`Formats`** - Output format constants: | ||
| ```javascript | ||
| Formats.NONE // Default simple schema | ||
| Formats.JSONSCHEMA // JSON Schema format | ||
| ``` | ||
| ### `predictDataTypes(input, firstRowIsHeader)` | ||
| ### Legacy API | ||
| **`predictDataTypes(input, firstRowIsHeader)`** - For CSV strings only (use `infer()` instead) | ||
| <details> | ||
| <summary>Show legacy API details</summary> | ||
| **Parameters:** | ||
@@ -292,23 +400,49 @@ - `input` (string): Comma-separated string to analyze | ||
| **Throws:** Error if input is null, undefined, or not a string | ||
| **Example:** | ||
| ```javascript | ||
| const types = predictDataTypes('name,age\nAlice,25', true); | ||
| // { name: 'string', age: 'number' } | ||
| ``` | ||
| ### `infer(input)` | ||
| **Note:** This function is maintained for backwards compatibility. New code should use `infer()`. | ||
| **Smart inference for any input type:** | ||
| </details> | ||
| **Parameters:** | ||
| - `input` (string | string[] | Object | Object[]): Value(s) to analyze | ||
| **Returns:** | ||
| - DataType (string) for primitive values and arrays of primitives | ||
| - Schema (Object) for objects and arrays of objects | ||
| ## TypeScript vs. This Library | ||
| **Examples:** | ||
| ```javascript | ||
| infer("42") // → 'number' | ||
| infer(["1", "2"]) // → 'number' | ||
| infer({ age: "25" }) // → { age: 'number' } | ||
| infer([{ age: "25" }]) // → { age: 'number' } | ||
| **Common Misconception:** "Doesn't TypeScript already do this?" | ||
| **No!** TypeScript and this library serve completely different purposes: | ||
| | Feature | TypeScript | This Library | | ||
| |---------|-----------|--------------| | ||
| | **When it works** | Compile-time | Runtime | | ||
| | **What it checks** | Your code structure | Actual data content | | ||
| | **Scope** | Static type annotations | Dynamic string analysis | | ||
| | **Use case** | Prevent coding errors | Analyze user-provided data | | ||
| **Example:** | ||
| ```typescript | ||
| // TypeScript | ||
| const value: string = "test@example.com"; | ||
| // TypeScript knows: "value is a string" | ||
| // TypeScript DOESN'T know: "value contains an email address" | ||
| // This Library | ||
| const type = infer("test@example.com"); | ||
| // Returns: 'email' ✅ | ||
| // Detects the ACTUAL CONTENT at runtime | ||
| ``` | ||
| **When to use this library:** | ||
| - 📊 Users upload CSV/Excel files | ||
| - 🌐 API responses with unknown structure | ||
| - 📝 Form data that needs validation | ||
| - 🔄 ETL pipelines processing raw data | ||
| - 🎨 Dynamic form/UI generation | ||
| TypeScript can't help with any of these - you need runtime type detection! | ||
| ## Development | ||
@@ -334,2 +468,1 @@ | ||
| Author: [Melih Birim](https://github.com/melihbirim) | ||
46304
12.08%457
41.05%