Nucleic Photon Magnetizer

    commonform-serialize

    1.0.0 • Public • Published

    commonform-serialize

    serialize Common Forms

    This package defines a uniform, deterministic way to serialize Common Form objects, so they can be hashed consistently.

    var serialize = require('commonform-serialize')

    Sorts object keys:

    var assert = require('assert')
    assert.deepEqual(
      serialize.stringify({ a: '1', b: '2' }),
      serialize.stringify({ b: '2', a: '1' })
    )

    Outputs valid JSON String, Object, and Array:

    var a1 = { a: '1' }
    assert.deepEqual(
      JSON.parse(serialize.stringify(a1)),
      a1
    )
     
    var a12 = { a: [ '1', '2' ] }
    assert.deepEqual(
      JSON.parse(serialize.stringify(a12)),
      a12
    )
     
    var aEmpty = { a: [ ] }
    assert.deepEqual(
      JSON.parse(serialize.stringify(aEmpty)),
      aEmpty
    )

    Escapes quotation marks:

    assert.deepEqual(
      serialize.stringify({ a: '"this is a test"' }),
      '{"a":"\\"this is a test\\""}'
    )

    Throw errors for non-String, non-Object, non-Array content:

    var invalidValues = {
      boolean: true,
      number: 1,
      null: null,
      undefined: void 0
    }
     
    Object.getOwnPropertyNames(invalidValues).map(function (type) {
      assert.throws(
        function () {
          serialize.stringify({ a: invalidValues[type] })
        },
        /argument to stringify contains other than object, array, or string/
      )
    })

    Install

    npm i commonform-serialize

    DownloadsWeekly Downloads

    4

    Version

    1.0.0

    License

    BlueOak-1.0.0

    Unpacked Size

    4.6 kB

    Total Files

    4

    Last publish

    Collaborators

    • kemitchell