Skip to content

Commit 29f9125

Browse files
committed
monorepo
1 parent e0629d9 commit 29f9125

File tree

24 files changed

+2463
-201
lines changed

24 files changed

+2463
-201
lines changed

examples/tutorial/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "@json-spec/exapmle-turorial",
3+
"version": "0.1.0-SNAPSHOT",
4+
"private": true,
5+
"scripts": {
6+
"tut1": "node src/tut1.js"
7+
},
8+
"dependencies": {
9+
"@json-spec/core": "0.1.0-SNAPSHOT",
10+
"@json-spec/spec-basic": "0.1.0-SNAPSHOT",
11+
"@json-spec/spec-range": "0.1.0-SNAPSHOT",
12+
"@json-spec/spec-profiles": "0.1.0-SNAPSHOT"
13+
}
14+
}

examples/tutorial/src/tut1.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const s = require('@json-spec/core');
2+
const gen = require('@json-spec/core/gen');
3+
const sp = require('@json-spec/spec-profiles');
4+
const sb = require('@json-spec/spec-basic');
5+
6+
/*
7+
Specification of a person object.
8+
*/
9+
const person = s.object({
10+
firstName: sp.name({ size: 100 }),
11+
lastName: sp.name({ size: 100 }),
12+
birthDay: sp.birthDay,
13+
postalCd: sp.postalCode_JP,
14+
languages: s.array([
15+
"C", "C++", "Java"
16+
])
17+
});
18+
19+
console.log(gen.sample(s.gen(person)));

lib/core/gen.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const gen = require('./src/gen');
2+
module.exports = { ...gen };

lib/core/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@json-spec/core",
3+
"version": "0.1.0-SNAPSHOT",
4+
"main": "./src/index.js",
5+
"dependencies": {
6+
"@json-spec/testcheck": "^0.1.0-SNAPSHOT"
7+
}
8+
}

lib/core/src/__tests__/spec.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const s = require('../');
2+
const gen = require('../gen');
3+
const _ = require('lodash');
4+
const { I64 } = require('n64');
5+
6+
describe('conforms', () => {
7+
test('conform', () => {
8+
expect(s.conform(x => x%2 === 0, 1000)).toBe(1000);
9+
})
10+
11+
test('conform invalid', () => {
12+
expect(s.conform(x => x%2 === 0, 1001)).toBe(s.INVALID);
13+
})
14+
15+
test('and', () => {
16+
const spec = s.and(x => !isNaN(Number(x)), x => x > 1000)
17+
expect(s.conform(spec, 1001)).toBe(1001);
18+
})
19+
});
20+
21+
describe('composing predicates', () => {
22+
test('big-even', () => {
23+
const bigEven = s.and(x => !isNaN(Number(x)), x => x%2 === 0, x => x > 1000);
24+
expect(s.isValid(bigEven, "foo")).toBe(false);
25+
expect(s.isValid(bigEven, 10 )).toBe(false);
26+
expect(s.isValid(bigEven, 10000)).toBe(true);
27+
})
28+
});

src/spec.js renamed to lib/core/src/base.js

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
1-
const g = require('./spec/gen');
1+
const g = require('./gen');
22

33
class Invalid {
44
toString() {
55
return "INVALID";
66
}
77
}
8+
89
const INVALID = new Invalid();
910

11+
function gensub(specOrFunc, overrides, path, rmap, form) {
12+
const spec = specize(specOrFunc);
13+
const generator = spec.gen(overrides, path, rmap);
14+
if (generator) {
15+
return g.suchThat(x => isValid(spec, x), generator, 100);
16+
} else {
17+
throw(`Unable to construct gen at ${path}`);
18+
}
19+
}
20+
21+
class Spec {
22+
constructor(gfn) {
23+
this.gfn = gfn;
24+
}
25+
26+
withGen(gfn) {
27+
this.gfn = gfn;
28+
}
1029

11-
function Spec(form, pred, gfn, cpred) {
12-
this.form = form;
13-
this.pred = pred;
14-
this.gfn = gfn;
15-
this.cpred = cpred;
1630
}
1731

18-
Spec.prototype = {
19-
conform: function(x) {
20-
const ret = this.pred.call(this, x);
32+
class ScalarSpec extends Spec {
33+
constructor(form, pred, gfn, cpred) {
34+
super(gfn);
35+
this.form = form;
36+
this.pred = pred;
37+
this.cpred = cpred;
38+
}
39+
40+
conform(x) {
41+
let ret;
42+
if (this.pred instanceof Array) {
43+
ret = this.pred.includes(x);
44+
} else {
45+
ret = this.pred.call(this, x);
46+
}
2147
return this.cpred ? ret : ret ? x : INVALID;
22-
},
23-
explain: function(path, via, in_, x) {
48+
}
49+
50+
explain(path, via, in_, x) {
2451
if (this.conform(x) === INVALID) {
2552
return [{
2653
path: path,
@@ -31,49 +58,19 @@ Spec.prototype = {
3158
}];
3259
}
3360
return null;
34-
},
35-
gen: function() {
36-
return this.gfn ? this.gfn() : g.genForPred(this.pred)
37-
},
38-
withGen: function(gfn) {
39-
this.gfn = gfn;
4061
}
41-
}
4262

43-
44-
function specImpl(form, pred, gfn, cpred) {
45-
return new Spec(form, pred, gfn, cpred);
46-
}
47-
48-
function specize(spec) {
49-
if (spec instanceof Spec) {
50-
return spec;
51-
} else {
52-
return specImpl(spec, spec);
53-
}
54-
}
55-
56-
function gensub(specOrFunc, overrides, path, rmap, form) {
57-
const spec = specize(specOrFunc);
58-
const generator = spec.gen(overrides, path, rmap);
59-
if (generator) {
60-
return g.suchThat(x => isValid(spec, x), generator, 100);
61-
} else {
62-
throw(`Unable to construct gen at ${path}`);
63+
gen() {
64+
return this.gfn ? this.gfn() : g.genForPred(this.pred)
6365
}
6466
}
6567

66-
function dt(pred, x, form, cpred) {
67-
68-
}
69-
7068
class AndSpec extends Spec {
7169
constructor(forms, preds, gfn) {
72-
super();
70+
super(gfn);
7371
this.forms = forms;
7472
this.preds = preds;
7573
this.specs = preds.map(x => specize(x))
76-
this.gfn = gfn;
7774
}
7875

7976
conform(x) {
@@ -128,8 +125,8 @@ class ArraySpec extends Spec {
128125
}
129126

130127
class ObjectSpec extends Spec {
131-
constructor(predObj) {
132-
super();
128+
constructor(predObj, gfn) {
129+
super(gfn);
133130
this.predObj = predObj;
134131
}
135132

@@ -150,12 +147,35 @@ class ObjectSpec extends Spec {
150147
if (this.gfn) return this.gfn();
151148
const generators = {};
152149
for (const k in this.predObj) {
153-
generators[k] = gen(this.predObj[k]);
150+
generators[k] = gensub(this.predObj[k], overrides, [...path, k], rmap, this.predObj[k]);
154151
}
155152
return g.genObject(generators);
156153
}
157154
}
158155

156+
157+
function specize(spec) {
158+
if (spec instanceof Spec) {
159+
return spec;
160+
} else {
161+
return new ScalarSpec(spec, spec, null, null);
162+
}
163+
}
164+
165+
function spec(form, opts = {}) {
166+
if (form) {
167+
if (form instanceof Spec) {
168+
if (opts['gen']) {
169+
form.withGen(opts['gen']);
170+
}
171+
return form;
172+
} else {
173+
return new ScalarSpec(form, form, opts['gen'], null);
174+
}
175+
}
176+
return null;
177+
}
178+
159179
function and(...preds) {
160180
return new AndSpec(preds, preds, null);
161181
}
@@ -181,26 +201,6 @@ function gen(spec, overrides) {
181201
return gensub(spec, overrides, [], {recursionLimit: 0}, spec);
182202
}
183203

184-
function explainData_(spec, path, via, in_, x) {
185-
const problems = specize(spec).explain(path, via, in_, x);
186-
return {
187-
problems: problems,
188-
spec: spec,
189-
value: x
190-
};
191-
}
192-
193-
function explainData(spec, x) {
194-
explainData_();
195-
}
196-
197-
function explainPrinter(ed) {
198-
}
199-
200-
function explain(spec, x) {
201-
explainPrinter(explainData(spec, x));
202-
}
203-
204204
function withGen(specOrFunc, genFn) {
205205
const spec = specize(specOrFunc);
206206
spec.withGen(genFn);
@@ -209,13 +209,13 @@ function withGen(specOrFunc, genFn) {
209209

210210
module.exports = {
211211
conform,
212-
explain,
213212
and,
214213
array,
215214
object,
216215
isValid,
217216
specize,
218217
gen,
219218
withGen,
219+
spec,
220220
INVALID,
221221
};

src/spec/gen.js renamed to lib/core/src/gen.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
const _ = require('lodash');
21
const {
32
sample, choose, generators, elements, suchThat,
43
vector, genObject, tuple, fmap
5-
} = require('../generators');
4+
} = require('@json-spec/testcheck/generators');
65

76
const genBuiltins = new Map();
8-
genBuiltins.set(_.isString, generators.stringAlphanumeric);
9-
genBuiltins.set(_.isInteger, generators.largeInteger);
10-
genBuiltins.set(_.isBoolean, generators.boolean);
11-
genBuiltins.set(_.isDate, generators.date);
127

138
function genForPred(pred) {
149
if (pred instanceof Array) {
@@ -23,8 +18,10 @@ module.exports = {
2318
suchThat,
2419
sample,
2520
vector,
21+
elements,
2622
genObject,
2723
generators,
2824
tuple,
25+
choose,
2926
fmap
3027
}

lib/core/src/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const base = require('./base');
2+
module.exports = {
3+
...base
4+
}
5+
function explainData_(spec, path, via, in_, x) {
6+
const problems = specize(spec).explain(path, via, in_, x);
7+
return {
8+
problems: problems,
9+
spec: spec,
10+
value: x
11+
};
12+
}
13+
14+
function explainData(spec, x) {
15+
explainData_();
16+
}
17+
18+
function explainPrinter(ed) {
19+
}
20+
21+
function explain(spec, x) {
22+
explainPrinter(explainData(spec, x));
23+
}

lib/testcheck/generators.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const generators = require('./src/generators');
2+
module.exports = {
3+
...generators
4+
}

lib/testcheck/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@json-spec/testcheck",
3+
"version": "0.1.0-SNAPSHOT",
4+
"main": "./src/index.js"
5+
}

test/generators.test.js renamed to lib/testcheck/src/__tests__/generators.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { sample, choose,generators,genObject } = require('generators');
1+
const { sample, choose,generators,genObject } = require('../generators');
22

33
describe('generators', () => {
44
test('sample-choose', () => {

src/generators.js renamed to lib/testcheck/src/generators.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ module.exports = {
162162
fmap,
163163
sample,
164164
oneOf,
165+
elements,
165166
choose,
166167
genObject,
167168
vector,

package.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
{
2-
"name": "json-spec",
2+
"name": "@json-spec/root",
33
"version": "0.1.0-SNAPSHOT",
4-
"description": "A tool for describing the structure of JSON data",
5-
"license": "MIT",
4+
"private": true,
65
"repository": "github:kawasima/json-spec",
6+
"description": "A tool for describing the structure of JSON data",
7+
"license": "EPL",
8+
"workspaces": [
9+
"lib/*",
10+
"specs/*",
11+
"examples/*"
12+
],
713
"scripts": {
8-
"test": "jest"
14+
"test": "jest",
15+
"tutorial": "node examples/tutorial/src/tut1.js"
916
},
1017
"jest": {
1118
"moduleDirectories": [
1219
"src",
1320
"node_modules"
14-
],
15-
"testMatch": [
16-
"**/test/*.test.js?(x)"
1721
]
1822
},
1923
"devDependencies": {
20-
"jest": "^23.5.0"
24+
"jest": "^23.5.0",
25+
"lerna": "^3.3.0"
2126
},
2227
"dependencies": {
2328
"lodash": "^4.17.10",

0 commit comments

Comments
 (0)