55[ ![ Downloads] [ downloads-badge ]] [ downloads ]
66[ ![ Size] [ size-badge ]] [ size ]
77
8- Transform a “database” / basic (word, phrase) list from plain text to JSON.
8+ Transform basic plain-text lists or objects into arrays and objects.
9+
10+ ## Contents
11+
12+ * [ What is this?] ( #what-is-this )
13+ * [ When should I use this?] ( #when-should-i-use-this )
14+ * [ Install] ( #install )
15+ * [ Use] ( #use )
16+ * [ API] ( #api )
17+ * [ ` toJson(value[, options]) ` ] ( #tojsonvalue-options )
18+ * [ Data] ( #data )
19+ * [ Comments] ( #comments )
20+ * [ Whitespace] ( #whitespace )
21+ * [ Empty lines] ( #empty-lines )
22+ * [ Key/value pairs] ( #keyvalue-pairs )
23+ * [ Values] ( #values )
24+ * [ Errors] ( #errors )
25+ * [ Types] ( #types )
26+ * [ Compatibility] ( #compatibility )
27+ * [ Contribute] ( #contribute )
28+ * [ Security] ( #security )
29+ * [ License] ( #license )
30+
31+ ## What is this?
32+
33+ This package takes a file (a sort of simple database), parses it, and returns
34+ clean data.
35+
36+ ## When should I use this?
37+
38+ I found myself rewriting a simple transformation over and over to handle text
39+ files.
40+ One example is this source file in [ ` emoji-emotion ` ] [ emoji-emotion-example ]
41+ This project fixes that for me.
42+ You can use it too if it matches your needs.
943
1044## Install
1145
12- This package is ESM only: Node 12+ is needed to use it and it must be ` import ` ed
13- instead of ` require ` d.
14-
15- [ npm] [ ] :
46+ This package is [ ESM only] [ esm ] .
47+ In Node.js (version 14.14+, 16.0+), install with [ npm] [ ] :
1648
1749``` sh
1850npm install plain-text-data-to-json
1951```
2052
53+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
54+
55+ ``` js
56+ import {toJson } from ' https://esm.sh/plain-text-data-to-json@2'
57+ ```
58+
59+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
60+
61+ ``` html
62+ <script type =" module" >
63+ import {toJson } from ' https://esm.sh/plain-text-data-to-json@2?bundle'
64+ </script >
65+ ```
66+
2167## Use
2268
69+ If we have the following file ` input.txt ` :
70+
71+ ``` txt
72+ % A comment
73+
74+ alpha
75+ bravo
76+ charlie
77+ ```
78+
79+ …and our module ` example.js ` looks as follows:
80+
2381``` js
24- import fs from ' fs '
82+ import fs from ' node:fs/promises '
2583import {toJson } from ' plain-text-data-to-json'
2684
27- var doc = fs .readFileSync (' input.txt' , ' utf8' )
85+ const document = String (await fs .readFile (' input.txt' ))
86+
87+ const data = toJson (document )
2888
29- var data = toJson (doc)
89+ await fs .writeFile (' output.json' , JSON .stringify (data, null , 2 ) + ' \n ' )
90+ ```
91+
92+ …then running ` node example.js ` yields in ` output.json ` :
3093
31- fs .writeFileSync (' output.json' , JSON .stringify (data, null , 2 ) + ' \n ' )
94+ ``` json
95+ [
96+ " alpha" ,
97+ " bravo" ,
98+ " charlie"
99+ ]
32100```
33101
34102## API
35103
36- This package exports the following identifiers: ` toJson ` .
104+ This package exports the identifier ` toJson ` .
37105There is no default export.
38106
39107### ` toJson(value[, options]) `
40108
41- Transforms the given value (string) to JSON.
42- Don’t like the default comment and property-value pair delimiters?
43- Specify your own:
109+ Transform basic plain-text lists or objects into arrays and objects.
44110
45111##### ` options `
46112
47- ###### ` options.comment `
48-
49- Character(s) to use for line-comments, ` false ` turns off comments (` string ` ,
50- ` Array<string> ` , or ` boolean ` , default: ` '%' ` )
113+ Configuration (optional).
51114
52115###### ` options.delimiter `
53116
54- Character to use as delimiter between property-value pairs (` string ` , default:
55- ` ':' ` )
117+ Character to use as delimiter between key/value pairs (` string ` , default:
118+ ` ':' ` ).
119+
120+ ###### ` options.comment `
121+
122+ Character(s) to use for line comments, ` false ` turns off comments (` string ` ,
123+ ` Array<string> ` , or ` boolean ` , default: ` '%' ` )
56124
57125###### ` options.forgiving `
58126
59127How relaxed to be (` 'fix' ` or ` boolean ` , default: ` false ` ).
60128When ` true ` , doesn’t throw for duplicate keys.
61- When ` 'fix' ` , doesn’t throw for property- value pairs and overwrites (see
129+ When ` 'fix' ` , doesn’t throw for key/ value pairs and overwrites (see
62130[ errors] [ ] ).
63131
64132###### ` options.log `
65133
66- Whether to log when ` forgiving ` ignores an error (` boolean ` , default: ` true ` ).
134+ Whether to call ` console.log ` with info when ` forgiving ` ignores an error
135+ (` boolean ` , default: ` true ` ).
67136
68- ## Why
69-
70- I found myself rewriting a simple transformation over and over.
71- This (verbosely named) project fixes that.
72- It might not be useful, or too simple for others, but suites my use cases.
73-
74- ## “Plain text”
137+ ## Data
75138
76139The term plain text might be confusing.
77140It’s actually more of some (sparingly specified) standard.
@@ -109,7 +172,7 @@ Yields:
109172### Empty lines
110173
111174Empty lines are striped.
112- This includes blank ( whitespace only) lines.
175+ This includes whitespace only lines.
113176
114177``` txt
115178 %%% this file contains a value. %%%
@@ -123,7 +186,7 @@ Yields:
123186[' unicorn' ]
124187```
125188
126- ### Property- value pairs
189+ ### Key/ value pairs
127190
128191If a line includes a colon (by default), the library returns an object.
129192
@@ -155,12 +218,32 @@ Yields:
155218
156219Some errors are thrown when malformed “plain-text” is found, such as:
157220
158- * When lines both with and without colons exist
159- * In arrays, when duplicate values exist (unless ` forgiving: true ` )
160- * In objects, when duplicate properties exist (unless ` forgiving: true ` )
161- * In objects, when duplicate properties with different values exist (unless
221+ * when lines both with and without colons exist
222+ * in arrays, when duplicate values exist (unless ` forgiving: true ` )
223+ * in objects, when duplicate properties exist (unless ` forgiving: true ` )
224+ * in objects, when duplicate properties with different values exist (unless
162225 ` forgiving: "fix" ` )
163226
227+ ## Types
228+
229+ This package is fully typed with [ TypeScript] [ ] .
230+ It exports the additional type ` Options ` .
231+
232+ ## Compatibility
233+
234+ This package is at least compatible with all maintained versions of Node.js.
235+ As of now, that is Node.js 14.14+ and 16.0+.
236+ It also works in Deno and modern browsers.
237+
238+ ## Contribute
239+
240+ Yes please!
241+ See [ How to Contribute to Open Source] [ contribute ] .
242+
243+ ## Security
244+
245+ This package is safe.
246+
164247## License
165248
166249[ MIT] [ license ] © [ Titus Wormer] [ author ]
@@ -185,8 +268,18 @@ Some errors are thrown when malformed “plain-text” is found, such as:
185268
186269[ npm ] : https://docs.npmjs.com/cli/install
187270
271+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
272+
273+ [ esmsh ] : https://esm.sh
274+
275+ [ typescript ] : https://www.typescriptlang.org
276+
277+ [ contribute ] : https://opensource.guide/how-to-contribute/
278+
188279[ license ] : license
189280
190281[ author ] : https://wooorm.com
191282
192283[ errors ] : #errors
284+
285+ [ emoji-emotion-example ] : https://github.com/words/emoji-emotion/blob/main/faces.txt
0 commit comments