Skip to content

Commit

Permalink
ts-ignore, use instanceof, termtype, lint linkeddata#355
Browse files Browse the repository at this point in the history
  • Loading branch information
joepio committed Oct 11, 2019
1 parent 3320c43 commit 918730c
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/blank-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { IndexedFormula } from './index';
* @link https://rdf.js.org/data-model-spec/#blanknode-interface
*/
export default class BlankNode extends Node {

static termType: 'BlankNode';

/**
* The identifier for the blank node
*/
Expand All @@ -21,7 +24,6 @@ export default class BlankNode extends Node {
* The next unique identifier for blank nodes
*/
static nextId: number;
static termType: string;
static NTAnonymousNodePrefix: string;

/**
Expand Down
16 changes: 14 additions & 2 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ import { ValueType, Bindings } from './types';
* A collection of other RDF nodes
*/
export default class Collection extends Node {

static termType: 'Collection';

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

/**
* The nodes in this collection
*/
elements: Node[];

elements: Node[];

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

/**
* Initializes this collection
* @param initial The initial elements
Expand All @@ -35,26 +42,30 @@ export default class Collection extends 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
Expand All @@ -68,21 +79,22 @@ export default class Collection extends Node {
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']
Expand Down
5 changes: 3 additions & 2 deletions src/empty.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict'
import Node from './node-internal'

/**
* An empty node
*/
export default class Empty extends Node {
static termType: string

static termType: 'empty'

constructor () {
super()
this.termType = Empty.termType
Expand Down
29 changes: 17 additions & 12 deletions src/literal.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
'use strict'
import NamedNode from './named-node'
import Node from './node-internal'
import XSD from './xsd'
import { ValueType } from './types'
import classOrder from './class-order'

/**
* An RDF literal node, containing something different than a URI.
* @link https://rdf.js.org/data-model-spec/#literal-interface
*/
* An RDF literal node, containing something different than a URI.
* @link https://rdf.js.org/data-model-spec/#literal-interface
*/
export default class Literal extends Node {

termType: 'Literal'

/**
* The language for the literal
*/
Expand All @@ -20,8 +22,6 @@ export default class Literal extends Node {
*/
datatype!: NamedNode

termType: string

/**
* Initializes this literal
* @param value The literal's lexical value
Expand All @@ -34,23 +34,29 @@ export default class Literal extends Node {
this.value = value
if (language) {
this.lang = language
datatype = XSD.langString
this.datatype = XSD.langString
}
// If not specified, a literal has the implied XSD.string default datatype
if (datatype) {
this.datatype = NamedNode.fromValue(datatype)
}
if (!datatype) {
if (!datatype && !this.datatype) {
this.datatype = XSD.string
}
}
/**

/**
* Gets a copy of this literal
*/
copy (): Literal {
return new Literal(this.value, this.lang, this.datatype)
}
equals (other) {

/**
* Gets whether two literals are the same
* @param other The other statement
*/
equals (other: Literal): boolean {
if (!other) {
return false
}
Expand Down Expand Up @@ -146,8 +152,7 @@ export default class Literal extends Node {
if (typeof value === 'undefined' || value === null) {
return value
}
// @ts-ignore
if (typeof value === 'object' && value.termType) { // this is a Node instance
if (Object.prototype.hasOwnProperty.call(value, 'termType')) { // this is a Node instance
return value as Node
}
switch (typeof value) {
Expand Down
9 changes: 5 additions & 4 deletions src/named-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ValueType } from './types';
* A named (IRI) RDF node
*/
export default class NamedNode extends Node {
static termType: string;

static termType: 'NamedNode';

/**
* Initializes this node
Expand Down Expand Up @@ -37,6 +38,7 @@ export default class NamedNode extends Node {

this.value = iriString
}

/**
* Returns an RDF node for the containing directory, ending in slash.
*/
Expand All @@ -49,7 +51,7 @@ export default class NamedNode extends Node {
}

/**
* Returns an NamedNOde for the whole web site, ending in slash.
* Returns a NamedNode for the whole web site, ending in slash.
* Contrast with the "origin" which does NOT have a trailing slash
*/
site (): NamedNode {
Expand Down Expand Up @@ -107,8 +109,7 @@ export default class NamedNode extends Node {
if (typeof value === 'undefined' || value === null) {
return value
}
// @ts-ignore
const isNode = value && value.termType
const isNode = value && (value as Node).termType
if (isNode) {
return value as NamedNode
}
Expand Down
6 changes: 1 addition & 5 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Node from './node-internal'
import Collection from './collection'
import Literal from './literal'
import { ValueType } from './types'
import Namespace from './namespace'

export default Node

Expand All @@ -19,7 +20,6 @@ Node.fromValue = function (value: ValueType): Node | Literal | undefined | null
}
const isNode = Object.prototype.hasOwnProperty.call(value, 'termType')
if (isNode) { // a Node subclass or a Collection
// @ts-ignore
return value
}
if (Array.isArray(value)) {
Expand All @@ -28,7 +28,6 @@ Node.fromValue = function (value: ValueType): Node | Literal | undefined | null
return Literal.fromValue(value)
}

import Namespace from './namespace'
const ns = { xsd: Namespace('http://www.w3.org/2001/XMLSchema#') }

/**
Expand All @@ -40,14 +39,12 @@ Node.toJS = function (term: Node | Literal) {
return term.elements.map(Node.toJS) // Array node (not standard RDFJS)
}
// Node remains Node
// @ts-ignore
if (!term.datatype) return term // Objects remain objects
const literalTerm = term as Literal
// if (!Object.prototype.hasOwnProperty.call(term, 'dataType')) return term // Objects remain objects
if (literalTerm.datatype.sameTerm(ns.xsd('boolean'))) {
return literalTerm.value === '1'
}
console.log("4", term)
if (literalTerm.datatype.sameTerm(ns.xsd('dateTime')) ||
literalTerm.datatype.sameTerm(ns.xsd('date'))) {
return new Date(literalTerm.value)
Expand All @@ -60,6 +57,5 @@ Node.toJS = function (term: Node | Literal) {
let z = Number(literalTerm.value)
return Number(literalTerm.value)
}
console.log("7", term)
return literalTerm.value
}
2 changes: 1 addition & 1 deletion src/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class Statement {
return (
other.subject.equals(this.subject) &&
other.predicate.equals(this.predicate) &&
other.object.equals(this.object as Node) &&
other.object.equals(this.object as Literal) &&
other.graph.equals(this.graph)
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ArrayIndexOf } from './util'
import Formula from './formula'
import { RDFArrayRemove } from './util'
import Statement from './statement'
import Node from './node-internal'
import Node from './node'
import Variable from './variable'
import { Query, indexedFormulaQuery } from './query'

Expand Down
2 changes: 1 addition & 1 deletion src/variable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
import ClassOrder from './class-order'
import Node from './node-internal'
import Node from './node'
import * as Uri from './uri'

/**
Expand Down

0 comments on commit 918730c

Please sign in to comment.