Skip to content

Commit 10b2db2

Browse files
committed
Finish promitives
1 parent 1d2804c commit 10b2db2

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

src/index.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,51 @@
11
'use strict';
22

33
function convert (json, options = {}) {
4-
return {};
4+
5+
if (json === undefined) {
6+
return {};
7+
}
8+
9+
// primitives
10+
if (typeof json === 'string') {
11+
12+
// TODO: date format
13+
return {type: 'string'};
14+
}
15+
16+
if (typeof json === 'boolean') {
17+
return {type: 'boolean'};
18+
}
19+
20+
if (typeof json === 'number') {
21+
if (Number.isInteger(json)) {
22+
return {type: 'integer'};
23+
} else {
24+
return {type: 'number'};
25+
}
26+
}
27+
28+
if (json === null) {
29+
return {type: 'null'};
30+
}
31+
32+
if (Array.isArray(json)) {
33+
let schema = {type: 'array'};
34+
35+
if (!json.length) {
36+
return schema;
37+
}
38+
39+
// TODO: array items
40+
}
41+
42+
let schema = {type: 'object'};
43+
44+
if (!Object.keys(json).length) {
45+
return schema;
46+
}
47+
48+
549
}
650

751
export {convert};

test/driver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import expect from './expect.js';
77

88
describe('end to end', ()=> {
99

10-
readdirSync(join(__dirname, 'end-to-end')).forEach(testCase=> {
10+
readdirSync(join(__dirname, 'input-output')).forEach(testCase=> {
1111

1212
it(`preserves comments and styling for test case ${testCase}`, ()=> {
1313

test/expect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ export default function expecter(json, jsonSchema) {
77
it('produces correct JSON Schema', ()=> {
88
expect(convert(json)).to.deep.equal(jsonSchema);
99
});
10-
};
10+
};

test/primitives.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,37 @@ import expect from './expect';
55

66
describe('primitives', ()=> {
77

8+
describe('undefined', ()=> {
9+
expect(undefined, {});
10+
});
11+
812
describe('string', ()=> {
13+
expect('', {type: 'string'});
914
expect('a string', {type: 'string'});
1015
});
1116

1217
describe('boolean', ()=> {
1318
expect(true, {type: 'boolean'});
1419
});
1520

21+
describe('integer number', ()=> {
22+
expect(100, {type: 'integer'});
23+
expect(0, {type: 'integer'});
24+
});
25+
26+
describe('float number', ()=> {
27+
expect(100.0001, {type: 'number'});
28+
});
29+
30+
describe('infinity number', ()=> {
31+
expect(Infinity, {type: 'number'});
32+
});
33+
34+
describe('empty array', ()=> {
35+
expect([], {type: 'array'});
36+
});
37+
38+
describe('empty object', ()=> {
39+
expect({}, {type: 'object'});
40+
});
1641
});

0 commit comments

Comments
 (0)