erdtman/canonicalize

Possible/Bug shortcoming

Closed this issue · 5 comments

cyberphone/json-canonicalization#10

This is hardly a major problem but it might be worth looking into.

Ill have a look, when looking it seems like arrays with undefined values are also different

JSON.canonicalize([undefined,null,123]) -> [undefined,null,123]
JSON.stringify([undefined,null,123]) -> [null,null,123]

I assume stringify is correct?

Anders could you have a look at this PR that should resolve the issue #2

On my local repo I added the following tests to simpletests as well (feel free to add them to the PR without attribution):

test('undefined', t => {
  const input = undefined;
  const expected = undefined;
  const actual = JSON.canonicalize(input);
  t.is(actual, expected);
});

test('null', t => {
  const input = null;
  const expected = 'null';
  const actual = JSON.canonicalize(input);
  t.is(actual, expected);
});

test('symbol', t => {
  const input = Symbol('hello world');
  const expected = undefined;
  const actual = JSON.canonicalize(input);
  t.is(actual, expected);
});

test('object with symbol value', t => {
  const input = { test: Symbol('hello world') };
  const expected = '{}';
  const actual = JSON.canonicalize(input);
  t.is(actual, expected);
});

test('object with number key', t => {
  const input = { 42: 'foo' };
  const expected = '{"42":"foo"}';
  const actual = JSON.canonicalize(input);
  t.is(actual, expected);
});

test('object with symbol key', t => {
  const input = { [Symbol('hello world')]: 'foo' };
  const expected = '{}';
  const actual = JSON.canonicalize(input);
  t.is(actual, expected);
});

They all pass on this branch except for object with symbol key.

Thanks @yacinehmito I added your tests and did some fixing regarding Symbol. Please have a look

Just published a new release 1.0.4 with the fixes discussed.