link-rdflib
Version:
an RDF library for node.js, patched for speed.
68 lines (62 loc) • 1.62 kB
JavaScript
'use strict'
const BlankNode = require('./blank-node')
const ClassOrder = require('./class-order')
const Node = require('./node')
const Term = require('./term')
class Collection extends Term {
static mem(coll) {
if (coll.sI) {
throw new Error(`Collection ${coll} already registered`)
}
coll.sI = ++Term.termIndex
Term.termMap[coll.sI] = coll
return coll
}
/**
* To keep the code simple, all collections are instantiated uniquely,
* so keep track of the proper references.
*/
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))
})
}
Collection.mem(this)
}
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
module.exports = Collection