@tpluscode/rdfine
Advanced tools
Comparing version 0.4.7 to 0.4.8
@@ -6,2 +6,13 @@ # Change Log | ||
## [0.4.8](https://github.com/tpluscode/rdfine/compare/@tpluscode/rdfine@0.4.7...@tpluscode/rdfine@0.4.8) (2020-04-22) | ||
### Features | ||
* add more helpful getters to base class ([2e67a81](https://github.com/tpluscode/rdfine/commit/2e67a8148d1555e8adb6741980052b0496acb04f)) | ||
## [0.4.7](https://github.com/tpluscode/rdfine/compare/@tpluscode/rdfine@0.4.6...@tpluscode/rdfine@0.4.7) (2020-04-21) | ||
@@ -8,0 +19,0 @@ |
@@ -10,3 +10,3 @@ import * as rdfList from './rdf-list'; | ||
} | ||
return target._create(obj); | ||
return target._create(obj, [], { parent: target }); | ||
default: | ||
@@ -24,3 +24,3 @@ return obj.term; | ||
} | ||
const objects = target._selfGraph.out(target._selfGraph.namedNode(property.toString())); | ||
const objects = target._getObjects(property.toString()); | ||
if (objects.values.length === 0) { | ||
@@ -27,0 +27,0 @@ return undefined; |
import { Clownface, SingleContextClownface } from 'clownface'; | ||
import { DatasetCore, Term } from 'rdf-js'; | ||
import { RdfResource } from '../RdfResource'; | ||
export declare function isList(node: Clownface): boolean; | ||
export declare function enumerateList<T>(parent: RdfResource, listNode: SingleContextClownface, fromTerm: (obj: SingleContextClownface) => T): T[]; | ||
export declare function enumerateList<T, D extends DatasetCore = DatasetCore>(parent: RdfResource<D>, listNode: SingleContextClownface<Term, D>, fromTerm: (obj: SingleContextClownface<Term, D>) => T | T[]): T[]; |
@@ -20,3 +20,8 @@ import { rdf } from '@tpluscode/rdf-ns-builders'; | ||
}); | ||
items.push(...firstItem); | ||
firstItem.forEach(item => { | ||
if (Array.isArray(item)) { | ||
throw new Error('Malformed RDF List had multiple rdf:first objects'); | ||
} | ||
items.push(item); | ||
}); | ||
current = current.out(rdf.rest).map((quad, index) => { | ||
@@ -23,0 +28,0 @@ if (index > 0) { |
{ | ||
"name": "@tpluscode/rdfine", | ||
"version": "0.4.7", | ||
"version": "0.4.8", | ||
"description": "RDF/JS idiomatic, native, effective", | ||
@@ -72,3 +72,3 @@ "main": "main.js", | ||
}, | ||
"gitHead": "eb59090c2419f90e3fe7ed8bb09b19a0dfece00d" | ||
"gitHead": "c597d23d165f1404a06b466e0363d015e07c51b9" | ||
} |
@@ -13,2 +13,5 @@ /// <reference types="rdfjs__namespace" /> | ||
}; | ||
export interface GetOptions { | ||
strict: boolean; | ||
} | ||
export interface RdfResource<D extends DatasetCore = DatasetCore> { | ||
@@ -23,2 +26,31 @@ readonly id: ResourceIdentifier; | ||
hasType(type: string | NamedNode): boolean; | ||
/** | ||
* Gets the value of a property | ||
* @param property | ||
*/ | ||
get<T extends RdfResource = RdfResource>(property: string | NamedNode): T; | ||
get<T extends RdfResource = RdfResource>(property: string | NamedNode, options?: GetOptions): T | null; | ||
/** | ||
* Gets the value of a property and ensures that an array will be returned | ||
* @param property | ||
*/ | ||
getArray<T extends RdfResource = RdfResource>(property: string | NamedNode, options?: GetOptions): T[]; | ||
/** | ||
* Gets the property value if it's boolean. Throws if it's not | ||
* @param property | ||
*/ | ||
getBoolean(property: string | NamedNode, options?: GetOptions): boolean; | ||
/** | ||
* Gets the property value if it's a string. Throws if it's not | ||
* @param property | ||
*/ | ||
getString(property: string | NamedNode): string; | ||
getString(property: string | NamedNode, options?: GetOptions): string | null; | ||
/** | ||
* Gets the property value if it's a number. Throws if it's not | ||
* @param property | ||
*/ | ||
getNumber(property: string | NamedNode): number | null; | ||
getNumber(property: string | NamedNode, options?: GetOptions): number | null; | ||
_getObjects(property: string | NamedNode, options?: GetOptions): SafeClownface<Term, D>; | ||
_create<T extends RdfResource>(term: ResourceNode<D>, mixins?: Mixin[] | [Constructor, ...Mixin[]], options?: ResourceCreationOptions<D>): T & ResourceIndexer; | ||
@@ -42,2 +74,8 @@ } | ||
equals(other: RdfResource | undefined | null): boolean; | ||
get<T extends RdfResource = RdfResource>(property: string | NamedNode, options?: GetOptions): T | RdfResource<D> | null; | ||
getArray<T extends RdfResource = RdfResource>(property: string | NamedNode, options?: GetOptions): T[]; | ||
getNumber(property: string | NamedNode): number; | ||
getString(property: string | NamedNode): string; | ||
getBoolean(property: string | NamedNode, options?: GetOptions): boolean; | ||
_getObjects(property: string | NamedNode, { strict }?: GetOptions): SafeClownface<Term, D>; | ||
_create<T extends RdfResource>(term: ResourceNode<D>, mixins?: Mixin[] | [Constructor, ...Mixin[]], options?: ResourceCreationOptions<D>): T & ResourceIndexer; | ||
@@ -44,0 +82,0 @@ } |
@@ -1,2 +0,2 @@ | ||
/* eslint-disable camelcase,@typescript-eslint/camelcase */ | ||
/* eslint-disable camelcase,@typescript-eslint/camelcase,no-dupe-class-members */ | ||
import { defaultGraph } from '@rdfjs/data-model'; | ||
@@ -7,2 +7,3 @@ import cf from 'clownface'; | ||
import TypeCollectionCtor from './lib/TypeCollection'; | ||
import { xsd } from '@tpluscode/rdf-ns-builders'; | ||
export default class RdfResourceImpl { | ||
@@ -79,2 +80,57 @@ constructor(graph, init = {}, parent) { | ||
} | ||
get(property, options) { | ||
const objects = this.getArray(property, options); | ||
if (objects.length > 0) { | ||
return objects[0]; | ||
} | ||
return null; | ||
} | ||
getArray(property, options) { | ||
const values = this._getObjects(property, options) | ||
.map(obj => { | ||
return this._create(obj, [], { parent: this }); | ||
}); | ||
if (!values.length) { | ||
return []; | ||
} | ||
return values; | ||
} | ||
getNumber(property, options) { | ||
const [value] = this._getObjects(property, options).toArray(); | ||
if (typeof value === 'undefined') { | ||
return null; | ||
} | ||
if (value.term.termType === 'Literal') { | ||
return parseFloat(value.value); | ||
} | ||
throw new Error(`Expected property '${property}' to be a number but found '${value}'`); | ||
} | ||
getString(property, options) { | ||
const [value] = this._getObjects(property, options).toArray(); | ||
if (typeof value === 'undefined') { | ||
return null; | ||
} | ||
if (value.term.termType === 'Literal') { | ||
return value.value; | ||
} | ||
throw new Error(`Expected property '${property}' to be a literal but found '${value}'`); | ||
} | ||
getBoolean(property, options) { | ||
const [value] = this._getObjects(property, options).toArray(); | ||
if (typeof value === 'undefined') { | ||
return false; | ||
} | ||
if (value.term.termType === 'Literal' && xsd.boolean.equals(value.term.datatype)) { | ||
return value.term.equals(this._selfGraph.literal(true).term); | ||
} | ||
throw new Error(`Expected property '${property}' to be a boolean but found '${value}'`); | ||
} | ||
_getObjects(property, { strict } = { strict: false }) { | ||
const propertyNode = typeof property === 'string' ? this._selfGraph.namedNode(property) : property; | ||
const objects = this._selfGraph.out(propertyNode); | ||
if (!objects.terms.length && strict) { | ||
throw new Error(`Value for predicate <${property}> was missing`); | ||
} | ||
return objects; | ||
} | ||
_create(term, mixins, options = {}) { | ||
@@ -81,0 +137,0 @@ return this.constructor.factory.createEntity(term, mixins, options); |
65120
1028