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

@frogcat/ttl2jsonld

v0.0.9

Published

Turtle to JSON-LD converter for node.js and browser, no library dependencies

Downloads

16,326

Readme

@frogcat/ttl2jsonld

Turtle to JSON-LD converter for node.js and browser, no library dependencies.

Demo

Online demo is available here.

With this converter, you can obtain Output JSON-LD from Input Turtle, as shown in JSON-LD 1.0 Specification .

Input Turtle

@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://manu.sporny.org/about#manu> a foaf:Person;
  foaf:name "Manu Sporny";
  foaf:homepage <http://manu.sporny.org/> .

Output JSON-LD

{
  "@context": {
    "foaf": "http://xmlns.com/foaf/0.1/"
  },
  "@id": "http://manu.sporny.org/about#manu",
  "@type": "foaf:Person",
  "foaf:name": "Manu Sporny",
  "foaf:homepage": { "@id": "http://manu.sporny.org/" }
}

Install

node.js

$ npm install @frogcat/ttl2jsonld

browser

<script src="https://frogcat.github.io/ttl2jsonld/ttl2jsonld.js"></script>

Usage

node.js

You can write your own code.

const ttl2jsonld = require('@frogcat/ttl2jsonld').parse;

const ttl = `@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://manu.sporny.org/about#manu> a foaf:Person;
  foaf:name "Manu Sporny";
  foaf:homepage <http://manu.sporny.org/> .
`;

const jsonld = ttl2jsonld(ttl);

console.log(JSON.stringify(jsonld,null,2));

Command line interface is also available.

$ npm install @frogcat/ttl2jsonld
$ ttl2jsonld {input_turtle} > {output_jsonld}
or
$ cat {input_turtle} | ttl2jsonld > {output_jsonld}

browser

This converter is exported to ttl2jsonld global object. Call ttl2jsonld.parse(ttl) to perform conversion.

<!-- include ttl2jsonld.js -->
<script src="https://frogcat.github.io/ttl2jsonld/ttl2jsonld.js"></script>

<!-- run script -->
<script>
const ttl = `@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<http://manu.sporny.org/about#manu> a foaf:Person;
  foaf:name "Manu Sporny";
  foaf:homepage <http://manu.sporny.org/> .
`;

const jsonld = ttl2jsonld.parse(ttl);

console.log(JSON.stringify(jsonld,null,2));
</script>

API

Given turtle_string, ttl2jsonld.parse returns JSON Object.

var json_object = ttl2jsonld.parse(turtle_string);

When you want to pass baseIRI, give second argument like this.

var json_object = ttl2jsonld.parse(turtle_string, {
  baseIRI : "http://example.org/"  
});

For developers

building

  • Build with PEG.js.
  • Main code is spec/ttl2jsonld.pegjs.
  • When you edit code, run npm run build to generate ttl2jsonld.js.

testing

  • Tested with rdf-test-suite.
  • Currently 294 / 298 tests succeeded.
  • Run npm run spec-turtle to start rdr-test-suite.
  • Other miscellaneous tests are located in test/test.js, npm run test to run.