Skip to content

Commit 9ee8a26

Browse files
authored
Merge pull request #36 from mobinni/bugfix/function_regex
Fix for issue 35
2 parents e5ab17b + e7acb08 commit 9ee8a26

File tree

6 files changed

+139
-104
lines changed

6 files changed

+139
-104
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
.idea
12
node_modules
23
*.log

dist/json-formatter.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/json-formatter.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"karma-mocha-reporter": "^2.1.0",
3131
"karma-typescript-preprocessor": "^0.2.0",
3232
"karma-webpack": "^1.7.0",
33+
"less": "^2.7.1",
3334
"less-loader": "^2.2.3",
3435
"minimist": "^1.2.0",
3536
"mocha": "^3.0.2",

src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function getObjectName(object: Object):string {
2929
return 'Object';
3030
}
3131

32-
const funcNameRegex = /function (.{1,})\(/;
32+
const funcNameRegex = /function ([^(]*)/;
3333
const results = (funcNameRegex).exec((object).constructor.toString());
3434
if (results && results.length > 1) {
3535
return results[1];

test/spec.ts

Lines changed: 134 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -6,135 +6,168 @@ declare const expect;
66
declare const JSONFormatter;
77

88

9-
describe('null', ()=> {
10-
it('should render "null"', ()=> {
11-
const formatter = new JSONFormatter(null);
9+
describe('null', () => {
10+
it('should render "null"', () => {
11+
const formatter = new JSONFormatter(null);
1212

13-
expect(formatter.render().innerText).to.contain('null');
14-
});
13+
expect(formatter.render().innerText).to.contain('null');
14+
});
1515
});
1616

17-
describe('undefined', ()=> {
18-
it('should render "undefined"', ()=> {
19-
const formatter = new JSONFormatter(undefined);
17+
describe('undefined', () => {
18+
it('should render "undefined"', () => {
19+
const formatter = new JSONFormatter(undefined);
2020

21-
expect(formatter.render().innerText).to.contain('undefined');
22-
});
21+
expect(formatter.render().innerText).to.contain('undefined');
22+
});
2323
});
2424

25-
describe('function', ()=> {
26-
it('should render the function', ()=> {
27-
const formatter = new JSONFormatter(function add(a, b) { return a + b; });
28-
const elementText = formatter.render().innerText;
25+
describe('object function constructor', () => {
26+
it('should output "Format"', () => {
27+
function Format() {
28+
}
29+
const obj = new Format();
30+
31+
const formatter = new JSONFormatter(obj);
32+
expect(formatter.constructorName).to.equal('Format');
33+
});
2934

30-
expect(elementText).to.contain('function');
31-
expect(elementText).to.contain('add');
32-
expect(elementText).to.contain('(a, b)');
33-
expect(elementText.trim().match(/function\s[^\(]*\([^\)]*\)\s*(.*)/)[1]).to.equal('{…}');
34-
});
35+
it('should output "BrokenFormat"', () => {
36+
const failConstructor = 'function BrokenFormat() {Object.assign(}';
37+
const funcNameRegex = /function ([^(]*)/;
38+
const results = (funcNameRegex).exec(failConstructor.toString());
39+
expect(results[1]).to.equal('BrokenFormat');
40+
});
3541
});
3642

37-
describe('string', ()=> {
38-
it('should render "Hello World!"', ()=> {
39-
const formatter = new JSONFormatter('Hello World!');
4043

41-
expect(formatter.render().innerText).to.contain('Hello World');
42-
});
44+
describe('function', () => {
45+
it('should render the function', () => {
46+
const formatter = new JSONFormatter(function add(a, b) {
47+
return a + b;
48+
});
49+
const elementText = formatter.render().innerText;
50+
51+
expect(elementText).to.contain('function');
52+
expect(elementText).to.contain('add');
53+
expect(elementText).to.contain('(a, b)');
54+
expect(elementText.trim().match(/function\s[^\(]*\([^\)]*\)\s*(.*)/)[1]).to.equal('{…}');
55+
});
4356
});
4457

45-
describe('date string', ()=> {
46-
const formatter = new JSONFormatter(new Date(0).toString());
58+
describe('string', () => {
59+
it('should render "Hello World!"', () => {
60+
const formatter = new JSONFormatter('Hello World!');
4761

48-
it('should render "' + (new Date(0)).toString() + '"', ()=> {
49-
expect(formatter.render().innerText).to.contain('"' + (new Date(0)).toString() + '"');
50-
});
62+
expect(formatter.render().innerText).to.contain('Hello World');
63+
});
64+
});
65+
66+
describe('date string', () => {
67+
const formatter = new JSONFormatter(new Date(0).toString());
68+
69+
it('should render "' + (new Date(0)).toString() + '"', () => {
70+
expect(formatter.render().innerText).to.contain('"' + (new Date(0)).toString() + '"');
71+
});
5172

52-
it('should assing date class to date string', ()=> {
53-
const formatter = new JSONFormatter('2015-12-05T18:58:53.727Z');
54-
expect(formatter.render().querySelector('.json-formatter-date')).not.to.be.null;
55-
});
73+
it('should assing date class to date string', () => {
74+
const formatter = new JSONFormatter('2015-12-05T18:58:53.727Z');
75+
expect(formatter.render().querySelector('.json-formatter-date')).not.to.be.null;
76+
});
5677
});
5778

58-
describe('url string', ()=> {
59-
const formatter = new JSONFormatter('https://example.com');
79+
describe('url string', () => {
80+
const formatter = new JSONFormatter('https://example.com');
6081

61-
it('should render "https://example.com"', ()=> {
62-
expect(formatter.render().innerText).to.contain('"https://example.com"');
63-
});
82+
it('should render "https://example.com"', () => {
83+
expect(formatter.render().innerText).to.contain('"https://example.com"');
84+
});
6485

65-
it('should make a link and add class "url"', ()=> {
66-
expect(formatter.render().querySelector('a.json-formatter-url')).not.to.equal(null);
67-
});
86+
it('should make a link and add class "url"', () => {
87+
expect(formatter.render().querySelector('a.json-formatter-url')).not.to.equal(null);
88+
});
6889
});
6990

70-
describe('openAtDepth after rendering', ()=> {
71-
const formatter = new JSONFormatter({depth1: {depth2: {depth3 : {depth4: 21}}}}, Infinity, {animateOpen: false, animateClose: false});
72-
const element = formatter.render();
73-
74-
it('should open at depth 1', ()=> {
75-
formatter.openAtDepth();
76-
expect(element.outerHTML).to.contain('depth1');
77-
expect(element.outerHTML).to.not.contain('depth2');
78-
expect(element.outerHTML).to.not.contain('depth3');
79-
expect(element.outerHTML).to.not.contain('depth4');
80-
});
81-
82-
it('should collapses all', ()=> {
83-
formatter.openAtDepth(0);
84-
expect(element.outerHTML).to.not.contain('depth1');
85-
});
86-
87-
it('should expand all', ()=> {
88-
formatter.openAtDepth(Infinity);
89-
expect(element.outerHTML).to.contain('depth1');
90-
expect(element.outerHTML).to.contain('depth2');
91-
expect(element.outerHTML).to.contain('depth3');
92-
expect(element.outerHTML).to.contain('depth4');
93-
expect(element.outerHTML).to.contain('21');
94-
});
91+
describe('openAtDepth after rendering', () => {
92+
const formatter = new JSONFormatter({depth1: {depth2: {depth3: {depth4: 21}}}}, Infinity, {
93+
animateOpen: false,
94+
animateClose: false
95+
});
96+
const element = formatter.render();
97+
98+
it('should open at depth 1', () => {
99+
formatter.openAtDepth();
100+
expect(element.outerHTML).to.contain('depth1');
101+
expect(element.outerHTML).to.not.contain('depth2');
102+
expect(element.outerHTML).to.not.contain('depth3');
103+
expect(element.outerHTML).to.not.contain('depth4');
104+
});
105+
106+
it('should collapses all', () => {
107+
formatter.openAtDepth(0);
108+
expect(element.outerHTML).to.not.contain('depth1');
109+
});
110+
111+
it('should expand all', () => {
112+
formatter.openAtDepth(Infinity);
113+
expect(element.outerHTML).to.contain('depth1');
114+
expect(element.outerHTML).to.contain('depth2');
115+
expect(element.outerHTML).to.contain('depth3');
116+
expect(element.outerHTML).to.contain('depth4');
117+
expect(element.outerHTML).to.contain('21');
118+
});
95119
});
96120

97-
describe('openAtDepth before any rendering', ()=> {
98-
const formatter = new JSONFormatter({depth1: {depth2: {depth3 : {depth4: 21}}}}, Infinity, {animateOpen: false, animateClose: false});
121+
describe('openAtDepth before any rendering', () => {
122+
const formatter = new JSONFormatter({depth1: {depth2: {depth3: {depth4: 21}}}}, Infinity, {
123+
animateOpen: false,
124+
animateClose: false
125+
});
99126

100-
it('should open at depth 1', ()=> {
101-
formatter.openAtDepth();
102-
const element = formatter.render();
103-
expect(element.outerHTML).to.contain('depth1');
104-
expect(element.outerHTML).to.not.contain('depth2');
105-
expect(element.outerHTML).to.not.contain('depth3');
106-
expect(element.outerHTML).to.not.contain('depth4');
107-
});
127+
it('should open at depth 1', () => {
128+
formatter.openAtDepth();
129+
const element = formatter.render();
130+
expect(element.outerHTML).to.contain('depth1');
131+
expect(element.outerHTML).to.not.contain('depth2');
132+
expect(element.outerHTML).to.not.contain('depth3');
133+
expect(element.outerHTML).to.not.contain('depth4');
134+
});
108135
});
109136

110-
describe('toggleOpen after rendering', ()=> {
137+
describe('toggleOpen after rendering', () => {
111138

112-
it('should toggle', ()=> {
113-
const formatter = new JSONFormatter({depth1: {depth2: {depth3 : {depth4: 21}}}}, Infinity, {animateOpen: false, animateClose: false});
114-
const element = formatter.render();
139+
it('should toggle', () => {
140+
const formatter = new JSONFormatter({depth1: {depth2: {depth3: {depth4: 21}}}}, Infinity, {
141+
animateOpen: false,
142+
animateClose: false
143+
});
144+
const element = formatter.render();
115145

116-
expect(element.outerHTML).to.contain('Object');
117-
expect(element.outerHTML).to.contain('depth1');
118-
119-
formatter.toggleOpen();
120-
121-
expect(element.outerHTML).to.contain('Object');
122-
expect(element.outerHTML).to.not.contain('depth1');
123-
expect(element.outerHTML).to.not.contain('depth2');
124-
expect(element.outerHTML).to.not.contain('depth3');
125-
expect(element.outerHTML).to.not.contain('depth4');
126-
});
146+
expect(element.outerHTML).to.contain('Object');
147+
expect(element.outerHTML).to.contain('depth1');
148+
149+
formatter.toggleOpen();
150+
151+
expect(element.outerHTML).to.contain('Object');
152+
expect(element.outerHTML).to.not.contain('depth1');
153+
expect(element.outerHTML).to.not.contain('depth2');
154+
expect(element.outerHTML).to.not.contain('depth3');
155+
expect(element.outerHTML).to.not.contain('depth4');
156+
});
127157
});
128158

129-
describe('toggleOpen before any rendering', ()=> {
130-
it('should toggle', ()=> {
131-
const formatter = new JSONFormatter({depth1: {depth2: {depth3 : {depth4: 21}}}}, Infinity, {animateOpen: false, animateClose: false});
132-
formatter.toggleOpen();
133-
const element = formatter.render();
134-
expect(element.outerHTML).to.contain('Object');
135-
expect(element.outerHTML).to.not.contain('depth1');
136-
expect(element.outerHTML).to.not.contain('depth2');
137-
expect(element.outerHTML).to.not.contain('depth3');
138-
expect(element.outerHTML).to.not.contain('depth4');
159+
describe('toggleOpen before any rendering', () => {
160+
it('should toggle', () => {
161+
const formatter = new JSONFormatter({depth1: {depth2: {depth3: {depth4: 21}}}}, Infinity, {
162+
animateOpen: false,
163+
animateClose: false
164+
});
165+
formatter.toggleOpen();
166+
const element = formatter.render();
167+
expect(element.outerHTML).to.contain('Object');
168+
expect(element.outerHTML).to.not.contain('depth1');
169+
expect(element.outerHTML).to.not.contain('depth2');
170+
expect(element.outerHTML).to.not.contain('depth3');
171+
expect(element.outerHTML).to.not.contain('depth4');
139172
});
140173
});

0 commit comments

Comments
 (0)