Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

nested.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. var test = require('tape');
  3. var stringify = require('../');
  4. test('nested', function (t) {
  5. t.plan(1);
  6. var obj = { c: 8, b: [{z:6,y:5,x:4},7], a: 3 };
  7. t.equal(stringify(obj), '{"a":3,"b":[{"x":4,"y":5,"z":6},7],"c":8}');
  8. });
  9. test('cyclic (default)', function (t) {
  10. t.plan(1);
  11. var one = { a: 1 };
  12. var two = { a: 2, one: one };
  13. one.two = two;
  14. try {
  15. stringify(one);
  16. } catch (ex) {
  17. t.equal(ex.toString(), 'TypeError: Converting circular structure to JSON');
  18. }
  19. });
  20. test('cyclic (specifically allowed)', function (t) {
  21. t.plan(1);
  22. var one = { a: 1 };
  23. var two = { a: 2, one: one };
  24. one.two = two;
  25. t.equal(stringify(one, {cycles:true}), '{"a":1,"two":{"a":2,"one":"__cycle__"}}');
  26. });
  27. test('repeated non-cyclic value', function(t) {
  28. t.plan(1);
  29. var one = { x: 1 };
  30. var two = { a: one, b: one };
  31. t.equal(stringify(two), '{"a":{"x":1},"b":{"x":1}}');
  32. });
  33. test('acyclic but with reused obj-property pointers', function (t) {
  34. t.plan(1);
  35. var x = { a: 1 };
  36. var y = { b: x, c: x };
  37. t.equal(stringify(y), '{"b":{"a":1},"c":{"a":1}}');
  38. });