UNPKG

2.65 kBMarkdownView Raw
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
6A 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
13npm install simple-plist
14
15# via yarn
16yarn add simple-plist
17```
18
19## Synchronous API
20
21```js
22const plist = require("simple-plist");
23
24let data;
25
26// read
27data = plist.readFileSync("/path/to/some.plist");
28
29// write xml
30plist.writeFileSync("/path/to/plaintext.plist", data);
31
32// write binary
33plist.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
42const plist = require("simple-plist");
43
44let data;
45
46function callback(err, contents) {
47 if (err) throw err;
48 data = contents;
49}
50
51// read
52plist.readFile("/path/to/some.plist", callback);
53
54// write xml
55plist.writeFile("/path/to/plaintext.plist", data, callback);
56
57// write binary
58plist.writeBinaryFile("/path/to/binary.plist", data, callback);
59```
60
61## In Memory
62
63### `plist.stringify()`
64
65```js
66const plist = require("simple-plist");
67
68// Convert an object to a plist xml string
69plist.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
88const plist = require("simple-plist");
89
90const xml = `<plist>
91 <dict>
92 <key>name</key>
93 <string>Joe</string>
94 </dict>
95</plist>`;
96
97plist.parse(xml);
98// { "name": "Joe" }
99```
100
101## TypeScript Support
102
103All functions have typescript signatures, but there are a few handy generics
104that are worth pointing out. Those generics belong to `parse`, `readFile`,
105and `readFileSync`. Here's an example:
106
107```tsx
108import { parse, readFile, readFileSync } from "simple-plist";
109
110type Profile = {
111 name: string;
112 answer: number;
113};
114
115const 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
125const { answer } = parse<Profile>(xml);
126// answer = 42;
127
128// typed file loading
129const { name } = readFileSync<Profile>("/path/to/profile.plist");
130```