8000 GitHub - balsnub/tsutil
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

balsnub/tsutil

Repository files navigation

TSUtil - TypeScript Data Transformation Library

A comprehensive TypeScript library for data transformation, validation, and schema management. This library provides a flexible and extensible way to transform data between different formats and validate it B527 against schemas.

Features

  • Data format transformation (JSON, CSV, TSV, XML, YAML, etc.)
  • Schema validation
  • Data structure manipulation
  • Streaming support for large datasets
  • Pipeline-based transformations
  • Type-safe API

Installation

npm install tsutil

Usage

Basic Transformation

import { createTransformer } from 'tsutil';

const transformer = createTransformer();

// Transform JSON to CSV
const result = await transformer.transform(
  { name: 'John', age: 30 },
  'json',
  'csv'
);

console.log(result.data);
// name,age
// John,30

Schema Validation

import { SchemaValidator } from 'tsutil';

const schema = {
  fields: {
    name: { type: 'string', required: true },
    age: { type: 'number', min: 0, max: 120 }
  }
};

const data = { name: 'John', age: 30 };
const validation = SchemaValidator.validate(data, schema);

console.log(validation.valid); // true

Data Structure Manipulation

import { TransformUtils } from 'tsutil';

const data = [
  { user: { name: 'John', age: 30 } },
  { user: { name: 'Jane', age: 25 } }
];

// Flatten nested objects
const flattened = TransformUtils.flattenObject(data[0]);
console.log(flattened);
// { 'user.name': 'John', 'user.age': 30 }

// Pivot data
const pivoted = TransformUtils.pivotData(
  data,
  'user.name',
  'user.age',
  'value'
);

Streaming Support

import { StreamingTransformer } from 'tsutil';

const streamTransformer = new StreamingTransformer(transformer);

for await (const result of streamTransformer.transformStream(
  dataStream,
  'csv',
  'json'
)) {
  console.log(result.data);
}

Pipeline Transformations

import { PipelineTransformer } from 'tsutil';

const pipeline = {
  name: 'data-cleanup',
  steps: [
    {
      fromFormat: 'csv',
      toFormat: 'json',
      validation: [
        { field: 'name', required: true },
        { field: 'age', type: 'number' }
      ]
    },
    {
      fromFormat: 'json',
      toFormat: 'yaml',
      transform: (data) => ({
        ...data,
        processed: true
      })
    }
  ]
};

const pipelineTransformer = new PipelineTransformer(transformer);
const result = await pipelineTransformer.executePipeline(data, pipeline);

API Documentation

Core Classes

  • DataTransformer: Main class for data transformation
  • SchemaValidator: Schema validation utilities
  • TransformUtils: Data structure manipulation utilities
  • StreamingTransformer: Streaming support for large datasets
  • PipelineTransformer: Pipeline-based transformations

Format Handlers

  • JSONHandler: JSON format handler
  • CSVHandler: CSV format handler
  • TSVHandler: TSV format handler
  • XMLHandler: XML format handler
  • YAMLHandler: YAML format handler
  • PipeHandler: Pipe-delimited format handler
  • FixedWidthHandler: Fixed-width format handler

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0