Comparing version 1.3.0 to 1.4.0
@@ -0,1 +1,5 @@ | ||
# 1.4.0 - 2016-03-02 | ||
- Generate smart random data when elements have no sample or default value set. This can now be overridden with your own `defaultValue` function. | ||
# 1.3.0 - 2016-02-25 | ||
@@ -2,0 +6,0 @@ |
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var Eidolon, dereference, generateExample, generateSchema, inherit; | ||
var Eidolon, defaultValue, dereference, generateExample, generateSchema, inherit; | ||
defaultValue = require('./default-value'); | ||
dereference = require('./dereference'); | ||
@@ -14,4 +16,9 @@ | ||
Eidolon = (function() { | ||
function Eidolon(structures) { | ||
function Eidolon(structures, options1) { | ||
var base; | ||
this.structures = structures != null ? structures : {}; | ||
this.options = options1 != null ? options1 : {}; | ||
if ((base = this.options).defaultValue == null) { | ||
base.defaultValue = defaultValue; | ||
} | ||
} | ||
@@ -23,4 +30,7 @@ | ||
Eidolon.prototype.example = function(element) { | ||
return generateExample(element, this.structures); | ||
Eidolon.prototype.example = function(element, options) { | ||
if (options == null) { | ||
options = this.options; | ||
} | ||
return generateExample(element, this.structures, options); | ||
}; | ||
@@ -38,2 +48,3 @@ | ||
Eidolon: Eidolon, | ||
defaultValue: defaultValue, | ||
dereference: dereference, | ||
@@ -40,0 +51,0 @@ example: generateExample, |
// Generated by CoffeeScript 1.10.0 | ||
(function() { | ||
var defaultValue, dereference, generateExample; | ||
var defaultValue, dereference, faker, generateExample; | ||
dereference = require('./dereference'); | ||
defaultValue = function(type) { | ||
switch (type) { | ||
case 'boolean': | ||
return true; | ||
case 'number': | ||
return 1; | ||
case 'string': | ||
return 'Hello, world!'; | ||
} | ||
}; | ||
defaultValue = require('./default-value'); | ||
generateExample = function(root) { | ||
var i, item, j, key, len, len1, member, obj, ref, ref1, ref2, results; | ||
faker = require('faker'); | ||
generateExample = function(root, context) { | ||
var i, item, j, key, len, len1, member, obj, ref, ref1, ref2, ref3, ref4, ref5, ref6, results, value; | ||
switch (root.element) { | ||
@@ -30,3 +23,10 @@ case 'boolean': | ||
} else { | ||
return defaultValue(root.element); | ||
if ((ref1 = root.meta) != null ? ref1.id : void 0) { | ||
context.path.push(root.meta.id.toLowerCase()); | ||
} | ||
value = context.defaultValue(root, context.path); | ||
if ((ref2 = root.meta) != null ? ref2.id : void 0) { | ||
context.path.pop(); | ||
} | ||
return value; | ||
} | ||
@@ -36,9 +36,9 @@ } | ||
case 'enum': | ||
return generateExample(root.content[0]); | ||
return generateExample(root.content[0], context); | ||
case 'array': | ||
ref1 = root.content || []; | ||
ref3 = root.content || []; | ||
results = []; | ||
for (i = 0, len = ref1.length; i < len; i++) { | ||
item = ref1[i]; | ||
results.push(generateExample(item)); | ||
for (i = 0, len = ref3.length; i < len; i++) { | ||
item = ref3[i]; | ||
results.push(generateExample(item, context)); | ||
} | ||
@@ -49,5 +49,5 @@ return results; | ||
obj = {}; | ||
ref2 = root.content; | ||
for (j = 0, len1 = ref2.length; j < len1; j++) { | ||
member = ref2[j]; | ||
ref4 = root.content; | ||
for (j = 0, len1 = ref4.length; j < len1; j++) { | ||
member = ref4[j]; | ||
if (member.element === 'select') { | ||
@@ -57,3 +57,13 @@ member = member.content[0].content[0]; | ||
key = member.content.key.content; | ||
obj[key] = member.content.value ? generateExample(member.content.value) : defaultValue('string'); | ||
if ((ref5 = root.meta) != null ? ref5.id : void 0) { | ||
context.path.push(root.meta.id.toLowerCase()); | ||
} | ||
context.path.push(key.toLowerCase()); | ||
obj[key] = member.content.value ? generateExample(member.content.value, context) : context.defaultValue({ | ||
element: 'string' | ||
}, context.path); | ||
context.path.pop(); | ||
if ((ref6 = root.meta) != null ? ref6.id : void 0) { | ||
context.path.pop(); | ||
} | ||
} | ||
@@ -64,6 +74,22 @@ return obj; | ||
module.exports = function(root, dataStructures) { | ||
return generateExample(dereference(root, dataStructures)); | ||
module.exports = function(root, dataStructures, options) { | ||
var example, origPath; | ||
if (options == null) { | ||
options = {}; | ||
} | ||
if (options.defaultValue == null) { | ||
options.defaultValue = defaultValue; | ||
} | ||
origPath = options.path; | ||
if (options.path == null) { | ||
options.path = []; | ||
} | ||
if (options.seed) { | ||
faker.seed(options.seed); | ||
} | ||
example = generateExample(dereference(root, dataStructures), options); | ||
options.path = origPath; | ||
return example; | ||
}; | ||
}).call(this); |
{ | ||
"name": "eidolon", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Generate JSON or JSON Schema from Refract & MSON data structures", | ||
@@ -25,2 +25,3 @@ "main": "lib/eidolon.js", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"coffee-coverage": "^1.0.1", | ||
@@ -32,4 +33,8 @@ "coffee-script": "^1.10.0", | ||
"istanbul": "^0.4.2", | ||
"mocha": "^2.4.4" | ||
"mocha": "^2.4.4", | ||
"sinon": "^1.17.3" | ||
}, | ||
"dependencies": { | ||
"faker": "github:marak/faker.js#9bb2b7c" | ||
} | ||
} |
@@ -61,7 +61,7 @@ # Eidolon | ||
// Method 1: module methods | ||
example = eidolon.example(input, dataStructures); | ||
example = eidolon.example(input, dataStructures, options); | ||
schema = eidolon.schema(input, dataStructures); | ||
// Method 2: class instance | ||
const instance = new Eidolon(dataStructures); | ||
const instance = new Eidolon(dataStructures, options); | ||
example = instance.example(input); | ||
@@ -125,6 +125,13 @@ schema = instance.schema(input); | ||
### `eidolon.Eidolon([dataStructures])` | ||
### `eidolon.Eidolon([dataStructures], [options])` | ||
This class is used to save state between calls to `example` and `schema`. It is used just like the functions below, except that you pass your data structures to the constructor instead of to each method. | ||
Available options: | ||
Option Name | Description | Default | ||
------------ | ----------- | ------- | ||
`defaultValue` | Function to generate a default value `function (refractElement, path)` | Built-in [`eidolon.defaultValue`](https://github.com/danielgtaylor/eidolon/blob/master/src/default-value.coffee). | ||
`seed` | Seed for the random generator used to create default values | - | ||
```js | ||
@@ -141,6 +148,8 @@ import {Eidolon} from 'eidolon'; | ||
### `eidolon.example(input, [dataStructures])` | ||
### `eidolon.example(input, [dataStructures], [options])` | ||
Generate a new example from the given input refract object and an optional mapping of data structures, where the key is the data structure name and the value is the data structure definition. | ||
Available options are described in the Eidolon class above. | ||
```js | ||
@@ -147,0 +156,0 @@ import eidolon from 'eidolon'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
GitHub dependency
Supply chain riskContains a dependency which resolves to a GitHub URL. Dependencies fetched from GitHub specifiers are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
26417
11
522
250
0
1
9
1