1.4.3 • Published 3 years ago

@openactive/skos v1.4.3

Weekly downloads
18
License
MIT
Repository
github
Last release
3 years ago

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.

ParamTypeDescription
schemeObject | ArrayEither a JSON ConceptScheme object OR Array of Concepts
idStringThe scheme id, only required if an array is provided for scheme
filterObject | ArrayFilter 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

ParamTypeDescription
idStringThe 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

ParamTypeDescription
labelStringThe 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

ParamTypeDescription
filterObject | ArrayFilter 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

ParamTypeDescription
conceptObjectA 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

ParamTypeDescription
conceptConceptConcept 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

ParamTypeDescription
aConceptConcept A
bConceptConcept B

Example

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

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago