Skip to content

Commit

Permalink
Merge pull request #32 from vitaly-t/virtual-properties
Browse files Browse the repository at this point in the history
implementing #31
  • Loading branch information
vitaly-t committed May 13, 2020
2 parents a49c066 + 327c71e commit cd9f7c4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {inspect} from 'util';
import {EOL} from 'os';
import {HostType, IConnectionDefaults, IEncodingOptions, IHost, IParsedHost} from './types';
import {decode, encode, hasText, fullHostName, parseHost, validateUrl} from './static';

Expand Down Expand Up @@ -330,4 +332,26 @@ export class ConnectionString {
desc.enumerable = false;
Object.defineProperty(ConnectionString.prototype, prop, desc);
});

let inspecting = false;
// istanbul ignore else
if (inspect?.custom) {
Object.defineProperty(ConnectionString.prototype, inspect.custom, {
value: function () {
if (inspecting) {
return this;
}
inspecting = true;
const src = inspect(this, {colors: true});
const vp = inspect({
hostname: this.hostname,
port: this.port,
type: this.type
},
{colors: true});
inspecting = false;
return `${src}${EOL}Virtual Properties: ${vp}`;
}
});
}
})();
19 changes: 19 additions & 0 deletions test/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {inspect} from 'util';
import {EOL} from 'os';
import {expect} from './header';
import {ConnectionString, IConnectionDefaults, HostType, IHost, IParsedHost} from '../dist';

Expand All @@ -21,6 +23,11 @@ function create(defaults: IConnectionDefaults): string {
return (new ConnectionString('', defaults)).toString();
}

function removeColors(text: string) {
/*eslint no-control-regex: 0*/
return text.replace(/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/g, '');
}

const portErrMsg = (txt: string) => 'Invalid port: "' + txt + '". Valid port range is: [1...65535]';

describe('constructor', () => {
Expand Down Expand Up @@ -648,3 +655,15 @@ describe('parseHost', () => {
expect(parseHost('a\r\nb')).to.eql({name: 'a', type: 'domain'});
});
});

describe('inspection', () => {
const cs = parse('');
const out1 = inspect(cs);
const out2 = removeColors(out1);
it('must include virtual properties', () => {
expect(out2).to.eq(`ConnectionString {}${EOL}Virtual Properties: { hostname: undefined, port: undefined, type: undefined }`);
});
it('must produce color output', () => {
expect(out1).to.not.eq(out2);
});
});

0 comments on commit cd9f7c4

Please sign in to comment.