npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ro-crate

v3.4.2

Published

Research Object Crate (RO-Crate) utilities for making and consuming crates

Downloads

384

Readme

Research Object Crate (RO-Crate) JavaScript Library

This is a JavaScript library to create and manipulate Research Object Crate.

Install

Install the library:

npm install ro-crate

Note: The minimum Node.js version is 16.11.0.

Docs & Other Resources

Usage

Import the ROCrate class and create a new empty crate with default configurations:

const {ROCrate} = require('ro-crate');
const crate = new ROCrate();

The ROCrate constructor accepts two optional arguments:

const fs = require('fs');

// load existing metadata
const data = JSON.parse(fs.readFileSync('ro-crate-metadata.json', 'utf8'));

// create a crate using the existing data and
// configure the crate to return a property of an Entity as an array and resolve linked entity as nested object
const crate = new ROCrate(data, { array: true, link: true });

To add an Entity to the crate:

// A license
const license = {
    '@id': 'https://creativecommons.org/licenses/by/4.0/',
    '@type': 'CreativeWork',
    'description': 'Attribution 4.0 International (CC BY 4.0) ...',
    'name': 'CC BY 4.0'
};
// add the license as an unconnected Entity
crate.addEntity(license);

// add the license to the root dataset
crate.rootDataset.license = {'@id': license['@id']};
// or alternatively, add a new entity directly into a property of other entity :
crate.rootDataset.license = license;

Use an entity just like a normal object:

let lic = create.getEntity(license['@id']);
console.log(lic.name); // prints 'CC BY 4.0';
// set a property directly
lic.name = 'CC BY 4.0 dummy';
// or with the setProperty method
crate.setProperty(license['@id'], 'name', 'CC BY 4.0 dummy');

console.log(lic.name); // prints 'CC BY 4.0 dummy';

Modifying an array of values in the property is not supported yet:

lic.test = [1,2,3];
lic.test.push(4); // this does not work
console.log(lic.test); // prints '[1,2,3]';
// use this instead
lic.test = lic.test.concat(4);
// or this
crate.addValues(license['@id'], 'test', 4);

Root Dataset is a special entity that is mandated by the standard:

const rootDataset = crate.rootDataset;
rootDataset.name = 'Tutorial Crate';
rootDataset.description = 'This is an example crate for educational purposes.'
const today = new Date().toISOString().split('T')[0]
rootDataset.datePublished = today;

The value of the returned property can be set to be always as an array:

const crate1 = new ROCrate();
const crate2 = new ROCrate({array: true});
crate1.rootDataset.name = 'Tutorial Crate';
crate1.rootDataset.test = ['test1', 'test2'];
crate2.rootDataset.name = 'Tutorial Crate';
crate2.rootDataset.test = ['test1', 'test2'];
console.log(crate1.rootDataset.name); // return 'Tutorial Crate'
console.log(crate1.rootDataset.name); // return ['test1', 'test2']
console.log(crate2.rootDataset.name); // return ['Tutorial Crate']
console.log(crate2.rootDataset.name); // return ['test1', 'test2']

Linked entities can be automatically resolved as nested objects:

const crate1 = new ROCrate();
const crate2 = new ROCrate({link: true});
const crate3 = new ROCrate({link: true, array: true});
crate1.rootDataset.license = license;
crate2.rootDataset.license = license;
crate3.rootDataset.license = license;
console.log(crate1.rootDataset.license.name); // return undefined
console.log(crate1.rootDataset.license); // return {'@id': 'https://creativecommons.org/licenses/by/4.0/'}
console.log(crate2.rootDataset.license.name); // return 'CC BY 4.0'
console.log(crate3.rootDataset.license.name); // return undefined, property license is a array
console.log(crate3.rootDataset.license[0].name); // return 'CC BY 4.0'

To save the rocrate data to a file, use JSON.stringify:

// Write pretty-printed JSONLD into the directory
fs.writeFileSync('ro-crate-metadata.json', JSON.stringify(crate, null, 2));

For more usage examples, see the test files under the test directory.

For more details, refer to the full API documentation.

HTML Rendering

Use the RO-Crate-HTML to generate a HTML preview from the RO-Crate Metadata File ro-crate-metadata.json.

Simple crate checker

There is a script included with this library that can check crates.

Check a crate:

roccheck /path/to/crate/directory

This is produce a simple report.