1.0.0 • Published 5 years ago

gronk v1.0.0

Weekly downloads
83
License
MIT
Repository
github
Last release
5 years ago

Gronk

Updated version of Gron which makes JSON greppable, now also works as a Node module.

Unlike the Gron package, this version exports a function for use within code rather than just a binary tool.

As a module within Node:

gronk({
	foo: {
		bar: {
			baz: [
				1,
				2,
				{
					quz: 'Quz!',
				},
			],
		},
	},
}) //=

foo = {}
foo.bar = {}
foo.bar.baz = []
foo.bar.baz.0 = 1
foo.bar.baz.1 = 2
foo.bar.baz.2 = {}
foo.bar.baz.2.quz = "Quz!"

As a command line tool:

> cat test/data/complex.json | gronk
foo = "Foo!"
bar = []
bar.0 = 1
bar.1 = 2
bar.2 = {}
bar.2.baz = []
bar.2.baz.0 = 1
bar.2.baz.1 = 2
bar.2.baz.2 = 3
quz = []
quz.0 = []
quz.0.0 = 10
quz.0.1 = 20
quz.0.2 = 30
quz.1 = 40
flarp = {}
flarp.boink = "Boink!"

Differences from Gron

This module has a few differences from the standard Gron binary:

  1. Path formatting uses dotted notation instead of JS notation. For example foo.1.bar.2.baz instead of foo[1].bar[2].baz. If you would like this format set {dotted: true} in the options.
  2. Recursion detection (disable with {detectRecursion: false} if you really need to)
  3. Type detection + serialization system - the base module provides a set of base types which can be expanded for custom output

API

This module exports two items, the main function and a defaults object which allows global alteration of the default settings.

gronk(data, options)

Accept a complex data object and return a dotted notation path + values.

Valid options are:

KeyTypeDefaultDescription
wantstring'string'How to return the output, values are 'array', 'object' and 'string'
dottedbooleantrueUse dotted notation rather than JS notation
stubArraystring'[]'How to show the marker for an array entry, set this to falsy to disable
stubObjectstring'{}'How to show the marker for an object entry, set this to falsy to disable
detectRecursionbooleantrueIf recursion is detected, stop processing. Disable this only if you are absolutely sure the input is not circular
baseTypesbooleantrueUse the inbuilt type system which supports only Object, Array, String and Number if disabled the type system lookup gets used instead
typeDetailbooleanfalseUse full type values, rather than abbreivating them as a digest (If enabled buffers are output as full Base64 rather than just their length)
formatfunctionSee codeFormatting function to use when processing each 'line', only used when want is 'array' or 'string'
formatPathfunctionSee codeFormatting function to use to encode the path portion of each item
formatDatafunctionSee codeFormatting function to use to encode the data portion of each item
formatStringfunctionSee codeUsed when want is 'string' to format a processed string, by default this just adds '\n' to the end
typesarraySee codeDefines the type detection + serialization system when {baseTypes: false}

gronk.defaults

Object containing the global Gronk defaults.