jsondiffpatch icon indicating copy to clipboard operation
jsondiffpatch copied to clipboard

Formatters are not available after create

Open piomar123 opened this issue 9 years ago • 8 comments

In node.js after using

var jsondiffpatch = require('jsondiffpatch').create(options);

to create specialized instance, property formatters (as well as console) is undefined on jsondiffpatch object whereas without using create they work correctly. Not a big issue but took me some time to find the problem in the code. I couldn't find anything in the documentation saying it is not a bug, moreover it says this is a recommended way of using jsondiffpatch in node.js.

piomar123 avatar Sep 15 '15 20:09 piomar123

that's correct, formatters are not part of a jsondiffpatch instance. instances don't inherit all members of the static main module, I guess maybe that code snippet could make it more clear by not naming the created instance with the same name as the library?

var jsondiffpatch = require('jsondiffpatch');
var jsondiffpatchInstance = jsondiffpatch.create(options);

benjamine avatar Sep 17 '15 17:09 benjamine

That looks great and definitely makes it easier to understand the concept for new users. Especially, the line about initialization I was talking above may be misleading a little. Anyway, great project, keep it up [;

piomar123 avatar Sep 17 '15 19:09 piomar123

This worked for me (ES6 syntax):

import { diff } from 'jsondiffpatch';
import formatters from 'jsondiffpatch/src/formatters';

chinshr avatar Aug 31 '16 22:08 chinshr

I am trying to get access to the formatters using the recommended ways but have had no luck. Maybe they don't work in browserified environment? After requiring 'jsondiffpatch' the formatters property is undefined. I am using it within karma running a browserified app bundle.

bennidi avatar Sep 11 '16 16:09 bennidi

@chinshr apparently src disappeared from npm dist so:

import formatters from 'jsondiffpatch/src/formatters';

no longer works.

@benjamine any es6 import analogy to suggest for below?:

var jsondiffpatch = require('jsondiffpatch');
var jsondiffpatchInstance = jsondiffpatch.create(options);

sadly, i'm not up enough on modules to know why the following results in a null for jsondiffpatch:

import jsondiffpatch from 'jsondiffpatch'

tony-kerz avatar Apr 10 '18 04:04 tony-kerz

formatters continue to exist under the main module:

const jsondiffpatch = require('jsondiffpatch');
console.log(jsondiffpatch.formaters));

you can also try console.log(jsondiffpatch.formaters)); at https://benjamine.github.io/jsondiffpatch/demo/index.html (which uses the umd version)

@tony-kerz in ES modules you can import specific members, for example:

import { diff, formatters } from 'jsondiffpatch';
formatters.console.log(diff({ a: 23 }, { a: 3, b: 31 }));

benjamine avatar Apr 10 '18 13:04 benjamine

thanks @benjamine the following works great!:

import { diff, formatters } from 'jsondiffpatch';
formatters.console.log(diff({ a: 23 }, { a: 3, b: 31 }));

tony-kerz avatar Apr 12 '18 17:04 tony-kerz

I am unable to access the jsonpatch formatter (RFC 6902) from the npm package

marcusnewton avatar Jul 02 '20 04:07 marcusnewton

Hello, sorry for the delayed response!

I believe that formatters are available as described in this comment, so I'll go ahead and close this issue. Let me know if you're still having issues and I can reopen this.

Methuselah96 avatar Aug 26 '23 19:08 Methuselah96