mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 06:47:03 +00:00
Merge pull request #29049 from higuri/plist
Add type definitions for 'plist'
This commit is contained in:
Vendored
+29
@@ -0,0 +1,29 @@
|
||||
// Type definitions for plist 3.0
|
||||
// Project: https://github.com/TooTallNate/node-plist#readme
|
||||
// Definitions by: Yusuke Higuchi <https://github.com/higuri>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
/// <reference types="node" />
|
||||
import { XMLToStringOptions } from 'xmlbuilder';
|
||||
|
||||
// plist
|
||||
export as namespace plist;
|
||||
|
||||
// plist.parse()
|
||||
export function parse(xml: string): PlistValue;
|
||||
// plist.build()
|
||||
export function build(obj: PlistValue, opts?: PlistBuildOptions): string;
|
||||
|
||||
// PlistValue
|
||||
export type PlistValue =
|
||||
string | number | boolean |
|
||||
Date | Buffer | PlistObject | PlistArray;
|
||||
export interface PlistObject {
|
||||
[x: string]: PlistValue;
|
||||
}
|
||||
export interface PlistArray extends Array<PlistValue> { }
|
||||
|
||||
// PlistBuildOptions
|
||||
// The instance of this type is passed to 'xmlbuilder' module as it is.
|
||||
export type PlistBuildOptions = XMLToStringOptions;
|
||||
@@ -0,0 +1,81 @@
|
||||
import * as fs from "fs";
|
||||
import * as plist from "plist";
|
||||
|
||||
///
|
||||
/// Typing for test
|
||||
///
|
||||
|
||||
// Person
|
||||
interface Person {
|
||||
id: number;
|
||||
name: string;
|
||||
birthday: Date;
|
||||
data: Buffer;
|
||||
}
|
||||
|
||||
// instanceOfPerson(obj)
|
||||
function instanceOfPerson(obj: any): obj is Person {
|
||||
return (
|
||||
"id" in obj && typeof obj.id === "number" &&
|
||||
"name" in obj && typeof obj.name === "string" &&
|
||||
"birthday" in obj && obj.birthday instanceof Date &&
|
||||
"data" in obj && obj.data instanceof Buffer);
|
||||
}
|
||||
|
||||
///
|
||||
/// parse()
|
||||
///
|
||||
|
||||
// parsing a plist from string
|
||||
const plistString = `
|
||||
<plist>
|
||||
<array>
|
||||
<dict>
|
||||
<key>id</key>
|
||||
<integer>123</integer>
|
||||
<key>name</key>
|
||||
<string>Alice</string>
|
||||
<key>birthday</key>
|
||||
<date>2000-01-01T01:01:01Z</date>
|
||||
<key>data</key>
|
||||
<data>QWxpY2U=</data>
|
||||
</dict>
|
||||
<dict>
|
||||
<key>id</key>
|
||||
<integer>456</integer>
|
||||
<key>name</key>
|
||||
<string>Bob</string>
|
||||
<key>birthday</key>
|
||||
<date>2000-12-31T23:59:59Z</date>
|
||||
<key>data</key>
|
||||
<data>Qm9i</data>
|
||||
</dict>
|
||||
</array>
|
||||
</plist>`;
|
||||
|
||||
const plistValue1 = plist.parse(plistString);
|
||||
if (plistValue1 instanceof Array) {
|
||||
for (const elem of plistValue1) {
|
||||
if (instanceOfPerson(elem)) {
|
||||
console.log(`id: ${elem.id}`);
|
||||
console.log(`name: ${elem.name}`);
|
||||
console.log(`birthday: ${elem.birthday.toDateString()}`);
|
||||
console.log(`data: ${elem.data.toString()}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// parsing a plist from filename
|
||||
const plistValue2 = plist.parse(
|
||||
fs.readFileSync("plist/sample.plist", "utf8"));
|
||||
console.log(plistValue2);
|
||||
|
||||
///
|
||||
/// build()
|
||||
///
|
||||
|
||||
const plistString1 = plist.build(plistValue1);
|
||||
console.log(plistString1);
|
||||
|
||||
const plistString2 = plist.build(plistValue2, {pretty: false});
|
||||
console.log(plistString2);
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"strictFunctionTypes": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"plist-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user