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

plain-text-data-to-json

v2.0.0

Published

Transform a simple plain-text database to JSON

Downloads

659

Readme

plain-text-data-to-json

Build Coverage Downloads Size

Transform a “database” / basic (word, phrase) list from plain text to JSON.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install plain-text-data-to-json

Use

var fs = require('fs')
var toJSON = require('plain-text-data-to-json')

var doc = fs.readFileSync('input.txt', 'utf8')

var data = toJSON(doc)

fs.writeFileSync('output.json', JSON.stringify(data, null, 2) + '\n')

API

This package exports the following identifiers: toJson. There is no default export.

toJson(value[, options])

Transforms the given value (string) to JSON. Don’t like the default comment and property-value pair delimiters? Specify your own:

options
options.comment

Character(s) to use for line-comments, false turns off comments (string, Array.<string>, or boolean, default: '%')

options.delimiter

Character to use as delimiter between property-value pairs (string, default: ':')

options.forgiving

How relaxed to be ('fix' or boolean, default: false). When true, doesn’t throw for duplicate keys. When 'fix', doesn’t throw for property-value pairs and overwrites (see errors).

options.log

Whether to log when forgiving ignores an error (boolean, default: true).

Why

I found myself rewriting a simple transformation over and over. This (verbosely named) project fixes that. It might not be useful, or too simple for others, but suites my use cases.

“Plain text”

The term plain text might be confusing. It’s actually more of some (sparingly specified) standard.

Comments

Use a percentage sign (by default) to specify a comment. The comment will last until the end of line.

% This is a completely commented line.
unicorn % This is a partially commented line.

Yields:

['unicorn']

Whitespace

Initial or final white space (\s) is trimmed from values.

       unicorn     % some value

Yields:

['unicorn']

Empty lines

Empty lines are striped. This includes blank (whitespace only) lines.

    %%% this file contains a value. %%%

unicorn

Yields:

['unicorn']

Property-value pairs

If a line includes a colon (by default), the library returns an object.

unicorn : magic creature

Yields:

{unicorn: 'magic creature'}

Values

All other lines are treated as array values.

unicorn

Yields:

["unicorn"]

Errors

Some errors are thrown when malformed “plain-text” is found, such as:

  • When lines both with and without colons exist
  • In arrays, when duplicate values exist (unless forgiving: true)
  • In objects, when duplicate properties exist (unless forgiving: true)
  • In objects, when duplicate properties with different values exist (unless forgiving: "fix")

License

MIT © Titus Wormer