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

immutable-jsonld

v2.1.1

Published

Immutable JSON-LD data structures and utility functions

Downloads

18

Readme

fromJSONLD

Use fromJSONLD to asynchronously load JSON-LD from a URL or JavaScript objects or arrays. Returns an Immutable.List of JSONLDNodes.

var IJLD = require('immutable-jsonld')
IJLD.fromJSONLD({'@context': {name: 'http://xmlns.com/foaf/0.1/name'},
                 '@id': 'http://viaf.org/viaf/61794068', 'name': 'Kanye'})
    .then(function(nodes) { console.log(nodes) })

List [ JSONLDNode { "@id": "http://viaf.org/viaf/61794068", "http://xmlns.com/foaf/0.1/name": List [ JSONLDValue { "@value": "Kanye" } ] } ]

JSONLDValue

A JSONLDValue is just an Immutable.Map interface to the expanded form of a JSON-LD value object, with a few convenience methods and getters added:

var IJLD = require('immutable-jsonld')
var value = IJLD.JSONLDValue(
  { '@value': '頑張れ日本'
  , '@type': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString'
  , '@language': 'ja'
  }
)
console.log(value.value)
console.log(value.type)
console.log(value.language)

頑張れ日本

http://www.w3.org/1999/02/22-rdf-syntax-ns#langString

ja

JSONLDNode

A JSONLDNode is just an Immutable.Map interface to the expanded form of a JSON-LD node object, with a few convenience methods and getters added:

var IJLD = require('immutable-jsonld')
IJLD.fromJSONLD({'@context': {name: 'http://xmlns.com/foaf/0.1/name'},
                 '@id': 'http://viaf.org/viaf/61794068', 'name': 'Kanye',
                 '@type': 'http://xmlns.com/foaf/0.1/Person'})
    .then(function(nodes) {
      node = nodes.first()
      console.log(node.id)
      console.log(node.types)
      console.log(nodes.propertySeq())
    })

http://viaf.org/viaf/61794068

Set { "http://xmlns.com/foaf/0.1/Person" }

Seq [ http://xmlns.com/foaf/0.1/name, List [ JSONLDValue { "@value": "Kanye" } ] ]

childNodes() returns an Immutable.List of predicate-JSONLDNode pairs (these pairs are also Immutable.Lists), one for each node object value of the node's properties. (See the source JSON-LD for these examples.)

var IJLD = require('immutable-jsonld')
var url = 'https://gist.githubusercontent.com/rybesh/3cbacf6cbc539b7c22f7/raw/2c15ecbd3e878dd40523fa1ad8c70f004a1bb193/stupid.json'
IJLD.fromJSONLD(url).then(function(nodes) {
  console.log(nodes.first().childNodes())
})

List [ List [ "http://stupid.com/wheels", JSONLDNode { "http://stupid.com/hubcap": List [ JSONLDNode { "http://stupid.com/color": List [ JSONLDValue { "@value": "black" } ] } ], "http://stupid.com/location": List [ JSONLDValue { "@value": "front right" } ] } ], List [ "http://stupid.com/wheels", JSONLDNode { "http://stupid.com/hubcap": List [ JSONLDNode { "http://stupid.com/color": List [ JSONLDValue { "@value": "unknown" } ], "http://stupid.com/status": List [ JSONLDValue { "@value": "missing" } ] } ], "http://stupid.com/location": List [ JSONLDValue { "@value": "front left" } ] } ], List [ "http://stupid.com/wheels", JSONLDNode { "http://stupid.com/hubcap": List [ JSONLDNode { "http://stupid.com/color": List [ JSONLDValue { "@value": "black" } ] } ], "http://stupid.com/location": List [ JSONLDValue { "@value": "rear right" } ] } ], List [ "http://stupid.com/wheels", JSONLDNode { "http://stupid.com/hubcap": List [ JSONLDNode { "http://stupid.com/color": List [ JSONLDValue { "@value": "black" } ] } ], "http://stupid.com/location": List [ JSONLDValue { "@value": "rear left" } ] } ] ]

descendantNodes() returns an Immutable.List of property path-JSONLDNode pairs (these pairs are Immutable.Lists), one for the node itself (i.e. the node reached via the empty property path), and one for every other node nested within it (in depth-first order). The property paths are also Immutable.Lists.

var IJLD = require('immutable-jsonld')
var url = 'https://gist.githubusercontent.com/rybesh/3cbacf6cbc539b7c22f7/raw/2c15ecbd3e878dd40523fa1ad8c70f004a1bb193/stupid.json'
IJLD.fromJSONLD(url).then(function(nodes) {
  console.log(nodes.first().descendantNodes()
                           .map(function (pair) { return pair.get(0) }))
})

List [ List [], List [ "http://stupid.com/wheels" ], List [ "http://stupid.com/wheels", "http://stupid.com/hubcap" ], List [ "http://stupid.com/wheels" ], List [ "http://stupid.com/wheels", "http://stupid.com/hubcap" ], List [ "http://stupid.com/wheels" ], List [ "http://stupid.com/wheels", "http://stupid.com/hubcap" ], List [ "http://stupid.com/wheels" ], List [ "http://stupid.com/wheels", "http://stupid.com/hubcap" ] ]

getAt() takes a property path and returns an Immutable.Set of JSONLDNodes.

var IJLD = require('immutable-jsonld')
var url = 'https://gist.githubusercontent.com/rybesh/3cbacf6cbc539b7c22f7/raw/2c15ecbd3e878dd40523fa1ad8c70f004a1bb193/stupid.json'
IJLD.fromJSONLD(url).then(function(nodes) {
  console.log(nodes.first().getAt(
    [ 'http://stupid.com/wheels'
    , 'http://stupid.com/hubcap'
    , 'http://stupid.com/color'
    ]))
})

Set { JSONLDValue { "@value": "black" }, JSONLDValue { "@value": "unknown" } }

push() takes a predicate URI or JSON-LD keyword and appends the supplied object to the appropriate property.

JSONLDNode().push('http://xmlns.com/foaf/0.1/name', JSONLDValue { "@value": "Coltrane" })

JSONLDNode { "http://xmlns.com/foaf/0.1/name": List [ JSONLDValue { "@value": "Coltrane" } ] }

JSONLDNode().push('@type', 'http://xmlns.com/foaf/0.1/Agent')

JSONLDNode { "http://xmlns.com/foaf/0.1/Agent": List [ "http://xmlns.com/foaf/0.1/Agent" ] }

preferredLabel() returns a usable label, if one exists.

JSONLDNode()
  .push('http://www.w3.org/2000/01/rdf-schema#label', JSONLDValue { "@value": "something" })
  .preferredLabel()

something

JSONLDNode()
  .push('http://www.w3.org/2000/01/rdf-schema#label', JSONLDValue { "@value": "something" })
  .push('http://www.w3.org/2004/02/skos/core#prefLabel', JSONLDValue { "@value": "something else" })
  .preferredLabel()

something else

JSONLDNode()
  .push('http://www.w3.org/2000/01/rdf-schema#label',
        JSONLDValue { "@value": "English", "@language": "en" })
  .push('http://www.w3.org/2000/01/rdf-schema#label',
        JSONLDValue { "@value": "日本語", "@language": "ja" })
  .preferredLabel('ja')

日本語

See the tests for more examples.

Key path validation

Using fromJSONLD() will always return a list of JSONLDNodes that, when serialized back to JSON, will result in valid JSON-LD. However, it is possible to programmatically create a JSONLDNode that, when serialized back to JSON, is not valid JSON-LD. To help you avoid doing this, the JSONLDNode methods for making persistent changes will do some validation of the key path you provide, and throw an exception if your change would result in an invalid serialization. If the NODE_ENV environment variable is set to production, this validation will not be performed.

setIn() and updateIn() key path handling

With the implementations of setIn() and updateIn() in Immutable.Map, a new Immutable.Map is created at each key that does not exist. The implementations of setIn() and updateIn() in JSONLDNode differ such that if any keys in the key path (prior to the last) do not exist, an exception will be thrown. This is to help prevent the creation of structures that would result in invalid expanded JSON-LD. If the NODE_ENV environment variable is set to production, these checks will not be performed.