1 | # `simple-plist`
|
2 |
|
3 | [![npm](https://img.shields.io/npm/dw/simple-plist.svg?style=popout&logo=npm)](https://www.npmjs.org/package/simple-plist)
|
4 | [![npm](https://img.shields.io/npm/v/simple-plist.svg?style=popout&logo=npm)](https://www.npmjs.com/package/simple-plist)
|
5 |
|
6 | A simple API for interacting with binary and plain text
|
7 | [plist](https://en.wikipedia.org/wiki/Property_list) data.
|
8 |
|
9 | ## Installation
|
10 |
|
11 | ```sh
|
12 | # via npm
|
13 | npm install simple-plist
|
14 |
|
15 | # via yarn
|
16 | yarn add simple-plist
|
17 | ```
|
18 |
|
19 | ## Synchronous API
|
20 |
|
21 | ```js
|
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);
|
34 | ```
|
35 |
|
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).
|
40 |
|
41 | ```js
|
42 | const plist = require("simple-plist");
|
43 |
|
44 | let data;
|
45 |
|
46 | function callback(err, contents) {
|
47 | if (err) throw err;
|
48 | data = contents;
|
49 | }
|
50 |
|
51 | // read
|
52 | plist.readFile("/path/to/some.plist", callback);
|
53 |
|
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);
|
59 | ```
|
60 |
|
61 | ## In Memory
|
62 |
|
63 | ### `plist.stringify()`
|
64 |
|
65 | ```js
|
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>`;
|
123 |
|
124 | // typed string parsing
|
125 | const { answer } = parse<Profile>(xml);
|
126 | // answer = 42;
|
127 |
|
128 | // typed file loading
|
129 | const { name } = readFileSync<Profile>("/path/to/profile.plist");
|
130 | ```
|