benjamine / jsondiffpatch

Diff & patch JavaScript objects
MIT License
4.86k stars 473 forks source link

map all properties of an json [ jsondiffpatch.formatters.html.format] #195

Open roberto-slopez opened 7 years ago

roberto-slopez commented 7 years ago

I need map all properties of json, currently only iterates on the properties of modified. Properties that do not have modifications have a value in string

benjamine commented 7 years ago

hi roberto, in order to better understand the idea, can you share an example of input (left and right json) and expected output (what would like to obtain)?

roberto-slopez commented 7 years ago

Left

{
  "colors": {
    "blueviolet": "#8a2be2",
    "brown": "#a52a2a"
  },
  "countries": [
    {
      "name": "American Samoa",
      "code": "AS"
    },
    {
      "name": "AndorrA",
      "code": "AD"
    },
    {
      "name": "Angola",
      "code": "AO"
    },
    {
      "name": "Anguilla",
      "code": "AI"
    }
  ]
}

Right

{
  "colors": {
    "aliceblue": "#f0f8ff",
    "antiquewhite": "#faebd7",
    "aqua": "#00ffff",
    "aquamarine": "#7fffd4",
    "azure": "#f0ffff",
    "beige": "#f5f5dc",
    "bisque": "#ffe4c4",
    "black": "#000000",
    "blanchedalmond": "#ffebcd",
    "blue": "#0000ff",
    "blueviolet": "#8a2be2",
    "brown": "#a52a2a"
  }
}

In the output the color section has html elements for each property of the object, but not countries, I would like countries to also generate elements by property

benjamine commented 7 years ago

what is the html you're getting now? because the html formatter should be outputing all the unchanged parts, you can see this at work at the DEMO page linked in the README, unchanged parts show up grayed out (showing/hiding unchanged values can be toggled with css)

roberto-slopez commented 7 years ago

When there are changes inside an object nodes are created < li > but when you delete a property from an object and it contains an array, the array is displayed as text and not as nodes < li >, I'm trying to add translation to some values ​​but I can only get the nodes < li >, but when it's text, I find this difficult

benjamine commented 7 years ago

I see, yes when a whole property has been added or removed, that's considered an atomic "value" (jsondiffpatch won't drill down on those). if you want you could modify how an unchanged value is formatted by changing this function: https://github.com/benjamine/jsondiffpatch/blob/master/src/formatters/html.js#L132

you can do that by creating a new formatter, inherit from the HtmlFormatter (as it inherits from BaseFormatter), and only override format_unchanged method.