Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | 1x 1x 3x 3x 3x 21x 4x 17x 17x 17x 17x 21x 21x 21x 21x 21x 2x 2x 1x 11x 3x 1x 1x 2x 2x 1x 15x 3x 3x 3x 3x | import faker from 'faker'
import { numbers } from './helpers'
function configureFaker(config) {
const { locale = 'en' } = config
faker.locale = locale
}
/**
* parseModel - Iterates over named keys of an object while recursively parses
* the given node, until every leaf is parsed. Starts parsing parents, then
* that parent's children until it ends.
* -> An Object model type is considered a Node.
* -> A model type different than Object is considered a Leaf.
*
* @param {Object} model an object with domain-specific keys
* @return {Object} A processed model
*/
function parseModel(model, options) {
if (isLeafModel(model)) {
return generateModel(model, options)
}
const modelKeys = Object.keys(model)
return modelKeys.reduce((accumulator, currentValue) => {
const value = generateModel(model[currentValue], options)
// assigns currentValue, which is the current scanned attribute of the
// model, starting from parents to children
return Object.assign(accumulator, { [currentValue]: value })
}, {})
}
/**
* generateModel Given a model and options, processes it against the avaialble
* model types and returns the result.
* @param {Object} model A user defined model
* @param {Object} options User defined options
* @return {Object} A processed model
*/
function generateModel(model, options) {
const { type, value, options: currentModelOptions = {} } = model
// Propagates initial options
const updatedOptions = Array.isArray(currentModelOptions)
? [...currentModelOptions, options]
: Object.assign({}, options, currentModelOptions)
return modelAttributeTypes[type](value, updatedOptions)
}
/**
* isLeafModel - Given an Object determines if it should be treated as a final
* Model, meaning there are no more nested models inside it.
* @param {Object} model An Object that might have a `type` attribute of String
* type with a Data Generator function as value.
* @return {Boolean}
*/
function isLeafModel(model) {
return model.hasOwnProperty('type') && typeof model.type !== 'object'
}
/**
* parseArray - Takes a model, options, and size. If the size is an Array (ex. [1, 20])
* it will use the randomBetween method from numbers to get a random number between
* the first and second index.
* If size is a simple number, it just uses that.
* -> parseModel can be either a Node or a Leaf.
*
* @param {Object} model A model Node
* @param {Object} options [size: Number]
* @return {Array} A parsed model
*/
function parseArray(model, options) {
let size = options.size;
if (Array.isArray(size)) {
size = numbers.randomBetween(size)
}
return [...Array(size).keys()].map(() => parseModel(model.value, options));
}
/**
* parseLiteral - For those times when you simply need a literal value
*
* @param {Any} model A model Node
* @return {Any} Any given value
*/
function parseLiteral(model) {
return model;
}
/**
* parseString - For those times when you simply need a string value
*
* @param {Any} model A model Node
* @return {Any} Any given value
*/
function parseString(model) {
console.warn('\x1b[33m%s\x1b[0m', 'Deprecation warning: Please use \'Literal\' instead of \'String\'. See more: https://github.com/Cambalab/fake-data-generator/tree/develop#literal')
return model;
}
/**
* append - Given a model and options, appends a value to the parsed model.
* -> parsedModel should return a Leaf.
* @param {Object} model A model Node
* @param {Object} options [text: Number|String>]
* @return {String} A parsed model
*/
function append(model, options) {
return `${parseModel(model)}${options.text}`
}
/**
* prepend - Given a model and options, prepends a value to the parsed model.
* -> parsedModel should return a Leaf.
* @param {Object} model A model Node
* @param {Object} options [text: Number|String>]
* @return {String} A parsed model
*/
function prepend(model, options) {
return `${options.text}${parseModel(model)}`
}
const modelAttributeTypes = {
// Structure types
Object: parseModel,
Array: parseArray,
Literal: parseLiteral,
String: parseString,
// Data generators types
// -- external libs
faker: (args, options = {}) => Object.byString(faker, args)(...options),
// -- internal libs
// ---- strings
append,
prepend,
// ---- numbers
incrementNumber: numbers.incrementNumber,
incrementNumberBy: numbers.incrementNumberBy,
randomNumberBetween: numbers.randomBetween,
randomElementInArray: numbers.randomElementInArray,
randomElementsInArray: numbers.randomElementsInArray,
randomNumberBetweenWithString: numbers.randomBetweenWithString
}
export {
parseArray,
parseLiteral,
parseModel,
parseString,
append,
prepend
}
/**
* parseModelData- Given a model, configures faker and returns a parsed model.
* @param {Object} modelData An object containing the model data, usually
* containing a 'config' and 'model' parent attributes
* @param {Object} options Different options provided from the model creation
* step
* @return {Any} Returns a parsed model
*/
export default (modelData, options = {}) => {
const { config = {}, model } = modelData
configureFaker(config)
return parseModel(model, options)
}
|