diff --git a/n3/index.d.ts b/n3/index.d.ts new file mode 100644 index 0000000000..4e2dc55ce0 --- /dev/null +++ b/n3/index.d.ts @@ -0,0 +1,125 @@ +// Type definitions for N3 +// Project: https://github.com/RubenVerborgh/N3.js +// Definitions by: Fred Eisele +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + + +import * as fs from "fs"; +import * as stream from "stream"; + +declare namespace N3 { + + 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: stream.Writable | N3StreamWriter): void; + } + + + 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 = N3; + diff --git a/n3/n3-tests.ts b/n3/n3-tests.ts index 0ca9c5385c..a7e529bc7d 100644 --- a/n3/n3-tests.ts +++ b/n3/n3-tests.ts @@ -1,5 +1,4 @@ -/// -/// +/// import * as N3 from "n3"; import * as fs from "fs"; @@ -63,7 +62,7 @@ function test_doc_rdf_stream_to_triples_1() { var streamParser = N3.StreamParser(); var rdfStream = fs.createReadStream('cartoons.ttl'); - // rdfStream.pipe(streamParser); + rdfStream.pipe(streamParser); streamParser.pipe(new SlowConsumer()); class SlowConsumer extends stream.Writable { @@ -107,12 +106,12 @@ function test_doc_from_triples_to_rdf_stream() { } function test_doc_from_triple_stream_to_rdf_stream() { - var streamParser = N3.StreamParser(), - // inputStream = fs.createReadStream('cartoons.ttl'), - streamWriter = N3.StreamWriter({ prefixes: { c: 'http://example.org/cartoons#' } }); - // inputStream.pipe(streamParser); + var streamParser = new N3.StreamParser(), + inputStream = fs.createReadStream('cartoons.ttl'), + streamWriter = /* new */ N3.StreamWriter({ prefixes: { c: 'http://example.org/cartoons#' } }); + inputStream.pipe(streamParser); streamParser.pipe(streamWriter); - // streamWriter.pipe(process.stdout); + streamWriter.pipe(process.stdout); } function test_doc_blank_nodes_and_lists() { @@ -168,7 +167,7 @@ function test_doc_utility() { N3Util.isLiteral('"This word is "quoted"!"'); // true N3Util.isLiteral('"3"^^http://www.w3.org/2001/XMLSchema#integer'); // true - N3.Parser().parse(' "This word is \\"quoted\\"!".', console.log); + new N3.Parser().parse(' "This word is \\"quoted\\"!".', console.log); // { subject: 'a', predicate: 'b', object: '"This word is "quoted"!"' } N3Util.createLiteral('My text', 'en-gb'); diff --git a/n3/tsconfig.json b/n3/tsconfig.json new file mode 100644 index 0000000000..53af13b215 --- /dev/null +++ b/n3/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [ + "node" + ], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "n3-tests.ts" + ] +} \ No newline at end of file