22
33[ ![ npm] ( https://img.shields.io/npm/dw/simple-plist.svg?style=popout&logo=npm )] ( https://www.npmjs.org/package/simple-plist )
44[ ![ npm] ( https://img.shields.io/npm/v/simple-plist.svg?style=popout&logo=npm )] ( https://www.npmjs.com/package/simple-plist )
5- [ ![ Travis (.com) branch] ( https://img.shields.io/travis/com/wollardj/node-simple-plist/develop.svg?style=popout&logo=Travis%20CI )] ( https://travis-ci.com/wollardj/node-simple-plist )
65
7- A simple API for interacting with binary and plain text plist data.
6+ A simple API for interacting with binary and plain text
7+ [ plist] ( https://en.wikipedia.org/wiki/Property_list ) data.
88
99## Installation
1010
@@ -16,63 +16,115 @@ npm install simple-plist
1616yarn add simple-plist
1717```
1818
19- ## Reading Data
19+ ## Synchronous API
2020
2121``` js
22- var plist = require (" simple-plist" );
23-
24- // Read data from a file (xml or binary) (asynchronous)
25- plist .readFile (" /path/to/some.plist" , function (err , data ) {
26- if (err) {
27- throw err;
28- }
29- console .log (JSON .stringify (data));
30- });
31-
32- // Read data from a file (xml or binary) (synchronous)
33- var data = plist .readFileSync (" /path/to/some.plist" );
34- console .log (JSON .stringify (data));
22+ const plist = require (" simple-plist" );
23+
24+ let data;
25+
26+ // read
27+ data = plist .readFileSync (" /path/to/some.plist" );
28+
29+ // write xml
30+ plist .writeFileSync (" /path/to/plaintext.plist" , data);
31+
32+ // write binary
33+ plist .writeBinaryFileSync (" /path/to/binary.plist" , data);
3534```
3635
37- ## Writing Data
36+ ## Asynchronous API
37+
38+ > Note: all of the async examples can optionally be converted to promises using
39+ > node's [ ` util.promisify ` ] ( https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original ) .
3840
3941``` js
40- var plist = require (" simple-plist" ),
41- data = plist .readFileSync (" /path/to/some.plist" );
42+ const plist = require (" simple-plist" );
4243
43- // Write data to a xml file (asynchronous)
44- plist .writeFile (" /path/to/plaintext.plist" , data, function (err ) {
45- if (err) {
46- throw err;
47- }
48- });
44+ let data;
4945
50- // Write data to a xml file (synchronous)
51- plist .writeFileSync (" /path/to/plaintext.plist" , data);
46+ function callback (err , contents ) {
47+ if (err) throw err;
48+ data = contents;
49+ }
5250
53- // Write data to a binary plist file (asynchronous)
54- plist .writeBinaryFile (" /path/to/binary.plist" , data, function (err ) {
55- if (err) {
56- throw err;
57- }
58- });
51+ // read
52+ plist .readFile (" /path/to/some.plist" , callback);
5953
60- // Write data to a binary plist file (synchronous)
61- plist .writeBinaryFileSync (" /path/to/binary.plist" , data);
54+ // write xml
55+ plist .writeFile (" /path/to/plaintext.plist" , data, callback);
56+
57+ // write binary
58+ plist .writeBinaryFile (" /path/to/binary.plist" , data, callback);
6259```
6360
64- ## Mutating Plists In Memory
61+ ## In Memory
62+
63+ ### ` plist.stringify() `
6564
6665``` js
67- var plist = require (" simple-plist" );
66+ const plist = require (" simple-plist" );
67+
68+ // Convert an object to a plist xml string
69+ plist .stringify ({ name: " Joe" , answer: 42 });
70+
71+ /*
72+ <?xml version="1.0" encoding="UTF-8"?>
73+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
74+ <plist version="1.0">
75+ <dict>
76+ <key>name</key>
77+ <string>Joe</string>
78+ <key>answer</key>
79+ <integer>42</integer>
80+ </dict>
81+ </plist>
82+ */
83+ ```
84+
85+ ### ` plist.parse() `
86+
87+ ``` js
88+ const plist = require (" simple-plist" );
89+
90+ const xml = ` <plist>
91+ <dict>
92+ <key>name</key>
93+ <string>Joe</string>
94+ </dict>
95+ </plist>` ;
96+
97+ plist .parse (xml);
98+ // { "name": "Joe" }
99+ ```
100+
101+ ## TypeScript Support
102+
103+ All functions have typescript signatures, but there are a few handy generics
104+ that are worth pointing out. Those generics belong to ` parse ` , ` readFile ` ,
105+ and ` readFileSync ` . Here's an example:
106+
107+ ``` tsx
108+ import { parse , readFile , readFileSync } from " simple-plist" ;
109+
110+ type Profile = {
111+ name: string ;
112+ answer: number ;
113+ };
114+
115+ const xml = ` <plist>
116+ <dict>
117+ <key>name</key>
118+ <string>Joe</string>
119+ <key>answer</key>
120+ <integer>42</integer>
121+ </dict>
122+ </plist> ` ;
68123
69- // Convert a Javascript object to a plist xml string
70- var xml = plist . stringify ({ name : " Joe " , answer: 42 } );
71- console . log (xml); // output is a valid plist xml string
124+ // typed string parsing
125+ const { answer } = parse < Profile >( xml );
126+ // answer = 42;
72127
73- // Convert a plist xml string or a binary plist buffer to a Javascript object
74- var data = plist .parse (
75- " <plist><dict><key>name</key><string>Joe</string></dict></plist>"
76- );
77- console .log (JSON .stringify (data));
128+ // typed file loading
129+ const { name } = readFileSync <Profile >(" /path/to/profile.plist" );
78130```
0 commit comments