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

node-jsonld

v1.0.0

Published

NodeJS interface with JSON-LD library. Run compact, expand, flatten algorithms, etc., by reading from and writing to local files. Verbose error messages and help messages are built in. Uses jsonld.js as a peer dependency and requires Node 8 or higher.

Downloads

8

Readme

node-jsonld

NodeJS interface with JSON-LD library. Run compact, expand, flatten algorithms, etc., by reading from and writing to local files. Verbose error messages and help messages are built in. Uses jsonld.js as a peer dependency and requires Node 8 or higher.

Installation

You will need to install node-jsonld and three peer dependencies: jsonld (the main library), inversify and reflect-metadata (these latter two support the Inversion-of-Control-friendly code of node-jsonld). If programming in Typescript, it's also advisable to install the types for jsonld as a dev dependency.

npm install jsonld inversify reflect-metadata node-jsonld
npm install types/jsonld --save-dev

IMPORTANT: You need Python 2.x (2.7 recommended) in order to build jsonld. jsonld has the node-gyp library as a dependency. Building the node-gyp library requires Python 2.x; it does not work with Python 3.x. You do not need to know any Python to use this tool; the language is invisible after the build process.

Usage

JSON-LD Operations

You can either use the NodeJS require function, or ES6 import syntax, as follows.

import { nodeJsonLd } from 'node-jsonld';

const sourceFile = 'NameOfSourceFile.json';
const contextFile = 'NameOfContextFile.json';
const targetFile = 'NameOfTargetFile.json';

nodejsonLd.compact(sourceFile, contextFile, targetFile).then(res => console.log(res)); 

// res is 'successful' if the compact operation was successful
//     is an error message otherwise

If not specified, the default name for the target file is targetFile.json. Advanced users can specify options for the JSON-LD operations. If not specified, simple default options are used.

Help messages

Help messages are available for all operations. The messages closely follow the documentation of jsonld.js. You can use a getter to grab each help message as a string, and then display it however you wish, such as in a tooltip.

import { nodeJsonLd } from 'node-jsonld';

const compactHelpMessage = nodeJsonLd.getCompactHelpMessage(); // returns a string

Related

See also the JSON-LD NodeJS command line tool jldc, which uses related code. Both node-jsonld and jldc have as a peer dependency the official JSON-LD Javascript library jsonld.js, written and supported by DigitalBazaar. For Angular users, ngx-jsonld-provider is available, and Cory Rylan has written ngx-json-ld, which uses an Angular component (not a provider) to connect to JSON-LD operations.

API

JSON-LD Operations

All these operations are of type Promise<string>. If successful, they return the string 'successful'. If unsuccessful, they return an error code that usually specifies both the type of error and the file that caused the error. If not specified, the default name for the target file is targetFile.json.

Compact

compact(sourceFile: string, contextFile: string, targetFile?: string, options?: CompactOptions): Promise<string>

Runs the JSON-LD compact algorithm on the contents of sourceFile, using the context in contextFile, and writes the value to targetFile. Default value for options is {}.

Expand

expand(sourceFile: string, targetFile?: string, options?: ExpandOptions): Promise<string>

Run the JSON-LD expand algorithm on the contents of sourceFile. Write the result to targetFile. Default value for options is {}.

Flatten

flatten(sourceFile: string, targetFile?: string, options?: FlattenOptions): Promise<string>

Run the JSON-LD flatten algorithm on the contents of sourceFile. Write the result to targetFile. Default value for options is {}.

Frame

frame(sourceFile: string, frameFile: string, targetFile?: string, options?: FrameOptions): Promise<string>

Run the JSON-LD frame algorithm on the contents of sourceFile, using the frame in frameFile, then write the result to targetFile. Default value for options is {}.

Normalize

normalize(sourceFile: string, targetFile?: string, options?: NormalizeOptions): Promise<string>

Run the JSON-LD normalize algorithm (also called the canonize algorithm) on the contents of sourceFile. Default value for options is {format: 'application/n-quads'}.

Serialize

serialize(sourceFile: string, targetFile:?: string, options?: ToRdfOptions): Promise<string>

Converts the JSON-LD in sourceFile to an RDF string. Default value for options is {format: 'application/n-quads'}.

Help Message Getters

The following help message getters are available. Each returns a string that is based on the jsonld.js documentation.

getCompactHelpMessage(): string 
getDeserializeHelpMessage(): string
getExpandHelpMessage(): string 
getFlattenHelpMessage(): string 
getFrameHelpMessage(): string 
getNormalizeHelpMessage(): string 
getSerializeHelpMessage(): string