Skip to content

Commit

Permalink
Data factory types fix linkeddata#355
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Nov 4, 2019
1 parent e7a8cb9 commit aab6b50
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 146 deletions.
32 changes: 24 additions & 8 deletions src/data-factory-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
PredicateType,
ObjectType,
GraphType,
TermType,
TFDataFactory,
} from './types'
import { Feature, IdentityFactory } from './data-factory-type'
import { Feature, IdentityFactory, Indexable } from './data-factory-type'
import { Node } from './index'

export const defaultGraphURI = 'chrome:theSession'
Expand All @@ -35,7 +37,7 @@ function defaultGraph(): NamedNode {
*
* Equivalent to {Term.hashString}
*/
function id (term: Node) {
function id (term: Node): string | undefined {
if (!term) {
return term
}
Expand All @@ -47,13 +49,13 @@ function id (term: Node) {
}

switch (term.termType) {
case "NamedNode":
case TermType.NamedNode:
return '<' + term.value + '>'
case "BlankNode":
case TermType.BlankNode:
return '_:' + term.value
case "Literal":
return Literal.toNT(term)
case "Variable":
case TermType.Literal:
return Literal.toNT(term as Literal)
case TermType.Variable:
return Variable.toString(term)
default:
return undefined
Expand Down Expand Up @@ -118,7 +120,21 @@ function variable(name?: string): Variable {
return new Variable(name)
}

const CanonicalDataFactory: IdentityFactory = {
const CanonicalDataFactory: TFDataFactory<
NamedNode,
BlankNode,
Literal,
SubjectType,
PredicateType,
ObjectType,
GraphType,
// DefaultGraph is:
NamedNode | BlankNode,
Statement
> & IdentityFactory <
Statement,
NamedNode | BlankNode | Literal | Variable
> = {
blankNode,
defaultGraph,
literal,
Expand Down
53 changes: 31 additions & 22 deletions src/data-factory-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TFNamedNode, TFBlankNode, TFLiteral, TFQuad, TFTerm, TFDataFactory } from "./types"
import { TFNamedNode, TFBlankNode, TFLiteral, TFQuad, TFTerm, TFDataFactory, TFDefaultGraph, TFSubject, TFPredicate, TFObject, TFGraph } from "./types"

/**
* Defines a strict subset of the DataFactory as defined in the RDF/JS: Data model specification
Expand All @@ -9,8 +9,28 @@ export interface DataFactory<
NamedNode extends TFNamedNode = TFNamedNode,
BlankNode extends TFBlankNode = TFBlankNode,
Literal extends TFLiteral = TFLiteral,
FactoryTypes = NamedNode | TFBlankNode | Literal | TFQuad
> extends TFDataFactory {
Quad = TFQuad,
FactoryTypes = NamedNode | TFBlankNode | Literal | Quad,
Subject = TFSubject,
Predicate = TFPredicate,
Object = TFObject,
Graph = TFGraph,
DefaultGraph = NamedNode | BlankNode,
> extends TFDataFactory<
NamedNode,
BlankNode,
Literal,
Subject,
Predicate,
Object,
Graph,
DefaultGraph,
Quad
> {
/**
* BlankNode index
* @private
*/
bnIndex?: number

supports: SupportTable
Expand All @@ -19,30 +39,20 @@ export interface DataFactory<

literal(value: unknown): Literal

defaultGraph(): NamedNode | BlankNode

quad(
subject: NamedNode | BlankNode,
predicate: NamedNode,
object: NamedNode | BlankNode | Literal,
graph?: NamedNode
): TFQuad

isQuad(obj: any): obj is TFQuad

fromTerm(original: Literal | TFTerm): TFTerm

fromQuad(original: TFQuad): TFQuad
isQuad(obj: any): obj is Quad

equals(a: Comparable, b: Comparable): boolean

toNQ(term: FactoryTypes): string
}

export type TFIDFactoryTypes = TFNamedNode | TFBlankNode | TFLiteral | TFQuad

export interface IdentityFactory<
Quad = TFQuad,
IDFactoryTypes = TFNamedNode | TFBlankNode | TFLiteral | Quad,
IndexType = Indexable,
FactoryTypes = TFNamedNode | TFBlankNode | TFLiteral | TFQuad
> extends DataFactory<FactoryTypes> {
> {
/**
* Generates a unique session-idempotent identifier for the given object.
*
Expand All @@ -51,8 +61,7 @@ export interface IdentityFactory<
*
* @return {Indexable} A unique value which must also be a valid JS object key type.
*/
id(obj: FactoryTypes): IndexType | unknown

id(obj: IDFactoryTypes): IndexType
}

/**
Expand All @@ -65,7 +74,7 @@ export interface IdentityFactory<
export interface ReversibleIdentityFactory<
IndexType = Indexable,
FactoryTypes = TFNamedNode | TFBlankNode | TFLiteral | TFQuad
> extends IdentityFactory<FactoryTypes> {
> extends IdentityFactory<Indexable, FactoryTypes> {
fromId(id: IndexType): FactoryTypes;
}

Expand Down
Loading

0 comments on commit aab6b50

Please sign in to comment.