UNPKG

wheel-of-fortune-participants

Version:

Easy generate files for https://github.com/finanzcheck/wheel-of-fortune

155 lines (138 loc) 3.29 kB
import faker from 'faker'; import fs from 'fs'; import path from 'path'; import { mkdir } from 'shelljs'; import Chance from 'chance'; var chance = new Chance(); const MIN = 20; const MAX = 50; class Generator { /** * * @param {String} ext * @param {String} filename * @param {Number} [min] * @param {Number} [max] */ constructor(ext, filename, min = MIN, max = MAX) { this.ext = ext; this._filename = filename; this.min = min; this.max = max; this._participants = null; } /** * * @returns {Generator} */ generate() { mkdir('-p', path.dirname(this.filename)); fs.writeFileSync(this.filename, this, 'utf8'); return this; } /** * * @returns {String} */ get filename() { const { _filename, ext } = this; return `${ _filename }.${ ext }`; } /** * * @returns {[{name:String, spokes:Number}]} */ get participants() { const { _participants, min, max } = this; if (null !== _participants) { return _participants; } const p = () => { let spokes = chance.integer({min: 0, max: 79}); let name = faker.fake('{{name.firstName}} {{name.lastName}}'); return {spokes, name}; }; let participants = []; let i = 0, amount = chance.integer({min, max}); for (i; i < amount; i++) { participants.push(p()); } this._participants = participants; return participants; } /** * * @returns {string} */ toString() { return ''; } /** * * @returns {Generator} */ log() { const { filename, participants } = this; console.log(`${ filename } successful generated with ${ participants.length } participants.`); return this; } /** * * @param generator * @returns {Generator} */ static file(generator) { return generator.generate().log(); } } class GeneratorCSV extends Generator { constructor(filename, cols) { super('csv', filename); this.cols = cols; } /** * * @returns {string} */ toString() { let participants = super.participants .map((data)=> { let p = []; this.cols.forEach((col) => { p.push(data[col]); }); return p.join(';'); }); return [this.cols.join(';')].concat(participants).join(require('os').EOL); } /** * * @param filename * @param cols * @returns {Generator} */ static file(filename, cols) { return super.file(new GeneratorCSV(filename, cols)); } } class GeneratorJSON extends Generator { constructor(filename) { super('json', filename); } /** * * @returns {String} */ toString() { return JSON.stringify(super.participants, null, 2); } /** * * @param filename * @returns {Generator} */ static file(filename) { return super.file(new GeneratorJSON(filename)); } } export { GeneratorCSV, GeneratorJSON };