5
5
[ ![ Downloads] [ downloads-badge ]] [ downloads ]
6
6
[ ![ Size] [ size-badge ]] [ size ]
7
7
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.
9
43
10
44
## Install
11
45
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] [ ] :
16
48
17
49
``` sh
18
50
npm install plain-text-data-to-json
19
51
```
20
52
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
+
21
67
## Use
22
68
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
+
23
81
``` js
24
- import fs from ' fs '
82
+ import fs from ' node:fs/promises '
25
83
import {toJson } from ' plain-text-data-to-json'
26
84
27
- var doc = fs .readFileSync (' input.txt' , ' utf8' )
85
+ const document = String (await fs .readFile (' input.txt' ))
86
+
87
+ const data = toJson (document )
28
88
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 ` :
30
93
31
- fs .writeFileSync (' output.json' , JSON .stringify (data, null , 2 ) + ' \n ' )
94
+ ``` json
95
+ [
96
+ " alpha" ,
97
+ " bravo" ,
98
+ " charlie"
99
+ ]
32
100
```
33
101
34
102
## API
35
103
36
- This package exports the following identifiers: ` toJson ` .
104
+ This package exports the identifier ` toJson ` .
37
105
There is no default export.
38
106
39
107
### ` toJson(value[, options]) `
40
108
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.
44
110
45
111
##### ` options `
46
112
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).
51
114
52
115
###### ` options.delimiter `
53
116
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: ` '%' ` )
56
124
57
125
###### ` options.forgiving `
58
126
59
127
How relaxed to be (` 'fix' ` or ` boolean ` , default: ` false ` ).
60
128
When ` 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
62
130
[ errors] [ ] ).
63
131
64
132
###### ` options.log `
65
133
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 ` ).
67
136
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
75
138
76
139
The term plain text might be confusing.
77
140
It’s actually more of some (sparingly specified) standard.
@@ -109,7 +172,7 @@ Yields:
109
172
### Empty lines
110
173
111
174
Empty lines are striped.
112
- This includes blank ( whitespace only) lines.
175
+ This includes whitespace only lines.
113
176
114
177
``` txt
115
178
%%% this file contains a value. %%%
@@ -123,7 +186,7 @@ Yields:
123
186
[' unicorn' ]
124
187
```
125
188
126
- ### Property- value pairs
189
+ ### Key/ value pairs
127
190
128
191
If a line includes a colon (by default), the library returns an object.
129
192
@@ -155,12 +218,32 @@ Yields:
155
218
156
219
Some errors are thrown when malformed “plain-text” is found, such as:
157
220
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
162
225
` forgiving: "fix" ` )
163
226
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
+
164
247
## License
165
248
166
249
[ MIT] [ license ] © [ Titus Wormer] [ author ]
@@ -185,8 +268,18 @@ Some errors are thrown when malformed “plain-text” is found, such as:
185
268
186
269
[ npm ] : https://docs.npmjs.com/cli/install
187
270
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
+
188
279
[ license ] : license
189
280
190
281
[ author ] : https://wooorm.com
191
282
192
283
[ errors ] : #errors
284
+
285
+ [ emoji-emotion-example ] : https://github.com/words/emoji-emotion/blob/main/faces.txt
0 commit comments