Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
riteshrao committed Mar 12, 2019
2 parents 4f57d29 + 45e1a42 commit 0394120
Show file tree
Hide file tree
Showing 12 changed files with 823 additions and 350 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"@types/shortid": "0.0.29",
"events": "^3.0.0",
"jsonld": "^1.5.4",
"shortid": "^2.2.14"
"shortid": "^2.2.14",
"uri-js": "^4.2.2"
},
"nyc": {
"all": true,
Expand Down
75 changes: 73 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@ export namespace Errors {
}
}

/**
* @description Error thrown when a duplicate prefix is detected.
* @export
* @class DuplicatePrefixError
* @extends {GraphError}
*/
export class DuplicatePrefixError extends GraphError {
/**
* Creates an instance of DuplicatePrefixError.
* @param {string} prefix The duplicate prefix name.
* @memberof DuplicatePrefixError
*/
constructor(readonly prefix: string) {
super(`The prefix ${prefix} has already been defined.`);
}
}

/**
* @description Error thrown when multiple prefixes are detected for a URI
* @export
* @class DuplicatePrefixUriError
* @extends {GraphError}
*/
export class DuplicatePrefixUriError extends GraphError {
/**
* Creates an instance of DuplicatePrefixUriError.
* @param {string} prefix The prefix that has already been registered for the URI.
* @param {string} uri The URI for which a prefix has already been registered.
* @memberof DuplicatePrefixUriError
*/
constructor(readonly prefix: string, readonly uri: string) {
super(`A prefix for uri ${uri} has already been registered with prefix ${prefix}`);
}
}

/**
* @description Error thrown when an referenced context is not found.
* @export
Expand Down Expand Up @@ -116,8 +151,8 @@ export namespace Errors {
* @memberof IndexEdgeDuplicateError
*/
constructor(
public readonly label: string,
public readonly fromNodeId: string,
public readonly label: string,
public readonly fromNodeId: string,
public readonly toNodeId: string) {

super(`Duplicate edge ${label} from node ${fromNodeId} to node ${toNodeId}`);
Expand Down Expand Up @@ -184,6 +219,42 @@ export namespace Errors {
this.name = 'IndexNodeNotFoundError';
}
}

/**
* @description Error thrown when an invalid IRI is found.
* @export
* @class InvalidIriError
* @extends {GraphError}
*/
export class InvalidIriError extends GraphError {
/**
* Creates an instance of InvalidIriError.
* @param {string} iri The invalid iri string.
* @param {string} error Error details.
* @memberof InvalidIriError
*/
constructor(public readonly iri: string, error: string) {
super(`Invalid iri ${iri}. Error: ${error}`);
}
}

/**
* @description Error thrown when an invalid prefix format is found.
* @export
* @class InvalidPrefixError
* @extends {GraphError}
*/
export class InvalidPrefixError extends GraphError {
/**
* Creates an instance of InvalidPrefixError.
* @param {string} prefix The invalid prefix string.
* @param {string} error Error details.
* @memberof InvalidPrefixError
*/
constructor(public readonly prefix: string, error: string) {
super(`Invalid prefix ${prefix}. Error: ${error}`);
}
}
}

export default Errors;
2 changes: 1 addition & 1 deletion src/formatOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @export
* @interface JsonFormatOptions
*/
export interface JsonFormatOptions {
export default interface JsonFormatOptions {
/**
* @description The base URI of the document.
* @type {string}
Expand Down
27 changes: 24 additions & 3 deletions src/graph.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { EventEmitter } from 'events';

import Edge from './edge';
import GraphIndex from './graphIndex';
import Iterable from './iterable';
import Vertex, { VertexSelector, VertexFilter } from './vertex';
import { EventEmitter } from 'events';
import { StrictEventEmitter } from './eventEmitter';
import { JsonFormatOptions } from './formatOptions';
import StrictEventEmitter from './eventEmitter';
import JsonFormatOptions from './formatOptions';

interface GraphEvents {
edgeAdded: (edge: Edge) => void;
Expand Down Expand Up @@ -69,6 +70,16 @@ export class JsonldGraph extends (EventEmitter as { new(): GraphEventEmitter })
this._index.addContext(uri, context);
}

/**
* @description Adds a prefix to the graph that allows accessing and creating edges & vertices using short ids containing the prefix.
* @param {string} prefix The prefix to add.
* @param {string} uri A valid URI that the prefix maps to.
* @memberof JsonldGraph
*/
addPrefix(prefix: string, uri: string): void {
this._index.addPrefix(prefix, uri);
}

/**
* @description Creates a new vertex.
* @param {string} id Id of the vertex to create.
Expand Down Expand Up @@ -222,6 +233,16 @@ export class JsonldGraph extends (EventEmitter as { new(): GraphEventEmitter })
return this._index.removeContext(uri);
}

/**
* @description Removes a prefix previously added to the graph.
* @param {string} prefix The prefix to remove from the graph.
* @returns {void}
* @memberof JsonldGraph
*/
removePrefix(prefix: string): void {
return this._index.removePrefix(prefix);
}

/**
* @description Removes a vertex from the graph.
* @param {(string | Vertex)} vertex The vertex id or vertex instance to remove from the graph.
Expand Down
Loading

0 comments on commit 0394120

Please sign in to comment.