You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@abdullah_zughbi/csvt-parse

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@abdullah_zughbi/csvt-parse

Type-safe CSV parser with schema validation

1.1.2
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

CSVT-Parse

A powerful Node.js library for parsing and writing CSV files with typed column definitions. CSVT-Parse provides comprehensive type validation and robust error handling for working with structured CSV data.

Features

  • 🎯 Typed Column Definitions: Define column types directly in the CSV header (name:string,age:number)
  • 🔍 Comprehensive Type Validation: Built-in validation for 20+ data types including primitives, dates, URLs, emails, UUIDs, and more
  • 🔧 Enum Types: Define columns with restricted value sets (status:enum(pending,approved,rejected))
  • 🔀 Union Types: Support multiple types per column (value:string|number|boolean)
  • 🌟 Any Type: Auto-detecting flexible type that infers the best match (data:any)
  • 📁 File I/O Support: Read from and write to files with async/sync APIs
  • 🛡️ Error Handling: Comprehensive error reporting with line and column context
  • 🚀 High Performance: Optimized parsing with minimal overhead
  • 📦 TypeScript First: Full TypeScript support with complete type definitions
  • 🔒 Source Protection: Obfuscated distribution for commercial use

Installation

npm install @abdullah_zughbi/csvt-parse

Quick Start

Basic Usage

const csvt = require('@abdullah_zughbi/csvt-parse');

// Parse CSVT content with typed columns
const csvtContent = `name:string,age:number,active:boolean
"John Doe",30,true
"Jane Smith",25,false`;

const result = csvt.parseCSVTSync(csvtContent);
console.log(result.data);
// Output: [
//   { name: "John Doe", age: 30, active: true },
//   { name: "Jane Smith", age: 25, active: false }
// ]

File Operations

// Read from CSVT file
const fileData = csvt.readFileSync('data.csvt');
console.log(fileData.data);

// Write to CSVT file
const data = [
  { name: 'Alice', score: 95, active: true },
  { name: 'Bob', score: 87, active: false }
];

const columns = [
  { name: 'name', type: 'string' },
  { name: 'score', type: 'number' },
  { name: 'active', type: 'boolean' }
];

csvt.writeFileSync('output.csvt', data, columns);

Complex Data Types

// JSON objects and arrays
const complexCSVT = `user:string,preferences:json,history:array
"alice","{""theme"":""dark"",""lang"":""en""}","[""login"",""purchase""]"`;

const result = csvt.parseCSVTSync(complexCSVT);
console.log(result.data[0]);
// Output: {
//   user: "alice",
//   preferences: { theme: "dark", lang: "en" },
//   history: ["login", "purchase"]
// }

Enum Types

// Restricted value sets
const enumCSVT = `status:enum(pending,approved,rejected),priority:enum(low,medium,high)
"pending","high"
"approved","medium"
"rejected","low"`;

const enumResult = csvt.parseCSVTSync(enumCSVT);
console.log(enumResult.data);
// Output: [
//   { status: "pending", priority: "high" },
//   { status: "approved", priority: "medium" },
//   { status: "rejected", priority: "low" }
// ]

Union Types

// Multiple type options per column
const unionCSVT = `id:string|number,value:number|boolean,mixed:string|number|boolean
"user123",42,true
"456",3.14,"hello"
"789",0,false`;

const unionResult = csvt.parseCSVTSync(unionCSVT);
console.log(unionResult.data);
// Output: [
//   { id: "user123", value: 42, mixed: "true" },
//   { id: "456", value: 3.14, mixed: "hello" },
//   { id: "789", value: 0, mixed: "false" }
// ]

Any Type

// Auto-detecting flexible type
const anyCSVT = `name:string,data:any,info:any
"John",42,"text value"
"Jane",true,"[1,2,3]"
"Bob","{""key"":""value""}",123.45`;

const anyResult = csvt.parseCSVTSync(anyCSVT);
console.log(anyResult.data);
// Output: [
//   { name: "John", data: 42, info: "text value" },
//   { name: "Jane", data: true, info: [1,2,3] },
//   { name: "Bob", data: {"key":"value"}, info: 123.45 }
// ]

Escape Sequences

// Enable escape sequence processing for string fields
const escapeCSVT = `message:string,path:string
"Line 1\\nLine 2","C:\\\\Users\\\\file.txt"`;

// Default behavior - preserves literal backslashes
const defaultResult = csvt.parseCSVTSync(escapeCSVT, { processEscapes: false });
console.log(defaultResult.data[0].message); // "Line 1\\nLine 2"

// With escape processing - converts sequences to actual characters
const processedResult = csvt.parseCSVTSync(escapeCSVT, { processEscapes: true });
console.log(processedResult.data[0].message); 
// Output: "Line 1
//          Line 2"

// Supported escape sequences: \\n \\t \\r \\" \\\\

Supported Types

Basic Types

TypeDescriptionExample
stringText values"Hello World"
numberInteger or float42, 3.14
booleanBoolean valuestrue, false, 1, 0, yes, no
arrayJSON arrays["item1", "item2"]
jsonJSON objects{"key": "value"}
anyAuto-detecting typeAuto-infers best type
voidNull/empty valuesnull, ""

Date & Time Types

TypeDescriptionExample
dateDate only"2024-01-15"
datetimeDate with time"2024-01-15T10:30:00Z"
timeTime only"10:30:00"

Web & Network Types

TypeDescriptionExample
urlHTTP/HTTPS URLs"https://example.com"
emailEmail addresses"user@example.com"
ipaddressIP addresses"192.168.1.1"

Visual Types

TypeDescriptionExample
hexcolorHex colors"#FF0000"
rgbaRGBA colors"rgba(255,0,0,0.5)"
colorNamed/hex colors"red", "#FF0000"

Identifier Types

TypeDescriptionExample
uuidUUIDs"550e8400-e29b-41d4-a716-446655440000"
percentagePercentages"75%"

Advanced Types

TypeDescriptionExample
enumRestricted value setstatus:enum(pending,approved,rejected)
unionMultiple type optionsvalue:string|number|boolean

Length-Specified Text

TypeDescriptionExample
textUnlimited text lengthdescription:text
varchar(n)Variable length, max n charsname:varchar(50)
char(n)Fixed length, padded to n charscode:char(10)

API Reference

Parsing Functions

  • parseCSVTSync(content, options) - Synchronous parsing
  • parseCSVT(content, options) - Asynchronous parsing (returns Promise)

File Operations

  • readFileSync(filename, options) - Synchronous file reading
  • readFile(filename, options) - Asynchronous file reading
  • writeFileSync(filename, data, columns, options) - Synchronous file writing
  • writeFile(filename, data, columns, options) - Asynchronous file writing

Error Handling

try {
  const result = csvt.parseCSVTSync(content, { strict: true });
} catch (error) {
  if (error instanceof csvt.ValidationError) {
    console.log(`Error in column "${error.column}" at line ${error.line}`);
    console.log(`Expected: ${error.expectedType}, Got: ${error.actualValue}`);
  }
}

Options

const options = {
  delimiter: ',',        // Field delimiter
  quote: '"',           // Quote character
  escape: '"',          // Escape character
  skipEmptyLines: true, // Skip empty lines
  trim: true,           // Trim whitespace
  strict: false,        // Strict error handling
  processEscapes: false // Process escape sequences in string fields
};

Escape Sequence Processing

When processEscapes: true is enabled, the following escape sequences are processed in string fields:

SequenceResultDescription
\\n\nNewline character
\\t\tTab character
\\r\rCarriage return
\\""Quote character
\\\\\Backslash character

Important Notes:

  • Escape processing only applies to string type fields
  • For union types, processing applies only if the final parsed value is a string
  • Complex types like json and array are not affected by escape processing
  • Default behavior preserves literal backslashes for compatibility

License

MIT License © 2025 Abdullah Zughbi

Keywords

csv

FAQs

Package last updated on 21 Jul 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

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.