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

Remove jsf.utils, use jsf.random instead; fix

parent 514b13eb
Branches
Tags
No related merge requests found
...@@ -405,7 +405,7 @@ Additionally, you can add custom generators for those: ...@@ -405,7 +405,7 @@ Additionally, you can add custom generators for those:
```javascript ```javascript
jsf.format('semver', function() { jsf.format('semver', function() {
return jsf.utils.randexp('\\d\\.\\d\\.[1-9]\\d?'); return jsf.random.randexp('\\d\\.\\d\\.[1-9]\\d?');
}); });
``` ```
......
...@@ -2,7 +2,7 @@ module.exports = { ...@@ -2,7 +2,7 @@ module.exports = {
register: function (jsf) { register: function (jsf) {
return jsf.format({ return jsf.format({
semver: function () { semver: function () {
return jsf.utils.randexp('\\d\\.\\d\\.[1-9]\\d?'); return jsf.random.randexp('\\d\\.\\d\\.[1-9]\\d?');
} }
}); });
} }
......
...@@ -3,6 +3,21 @@ ...@@ -3,6 +3,21 @@
import optionAPI from '../api/option'; import optionAPI from '../api/option';
import env from '../core/constants'; import env from '../core/constants';
import RandExp from 'randexp';
function _randexp(value: string) {
// set maximum default, see #193
RandExp.prototype.max = optionAPI('defaultRandExpMax');
// same implementation as the original except using our random
RandExp.prototype.randInt = (a, b) =>
a + Math.floor(optionAPI('random')() * (1 + b - a));
var re = new RandExp(value);
return re.gen();
}
/** /**
* Returns random element of a collection * Returns random element of a collection
* *
...@@ -117,6 +132,7 @@ function date(step) { ...@@ -117,6 +132,7 @@ function date(step) {
export default { export default {
pick: pick, pick: pick,
date: date, date: date,
randexp: _randexp,
shuffle: shuffle, shuffle: shuffle,
number: number, number: number,
}; };
import optionAPI from '../api/option'; import optionAPI from '../api/option';
import env from '../core/constants'; import env from '../core/constants';
import RandExp from 'randexp';
function _randexp(value: string) {
// set maximum default, see #193
RandExp.prototype.max = optionAPI('defaultRandExpMax');
// same implementation as the original except using our random
RandExp.prototype.randInt = (a, b) =>
a + Math.floor(optionAPI('random')() * (1 + b - a));
var re = new RandExp(value);
return re.gen();
}
function getSubAttribute(obj: any, dotSeparatedKey: string): any { function getSubAttribute(obj: any, dotSeparatedKey: string): any {
var keyElements: string[] = dotSeparatedKey.split('.'); var keyElements: string[] = dotSeparatedKey.split('.');
...@@ -281,7 +266,6 @@ export default { ...@@ -281,7 +266,6 @@ export default {
merge: merge, merge: merge,
clean: clean, clean: clean,
short: short, short: short,
randexp: _randexp,
notValue: notValue, notValue: notValue,
anyValue: anyValue, anyValue: anyValue,
validate: validate, validate: validate,
......
import utils from '../core/utils'; import random from '../core/random';
/** /**
* Predefined core formats * Predefined core formats
...@@ -18,8 +18,8 @@ var regexps: IStringMap = { ...@@ -18,8 +18,8 @@ var regexps: IStringMap = {
* @returns {string} * @returns {string}
*/ */
function coreFormatGenerator(coreFormat: string): string { function coreFormatGenerator(coreFormat: string): string {
return utils.randexp(regexps[coreFormat]).replace(/\{(\w+)\}/, function(match: string, key: string) { return random.randexp(regexps[coreFormat]).replace(/\{(\w+)\}/, function(match: string, key: string) {
return utils.randexp(regexps[key]); return random.randexp(regexps[key]);
}); });
} }
......
...@@ -106,14 +106,12 @@ jsf.resolve = <jsfAPI>function(schema: JsonSchema, refs?: any, cwd?: string) { ...@@ -106,14 +106,12 @@ jsf.resolve = <jsfAPI>function(schema: JsonSchema, refs?: any, cwd?: string) {
}).then((sub) => run($refs, sub, container)); }).then((sub) => run($refs, sub, container));
}; };
jsf.utils = utils;
jsf.format = format; jsf.format = format;
jsf.option = option; jsf.option = option;
jsf.random = random;
// built-in support // built-in support
container.define('pattern', utils.randexp); container.define('pattern', random.randexp);
// skip default generators // skip default generators
container.define('jsonPath', (value, schema) => { container.define('jsonPath', (value, schema) => {
......
...@@ -75,7 +75,7 @@ var objectType: FTypeGenerator = function objectType(value: IObjectSchema, path, ...@@ -75,7 +75,7 @@ var objectType: FTypeGenerator = function objectType(value: IObjectSchema, path,
patternPropertyKeys.forEach(function (_key) { patternPropertyKeys.forEach(function (_key) {
if (key.match(new RegExp(_key))) { if (key.match(new RegExp(_key))) {
found = true; found = true;
props[utils.randexp(key)] = patternProperties[_key]; props[random.randexp(key)] = patternProperties[_key];
} }
}); });
...@@ -87,7 +87,7 @@ var objectType: FTypeGenerator = function objectType(value: IObjectSchema, path, ...@@ -87,7 +87,7 @@ var objectType: FTypeGenerator = function objectType(value: IObjectSchema, path,
if (subschema) { if (subschema) {
// otherwise we can use additionalProperties? // otherwise we can use additionalProperties?
props[patternProperties[key] ? utils.randexp(key) : key] = subschema; props[patternProperties[key] ? random.randexp(key) : key] = subschema;
} else { } else {
missing.push(key); missing.push(key);
} }
...@@ -128,7 +128,7 @@ var objectType: FTypeGenerator = function objectType(value: IObjectSchema, path, ...@@ -128,7 +128,7 @@ var objectType: FTypeGenerator = function objectType(value: IObjectSchema, path,
current += 1; current += 1;
} }
} else { } else {
var word = words(1) + utils.randexp('[a-f\\d]{1,3}'); var word = words(1) + random.randexp('[a-f\\d]{1,3}');
if (!props[word]) { if (!props[word]) {
props[word] = additionalProperties || anyType; props[word] = additionalProperties || anyType;
...@@ -138,7 +138,7 @@ var objectType: FTypeGenerator = function objectType(value: IObjectSchema, path, ...@@ -138,7 +138,7 @@ var objectType: FTypeGenerator = function objectType(value: IObjectSchema, path,
} }
patternPropertyKeys.forEach(function (_key) { patternPropertyKeys.forEach(function (_key) {
var word = utils.randexp(_key); var word = random.randexp(_key);
if (!props[word]) { if (!props[word]) {
props[word] = patternProperties[_key]; props[word] = patternProperties[_key];
......
...@@ -4,6 +4,7 @@ import dateTime from '../generators/dateTime'; ...@@ -4,6 +4,7 @@ import dateTime from '../generators/dateTime';
import coreFormat from '../generators/coreFormat'; import coreFormat from '../generators/coreFormat';
import optionAPI from '../api/option'; import optionAPI from '../api/option';
import format from '../api/format'; import format from '../api/format';
import random from '../core/random';
import utils from '../core/utils'; import utils from '../core/utils';
function generateFormat(value: IStringSchema, invalid: () => string): string { function generateFormat(value: IStringSchema, invalid: () => string): string {
...@@ -15,6 +16,7 @@ function generateFormat(value: IStringSchema, invalid: () => string): string { ...@@ -15,6 +16,7 @@ function generateFormat(value: IStringSchema, invalid: () => string): string {
switch (value.format) { switch (value.format) {
case 'date-time': case 'date-time':
case 'datetime':
return dateTime(); return dateTime();
case 'ipv4': case 'ipv4':
return ipv4(); return ipv4();
...@@ -48,7 +50,7 @@ var stringType: FTypeGenerator = function stringType(value: IStringSchema): stri ...@@ -48,7 +50,7 @@ var stringType: FTypeGenerator = function stringType(value: IStringSchema): stri
} }
if (value.pattern) { if (value.pattern) {
return utils.randexp(value.pattern); return random.randexp(value.pattern);
} }
return thunk(opts.minLength, opts.maxLength); return thunk(opts.minLength, opts.maxLength);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment