Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move template functions into a separate module.

  • Loading branch information...
commit 8d24dc545e8fc5219978d99070e62dd1ab11b395 1 parent bb03370
@cliffano authored
Showing with 220 additions and 99 deletions.
  1. +145 −0 lib/functions.js
  2. +2 −99 lib/worker.js
  3. +73 −0 test/functions.js
View
145 lib/functions.js
@@ -0,0 +1,145 @@
+var dateFormat = require('dateformat'),
+ nonsense = require('Nonsense'),
+ ns = new nonsense();
+
+/**
+ * functions#integer
+ * - min (Number): minimum integer
+ * - max (Number): maximum integer
+ * - cb (Function): jazz cb(data) callback
+ *
+ * Template function to generate a random integer.
+ **/
+function integer(min, max, cb) {
+ var value;
+ // template: {integer(min, max)}
+ if (min && max && cb) {
+ value = ns.integerInRange(min, max);
+ // template: {integer()}
+ } else {
+ cb = min;
+ value = ns.integer();
+ }
+ cb(value);
+}
+
+/**
+ * functions#float
+ * - min (Number): minimum float
+ * - max (Number): maximum float
+ * - cb (Function): jazz cb(data) callback
+ *
+ * Template function to generate a random float.
+ **/
+function float(min, max, cb) {
+ var value;
+ // template: {float(min, max)}
+ if (min && max && cb) {
+ value = ns.realInRange(min, max);
+ // template: {float()}
+ } else {
+ cb = min;
+ value = ns.real();
+ }
+ cb(value);
+}
+
+/**
+ * functions#date
+ * - format (String): date format (felixge/node-dateformat)
+ * - min (Number): minimum year
+ * - max (Number): maximum year
+ * - cb (Function): jazz cb(data) callback
+ *
+ * Template function to generate a random date.
+ **/
+function date(format, min, max, cb) {
+
+ function _timestamp(year) {
+ return new Date(year, 0, 1).getTime();
+ }
+
+ var value;
+ // template: {date(format, min, max)}
+ if (format && min && max && cb) {
+ value = ns.timestamp(_timestamp(min), _timestamp(max));
+ // template: {date(min, max)}
+ } else if (format && min && max) {
+ cb = max;
+ value = ns.timestamp(_timestamp(format), _timestamp(min));
+ format = 'isoDateTime';
+ // template: {date(format)}
+ } else if (format && min) {
+ cb = min;
+ value = ns.timestamp(_timestamp(1970), _timestamp(2020));
+ // template: {date()}
+ } else {
+ cb = format;
+ format = 'isoDateTime';
+ value = ns.timestamp(_timestamp(1970), _timestamp(2020));
+ }
+ cb(dateFormat(new Date(value), format));
+}
+
+/**
+ * functions#select
+ * - cb (Function): jazz cb(data) callback
+ *
+ * Template function to select an item out of the arguments.
+ * Last argument must be the callback.
+ **/
+function select() {
+ // template: {select(arg1, arg2, ..., argN)}
+ var args = Array.prototype.slice.call(arguments);
+ args[args.length - 1](ns.pick(args.slice(0, args.length - 1)));
+}
+
+/**
+ * functions#word
+ * - num (Number): how many random words to generate
+ * - cb (Function): jazz cb(data) callback
+ *
+ * Template function to generate random word(s) from Lorem Ipsum.
+ **/
+function word(num, cb) {
+ var value;
+ // template: {word(num)}
+ if (num && cb) {
+ value = ns.words(num);
+ // template: {word()}
+ } else {
+ cb = num;
+ value = ns.word();
+ }
+ cb(value);
+}
+
+/**
+ * functions#firstName
+ * - cb (Function): jazz cb(data) callback
+ *
+ * Template function to generate a random first name.
+ **/
+function firstName(cb) {
+ cb(ns.firstName());
+}
+
+/**
+ * functions#lastName
+ * - cb (Function): jazz cb(data) callback
+ *
+ * Template function to generate a random last name.
+ **/
+function lastName(cb) {
+ cb(ns.lastName());
+}
+
+module.exports = {
+ integer: integer,
+ float: float,
+ date: date,
+ select: select,
+ word: word,
+ first_name: firstName,
+ last_name: lastName
+};
View
101 lib/worker.js
@@ -1,7 +1,6 @@
var bag = require('bagofholding'),
- dateFormat = require('dateformat'),
fs = require('fs'),
- nonsense = require('Nonsense');
+ functions = require('./functions');
/**
* class Worker
@@ -11,102 +10,6 @@ function Worker(workerId) {
this.workerId = workerId;
}
-/** internal
- * Worker#_templateFunctions -> Object
- *
- * Template functions to be used in template evaluation as parameters.
- **/
-Worker.prototype._templateFunctions = function () {
-
- var ns = new nonsense();
-
- function integer(min, max, cb) {
- var value;
- // template: {integer(min, max)}
- if (min && max && cb) {
- value = ns.integerInRange(min, max);
- // template: {integer()}
- } else {
- cb = min;
- value = ns.integer();
- }
- cb(value);
- }
- function float(min, max, cb) {
- var value;
- // template: {float(min, max)}
- if (min && max && cb) {
- value = ns.realInRange(min, max);
- // template: {float()}
- } else {
- cb = min;
- value = ns.real();
- }
- cb(value);
- }
- function date(format, min, max, cb) {
-
- function _timestamp(year) {
- return new Date(year, 0, 1).getTime();
- }
-
- var value;
- // template: {date(format, min, max)}
- if (format && min && max && cb) {
- value = ns.timestamp(_timestamp(min), _timestamp(max));
- // template: {date(min, max)}
- } else if (format && min && max) {
- cb = max;
- value = ns.timestamp(_timestamp(format), _timestamp(min));
- format = 'isoDateTime';
- // template: {date(format)}
- } else if (format && min) {
- cb = min;
- value = ns.timestamp(_timestamp(1970), _timestamp(2020));
- // template: {date()}
- } else {
- cb = format;
- format = 'isoDateTime';
- value = ns.timestamp(_timestamp(1970), _timestamp(2020));
- }
- cb(dateFormat(new Date(value), format));
- }
- // template: {select(arg1, arg2, ..., argN)}
- function select() {
- var args = Array.prototype.slice.call(arguments);
- args[args.length - 1](ns.pick(args.slice(0, args.length - 1)));
- }
- function word(num, cb) {
- var value;
- // template: {word(num)}
- if (num && cb) {
- value = ns.words(num);
- // template: {word()}
- } else {
- cb = num;
- value = ns.word();
- }
- cb(value);
- }
- function firstName(cb) {
- cb(ns.firstName());
- }
- function lastName(cb) {
- cb(ns.lastName());
- }
-
- return {
- integer: integer,
- float: float,
- date: date,
- select: select,
- word: word,
- first_name: firstName,
- last_name: lastName
- };
-
-};
-
/**
* Worker#write(templates, genId, numSegments, outFile, cb)
* - templates (Object): data file templates in the format of { header: '', segment: '', footer: '' }
@@ -124,7 +27,7 @@ Worker.prototype.write = function (templates, genId, numSegments, outFile, cb) {
var stream = fs.createWriteStream(outFile + this.workerId, { flags: 'w', encoding: 'utf-8' }),
segmentId = 0,
segmentTemplate = bag.text.compile(templates.segment),
- params = this._templateFunctions(),
+ params = functions,
status;
params.gen_id = genId;
View
73 test/functions.js
@@ -0,0 +1,73 @@
+var _ = require('underscore'),
+ bag = require('bagofholding'),
+ sandbox = require('sandboxed-module'),
+ should = require('should'),
+ checks, mocks,
+ functions;
+
+describe('functions', function () {
+
+ function create(checks, mocks) {
+ return sandbox.require('../lib/functions', {
+ requires: mocks ? mocks.requires : {},
+ globals: {}
+ });
+ }
+
+ beforeEach(function () {
+ checks = {};
+ mocks = {};
+ functions = require('../lib/functions');
+ });
+
+ describe('integer', function () {
+
+ it('should evaluate integer function when it has no argument', function (done) {
+ functions.integer(function (data) {
+ checks.data = data;
+ done();
+ });
+ (_.isNaN(checks.data)).should.equal(false);
+ checks.data.should.not.match(/.+\..+/);
+ });
+
+ it('should evaluate ranged integer function when it has min max arguments', function (done) {
+ functions.integer(100, 200, function (data) {
+ checks.data = data;
+ done();
+ });
+ (_.isNaN(checks.data)).should.equal(false);
+ checks.data.should.not.match(/.+\..+/);
+ parseInt(checks.data, 10).should.be.within(100, 200);
+ });
+
+ it('should evaluate identical integer function when it has identical min max arguments', function (done) {
+ functions.integer(100, 100, function (data) {
+ checks.data = data;
+ done();
+ });
+ (_.isNaN(checks.data)).should.equal(false);
+ checks.data.should.not.match(/.+\..+/);
+ parseInt(checks.data, 10).should.equal(100);
+ });
+ });
+
+ describe('float', function () {
+ });
+
+ describe('date', function () {
+ });
+
+ describe('select', function () {
+ });
+
+ describe('word', function () {
+ });
+
+ describe('firstName', function () {
+ });
+
+ describe('lastName', function () {
+ });
+});
+
Please sign in to comment.
Something went wrong with that request. Please try again.