JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 13
  • Score
    100M100P100Q49227F
  • License Apache-2.0

serialize Common Forms

Package Exports

  • commonform-serialize

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (commonform-serialize) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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') })