npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

structured-txt

v1.0.0

Published

"Package to create structured .txt like files"

Downloads

12

Readme

StructuredTXT 📃

A 0-dependency and 100% code-coverage library to build a structured TXT file, where the data position in it matters

License Version npm bundle size Tests

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 the data (or the data parsed by itemsBuilder) and returns the item value
  • length: length this field will occupy in the text
  • fillEnd (optional, default: ' '): what we will fill in the end of the field, if the size of the field is less than length
  • fillStart (optional): if this value is passed, fillEnd is ignored. What we will fill in the start of the field, if the size of the field is less than length
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 perso

itemsBuilder

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 l

License

This project is licensed with ISC