predict-data-types
Advanced tools
+35
-15
| const moment = require('moment'); | ||
| const os = require('os'); | ||
@@ -80,2 +81,3 @@ function isDate(value) { | ||
| }; | ||
| if (char === '{' || char === '[') { | ||
@@ -119,33 +121,51 @@ let j = i + 1; | ||
| function predictDataTypes(str) { | ||
| function predictDataTypes(str, firstRowIsHeader = false) { | ||
| let header = ""; | ||
| let data = str; | ||
| if (firstRowIsHeader) { | ||
| let tmp = str.split(os.EOL); | ||
| if (tmp.length > 1) { | ||
| header = tmp[0].split(","); | ||
| data = tokenize(tmp[1]); | ||
| }else{ | ||
| return {}; | ||
| } | ||
| } else { | ||
| data = tokenize(str); | ||
| header = data; | ||
| } | ||
| const types = {}; | ||
| const parts = tokenize(str); | ||
| for (let i = 0; i < parts.length; i++) { | ||
| const part = parts[i].trim(); | ||
| console.log(`header ${header}`) | ||
| console.log(`data ${data}`) | ||
| for (let i = 0; i < data.length; i++) { | ||
| const part = data[i].trim(); | ||
| const field = header[i].trim(); | ||
| if (isBoolean(part)) { | ||
| types[part] = "boolean"; | ||
| types[field] = "boolean"; | ||
| } else if (!isNaN(parseFloat(part)) && isFinite(part)) { | ||
| types[part] = "number"; | ||
| types[field] = "number"; | ||
| } else if (isDate(part)) { | ||
| types[part] = "date"; | ||
| types[field] = "date"; | ||
| } else if (isURL(part)) { | ||
| types[part] = "url"; | ||
| types[field] = "url"; | ||
| } else if (isUUID(part)) { | ||
| types[part] = "uuid"; | ||
| types[field] = "uuid"; | ||
| } else if (isPhoneNumber(part)) { | ||
| types[part] = "phone"; | ||
| types[field] = "phone"; | ||
| } else if (isEmail(part)) { | ||
| types[part] = "email"; | ||
| types[field] = "email"; | ||
| } else if (part.startsWith("[") && part.endsWith("]")) { | ||
| types[part] = "array"; | ||
| types[field] = "array"; | ||
| } else if (part.startsWith("{") && part.endsWith("}")) { | ||
| types[part] = "object"; | ||
| types[field] = "object"; | ||
| } | ||
| else { | ||
| types[part] = "string"; | ||
| types[field] = "string"; | ||
| } | ||
| } | ||
| console.log(types); | ||
| return types; | ||
@@ -152,0 +172,0 @@ |
+1
-1
| { | ||
| "name": "predict-data-types", | ||
| "version": "1.0.0", | ||
| "version": "1.1.0", | ||
| "description": "A simple npm package that predicts data types for comma-separated values, including JSON objects, and validates URLs, phone numbers, email addresses, and geolocation data within string values.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,2 +6,3 @@ const chai = require('chai'); | ||
| describe('predictDataTypes', () => { | ||
| it('should predict data types for string and url', () => { | ||
@@ -38,3 +39,3 @@ const text = 'John, http://asd.com'; | ||
| }); | ||
| it('should predict data types for arrays', () => { | ||
@@ -50,13 +51,12 @@ const text = '[["apple"], "banana", "orange"], {"name": "John", "age": 30}'; | ||
| it('should predict data types for JSON objects', () => { | ||
| const text = 'John,30, true,1991-05-12'; | ||
| const types = predictDataTypes(text); | ||
| const text = 'name, age, married, dob \n John,30, true, 1991-05-12'; | ||
| const types = predictDataTypes(text, true); | ||
| expect(types).to.deep.equal({ | ||
| 'John': 'string', | ||
| '30': 'number', | ||
| 'true': 'boolean', | ||
| '1991-05-12': 'date' | ||
| 'name': 'string', | ||
| 'age': 'number', | ||
| 'married': 'boolean', | ||
| 'dob': 'date' | ||
| }); | ||
| }); | ||
| }) |
9446
5.58%206
8.42%