Skip to content

Commit

Permalink
Typescript rebase WIP linkeddata#355
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Oct 22, 2019
1 parent 524288e commit 7724373
Show file tree
Hide file tree
Showing 29 changed files with 2,677 additions and 654 deletions.
291 changes: 153 additions & 138 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
{
"name": "Daniel Friedman",
"url": "https://github.com/dan-f/"
},
{
"name": "Cénotélie",
"url": "https://github.com/cenotelie/"
},
{
"name": "Joep Meindertsma",
"url": "https://github.com/joepio/"
}
],
"license": "MIT",
Expand Down Expand Up @@ -49,13 +57,13 @@
"@babel/register": "^7.5.5",
"@types/chai": "^4.2.3",
"@types/express": "^4.17.1",
"@types/jsonld": "^1.5.0",
"@types/mocha": "^5.2.7",
"babel-loader": "^8.0.6",
"chai": "^4.2.0",
"diff": "^4.0.1",
"dirty-chai": "^2.0.1",
"fs-grep": "^0.0.5",
"jsdoc": "^3.6.3",
"mocha": "^6.2.0",
"nock": "^10.0.6",
"node-fetch": "^2.6.0",
Expand All @@ -64,6 +72,7 @@
"sinon": "^7.4.1",
"sinon-chai": "^3.3.0",
"source-map-loader": "^0.2.4",
"typedoc": "^0.15.0",
"typescript": "^3.6.3",
"webpack": "^4.39.2",
"webpack-cli": "^3.3.6",
Expand All @@ -74,7 +83,7 @@
"build": "babel src --extensions \".ts,.js\" -d lib",
"build:browser": "webpack --progress",
"build:types": "tsc --emitDeclarationOnly -d --declarationDir lib --allowJs false",
"doc": "rm -r doc ; jsdoc -d doc README.md src/*.js",
"doc": "rm -r doc ; typedoc",
"prepare": "npm run build && npm run build:browser",
"start": "webpack-dev-server --https --port 4800",
"test": "npm run test:unit && npm run test:serialize",
Expand Down
48 changes: 41 additions & 7 deletions src/blank-node.js → src/blank-node.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
'use strict'
import ClassOrder from './class-order'
import Node from './node-internal'
import { IndexedFormula } from './index';
import { TermType, BlankNodeTermType, TFBlankNode } from './types';

export default class BlankNode extends Node {
constructor (id) {
/**
* An RDF blank node is a Node without a URI
* @link https://rdf.js.org/data-model-spec/#blanknode-interface
*/
export default class BlankNode extends Node implements TFBlankNode {

termType: BlankNodeTermType;
static termType: BlankNodeTermType;

/**
* The identifier for the blank node
*/
id: string;

/**
* Whether this is a blank node
*/
isBlank: boolean;

/**
* The next unique identifier for blank nodes
*/
static nextId: number;
static NTAnonymousNodePrefix: string;

/**
* Initializes this node
* @param id The identifier for the blank node
*/
constructor (id?: string) {
super()
this.termType = BlankNode.termType
this.isBlank = true

if (id) {
if (typeof id !== 'string') {
Expand Down Expand Up @@ -42,7 +72,11 @@ export default class BlankNode extends Node {
return 0
}

copy (formula) { // depends on the formula
/**
* Gets a copy of this blank node in the specified formula
* @param formula The formula
*/
copy (formula: IndexedFormula): BlankNode { // depends on the formula
var bnodeNew = new BlankNode()
formula.copyTo(this, bnodeNew)
return bnodeNew
Expand All @@ -58,8 +92,8 @@ export default class BlankNode extends Node {
}

BlankNode.nextId = 0
BlankNode.termType = 'BlankNode'
BlankNode.termType = TermType.BlankNode
BlankNode.NTAnonymousNodePrefix = '_:'
BlankNode.prototype.classOrder = ClassOrder['BlankNode']
BlankNode.prototype.isBlank = 1
BlankNode.prototype.isVar = 1
BlankNode.prototype.isBlank = true
BlankNode.prototype.isVar = true
9 changes: 8 additions & 1 deletion src/class-order.js → src/class-order.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export default {
/**
* Class orders
*/
const ClassOrder: {
[id: string]: number;
} = {
'Literal': 1,
'Collection': 3,
'Graph': 4,
'NamedNode': 5,
'BlankNode': 6,
'Variable': 7
}

export default ClassOrder
51 changes: 0 additions & 51 deletions src/collection.js

This file was deleted.

109 changes: 109 additions & 0 deletions src/collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import BlankNode from './blank-node'
import ClassOrder from './class-order'
import Node from './node-internal'
import { ValueType, Bindings, TFTerm, CollectionTermType, TermType } from './types';

export function isCollection<T>(value: T | TFTerm): value is Collection {
return (value as Node).termType === 'Collection'
}

/**
* A collection of other RDF nodes
*/
export default class Collection extends Node {

static termType: CollectionTermType;

/**
* The identifier for this collection
*/
id: number;

/**
* The nodes in this collection
*/

elements: Node[];

/**
* Whether this collection is closed
*/
closed: boolean;

/**
* Initializes this collection
* @param initial The initial elements
*/
constructor(initial?: ReadonlyArray<ValueType>) {
super()
this.termType = Collection.termType
this.id = BlankNode.nextId++
this.elements = []
this.closed = false
if (initial && initial.length > 0) {
initial.forEach(element => {
this.elements.push(Node.fromValue(element) as Node)
})
}
}

/**
* Appends an element to this collection
* @param element The new element
*/
append (element: Node): number {
return this.elements.push(element)
}

/**
* Closes this collection
*/
close (): boolean {
this.closed = true
return this.closed
}

/**
* Removes the first element from the collection (and return it)
*/
shift (): Node | undefined {
return this.elements.shift()
}

/**
* Gets a new Collection with the substituting bindings applied
* @param bindings The bindings to substitute
*/
substitute(bindings: Bindings): Collection {
var elementsCopy = this.elements.map(function (ea) {
ea.substitute(bindings)
})
return new Collection(elementsCopy as [])
}
toNT () {
return Collection.toNT(this)
}
static toNT (collection) {
return BlankNode.NTAnonymousNodePrefix + collection.id
}

/**
* Serializes the collection to a string.
* Surounded by (parantheses) and seperated by spaces.
*/
toString () {
return '(' + this.elements.join(' ') + ')'
}

/**
* Preprends the specified element to the colelction's front
* @param element The element to preprend
*/
unshift (element: Node): number {
return this.elements.unshift(element)
}
}
Collection.termType = TermType.Collection
Collection.prototype.classOrder = ClassOrder['Collection']
Collection.prototype.compareTerm = BlankNode.prototype.compareTerm
Collection.prototype.isVar = false
13 changes: 0 additions & 13 deletions src/default-graph.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/default-graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Node from './node-internal'
import { TFDefaultGraph } from './types';

export default class DefaultGraph extends Node implements TFDefaultGraph {
value: ''

constructor () {
super()
this.termType = 'DefaultGraph'
this.value = ''
}
toCanonical () {
return this.value
}
}
10 changes: 6 additions & 4 deletions src/empty.js → src/empty.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict'
import Node from './node'
import Node from './node-internal'

/**
* Singleton subclass of an empty Collection.
*/
* An empty node
*/
export default class Empty extends Node {

static termType: 'empty'

constructor () {
super()
this.termType = Empty.termType
Expand Down
Loading

0 comments on commit 7724373

Please sign in to comment.