predict-data-types
Advanced tools
+1
-1
| { | ||
| "name": "predict-data-types", | ||
| "version": "1.3.0", | ||
| "version": "1.3.1", | ||
| "description": "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.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+57
-161
@@ -6,15 +6,14 @@ # Predict Data Types | ||
| 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. | ||
| A lightweight npm package that automatically predicts data types for comma-separated values. Supports 14 data types including primitives, URLs, emails, UUIDs, dates, IP addresses, colors, percentages, and currency. | ||
| ## โจ Features | ||
| ## 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 | ||
| - Automatic detection of 14 data types | ||
| - CSV parsing with optional headers | ||
| - TypeScript definitions included | ||
| - Minimal dependencies (only dayjs) | ||
| - Comprehensive test coverage | ||
| - Optimized regex patterns | ||
| ## ๐ฆ Installation | ||
| ## Installation | ||
@@ -25,24 +24,24 @@ ```bash | ||
| ## ๐ง Supported Data Types | ||
| ## Supported 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}` | | ||
| | 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"}` | | ||
| ## ๐ Usage | ||
| ## Usage | ||
| ### Basic Usage | ||
| ### Basic Example | ||
@@ -56,3 +55,2 @@ ```javascript | ||
| console.log(types); | ||
| // Output: | ||
| // { | ||
@@ -67,14 +65,9 @@ // 'John': 'string', | ||
| ### Advanced Examples | ||
| ### CSV with Headers | ||
| #### CSV-like Data with Headers | ||
| ```javascript | ||
| 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 csvData = `name,age,active,email | ||
| John,30,true,john@example.com`; | ||
| const types = predictDataTypes(csvData, true); // true = first row is header | ||
| console.log(types); | ||
| // Output: | ||
| const types = predictDataTypes(csvData, true); | ||
| // { | ||
@@ -84,149 +77,52 @@ // 'name': 'string', | ||
| // 'active': 'boolean', | ||
| // 'email': 'email', | ||
| // 'signup_date': 'date' | ||
| // 'email': 'email' | ||
| // } | ||
| ``` | ||
| #### Mixed Complex Data | ||
| ### Complex Data | ||
| ```javascript | ||
| 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: | ||
| const data = "192.168.1.1, #FF0000, 50%, $100, 2023-12-31"; | ||
| const types = predictDataTypes(data); | ||
| // { | ||
| // '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' | ||
| // '192.168.1.1': 'ip', | ||
| // '#FF0000': 'color', | ||
| // '50%': 'percentage', | ||
| // '$100': 'currency', | ||
| // '2023-12-31': 'date' | ||
| // } | ||
| ``` | ||
| #### Date Format Detection | ||
| ## API | ||
| ```javascript | ||
| 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): Comma-separated string to analyze | ||
| - `firstRowIsHeader` (boolean): Treat first row as headers (default: `false`) | ||
| - `input` (string): The comma-separated string to analyze | ||
| - `firstRowIsHeader` (boolean, optional): Whether to treat the first row as column headers (default: `false`) | ||
| **Returns:** Object mapping field names/values to their data types | ||
| **Returns:** | ||
| **Throws:** Error if input is null, undefined, or not a string | ||
| - `Object<string, string>`: Mapping of field names/values to their predicted data types | ||
| ## Development | ||
| **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: | ||
| ```javascript | ||
| // 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 | ||
| ```bash | ||
| npm test # Run all tests | ||
| npm run lint # Check code quality | ||
| npm run lint:fix # Auto-fix lint issues | ||
| npm test # Run tests | ||
| npm run test:coverage # Run tests with coverage | ||
| npm run lint # Check code quality | ||
| npm run lint:fix # Fix lint issues | ||
| ``` | ||
| ### Test Coverage | ||
| ## License | ||
| The package includes comprehensive tests covering: | ||
| MIT License - see [LICENSE](LICENSE) file for details. | ||
| - โ All supported data types | ||
| - โ Edge cases and error conditions | ||
| - โ Input validation | ||
| - โ Complex nested structures | ||
| - โ Various date formats | ||
| - โ Header mode functionality | ||
| ## Contributing | ||
| ## ๐ Changelog | ||
| See [CONTRIBUTING.md](CONTRIBUTING.md) for contribution guidelines. | ||
| ### 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 | ||
| Author: [Melih Birim](https://github.com/melihbirim) | ||
| ## ๐ค Contributing | ||
| Contributions are welcome! Please read our [Contributing Guidelines](CONTRIBUTING.md) for details on our code of conduct and development process. | ||
| 1. Fork the repository | ||
| 2. Create your feature branch (`git checkout -b feature/amazing-feature`) | ||
| 3. Run tests (`npm test`) | ||
| 4. Commit your changes (`git commit -m 'Add amazing feature'`) | ||
| 5. Push to the branch (`git push origin feature/amazing-feature`) | ||
| 6. Open a Pull Request | ||
| ## ๐ License | ||
| This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. | ||
| ## ๐ Support | ||
| - ๐ [Documentation](README.md) | ||
| - ๏ฟฝ๏ธ [Roadmap](ROADMAP.md) - Planned features and improvements | ||
| - ๏ฟฝ๐ [Issue Tracker](https://github.com/melihbirim/predict-data-types/issues) | ||
| - ๐ฌ [Discussions](https://github.com/melihbirim/predict-data-types/discussions) | ||
| --- | ||
| Made with โค๏ธ by [Melih Birim](https://github.com/melihbirim) |
19893
-17.48%124
-45.61%