Skip to content

Commit

Permalink
small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
s-tittel committed Jan 8, 2024
1 parent 0d11aaa commit 613dff6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { createShaclGroup } from './group'
import { v4 as uuidv4 } from 'uuid'
import { createShaclOrConstraint } from './constraints'
import { Config } from './config'
import { findLabel } from './util'

export class ShaclNode extends HTMLElement {
shaclSubject: NamedNode
Expand Down
3 changes: 2 additions & 1 deletion src/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { SHAPES_GRAPH } from './constants'
import { createShaclOrConstraint, resolveShaclOrConstraint } from './constraints'
import { Config } from './config'
import { ShaclPropertyTemplate } from './property-template'
import { Editor, fieldFactory, toRDF } from './theme'
import { Editor, fieldFactory } from './theme'
import { toRDF } from './serialize'

export class ShaclProperty extends HTMLElement {
template: ShaclPropertyTemplate
Expand Down
28 changes: 26 additions & 2 deletions src/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Writer, Quad, Literal, Prefixes } from 'n3'
import { PREFIX_XSD, RDF_PREDICATE_TYPE } from './constants'
import { DataFactory, NamedNode, Writer, Quad, Literal, Prefixes } from 'n3'
import { PREFIX_XSD, RDF_PREDICATE_TYPE, PREFIX_SHACL } from './constants'
import { Editor } from './theme'
import { NodeObject } from 'jsonld'

export function serialize(quads: Quad[], format: string, prefixes?: Prefixes): string {
Expand Down Expand Up @@ -43,3 +44,26 @@ function serializeJsonld(quads: Quad[]): string {
}
return JSON.stringify(triples)
}

export function toRDF(editor: Editor): Literal | NamedNode | undefined {
let languageOrDatatype: NamedNode<string> | string | undefined = editor['shaclDatatype']
let value: number | string = editor.value
if (value) {
if (editor.dataset.class || editor.dataset.nodeKind === PREFIX_SHACL + 'IRI') {
return DataFactory.namedNode(value)
} else {
if (editor.dataset.lang) {
languageOrDatatype = editor.dataset.lang
}
else if (editor['type'] === 'number') {
value = parseFloat(value)
}
return DataFactory.literal(value, languageOrDatatype)
}
} else if (editor['type'] === 'checkbox' || editor.getAttribute('type') === 'checkbox') {
// emit boolean 'false' only when required
if (editor['checked'] || parseInt(editor.dataset.minCount || '0') > 0) {
return DataFactory.literal(editor['checked'] ? 'true' : 'false', languageOrDatatype)
}
}
}
27 changes: 2 additions & 25 deletions src/theme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataFactory, Literal, NamedNode } from 'n3'
import { Literal, NamedNode } from 'n3'
import { Term } from '@rdfjs/types'
import { PREFIX_SHACL, PREFIX_XSD, PREFIX_RDF, SHAPES_GRAPH } from './constants'
import { PREFIX_XSD, PREFIX_RDF, SHAPES_GRAPH } from './constants'
import { createInputListEntries, findInstancesOf, findLabel, isURL } from './util'
import { ShaclPropertyTemplate } from './property-template'
import css from './styles.css'
Expand Down Expand Up @@ -72,29 +72,6 @@ export abstract class Theme {
abstract createButton(label: string, primary: boolean): HTMLElement
}

export function toRDF(editor: Editor): Literal | NamedNode | undefined {
let languageOrDatatype: NamedNode<string> | string | undefined = editor['shaclDatatype']
let value: number | string = editor.value
if (value) {
if (editor.dataset.class || editor.dataset.nodeKind === PREFIX_SHACL + 'IRI') {
return DataFactory.namedNode(value)
} else {
if (editor.dataset.lang) {
languageOrDatatype = editor.dataset.lang
}
else if (editor['type'] === 'number') {
value = parseFloat(value)
}
return DataFactory.literal(value, languageOrDatatype)
}
} else if (editor['type'] === 'checkbox' || editor.getAttribute('type') === 'checkbox') {
// emit boolean 'false' only when required
if (editor['checked'] || parseInt(editor.dataset.minCount || '0') > 0) {
return DataFactory.literal(editor['checked'] ? 'true' : 'false', languageOrDatatype)
}
}
}

export function fieldFactory(template: ShaclPropertyTemplate, value: Term | null): HTMLElement {
if (template.config.editMode) {
const required = template.minCount !== undefined && template.minCount > 0
Expand Down

0 comments on commit 613dff6

Please sign in to comment.