Skip to content

Commit

Permalink
Merge pull request #15 from dan-f/native-field-values
Browse files Browse the repository at this point in the history
Convert rdf types to native JS types in field values
  • Loading branch information
dan-f committed Dec 13, 2016
2 parents f035552 + c9e6104 commit 4b8111e
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 47 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"pretest": "npm run lint",
"posttest": "npm run coverage:check",
"test": "nyc mocha --compilers js:babel-core/register test/**.spec.js",
"test:develop": "mocha -d -w --compilers js:babel-core/register test/*.spec.js",
"test:develop": "mocha -d -w -G --compilers js:babel-core/register test/*.spec.js",
"coverage:check": "nyc check-coverage --lines 98",
"coverage:report": "nyc report --repoprter=text-lcov --reporter=html",
"coverage:coveralls": "nyc report --reporter=text-lcov | coveralls",
Expand Down Expand Up @@ -50,7 +50,7 @@
"expect": "^1.20.2",
"mocha": "^3.0.2",
"nyc": "^8.1.0",
"rdflib": "^0.9.0",
"rdflib": "^0.12.3",
"sinon": "^1.17.5",
"solid-namespace": "^0.1.0",
"standard": "^7.1.2",
Expand All @@ -60,7 +60,6 @@
"immutable": "^3.8.1",
"lodash": "^4.14.2",
"node-uuid": "^1.4.7",
"rdflib": "^0.9.0",
"valid-url": "^1.0.9"
}
}
62 changes: 58 additions & 4 deletions src/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class Field {
// 'value' and 'listed'.
if (isDefined(originalObject)) {
this.originalObject = originalObject
this.value = originalObject.value
this.value = rdfToJs(originalObject)
}
if (isDefined(originalSource)) {
this.originalSource = originalSource
Expand Down Expand Up @@ -164,7 +164,7 @@ export class Field {
} else {
sourceURI = this.listed ? defaultSources.listed : defaultSources.unlisted
}
return rdf.namedNode(sourceURI)
return rdf.NamedNode.fromValue(sourceURI)
}

/**
Expand All @@ -179,13 +179,14 @@ export class Field {
let object
if (isDefined(this.originalObject)) {
object = clone(this.originalObject)
object.value = this.value
// Convert the native JS value back to the corresponding RDF string value
object.value = this.originalObject.constructor.fromValue(this.value).value
if (isDefined(object.uri)) {
object.uri = this.value
}
} else {
object = isUri(this.value)
? rdf.namedNode(this.value)
? rdf.NamedNode.fromValue(this.value)
: rdf.Literal.fromValue(this.value)
}

Expand Down Expand Up @@ -277,3 +278,56 @@ export class Field {
})
}
}

/**
* Extracts the value of an rdf node into the native JS representation of that
* node's type/value. For example, it will extract booleans from a node with a
* datatype of xsd:boolean and a value of '0' or '1'.
*
* @param {Object} node - The rdf node object.
* @returns The value of that node.
*/
function rdfToJs (node) {
let value
const rdfVal = node.value
const datatype = node.datatype
const throwError = () => {
throw new Error(
`Cannot parse rdf type/value to JS value. Given value [${rdfVal}] of type [${datatype}].`
)
}
if (datatype) {
const XMLSchema = 'http://www.w3.org/2001/XMLSchema#'
switch (datatype.value) {
case `${XMLSchema}boolean`:
if (rdfVal === '1') {
value = true
} else if (rdfVal === '0') {
value = false
} else {
throwError()
}
break
case `${XMLSchema}dateTime`:
// Format of date string can be found at: http://books.xmlschemata.org/relaxng/ch19-77049.html
value = new Date(rdfVal)
break
case `${XMLSchema}decimal`:
case `${XMLSchema}double`:
value = Number.parseFloat(rdfVal)
break
case `${XMLSchema}integer`:
value = Number.parseInt(rdfVal)
break
case 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString':
case `${XMLSchema}string`:
default:
value = rdfVal
break
}
} else {
// Assume string if there's no provided datatype
value = rdfVal
}
return value
}
2 changes: 1 addition & 1 deletion src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function modelFactory (rdf, sourceConfig, fieldMap) {
const factory = fieldFactory(sourceConfig)
return (graph, subjectStr) => {
const fieldCreators = {}
const subject = rdf.namedNode(subjectStr)
const subject = rdf.NamedNode.fromValue(subjectStr)
const fields = Immutable.Map(
Object.keys(fieldMap).reduce((prevFields, fieldName) => {
const fieldPredicate = fieldMap[fieldName]
Expand Down
Loading

0 comments on commit 4b8111e

Please sign in to comment.