/terms2js

Metadata reference data builder for JavaScript and JSON.

Primary LanguageJavaScript

terms2js

This a basic and lightweight JavaScript and JSON version of Metadata Reference Data & Classification Schemes.

Stores the vocabularies as JSON objects, accesible as node modules or by browser javascript interpreter. Generated vocabulariy files have no dependencies and can be used stand-alone.

The package is completely generic and may be used to generate your own hierarchical reference vocabularies such as the ones expressed with the TV-Anytime XML Schema.

Command line

To built a new version of vocabularies in refdata or transform your own you can use the 'terms2js' command.

Note: In order to run the 'terms2js' command you need to install these node modules:

npm install -g optimist
npm install -g uglify-js

For example

terms2js test/*.xml --output-dir test

Generated Vocabularies usage

From javascript the JSON object is not accesible directly. You must use this public functions:

  • .isValid (term): Return boolean value, true if term (termId or termUri) is supported.
  • .getTermId (term): Return a string containing the termId given the fully qualified term URI. If term isn't matching return null.
  • .getTermUri (termId): Return a string containing the fully qualified term URI.
  • .isDeprecated (termId): Return boolean value, true if termId is deprecated.
  • .getAllTermId (): Return an array with all the termId codes supported.
  • .getAllTermUri (): Return an array with all the term URI supported.
  • .getAllTermIdMatching (string): Return an array with all the termId matching string. String can be a literal string or a regular expression object.
  • .getAllTermUriMatching (string): Return an array with all the term URI matching string. String can be a literal string or a regular expression object.
  • .getTermInfo (termId): Return object {"Name"}. If termId isn't supported return {}.
  • .isTermUri (term): Return true if term is the fully qualified term URI, false otherwise.
  • .getUri (): Return a string containing the vocabulary namespace URI.
  • .getVersionDate (): Return a string containing the vocabulary namespace version date.
  • .getLang(): Return a string containing the vocabulary language.
  • .stringify(): JSON stringify the vocabulary.
  • .getEquivalent(termId): Return an array of fully qualified term URI of equivalent terms in other classification schemas.
  • .getMappings(termId, [uri]): *Return an array of fully qualified term URI with which termId can be matched in other vocabularies. Results may be filtered by external uri if specified. The external uri may be a single string or an array of strings. *
  • .getMappingsMatching (string, [uri]): Return an array of fully qualified term URI mappings in other vocabularies which the termId label matches string. String can be a literal string or a regular expression object. Results may be filtered by external uri if specified. The external uri may be a single string or an array of strings.

See the test folder for more usage examples:

From nodejs

// From node the module is accesible with a simple require
var roles = require ('../test/en/ebu_RoleCodeCS.min.js').Vocabulary;
var num_roles = 0;

// roles.getAllTermId () return an array of all EBU Role terms supported
var rolecodes = roles.getAllTermId ();
// iterate this array
for (num_roles=0; num_roles<rolecodes.length; num_roles++) {
  // show a string representation of the object return by roles.getTermInfo(termId)
        console.log (rolecodes[num_roles]);
        console.log ("   "+JSON.stringify(roles.getTermInfo(rolecodes[num_roles])));
}
// show the number of roles supported
console.log ("Roles supported: "+num_roles);
// test roles.isValid (termId) function
console.log("Is '4.2' a role code? "+roles.isValid ('4.2'));
console.log("What is the URI of role code '4.2'? "+roles.getTermUri ('4.2'));
console.log("What is the role code for 'Actor'? "+roles.getAllTermIdMatching ('Actor'));

From browser

<!doctype html>
<html>
<head>
    <title>Test EBU roles module</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
    <!--<script src="../test/en/ebu_RoleCodeCS.min.js"></script>-->
    <script src="../test/en/ebu_RoleCodeCS.js"></script>
    <style>
    body {
        background-color: #eee;
    }
    .center {
        width:700px;
        margin:10px auto;
        border:1px solid #ccc;
        padding:20px;
        background-color:#fff;
    }
    </style>
</head>
<body>
    <div class="center">
        <h1>Test EBU roles module (Browser client side)</h1>
        <hr />
        <div id="test"></div>
    </div>
    <script>
        var num_roles = 0,
        text = '';

        // terms2js.en.ebu_RoleCodeCS.Vocabulary.getAllTermId() return an array of all EBU Role terms supported
        var rolecodes = terms2js.en.ebu_RoleCodeCS.Vocabulary.getAllTermId();
        // iterate this array
        for (num_roles=0; num_roles<rolecodes.length; num_roles++) {
            // save in text variable a string representation of the object return by terms2js.en.ebu_RoleCodeCS.Vocabulary.getTermInfo(termId)
            var termId = rolecodes[num_roles];
            text+='<b>'+termId+'</b> '+JSON.stringify(terms2js.en.ebu_RoleCodeCS.Vocabulary.getTermInfo(termId))+'<br />';
        }
        // save the number of roles supported
        text = '<h2>Roles supported: '+num_roles+'</h2>'+text;
        // write the test result in DOM element with id='test'
        document.getElementById('test').innerHTML = text;
    </script>
</body>
</html>

See Also

Aknowledgments

This package is inspired to languages4translatewiki.js.