Skip to content
Snippets Groups Projects
Commit ab2a1e46 authored by Alvaro Cabrera's avatar Alvaro Cabrera
Browse files

Basic support for casual; fixes #210

parent 38fef547
Branches
Tags
No related merge requests found
File suppressed by a .gitattributes entry, the file's encoding is unsupported, or the file size exceeds the limit.
File suppressed by a .gitattributes entry, the file's encoding is unsupported, or the file size exceeds the limit.
File suppressed by a .gitattributes entry, the file's encoding is unsupported, or the file size exceeds the limit.
File suppressed by a .gitattributes entry, the file's encoding is unsupported, or the file size exceeds the limit.
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
}, },
"devDependencies": { "devDependencies": {
"browserify": "^13.1.1", "browserify": "^13.1.1",
"casual": "^1.5.8",
"clone": "^2.1.0", "clone": "^2.1.0",
"codecov": "^1.0.1", "codecov": "^1.0.1",
"eslint": "^3.10.2", "eslint": "^3.10.2",
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
"chance": "url", "chance": "url",
"faker": "internet.email" "faker": "internet.email"
}, },
"throws": "ambiguous generator when using both faker and chance: .+?" "throws": "ambiguous generator .+: .+?"
} }
] ]
} }
......
...@@ -14,7 +14,7 @@ type Registry = { ...@@ -14,7 +14,7 @@ type Registry = {
}; };
/** /**
* Container is used to wrap external libraries (faker, chance, randexp) that are used among the whole codebase. These * Container is used to wrap external libraries (faker, chance, casual, randexp) that are used among the whole codebase. These
* libraries might be configured, customized, etc. and each internal JSF module needs to access those instances instead * libraries might be configured, customized, etc. and each internal JSF module needs to access those instances instead
* of pure npm module instances. This class supports consistent access to these instances. * of pure npm module instances. This class supports consistent access to these instances.
*/ */
...@@ -27,6 +27,7 @@ class Container { ...@@ -27,6 +27,7 @@ class Container {
this.registry = { this.registry = {
faker: null, faker: null,
chance: null, chance: null,
casual: null,
// randexp is required for "pattern" values // randexp is required for "pattern" values
randexp: RandExp randexp: RandExp
}; };
...@@ -77,7 +78,8 @@ class Container { ...@@ -77,7 +78,8 @@ class Container {
return { return {
faker: this.get('faker'), faker: this.get('faker'),
chance: this.get('chance'), chance: this.get('chance'),
randexp: this.get('randexp') randexp: this.get('randexp'),
casual: this.get('casual')
}; };
} }
} }
......
...@@ -6,7 +6,7 @@ import types = require('../types/index'); ...@@ -6,7 +6,7 @@ import types = require('../types/index');
import option = require('../api/option'); import option = require('../api/option');
function isExternal(schema: IGeneratorSchema): boolean { function isExternal(schema: IGeneratorSchema): boolean {
return schema.faker || schema.chance; return schema.faker || schema.chance || schema.casual;
} }
function reduceExternal(schema: IGeneratorSchema, path: SchemaPath): IGeneratorSchema { function reduceExternal(schema: IGeneratorSchema, path: SchemaPath): IGeneratorSchema {
...@@ -16,11 +16,17 @@ function reduceExternal(schema: IGeneratorSchema, path: SchemaPath): IGeneratorS ...@@ -16,11 +16,17 @@ function reduceExternal(schema: IGeneratorSchema, path: SchemaPath): IGeneratorS
if (schema['x-chance']) { if (schema['x-chance']) {
schema.chance = schema['x-chance']; schema.chance = schema['x-chance'];
} }
if (schema['x-casual']) {
schema.casual = schema['x-casual'];
}
var count: number = // sum and test later
(schema.faker !== undefined ? 1 : 0) +
(schema.chance !== undefined ? 1 : 0) +
(schema.casual !== undefined ? 1 : 0);
var fakerUsed: boolean = schema.faker !== undefined, if (count > 1) {
chanceUsed: boolean = schema.chance !== undefined; throw new ParseError('ambiguous generator mixing faker, chance or casual: ' + JSON.stringify(schema), path);
if (fakerUsed && chanceUsed) {
throw new ParseError('ambiguous generator when using both faker and chance: ' + JSON.stringify(schema), path);
} }
return schema; return schema;
......
...@@ -28,7 +28,7 @@ function hasProperties(obj: Object, ...properties: string[]): boolean { ...@@ -28,7 +28,7 @@ function hasProperties(obj: Object, ...properties: string[]): boolean {
/** /**
* Returns typecasted value. * Returns typecasted value.
* External generators (faker, chance) may return data in non-expected formats, such as string, when you might expect an * External generators (faker, chance, casual) may return data in non-expected formats, such as string, when you might expect an
* integer. This function is used to force the typecast. * integer. This function is used to force the typecast.
* *
* @param value * @param value
......
...@@ -6,6 +6,8 @@ interface IGeneratorSchema { ...@@ -6,6 +6,8 @@ interface IGeneratorSchema {
'x-faker'?: any; 'x-faker'?: any;
chance?: any; chance?: any;
'x-chance'?: any; 'x-chance'?: any;
casual?: any;
'x-casual'?: any;
} }
interface IStringSchema extends IGeneratorSchema { interface IStringSchema extends IGeneratorSchema {
......
...@@ -5,9 +5,9 @@ import container = require('../class/Container'); ...@@ -5,9 +5,9 @@ import container = require('../class/Container');
type ExternalParameters = any[]; type ExternalParameters = any[];
var externalType: FTypeGenerator = function externalType(value: JsonSchema, path: string|any): string|any { var externalType: FTypeGenerator = function externalType(value: JsonSchema, path: string|any): string|any {
var libraryName: string = value.faker ? 'faker' : 'chance', var libraryName: string = value.faker ? 'faker' : (value.chance ? 'chance' : 'casual'),
libraryModule = value.faker ? container.get('faker') : container.get('chance'), libraryModule = container.get(libraryName),
key = value.faker || value.chance, key = value.faker || value.chance || value.casual,
path = key, path = key,
args: ExternalParameters = []; args: ExternalParameters = [];
...@@ -42,6 +42,10 @@ var externalType: FTypeGenerator = function externalType(value: JsonSchema, path ...@@ -42,6 +42,10 @@ var externalType: FTypeGenerator = function externalType(value: JsonSchema, path
} }
if (typeof genFunction !== 'function') { if (typeof genFunction !== 'function') {
if (libraryName === 'casual') {
return utils.typecast(genFunction, value.type);
}
throw new Error('unknown ' + libraryName + '-generator for ' + JSON.stringify(key)); throw new Error('unknown ' + libraryName + '-generator for ' + JSON.stringify(key));
} }
......
...@@ -417,6 +417,13 @@ caseless@~0.11.0: ...@@ -417,6 +417,13 @@ caseless@~0.11.0:
version "0.11.0" version "0.11.0"
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.11.0.tgz#715b96ea9841593cc33067923f5ec60ebda4f7d7" resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.11.0.tgz#715b96ea9841593cc33067923f5ec60ebda4f7d7"
casual:
version "1.5.8"
resolved "https://registry.yarnpkg.com/casual/-/casual-1.5.8.tgz#82009d3cd39b79e7ec8db7a6245699fcfbcb3691"
dependencies:
mersenne-twister "^1.0.1"
moment "^2.15.2"
center-align@^0.1.1: center-align@^0.1.1:
version "0.1.3" version "0.1.3"
resolved "https://registry.yarnpkg.com/center-align/-/center-align-0.1.3.tgz#aa0d32629b6ee972200411cbd4461c907bc2b7ad" resolved "https://registry.yarnpkg.com/center-align/-/center-align-0.1.3.tgz#aa0d32629b6ee972200411cbd4461c907bc2b7ad"
...@@ -1625,6 +1632,10 @@ marked@^0.3.5: ...@@ -1625,6 +1632,10 @@ marked@^0.3.5:
version "0.3.6" version "0.3.6"
resolved "https://registry.yarnpkg.com/marked/-/marked-0.3.6.tgz#b2c6c618fccece4ef86c4fc6cb8a7cbf5aeda8d7" resolved "https://registry.yarnpkg.com/marked/-/marked-0.3.6.tgz#b2c6c618fccece4ef86c4fc6cb8a7cbf5aeda8d7"
mersenne-twister@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/mersenne-twister/-/mersenne-twister-1.1.0.tgz#f916618ee43d7179efcf641bec4531eb9670978a"
miller-rabin@^4.0.0: miller-rabin@^4.0.0:
version "4.0.0" version "4.0.0"
resolved "https://registry.yarnpkg.com/miller-rabin/-/miller-rabin-4.0.0.tgz#4a62fb1d42933c05583982f4c716f6fb9e6c6d3d" resolved "https://registry.yarnpkg.com/miller-rabin/-/miller-rabin-4.0.0.tgz#4a62fb1d42933c05583982f4c716f6fb9e6c6d3d"
...@@ -1701,6 +1712,10 @@ module-deps@^4.0.8: ...@@ -1701,6 +1712,10 @@ module-deps@^4.0.8:
through2 "^2.0.0" through2 "^2.0.0"
xtend "^4.0.0" xtend "^4.0.0"
moment@^2.15.2:
version "2.17.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.17.0.tgz#a4c292e02aac5ddefb29a6eed24f51938dd3b74f"
ms@0.7.2: ms@0.7.2:
version "0.7.2" version "0.7.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765" resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment