Git Product home page Git Product logo

plain-text-data-to-json's Introduction

plain-text-data-to-json

Build Coverage Downloads Size

Transform basic plain-text lists or objects into arrays and objects.

Contents

What is this?

This package takes a file (a sort of simple database), parses it, and returns clean data.

When should I use this?

I found myself rewriting a simple transformation over and over to handle text files. One example is this source file in emoji-emotion This project fixes that for me. You can use it too if it matches your needs.

Install

This package is ESM only. In Node.js (version 14.14+, 16.0+), install with npm:

npm install plain-text-data-to-json

In Deno with esm.sh:

import {toJson} from 'https://esm.sh/plain-text-data-to-json@2'

In browsers with esm.sh:

<script type="module">
  import {toJson} from 'https://esm.sh/plain-text-data-to-json@2?bundle'
</script>

Use

If we have the following file input.txt:

% A comment

alpha
bravo
charlie

…and our module example.js looks as follows:

import fs from 'node:fs/promises'
import {toJson} from 'plain-text-data-to-json'

const document = String(await fs.readFile('input.txt'))

const data = toJson(document)

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

…then running node example.js yields in output.json:

[
  "alpha",
  "bravo",
  "charlie"
]

API

This package exports the identifier toJson. There is no default export.

toJson(value[, options])

Transform basic plain-text lists or objects into arrays and objects.

options

Configuration (optional).

options.delimiter

Character to use as delimiter between key/value pairs (string, default: ':').

options.comment

Character(s) to use for line comments, false turns off comments (string, Array<string>, or boolean, 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 key/value pairs and overwrites (see errors).

options.log

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

Data

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 whitespace only lines.

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

unicorn

Yields:

['unicorn']

Key/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")

Types

This package is fully typed with TypeScript. It exports the additional type Options.

Compatibility

This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. It also works in Deno and modern browsers.

Contribute

Yes please! See How to Contribute to Open Source.

Security

This package is safe.

License

MIT © Titus Wormer

plain-text-data-to-json's People

Contributors

greenkeeperio-bot avatar rafarvns avatar wooorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

plain-text-data-to-json's Issues

CLI

A cli would be very much appreciated

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.