@nan0web/transformer
v1.0.0
Published
Basic and agnostic transformer.
Downloads
5
Readme
@nan0web/transformer
Basic and agnostic transformer.
|Package name|Status|Documentation|Test coverage|Features|Npm version|
|---|---|---|---|---|---|
|@nan0web/transformer |🟢 99.7% |🧪 English 🏴Українською 🇺🇦 |🟢 100.0% |✅ d.ts 📜 system.md 🕹️ playground |— |
Description
The @nan0web/transformer package provides a lightweight and flexible foundation for sequential data transformation.
It allows you to chain multiple transformation steps (transformers) that can encode and decode data in a predictable, composable way.
A Transformer instance collects transformer objects and applies their encode or decode methods in sequence.
Each transformer may implement one or both methods, and the process is fully asynchronous, allowing integration with async operations.
This package is ideal for:
- Building data pipelines (e.g., encryption, compression, formatting)
- Creating serialization/deserialization layers
- Developing middleware-like processing sequences
- Any scenario where data must pass through multiple stages of transformation
Installation
How to install with npm?
npm install @nan0web/transformerHow to install with pnpm?
pnpm add @nan0web/transformerHow to install with yarn?
yarn add @nan0web/transformerUsage
Basic Transformation
Create a Transformer and add transformer objects with encode and/or decode methods.
How to chain multiple encoders?
import { Transformer } from '@nan0web/transformer'
const transformer = new Transformer()
const upperCase = {
encode: async (data) => data.toUpperCase(),
decode: async (data) => data.toLowerCase()
}
const addPrefix = {
encode: async (data) => `[ENC] ${data}`,
decode: async (data) => data.replace(/^\[ENC\]\s/i, '')
}
transformer.addTransformer(upperCase)
transformer.addTransformer(addPrefix)
const encoded = await transformer.encode("hello world")
console.info(encoded) // [ENC] HELLO WORLD
const decoded = await transformer.decode(encoded)
console.info(decoded) // hello worldHow to add and remove transformers dynamically?
import { Transformer } from '@nan0web/transformer'
const transformer = new Transformer()
const spyTransformer = {
encode: async (data) => `${data} • spy`,
decode: async (data) => data.replace(/ • spy$/, '')
}
transformer.addTransformer(spyTransformer)
console.info(transformer.transformers) // ← [spyTransformer]
transformer.removeTransformer(spyTransformer)
console.info(transformer.transformers) // ← []Skip Non-Implementing Transformers
Transformers without encode or decode methods are skipped automatically.
How to ensure only transformers with encode/decode are applied?
import { Transformer } from '@nan0web/transformer'
const transformer = new Transformer()
const validEncoder = {
encode: async (data) => data + "•encoded"
}
const invalidTransformer = {
process: async (data) => data
}
transformer.addTransformer(validEncoder)
transformer.addTransformer(invalidTransformer)
const result = await transformer.encode("data")
console.info(result) // data•encodedAsynchronous Transformers
All transformations are async, enabling integration with promises and async operations.
How to use asynchronous transformations with delays?
import { Transformer } from '@nan0web/transformer'
const transformer = new Transformer()
const delayEncode = {
encode: async (data) => {
await new Promise(r => setTimeout(r, 10))
return `[DELAYED] ${data}`
}
}
transformer.addTransformer(delayEncode)
const result = await transformer.encode("test")
console.info(result) // [DELAYED] testAPI
Transformer
A class that manages a sequence of transformers.
Constructor
new Transformer()— creates an empty transformer chain.
Properties
transformers– array of transformer objects added viaaddTransformer.
Methods
encode(data)– applies allencodemethods in sequence.decode(data)– applies alldecodemethods in sequence.addTransformer(t)– adds a transformer object to the chain.removeTransformer(t)– removes a specific transformer object from the chain.
All methods return promises and are await-safe.
Java•Script
Uses d.ts files for autocompletion
CLI Playground
Run local experiments using the playground script.
How to run playground script?
# Run the playground
npm run playContributing
This project follows strict testing and linting rules.
How to contribute? - check here
License
Licensed under ISC.
How to license ISC? - check here
