@openactive/skos

1.4.3 • Public • Published

npm version Build Status Coverage Status

SKOS.js

Simple JavaScript library to wrap the OpenActive JSON-LD representation of SKOS.

Compatible Platforms and Browsers

SKOS.js will run on any version of Node.js, and is built to use CommonJS so can be built with Webpack and Browserify.

SKOS.js has been tested on IE 9 and above without transpilation or polyfills, and all other major browsers.

Installation

Dependencies

SKOS.js does not have any runtime dependencies. It is written natively in ES5.

As a library

Simply install using npm:

$ npm install @openactive/skos --save

Now you can begin using it on either the client or server side.

var skos = require('@openactive/skos');

// returns an array of the names of all types of Yoga
var response = request('GET', 'https://openactive.io/activity-list', { headers: { accept: 'application/ld+json' } });
if (response && response.statusCode == 200) {
  var activityListJsonObject = JSON.parse(response.getBody('utf8'));

  var scheme = new skos.ConceptScheme(activityListJsonObject);
  return scheme.getConceptByLabel('Yoga').getBroaderTransitive().map(concept => concept.prefLabel);
}

In the browser

See the live demo.

<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/@openactive/skos/dist/skos.min.js" type="text/javascript"></script>
<script type="text/javascript">
  $.getJSON('https://openactive.io/activity-list/activity-list.jsonld', function(activityListJsonObject) {
    var scheme = new skos.ConceptScheme(activityListJsonObject);
    var labels = scheme.getConceptByLabel('Yoga').getNarrowerTransitive();
    $.each(labels, function(index, concept) {
      $('#activity-list').append($('<p>', {
        text: concept.prefLabel
      }));
    });
  });
</script>

API Reference

Note this library is written in ES5 to provide client-side compatibility without requiring transpiling. It has been tested on IE9 upwards.

skos~ConceptScheme

Kind: inner class of skos
Access: public

new ConceptScheme(scheme, [id], [filter])

ConceptScheme constructor.

Param Type Description
scheme Object | Array Either a JSON ConceptScheme object OR Array of Concepts
[id] String The scheme id, only required if an array is provided for scheme
[filter] Object | Array Filter of ids to be included in the ConceptScheme. Values in the object literal must be true or contain an object of keys which can be assigned on each resulting Concept. Prefer generateSubset() for most use cases.

Example

// returns Concept for American Football
var activityListJsonObject = JSON.parse(response.getBody('utf8'));
var scheme = new skos.ConceptScheme(activityListJsonObject);
return scheme.getConceptByID('https://openactive.io/activity-list#9caeb442-2834-4859-b660-9172ed61ee71');

Example

// returns ConceptScheme for a provided custom subset of the Activity List
var activityListConceptArray = myApiResult.items;
var scheme = new skos.ConceptScheme(activityListConceptArray, 'https://openactive.io/activity-list');
return scheme;

conceptScheme.getConceptByID(id) ⇒ Concept

Get Concept by ID

Kind: instance method of ConceptScheme
Returns: Concept - the Concept, or null if no matching concept exists

Param Type Description
id String The id of the Concept

Example

// returns Concept for American Football
var scheme = new skos.ConceptScheme(activityListJsonObject);
return scheme.getConceptByID('https://openactive.io/activity-list#9caeb442-2834-4859-b660-9172ed61ee71');

conceptScheme.getConceptByLabel(label) ⇒ Concept

Get Concept by prefLabel / altLabel

This will return a case-sensitive exact match based on the prefLabel and altLabel

Kind: instance method of ConceptScheme
Returns: Concept - the Concept, or null if no matching concept exists

Param Type Description
label String The label of the Concept

Example

// returns Concept for American Football
var scheme = new skos.ConceptScheme(activityListJsonObject);
return scheme.getConceptByLabel('American Football');

conceptScheme.getAllConcepts() ⇒ Array

Return an array of all concepts in the scheme.

Kind: instance method of ConceptScheme
Returns: Array - an array of Concept

conceptScheme.getAllConceptsByID() ⇒ Array

Return a map of all concepts in the scheme, keyed by ID. This can be useful to power autocomplete dropdowns.

Kind: instance method of ConceptScheme
Returns: Array - an map of Concept by ID

conceptScheme.getAllConceptsByLabel() ⇒ Object

Return a map of all concepts in the scheme, keyed by altLabel and prefLabel. This can be useful to power autocomplete dropdowns.

Kind: instance method of ConceptScheme
Returns: Object - a map of Concept by altLabel / prefLabel

conceptScheme.getTopConcepts() ⇒ Array

Return an array of the top concepts in a hierarchical scheme.

Kind: instance method of ConceptScheme
Returns: Array - an array of Concept

conceptScheme.getJSON() ⇒ Object

Return the original JSON object representing the ConceptScheme.

Kind: instance method of ConceptScheme
Returns: Object - a JSON object

conceptScheme.toString() ⇒ String

Return a string rendering the ConceptScheme as Markdown.

Kind: instance method of ConceptScheme
Returns: String - a Markdown string

conceptScheme.generateSubset(filter) ⇒ ConceptScheme

Generate ConceptScheme subset

The subset will be generated to include all broader Concepts of any of those included in the filter, and will have pruned any references to related Concepts that are not included in the resulting subset.

Kind: instance method of ConceptScheme
Returns: ConceptScheme - the ConceptScheme subset

Param Type Description
filter Object | Array Filter of ids to be included in the ConceptScheme. Values in the object literal must be true or contain an object of keys which can be assigned on each resulting Concept.

Example

// returns ConceptScheme subset of just Pole Vault and its broader concepts (Athletics)
var scheme = new skos.ConceptScheme(activityListJsonObject);
return scheme.generateSubset(['https://openactive.io/activity-list#5df80216-2af8-4ad3-8120-a34c11ea1a87']);

Example

// returns ConceptScheme subset of just Pole Vault and its broader concepts (Athletics), including metadata attached to Pole Vault.
var scheme = new skos.ConceptScheme(activityListJsonObject);
return scheme.generateSubset({'https://openactive.io/activity-list#5df80216-2af8-4ad3-8120-a34c11ea1a87': {'ext:metadata': 34}});

skos~Concept

Kind: inner class of skos

new Concept(concept)

Concept class.

A wrapper for the SKOS Concept JSON object

Param Type Description
concept Object A Concept JSON object

concept.getNarrower() ⇒ Array

Get an array of immediately narrower concepts.

Kind: instance method of Concept
Returns: Array - an array of Concept
Example

// returns only the types of Yoga that are one level below "Yoga"
var scheme = new skos.ConceptScheme(activityListJsonObject);
return scheme.getConceptByLabel('Yoga').getNarrower();

concept.getNarrowerTransitive() ⇒ Array

Get an array of all narrower concepts following transitivity (all children).

Kind: instance method of Concept
Returns: Array - an array of Concept
Example

// returns all type of Yoga
var scheme = new skos.ConceptScheme(activityListJsonObject);
return scheme.getConceptByLabel('Yoga').getNarrowerTransitive();

concept.getBroader() ⇒ Array

Get an array of immediately broader concepts.

Kind: instance method of Concept
Returns: Array - an array of Concept
Example

// returns only the next level up in the hierarchy
var scheme = new skos.ConceptScheme(activityListJsonObject);
return scheme.getConceptByLabel('Yoga').getBroader();

concept.getBroaderTransitive() ⇒ Array

Get an array of all broader concepts following transitivity (all parents).

Kind: instance method of Concept
Returns: Array - an array of Concept
Example

// returns all the higher level categories above Yoga
var scheme = new skos.ConceptScheme(activityListJsonObject);
return scheme.getConceptByLabel('Yoga').getBroaderTransitive();

concept.getRelated() ⇒ Array

Get an array of related concepts.

Kind: instance method of Concept
Returns: Array - an array of Concept

concept.equals(concept) ⇒ boolean

Return true if two Concepts are equal and of the same type. If a raw JSON Concept is supplied it is coerced into a Concept object.

Kind: instance method of Concept
Returns: boolean - representing whether the two Concepts are equal

Param Type Description
concept Concept Concept to compare

concept.toString() ⇒ String

Return the prefLabel of the Concept.

Kind: instance method of Concept
Returns: String - a JSON string

concept.getJSON() ⇒ Object

Return the original JSON object representing the Concept.

Kind: instance method of Concept
Returns: Object - a JSON object

Concept.compare(a, b) ⇒ Integer

Compare two Concepts based on prefLabel, for use with native .sort()

Kind: static method of Concept
Returns: Integer - representing which should be sorted above the other

Param Type Description
a Concept Concept A
b Concept Concept B

Example

var sortedConcepts = concepts.sort(skos.Concept.compare);

Readme

Keywords

Package Sidebar

Install

npm i @openactive/skos

Weekly Downloads

52

Version

1.4.3

License

MIT

Unpacked Size

79.8 kB

Total Files

6

Last publish

Collaborators

  • ldodds-odi
  • ldodds
  • nickevans