diff --git a/types/n3/index.d.ts b/types/n3/index.d.ts index c773c85a28..0ea01c1883 100644 --- a/types/n3/index.d.ts +++ b/types/n3/index.d.ts @@ -1,125 +1,110 @@ -// Type definitions for N3 +// Type definitions for N3 0.12 // Project: https://github.com/RubenVerborgh/N3.js // Definitions by: Fred Eisele +// Ruben Taelman // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// - import * as fs from "fs"; import * as stream from "stream"; -declare namespace N3 { +export type ErrorCallback = (err: Error, result: any) => void; - type ErrorCallback = (err: Error, result: any) => void; - - interface Prefixes { - [key: string]: string; - } - - interface LiteralValue { - value: string | number; - } - - interface Triple { - subject: string, - predicate: string, - object: string, - graph?: string - } - - interface BlankTriple { - predicate: string, - object: string - } - - - interface ParserConstructor { - new (options?: ParserOptions): N3Parser; - (options?: ParserOptions): N3Parser; - } - const Parser: ParserConstructor; - - interface StreamParserConstructor { - new (options?: ParserOptions): N3StreamParser; - (options?: ParserOptions): N3StreamParser; - } - const StreamParser: StreamParserConstructor; - - interface ParserOptions { - format?: string, - prefixes?: string[] - } - - interface ParseCallback { - (error: Error, triple: Triple, prefixes: Prefixes): void; - } - - interface Logger { - (message?: any, ...optionalParams: any[]): void; - } - interface N3Parser { - parse(input: string, callback: ParseCallback): void; - parse(subject: string, predicate: string, object: string): void; - parse(triple: Triple): void; - parse(stream: fs.ReadStream, log: Logger): void; - } - - interface N3StreamParser extends N3Parser, fs.WriteStream { - pipe(consumer: T): T; - } - - - interface WriterConstructor { - new (options?: WriterOptions): N3Writer; - (options?: WriterOptions): N3Writer; - - new (fd: any, options?: WriterOptions): N3Writer; - (fd: any, options?: WriterOptions): N3Writer; - } - const Writer: WriterConstructor; - - interface N3Writer { - addTriple(subject: string, predicate: string, object: string): void; - addTriple(subject: string, predicate: string, object: string[]): void; - addTriple(triple: Triple): void; - end(err?: ErrorCallback, result?: any): void; - blank(ns: string, name: string): string; - blank(triple: BlankTriple[]): string; - list(triple: string[]): string[]; - } - - function StreamWriter(options: WriterOptions): N3StreamWriter; - - interface N3StreamWriter extends N3Writer { - pipe(consumer: NodeJS.WritableStream): void; - pipe(consumer: stream.Writable): void; - } - - interface WriterOptions { - format?: string, - prefixes?: Prefixes - } - - interface N3StoreWriter extends N3Writer { - find(subject: string, predicate: string | null, object: string | null): Triple[]; - } - function Store(): N3StoreWriter; - - namespace Util { - function createLiteral(value: any): string; - function createLiteral(value: any, type: string): string; - - function isIRI(value: string): boolean; - function isLiteral(value: string): boolean; - function getLiteralValue(value: string): string; - function getLiteralLanguage(value: string): string; - function getLiteralType(value: string): string; - function isBlank(value: string): boolean; - function isPrefixedName(name: string): boolean; - function expandPrefixedName(name: string, prefixes: Prefixes): string; - } +export interface Prefixes { + [key: string]: string; } -export = N3; +export interface LiteralValue { + value: string | number; +} +export interface Triple { + subject: string; + predicate: string; + object: string; + graph?: string; +} + +export interface BlankTriple { + predicate: string; + object: string; +} + +export interface ParserConstructor { + new (options?: ParserOptions): N3Parser; + (options?: ParserOptions): N3Parser; +} +export const Parser: ParserConstructor; + +export interface StreamParserConstructor { + new (options?: ParserOptions): N3StreamParser; + (options?: ParserOptions): N3StreamParser; +} +export const StreamParser: StreamParserConstructor; + +export interface ParserOptions { + format?: string; + prefixes?: string[]; +} + +export type ParseCallback = (error: Error, triple: Triple, prefixes: Prefixes) => void; + +export type Logger = (message?: any, ...optionalParams: any[]) => void; + +export interface N3Parser { + parse(input: string, callback: ParseCallback): void; + parse(subject: string, predicate: string, object: string): void; + parse(triple: Triple): void; + parse(stream: fs.ReadStream, log: Logger): void; +} + +export interface N3StreamParser extends N3Parser, fs.WriteStream { + pipe(consumer: T): T; +} + +export interface WriterConstructor { + new (options?: WriterOptions): N3Writer; + new (fd: any, options?: WriterOptions): N3Writer; + (options?: WriterOptions): N3Writer; + (fd: any, options?: WriterOptions): N3Writer; +} +export const Writer: WriterConstructor; + +export interface N3Writer { + addTriple(subject: string, predicate: string, object: string | string[]): void; + addTriple(triple: Triple): void; + end(err?: ErrorCallback, result?: any): void; + blank(ns: string, name: string): string; + blank(triple: BlankTriple[]): string; + list(triple: string[]): string[]; +} + +export function StreamWriter(options: WriterOptions): N3StreamWriter; + +export interface N3StreamWriter extends N3Writer { + pipe(consumer: NodeJS.WritableStream | stream.Writable): void; +} + +export interface WriterOptions { + format?: string; + prefixes?: Prefixes; +} + +export interface N3StoreWriter extends N3Writer { + find(subject: string, predicate: string | null, object: string | null): Triple[]; +} +export function Store(): N3StoreWriter; + +export namespace Util { + function createLiteral(value: any, type?: string): string; + + function isIRI(value: string): boolean; + function isLiteral(value: string): boolean; + function getLiteralValue(value: string): string; + function getLiteralLanguage(value: string): string; + function getLiteralType(value: string): string; + function isBlank(value: string): boolean; + function isPrefixedName(name: string): boolean; + function expandPrefixedName(name: string, prefixes: Prefixes): string; +} diff --git a/types/n3/n3-tests.ts b/types/n3/n3-tests.ts index d3c664c61e..275e0fc039 100644 --- a/types/n3/n3-tests.ts +++ b/types/n3/n3-tests.ts @@ -3,8 +3,7 @@ import * as fs from "fs"; import * as stream from "stream"; function test_serialize() { - - var writer: N3.N3Writer = N3.Writer( + const writer: N3.N3Writer = N3.Writer( { format: "ttl", prefixes: { @@ -24,56 +23,56 @@ function test_serialize() { }); } -/** +/* The following tests are taken from ... https://github.com/RubenVerborgh/N3.js/blob/master/README.md */ function test_doc_rdf_to_triples_1() { - var parser = N3.Parser(); - parser.parse('@prefix c: .\n' + - 'c:Tom a c:Cat.\n' + - 'c:Jerry a c:Mouse;\n' + - ' c:smarterThan c:Tom.', - function (error: Error, triple: N3.Triple, prefixes: N3.Prefixes) { + const parser = N3.Parser(); + parser.parse(`@prefix c: . + c:Tom a c:Cat. + c:Jerry a c:Mouse; + c:smarterThan c:Tom.`, + (error: Error, triple: N3.Triple, prefixes: N3.Prefixes) => { if (triple) console.log(triple.subject, triple.predicate, triple.object, '.'); else - console.log("# That's all, folks!", prefixes) + console.log("# That's all, folks!", prefixes); }); } function test_doc_rdf_to_triples_2() { - var parser1 = N3.Parser({ format: 'N-Triples' }); - var parser2 = N3.Parser({ format: 'application/trig' }); + const parser1 = N3.Parser({ format: 'N-Triples' }); + const parser2 = N3.Parser({ format: 'application/trig' }); // Notation3 (N3) is supported only through the format argument: - var parser3 = N3.Parser({ format: 'N3' }); - var parser4 = N3.Parser({ format: 'Notation3' }); - var parser5 = N3.Parser({ format: 'text/n3' }); + const parser3 = N3.Parser({ format: 'N3' }); + const parser4 = N3.Parser({ format: 'Notation3' }); + const parser5 = N3.Parser({ format: 'text/n3' }); } function test_doc_rdf_stream_to_triples_1() { - var parser = N3.Parser(); - var rdfStream = fs.createReadStream('cartoons.ttl'); - parser.parse(rdfStream, console.log); + const parser = N3.Parser(); + const rdfStream1 = fs.createReadStream('cartoons.ttl'); + parser.parse(rdfStream1, console.log); - var streamParser = N3.StreamParser(); - var rdfStream = fs.createReadStream('cartoons.ttl'); - rdfStream.pipe(streamParser); + const streamParser = N3.StreamParser(); + const rdfStream2 = fs.createReadStream('cartoons.ttl'); + rdfStream2.pipe(streamParser); streamParser.pipe(new class SlowConsumer extends stream.Writable { constructor() { super({ objectMode: true }); - this._write = function (triple, encoding, done) { + this._write = (triple, encoding, done) => { console.log(triple); setTimeout(done, 1000); }; } - }); + }()); } function test_doc_from_triples_to_string() { - var writer = N3.Writer({ prefixes: { c: 'http://example.org/cartoons#' } }); + const writer = N3.Writer({ prefixes: { c: 'http://example.org/cartoons#' } }); writer.addTriple('http://example.org/cartoons#Tom', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'http://example.org/cartoons#Cat'); @@ -82,14 +81,14 @@ function test_doc_from_triples_to_string() { predicate: 'http://example.org/cartoons#name', object: '"Tom"' }); - writer.end(function (error, result) { console.log(result); }); + writer.end((error, result) => { console.log(result); }); - var writer1 = N3.Writer({ format: 'N-Triples' }); - var writer2 = N3.Writer({ format: 'application/trig' }); + const writer1 = N3.Writer({ format: 'N-Triples' }); + const writer2 = N3.Writer({ format: 'application/trig' }); } function test_doc_from_triples_to_rdf_stream() { - var writer = N3.Writer(process.stdout, { prefixes: { c: 'http://example.org/cartoons#' } }); + const writer = N3.Writer(process.stdout, { prefixes: { c: 'http://example.org/cartoons#' } }); writer.addTriple('http://example.org/cartoons#Tom', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'http://example.org/cartoons#Cat'); @@ -102,16 +101,16 @@ function test_doc_from_triples_to_rdf_stream() { } function test_doc_from_triple_stream_to_rdf_stream() { - var streamParser = new N3.StreamParser(), - inputStream = fs.createReadStream('cartoons.ttl'), - streamWriter = /* new */ N3.StreamWriter({ prefixes: { c: 'http://example.org/cartoons#' } }); + const streamParser = new N3.StreamParser(); + const inputStream = fs.createReadStream('cartoons.ttl'); + const streamWriter = /* new */ N3.StreamWriter({ prefixes: { c: 'http://example.org/cartoons#' } }); inputStream.pipe(streamParser); streamParser.pipe(streamWriter); streamWriter.pipe(process.stdout); } function test_doc_blank_nodes_and_lists() { - var writer = N3.Writer({ + const writer = N3.Writer({ prefixes: { c: 'http://example.org/cartoons#', foaf: 'http://xmlns.com/foaf/0.1/' @@ -135,20 +134,20 @@ function test_doc_blank_nodes_and_lists() { 'http://example.org/cartoons#Tom', 'http://example.org/cartoons#Jerry' ])); - writer.end(function (error, result) { console.log(result); }); + writer.end((error, result) => { console.log(result); }); } function test_doc_storing() { - var store = N3.Store(); + const store = N3.Store(); store.addTriple('http://ex.org/Pluto', 'http://ex.org/type', 'http://ex.org/Dog'); store.addTriple('http://ex.org/Mickey', 'http://ex.org/type', 'http://ex.org/Mouse'); - var mickey = store.find('http://ex.org/Mickey', null, null)[0]; + const mickey = store.find('http://ex.org/Mickey', null, null)[0]; console.log(mickey.subject, mickey.predicate, mickey.object, '.'); } function test_doc_utility() { - var N3Util = N3.Util; + const N3Util = N3.Util; N3Util.isIRI('http://example.org/cartoons#Mickey'); // true N3Util.isLiteral('"Mickey Mouse"'); // true @@ -175,7 +174,7 @@ function test_doc_utility() { N3Util.isIRI('_:b1'); // false N3Util.isLiteral('_:b1'); // false - var prefixes: N3.Prefixes = { rdfs: 'http://www.w3.org/2000/01/rdf-schema#' }; + const prefixes: N3.Prefixes = { rdfs: 'http://www.w3.org/2000/01/rdf-schema#' }; N3Util.isPrefixedName('rdfs:label'); // true; N3Util.expandPrefixedName('rdfs:label', prefixes); // http://www.w3.org/2000/01/rdf-schema#label } diff --git a/types/n3/tsconfig.json b/types/n3/tsconfig.json index 4ba121903d..11e61e58e3 100644 --- a/types/n3/tsconfig.json +++ b/types/n3/tsconfig.json @@ -21,4 +21,4 @@ "index.d.ts", "n3-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/n3/tslint.json b/types/n3/tslint.json index a41bf5d19a..3db14f85ea 100644 --- a/types/n3/tslint.json +++ b/types/n3/tslint.json @@ -1,79 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } -} +{ "extends": "dtslint/dt.json" }