From b5ca615a9891b341dc8e8511fd1b1992578d587d Mon Sep 17 00:00:00 2001 From: Alan Plum Date: Sun, 2 Dec 2018 22:35:24 +0100 Subject: [PATCH 1/3] Add json_ml --- types/json_ml/index.d.ts | 20 ++++++++++++++++++++ types/json_ml/json_ml-tests.ts | 12 ++++++++++++ types/json_ml/tsconfig.json | 16 ++++++++++++++++ types/json_ml/tslint.json | 1 + 4 files changed, 49 insertions(+) create mode 100644 types/json_ml/index.d.ts create mode 100644 types/json_ml/json_ml-tests.ts create mode 100644 types/json_ml/tsconfig.json create mode 100644 types/json_ml/tslint.json diff --git a/types/json_ml/index.d.ts b/types/json_ml/index.d.ts new file mode 100644 index 0000000000..ba53b84cb4 --- /dev/null +++ b/types/json_ml/index.d.ts @@ -0,0 +1,20 @@ +// Type definitions for json_ml 0.0 +// Project: https://github.com/59naga/json-ml +// Definitions by: Alan Plum +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +export type Node = string | Element; +export type SimpleElement = [string, Attributes] | [string]; +export interface NodeList extends Array {} +export type Element = SimpleElement & NodeList; +export interface Attributes { + [key: string]: string; +} +export function parse(xml: string, trim?: boolean): Node[]; +export function stringify( + object: Node[], + replacer?: ((node: Cheerio) => any) | null, + indent?: number +): string; diff --git a/types/json_ml/json_ml-tests.ts b/types/json_ml/json_ml-tests.ts new file mode 100644 index 0000000000..ecc84d9d8b --- /dev/null +++ b/types/json_ml/json_ml-tests.ts @@ -0,0 +1,12 @@ +/// +import { Attributes, Element, Node, parse, stringify } from "json_ml"; + +const nodes: Node[] = parse("
  • hello
"); +const node1: Node = nodes[0]; +if (typeof node1 !== "string") { + const el: Element = node1; + const attrs = el[1] as Attributes; + console.log(el[0], attrs); +} + +console.log(stringify(nodes, el => (el.is("li") ? null : el), 2)); diff --git a/types/json_ml/tsconfig.json b/types/json_ml/tsconfig.json new file mode 100644 index 0000000000..951405762a --- /dev/null +++ b/types/json_ml/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "json_ml-tests.ts"] +} diff --git a/types/json_ml/tslint.json b/types/json_ml/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/json_ml/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From 98c3b9c6af9d4405288399a7a6af2ab9fb7b65ae Mon Sep 17 00:00:00 2001 From: Alan Plum Date: Tue, 4 Dec 2018 15:03:02 +0100 Subject: [PATCH 2/3] Simplify types --- types/json_ml/index.d.ts | 8 +------- types/json_ml/json_ml-tests.ts | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/types/json_ml/index.d.ts b/types/json_ml/index.d.ts index ba53b84cb4..e2eb2c7af4 100644 --- a/types/json_ml/index.d.ts +++ b/types/json_ml/index.d.ts @@ -5,13 +5,7 @@ /// -export type Node = string | Element; -export type SimpleElement = [string, Attributes] | [string]; -export interface NodeList extends Array {} -export type Element = SimpleElement & NodeList; -export interface Attributes { - [key: string]: string; -} +export type Node = string | [string, ...any[]]; export function parse(xml: string, trim?: boolean): Node[]; export function stringify( object: Node[], diff --git a/types/json_ml/json_ml-tests.ts b/types/json_ml/json_ml-tests.ts index ecc84d9d8b..05ca64da4a 100644 --- a/types/json_ml/json_ml-tests.ts +++ b/types/json_ml/json_ml-tests.ts @@ -1,12 +1,20 @@ /// -import { Attributes, Element, Node, parse, stringify } from "json_ml"; +import { Node, parse, stringify } from "json_ml"; -const nodes: Node[] = parse("
  • hello
"); -const node1: Node = nodes[0]; -if (typeof node1 !== "string") { - const el: Element = node1; - const attrs = el[1] as Attributes; - console.log(el[0], attrs); -} +const nodes: Node[] = parse( + '
  • First Item
  • Second Item
  • ThirdItem
' +); +stringify(nodes, el => (el.is("li") ? null : el), 2); -console.log(stringify(nodes, el => (el.is("li") ? null : el), 2)); +stringify( + [ + [ + "ul", + ["li", { style: "color: red" }], + ["li", { title: "Some hover text", style: "color: green" }], + ["li", ["span", { class: "code-example-third" }]] + ] + ], + null, + 2 +); From 8b5e6afb2fee6cb6ae8b31845d18e4ff85bc49ba Mon Sep 17 00:00:00 2001 From: Alan Plum Date: Wed, 5 Dec 2018 23:39:57 +0100 Subject: [PATCH 3/3] Unbreak the build --- types/json_ml/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/json_ml/index.d.ts b/types/json_ml/index.d.ts index e2eb2c7af4..7cab812880 100644 --- a/types/json_ml/index.d.ts +++ b/types/json_ml/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/59naga/json-ml // Definitions by: Alan Plum // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.0 ///