commonform-serialize
v1.0.0
Published
serialize Common Forms
Downloads
14
Maintainers
Readme
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/
)
})