added @types for N3, an RDF library (#12038)

* added @types for N3, an RDF package

* changed file names to match requirements for syncing with @types

* naming recommendation conformance

* removing declare module n3, making ProperModule

* changed reference path to types and corrected errors associated with strict null

* checkpoint

* test the project with "tsc --project tsconfig.json"

* remove comments and version
This commit is contained in:
Fred Eisele
2016-10-28 09:31:00 -05:00
committed by Masahiro Wakame
parent 973846f739
commit 8e36df975e
3 changed files with 154 additions and 9 deletions

125
n3/index.d.ts vendored Normal file
View File

@@ -0,0 +1,125 @@
// Type definitions for N3
// Project: https://github.com/RubenVerborgh/N3.js
// Definitions by: Fred Eisele <https://github.com/phreed>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
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;

View File

@@ -1,5 +1,4 @@
/// <reference path="./n3.d.ts" />
/// <reference path="../node/node.d.ts" />
/// <reference types="node" />
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('<a> <b> "This word is \\"quoted\\"!".', console.log);
new N3.Parser().parse('<a> <b> "This word is \\"quoted\\"!".', console.log);
// { subject: 'a', predicate: 'b', object: '"This word is "quoted"!"' }
N3Util.createLiteral('My text', 'en-gb');

21
n3/tsconfig.json Normal file
View File

@@ -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"
]
}