1.0.22 • Published 5 years ago

knora-jsonld-simplify v1.0.22

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

KnoraJsonldSimplify – a Simplifier for the "simple" JSON-LD returned by Knora

KnoraJsonldSimplify is a small TypeScript module that simplifies the output of the Knora V2 API, notably the results from a resource or gravsearch query. Usage is very simple. Let's assume that the variable jsonld contains the result of a gravsearch query either as JSON string or as an object:

let simplifier = new KnoraJsonldSimplify(jsonld);
const result = simplifier.simplify();

The result is an array of KnoraResource-instances:

Installation source coude

  • download it from github
  • yarn install to install and compile
  • yarn test to perform the testing

Installation npm

Knora-jsonld-simplify is now available as npm module

npm install knora-jsonld-simplify

In a angular projects

npm add knora.jsonld-simplify

should work...

Helpers

export enum KnoraCalendar { GREGORIAN, JULIAN, ISLAMIC, JEWISH }

export enum KnoraCalendarEra { BC, CE }

KnoraResource class

Member variables

A KnoraResource object provides the following member variables that are accessible readonly:

  • iri: readonly The Knora IRI of the resource
  • type: readonly The type of the resource as defined by the ontology, e.g., mls:Lemma
  • label: readonly The label of the resource

Member Methods

In order to access the properties of the resource, the following methods are provided:

  • getNProps(): Returns the number of properties that the resource actually has.
    getNProps(): number
  • getNValues(): Return the number of values available of the given property.
    getNValues(propertyname: string) : number
  • isValue(): Is True if thee value of the given property name and given index is of type KnoraValue, That is, it points to a property value (and is not a link to another resource)
    isValue(propertyname: string, index: number)
  • getValue(): Returns an KnoraValue instance.
    getValue(propertyname: string, index: number): Returns a KnoraValue instance.
  • isResource(): Is True if thee value of the given property name and given index is of type KnoraResource, that is, it is a link property that points to another resource. This resource may be empty (just providing the IRI without property values or it can be a full resource with properties values)
    isResource(propertyname: string, index: number)
  • getResource(): Returns a Knora_Resource (the property must be a link property)
    getResource(propname: string, index?: number) : KnoraResource

KnoraValue class

This class provides access to the property values. Usually KnoraValue it is subclassed to reflect the different data types that Knora provides. The following sublcasses are provided:

Member variables (also available in subclasses)

  • iri: string readonly: The IRI of this value
  • subtype: string readonly: Get string with subclass name (e.g. "KnoraTextValue")
  • strval: string readonly: The string representation of the value (available for all value types!)
  • comment: strong : The comment associated with a value

Member methods (also available in subclasses)

  • stringify: Returns a descriptive string representation (e.g. for logging etc.) stringify(indent: string = ''): string

KnoraTextValue

Represents a text value. This can be a richtext with standoff markup!

Member variables

  • strval: string: Text string or XML
  • mapping: string: The IRI of the XML mapping (see standoff of Knora)

KnoraIntValue

Represents an integer value

Member variables

  • ival: number: Returns the integer value

KnoraListValue

Represents a node from a list

Member variables

  • nodeIri: string: The IRI of the list node
  • label: string: The label of the nodse if available, otherwise undefined

KnoraColorValue

Represents a color value

Member variables

  • color: string: The color in HTML notation

KnoraDecimalValue

Represents a decimal value

Member variables

  • decimal: number: The decimal value

KnoraUriValue

Represents an URI

Member variables

  • uri: string: An URI

KnoraBooleanValue

Represents a boolean

Member variables

  • bool: boolean: A boolean value

KnoraGeomValue

Represents a geometry value of a region. This is a JSON string.

Member variables

  • geom: string: A geometry value as JSON string

KnoraGeonameValue

Represents a geographical location as given by a code by geonames.org

Member variables

  • geonameCode: string: Geonames.org geo code

KnoraIntervalValue

Represents a time interval

Member variables

  • start: number: Start time in seconds (including fractions of seconds)
  • end: number: End time in seconds (including fractions of seconds)

KnoraDateValue

Implemensts the Knora calendar date representation

Member variables

  • calendar: KnoraCalendar: The calendar, either
    • KnoraCalendar.GREGORIAN
    • KnoraCalendar.JULIAN
    • KnoraCalendar.ISLAMIC (NYI!)
    • KnoraCalendar.JEWISH (NYI!)
  • startEra: KnoraCalendarEra: Era of start date, either
    • KnoraCalendarEra.BC
    • KnoraCalendarEra.CE
  • startYear: number: Start year
  • startMonth: number: Start month
  • startDay: number: Start day
  • endEra: KnoraCalendarEra: Era of end date, see startEra
  • endYear: number: End year
  • endMonth: number: End month
  • endDay: number: End day

KnoraStillImageFileValue

Implements the still image value of a Knora StillImageResource

Member variables

  • strval: string: A IIIF compatible URL to access the full image as JPEG
  • filename: string: The filename/file-ID
  • baseUrl: string: The base URL of the IIIF server providing the image
  • dimX: number: X dimension of image (in pixels)
  • dimY: number: Y dimension of image (in pixels)

Example

// create the simplyfier instance
let simplifier = new KnoraJsonldSimplify(); 

// simplify the JSON from knora. We get an array of resources
const result = simplifier.simplify(json); 

// get the IRI of the first resource
let iri = result[0].iri

// get the label of the first resource
let label = result[0].label

// get the type of the first resource, eg. "kpt:restype1"
let type = result[0].type

// check if there is a property "kpt:titleprop"
if (result[0].isValue("kpt:titleprop")) {
  // get the string representation of the first value
  let strval = result[0].getValue("kpt:titleprop").strval
}

// Check if kpt:linkpropValue is a Resource (should be because its a link)
if (result[0].isResource("kpt:linkpropValue")) {
  let res = result[0].getResource("kpt:linkpropValue")
  let restype = res.type; // type of resource
  let resiri = res.iri; // IRI of resource 
  let nprops = res.getNProps(); // see if the resource has been retrieved completely
}
1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago