diff --git a/types/plist/index.d.ts b/types/plist/index.d.ts new file mode 100644 index 0000000000..b7064e55a3 --- /dev/null +++ b/types/plist/index.d.ts @@ -0,0 +1,29 @@ +// Type definitions for plist 3.0 +// Project: https://github.com/TooTallNate/node-plist#readme +// Definitions by: Yusuke Higuchi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// +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 { } + +// PlistBuildOptions +// The instance of this type is passed to 'xmlbuilder' module as it is. +export type PlistBuildOptions = XMLToStringOptions; diff --git a/types/plist/plist-tests.ts b/types/plist/plist-tests.ts new file mode 100644 index 0000000000..a94cb4cf71 --- /dev/null +++ b/types/plist/plist-tests.ts @@ -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 = ` + + + + id + 123 + name + Alice + birthday + 2000-01-01T01:01:01Z + data + QWxpY2U= + + + id + 456 + name + Bob + birthday + 2000-12-31T23:59:59Z + data + Qm9i + + + `; + +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); diff --git a/types/plist/tsconfig.json b/types/plist/tsconfig.json new file mode 100644 index 0000000000..2d380945f9 --- /dev/null +++ b/types/plist/tsconfig.json @@ -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" + ] +} diff --git a/types/plist/tslint.json b/types/plist/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/plist/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }