From 63bd337091ad0f6bc45d7a38f53715142fc7393a Mon Sep 17 00:00:00 2001 From: higuri Date: Thu, 20 Sep 2018 21:50:49 +0900 Subject: [PATCH 1/2] Add type definitions for 'plist' --- types/plist/index.d.ts | 30 ++++++++++++++ types/plist/plist-tests.ts | 81 ++++++++++++++++++++++++++++++++++++++ types/plist/tsconfig.json | 23 +++++++++++ types/plist/tslint.json | 1 + 4 files changed, 135 insertions(+) create mode 100644 types/plist/index.d.ts create mode 100644 types/plist/plist-tests.ts create mode 100644 types/plist/tsconfig.json create mode 100644 types/plist/tslint.json diff --git a/types/plist/index.d.ts b/types/plist/index.d.ts new file mode 100644 index 0000000000..def21edb10 --- /dev/null +++ b/types/plist/index.d.ts @@ -0,0 +1,30 @@ +// 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 + +/// + +// 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. +// So we want to import this type from '@types/xmlbuilder', +// but the type is not declared there... +export type PlistBuildOptions = object; diff --git a/types/plist/plist-tests.ts b/types/plist/plist-tests.ts new file mode 100644 index 0000000000..deaa0f9d50 --- /dev/null +++ b/types/plist/plist-tests.ts @@ -0,0 +1,81 @@ +import * as fs from "fs"; +import plist = require("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" } From d01bb09b8679c0fde321c91a61b0efa0bd2cfded Mon Sep 17 00:00:00 2001 From: higuri Date: Sun, 23 Sep 2018 23:27:06 +0900 Subject: [PATCH 2/2] Import type from xmlbuilder --- types/plist/index.d.ts | 7 +++---- types/plist/plist-tests.ts | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/types/plist/index.d.ts b/types/plist/index.d.ts index def21edb10..b7064e55a3 100644 --- a/types/plist/index.d.ts +++ b/types/plist/index.d.ts @@ -5,6 +5,7 @@ // TypeScript Version: 2.2 /// +import { XMLToStringOptions } from 'xmlbuilder'; // plist export as namespace plist; @@ -24,7 +25,5 @@ export interface PlistObject { export interface PlistArray extends Array { } // PlistBuildOptions -// The instance of this type is passed to 'xmlbuilder' module as it is. -// So we want to import this type from '@types/xmlbuilder', -// but the type is not declared there... -export type PlistBuildOptions = object; +// 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 index deaa0f9d50..a94cb4cf71 100644 --- a/types/plist/plist-tests.ts +++ b/types/plist/plist-tests.ts @@ -1,5 +1,5 @@ import * as fs from "fs"; -import plist = require("plist"); +import * as plist from "plist"; /// /// Typing for test