structured-txt v1.0.0
StructuredTXT 📃
A 0-dependency and 100% code-coverage library to build a structured TXT file, where the data position in it matters
Use cases
This library is intended to use, when you want to build a .txt-like file with structured data, this is, the position of the data in the file matters. This file format is mainly used to interact with old applications, where more structured data formats input is not available.
Examples and Usage
The library returns a buffer, which can easily be converted to string with Buffer.toString().
The usage of the available blueprints parameter can be seen below:
content: function which receives thedata(or the data parsed byitemsBuilder) and returns the item valuelength: length this field will occupy in the textfillEnd(optional, default:' '): what we will fill in the end of the field, if the size of the field is less thanlengthfillStart(optional): if this value is passed,fillEndis ignored. What we will fill in the start of the field, if the size of the field is less thanlength
const structuredTXT = require( 'structured-txt' )
const data = {
id: 5,
name: 'Very long person name'
}
const FIRST_BLOCK = {
blueprints: [
{ content: () => 'HEADER', length: 6 },
{ content: () => '1', length: 2 },
{ content: (data) => data.id, length: 4, fillEnd: '# },
]
}
const SECOND_BLOCK = {
blueprints: [
{ content: () => 'OTHER_LINE', length: 20, fillStart: ' ' },
{ content: (data) => data.name, length: 15 },
]
}
const blocks = [FIRST_BLOCK, SECOND_BLOCK]
const resultBuffer = structuredTXT.jsonToStructuredTxt(block, data)The result if it was written to a file would be the following:
HEADER1 5###
OTHER_LINEVery long persoitemsBuilder
If you want to build several lines, with the same block structure, you can use the itemsBuilder property in the block object. Example:
const structuredTXT = require( 'structured-txt' )
const data = {
id: 5,
products: [ 'Product 1', 'Other Product with long name' ]
}
const block = {
blueprints: [
{ content: () => 'LINE', length: 6 },
{ content: (data) => data.id, length: 4, fillEnd: '# },
{ content: (data) => data.idx, length: 2 },
{ content: (data) => data.productName, length: 20 },
],
itemsBuilder: (data) => data.products.map( (productName, idx) => { ...data, productName, idx: idx + 1 } )
}
const resultBuffer = structuredTXT.jsonToStructuredTxt([block], data)The result if it was written to a file would be the following:
LINE 5###1 Product 1
LINE 5###2 Other Product with lLicense
This project is licensed with ISC
5 years ago