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

jsonld-flatfile

v1.0.1

Published

stringify jsonld

Downloads

4

Readme

jsonld-flatfile

logo

Similar Projects

Q/A

Why use jsonld-flatfile instead of JSON.stringify?

This package's stringification creates deterministic output from human-authored input. Equivalent data results in equivalent serializations. JavaScript implementations and some REPLs maintain an insertion order on Object properties. Object.keys and JSON.stringify preserve that order in their output. However, other REPLs and debuggers present Object properties ordered lexicographically. The JSON-LD parser is indifferent to property order, and does not guarantee a stable sort on Object properties in output. This package's stringification enforces a lexicographic sort order on Object properties by default. Re-evaluated output is guareenteed line-by-line equivalence in text-editors and runtimes. Modified output in SCM always has minimal changesets in diff reports; no extra lines due to an unstable sort. Forgo meaningless peturbations and save time by sorting.

Why does stringify use jsonld.flatten?

Flattened form gives a stable sort on node order: lexicographically by @id value. It polarizes property direction by removing @reverse aliases and objects. Flattened form is also compacted form; it's more amenable to editing than expanded form.

Flattening collects all properties of a node in a single JSON object and labels all blank nodes with blank node identifiers. This ensures a shape of the data and consequently may drastically simplify the code required to process JSON-LD in certain applications. --JSON-LD 1.0

What does the output look like?

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  },
  "@graph": [
    {
      "@id": "_:b0",
      "rdf:type": {
        "@id": "http://localhost/o"
      }
    }
  ]
}

test/assets/src/default_graph.json

See JSON-LD Flattening

What should the input be?

With a single argument, the object should define the @context property. Otherwise, use options.ctx. Save test file in node_modules/jsonld-flatfile/test/assets/src and run:

cd node_modules/jsonld-flatfile
npm install
gulp proc
npm test

Modules/API

plugin

stringify in a vinyl object transform.

var gulp = require( 'gulp' )
var stringify = require( 'jsonld-flatfile/plugin' )

gulp.task( 'myproc', function () {
    var proc = stringify().on( 'error', log_json )
    return gulp.src( [ 'test/assets/src/test.json' ] )
      .pipe( proc )
      .pipe( gulp.dest( 'test/assets/flatten1/' ) )
  }
)

function log_json() {
  var o = JSON.stringify( arguments[0], null, 2 )
  console.log( o )
}

stringify

Flatten, compact, sort, and format JSON-LD as a String using jsonld.flatten and json-stable-stringify, and append a newline.

var stringify = require( 'jsonld-flatfile/stringify' )

stringify( jsonld, options, callback )

where:

  • jsonld is an Object or String
  • options is an Object with the optional properties:
    • cmp, a Function synchronously called during stringification (default null)
    • ctx, a JSON-LD context applied during compacting (default undefined, uses the jsonld parameter)
    • pre, a Function asynchronously called before stringification (default nop)
    • post, a Function asynchronously called after stringification (default nop)
    • replacer, a filtering Function or whitelist Array for JSON.stringify (default null)
    • space, the number of spaces to indent in JSON.stringify (default 2)
  • callback an asynchronous return function

and returns:

  • undefined / use a callback argument

options.cmp

passed through, see opts.cmp

options.ctx

a JSON-LD context object

options.pre

must be a function with the signature:

function ( error, json, callback )
  callback( error, json )
}

where:

  • error will always be null
  • json is an object instance
  • callback is a function back into stringify

and returns:

  • undefined / use callback
options.post

must be a function with signature:

function ( error, jsonld, callback )
  callback( error, jsonld )
}

where:

  • error may contain parsing errors from JSON-LD
  • jsonld is an object instance
  • callback is a function back into stringify
options.replacer

passed through, see opts.replacer

options.space

passed through, see opts.space