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

rdf-nx-parser

v1.0.3

Published

Non-validating tokenizer / parser for the RDF N-Triples and N-Quads serializations (or any “N-x”)

Downloads

518

Readme

rdf-nx-parser

A non-validating tokenizer and parser for the RDF N-Triples and N-Quads serializations (or any “N-x”).

Provides parsing of N-Triples and N-Quads from strings, or tokenizing any “N-x” string.

Coverage Status

Why?

There are enough parsers already that are faster (see last section), but having a parser for Node.js is useful for building smaller tools.

Usage

npm install --save rdf-nx-parser

The module exports a parser object:

var parser = require('rdf-nx-parser');

Parsing

Use parseTriple() to parse an N-Triples statement, parseQuads() for N-Quads. Both return an objects, or null if the input can't be parsed.

var quad = parser.parseQuad(
    '_:foo ' + 
    '<http://example.com/bar> ' + 
    '"\\u9B3C\\u8ECA"@jp ' + 
    '<http://example.com/baz> .'
);

console.log(JSON.stringify(quad, null, 4));
{
    "subject": {
        "type": "blankNode",
        "value": "foo"
    },
    "predicate": {
        "type": "iri",
        "value": "http://example.com/bar"
    },
    "object": {
        "type": "literal",
        "value": "鬼車",
        "language": "jp"
    },
    "graphLabel": {
        "type": "iri",
        "value": "http://example.com/baz"
    }
}

Literal objects can have an additional language or datatypeIri property.

The parser does not verify that the data adheres to the [grammar] 1. It will instead happily parse anything as good as it can:

> parser.parseQuad('<foo> <:///baz>     "bar"  <$!#]&> .');

{ subject: { type: 'iri', value: 'foo' },
  predicate: { type: 'iri', value: ':///baz' },
  object: { type: 'literal', value: 'bar' },
  graphLabel: { type: 'iri', value: '$!#]&' } }

You can optionally pass an options object to these methods as a second parameter, shown with the defaults here:

parser.parseTriple(input, {
    // Set to `true` to get unparsed strings as `value`
    //properties
    asString: false,  
    
    // Include the unparsed token as `valueRaw` property
    // when returning objects
    includeRaw: false,

    // Decode unicode escapes, `\uxxxx` and `Uxxxxxxxx`
    // (but not percent encoding or punycode)
    unescapeUnicode: true
});

Parsing a whole file of N-Triples / N-Quads lines can easily be done e. g. with Node's readline module, see the example.

Tokenization

An arbitrary number of “N-x” tokens can be extracted from a string into an array of token objects with the tokenize() method:

> parser.tokenize(
    '<foo> _:bar . "123"^^<http://example.com/int> ' +
    '"\u0068\u0065\u006C\u006C\u006F"@en-US . .'
);

[ { type: 'iri', value: 'foo' },
  { type: 'blankNode', value: 'bar' },
  { type: 'endOfStatement', value: '.' },
  { type: 'literal',
    value: '123',
    datatypeIri: 'http://example.com/int' },
  { type: 'literal',
    value: 'hello',
    language: 'en-US' },
  { type: 'endOfStatement', value: '.' },
  { type: 'endOfStatement', value: '.' } ]

Each token has at least a type and a value property. There are four token types: iri, literal, blankNode and endOfStatement (can be listed with the getTokenTypes() method).

Implementation

The implementation is based on regular expressions (to split the input into tokens) – they are pretty fast on V8. This regex-based implementation is faster than a previous simple state machine (that read the input in one scan). Seems like regexes can be compiled more effectively into machine code.

Node.js version support

Works with Node.js 0.10 and higher.

Tests

Run with: npm test (mocha, Chai, Istanbul)

Similar projects