diff --git a/src/collection.js b/src/collection.js deleted file mode 100644 index e73d2fa76..000000000 --- a/src/collection.js +++ /dev/null @@ -1,48 +0,0 @@ -'use strict' -import BlankNode from './blank-node' -import ClassOrder from './class-order' -import Node from './node-internal' - -export default class Collection extends Node { - constructor (initial) { - 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)) - }) - } - } - append (element) { - return this.elements.push(element) - } - close () { - this.closed = true - return this.closed - } - shift () { - return this.elements.shift() - } - substitute (bindings) { - var elementsCopy = this.elements.map(function (ea) { - ea.substitute(bindings) - }) - return new Collection(elementsCopy) - } - toNT () { - return BlankNode.NTAnonymousNodePrefix + this.id - } - toString () { - return '(' + this.elements.join(' ') + ')' - } - unshift (element) { - return this.elements.unshift(element) - } -} -Collection.termType = 'Collection' -Collection.prototype.classOrder = ClassOrder['Collection'] -Collection.prototype.compareTerm = BlankNode.prototype.compareTerm -Collection.prototype.isVar = 0 diff --git a/src/collection.ts b/src/collection.ts new file mode 100644 index 000000000..2b9627e1c --- /dev/null +++ b/src/collection.ts @@ -0,0 +1,90 @@ +import BlankNode from './blank-node' +import ClassOrder from './class-order' +import Node from './node-internal' +import { ValueType, Bindings } from './types'; + +/** +* A collection of other RDF nodes +*/ +export default class Collection extends Node { + /** + * 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) { + 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)) + }) + } + } + /** + * 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 BlankNode.NTAnonymousNodePrefix + this.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) + } + static termType: string; +} +Collection.termType = 'Collection' +Collection.prototype.classOrder = ClassOrder['Collection'] +Collection.prototype.compareTerm = BlankNode.prototype.compareTerm +Collection.prototype.isVar = false