Skip to content

Commit

Permalink
Implement diffAsString function
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Oct 27, 2023
1 parent b9808ce commit 1ed4cf2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 16 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -29,6 +29,27 @@ uninspected.diff({ foo: 'bar' }, { foo: 'baz' });
}
```

Or if you want to get the rendered diff as a string:

```javascript
const diff = uninspected.diffAsString({ foo: 'bar' }, { foo: 'baz' });
```

You can also ask for the diff with ANSI codes, or in HTML format:

```javascript
const ansiDiff = uninspected.diffAsString(
{ foo: 'bar' },
{ foo: 'baz' },
'ansi'
);
const htmlDiff = uninspected.diffAsString(
{ foo: 'bar' },
{ foo: 'baz' },
'html'
);
```

You can also use it instead of the console object:

```javascript
Expand Down
36 changes: 20 additions & 16 deletions lib/uninspected.js
Expand Up @@ -23,7 +23,7 @@ uninspected.inspect = (obj, options) => {

uninspected.defaultDepth = 6; // util's default level of 2 is annoyingly low
uninspected.output = unexpected.output;
uninspected.createOutput = unexpected.createOutput;
uninspected.createOutput = unexpected.createOutput.bind(unexpected);
uninspected.outputFormat = unexpected.outputFormat();

if (uninspected.outputFormat === 'html') {
Expand All @@ -36,9 +36,9 @@ const isChrome =
typeof navigator.userAgent === 'string' &&
/chrome/i.test(navigator.userAgent);

function createOutput() {
function createOutput(format) {
return uninspected.createOutput(
isChrome ? 'coloredConsole' : uninspected.outputFormat
format || (isChrome ? 'coloredConsole' : uninspected.outputFormat)
);
}

Expand Down Expand Up @@ -71,23 +71,27 @@ uninspected.assert = (subject) => {
unexpected(subject, 'to be truthy');
};

uninspected.diff = (a, b) => {
const result = unexpected.diff(a, b, createOutput());
function renderDiff(a, b, format) {
const result = unexpected.diff(a, b, createOutput(format));
if (result) {
uninspected.log(result);
return createOutput(format).append(result);
} else if (unexpected.equal(a, b)) {
uninspected.log(a);
return createOutput(format).appendInspected(a);
} else {
uninspected.log(
unexpected.output
.clone()
.text('-')
.append(unexpected.inspect(a))
.nl()
.text('+')
.append(unexpected.inspect(b))
);
return createOutput(format)
.text('-')
.append(unexpected.inspect(a))
.nl()
.text('+')
.append(unexpected.inspect(b));
}
}

uninspected.diffAsString = (a, b, format) =>
renderDiff(a, b, format || 'text').toString();

uninspected.diff = (a, b) => {
uninspected.log(renderDiff(a, b));
};

// Support trace, time, timeEnd etc.
Expand Down
41 changes: 41 additions & 0 deletions test/uninspected.js
Expand Up @@ -71,6 +71,47 @@ describe('uninspected', () => {
});
});

describe('#diffAsString', () => {
it('should render a diff in text format by default', () => {
expect(
uninspected.diffAsString({ foo: 'bar' }, { foo: 'baz' }),
'to equal',
'{\n' +
" foo: 'bar' // should equal 'baz'\n" +
' //\n' +
' // -bar\n' +
' // +baz\n' +
'}'
);
});

it('should render a diff in ansi format if requested', () => {
expect(
uninspected.diffAsString({ foo: 'bar' }, { foo: 'baz' }, 'ansi'),
'to equal',
'{\n' +
" \x1b[90m\x1b[38;5;242mfoo\x1b[39m: \x1b[36m'bar'\x1b[39m \x1b[31m\x1b[1m//\x1b[22m\x1b[39m \x1b[31m\x1b[1mshould equal\x1b[22m\x1b[39m \x1b[36m'baz'\x1b[39m\n" +
' \x1b[31m\x1b[1m//\x1b[22m\x1b[39m\n' +
' \x1b[31m\x1b[1m//\x1b[22m\x1b[39m \x1b[41m\x1b[30mbar\x1b[39m\x1b[49m\n' +
' \x1b[31m\x1b[1m//\x1b[22m\x1b[39m \x1b[42m\x1b[30mbaz\x1b[39m\x1b[49m\n' +
'}'
);
});

it('should render the actual value if there is no diff', () => {
expect(
uninspected.diffAsString({ foo: 'bar' }, { foo: 'bar' }),
'to equal',
"{\n foo: 'bar'\n}"
);
});

it('should do something reasonable when diffing primitive values with no specific built-in diff', () => {
uninspected.outputFormat = 'text';
expect(uninspected.diffAsString(123, 456), 'to equal', '-123\n+456');
});
});

describe('#diff', () => {
beforeEach(() => {
sinon.stub(console, 'log');
Expand Down

0 comments on commit 1ed4cf2

Please sign in to comment.