diff options
| author | LBuerstmayr <[email protected]> | 2021-03-02 16:52:50 +0100 |
|---|---|---|
| committer | Marak <[email protected]> | 2021-03-03 20:14:45 -0500 |
| commit | 9cf19e07906a501220a0d007b1f8274791ee5e83 (patch) | |
| tree | 4de15073ebc888a102b26feacb1e4426d3f1092e | |
| parent | adc8802d093dcee3bf14487f0d3432be77b65b62 (diff) | |
| download | faker-9cf19e07906a501220a0d007b1f8274791ee5e83.tar.xz faker-9cf19e07906a501220a0d007b1f8274791ee5e83.zip | |
Issue 1114: New datatype module
Current status:
- adding methods for string, json and respective tests
| -rw-r--r-- | lib/datatype.js | 73 | ||||
| -rw-r--r-- | test/datatype.unit.js | 48 |
2 files changed, 104 insertions, 17 deletions
diff --git a/lib/datatype.js b/lib/datatype.js index b7526907..f06e8fcc 100644 --- a/lib/datatype.js +++ b/lib/datatype.js @@ -41,12 +41,12 @@ function Datatype (faker, seed) { }
// Make the range inclusive of the max value
- var max = options.max;
+ let max = options.max;
if (max >= 0) {
max += options.precision;
}
- var randomNumber = Math.floor(
+ let randomNumber = Math.floor(
mersenne.rand(max / options.precision, options.min / options.precision));
// Workaround problem in Float point arithmetics for e.g. 6681493 / 0.01
randomNumber = randomNumber / (1 / options.precision);
@@ -68,8 +68,8 @@ function Datatype (faker, seed) { };
}
options = options || {};
- var opts = {};
- for (var p in options) {
+ let opts = {};
+ for (let p in options) {
opts[p] = options[p];
}
if (typeof opts.precision === 'undefined') {
@@ -83,6 +83,7 @@ function Datatype (faker, seed) { * this method uses a random number of milliseconds since 1. Jan 1970 UTC to return a Date object
*
* @method faker.datatype.date
+ * @param {mixed} options, pass min or max as number of milliseconds since 1. Jan 1970 UTC
*/
this.date = function (options) {
if (typeof options === "number") {
@@ -91,7 +92,7 @@ function Datatype (faker, seed) { };
}
- var minMax = 8640000000000000;
+ const minMax = 8640000000000000;
options = options || {};
@@ -103,20 +104,49 @@ function Datatype (faker, seed) { options.max = new Date().setFullYear(2100,1,1);
}
- var random = faker.datatype.number(options);
+ const random = faker.datatype.number(options);
return new Date(random);
};
/**
+ * Returns a string, containing UTF-16 chars between 33 and 125 ('!' to '}')
+ *
+ *
+ * @method faker.datatype.string
+ * @param { number } strLength: length of generated string, default = 10, max length = 2^20
+ */
+ this.string = function (strLength) {
+ if(strLength === undefined ){
+ strLength = 10;
+ }
+
+ const maxLength = Math.pow(2, 20);
+ if(strLength >= (maxLength)){
+ strLength = maxLength
+ }
+
+ const charCodeOption = {
+ min: 33,
+ max: 125
+ }
+ let returnString = '';
+
+ for(let i = 0; i < strLength; i++){
+ returnString += String.fromCharCode(faker.datatype.number(charCodeOption));
+ }
+ return returnString;
+ };
+
+ /**
* uuid
*
* @method faker.datatype.uuid
*/
this.uuid = function () {
- var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
- var replacePlaceholders = function (placeholder) {
- var random = faker.datatype.number({ min: 0, max: 15 });
- var value = placeholder == 'x' ? random : (random &0x3 | 0x8);
+ const RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
+ const replacePlaceholders = function (placeholder) {
+ const random = faker.datatype.number({ min: 0, max: 15 });
+ const value = placeholder == 'x' ? random : (random &0x3 | 0x8);
return value.toString(16);
};
return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders);
@@ -143,16 +173,33 @@ function Datatype (faker, seed) { count = 1;
}
- var wholeString = "";
- for(var i = 0; i < count; i++) {
+ let wholeString = "";
+ for(let i = 0; i < count; i++) {
wholeString += faker.random.arrayElement(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "A", "B", "C", "D", "E", "F"]);
}
return "0x"+wholeString;
};
- return this;
+ /**
+ * returns
+ *
+ * @method faker.datatype.json
+ */
+ this.json = function json() {
+ const properties = ['foo', 'bar', 'bike', 'a', 'b', 'name', 'prop']
+
+ let returnObject = {};
+ properties.map((prop, i) => {
+ returnObject[prop] = i%faker.datatype.number({min:1, max:3}) === 0 ?
+ faker.datatype.string() : faker.datatype.number();
+ })
+
+ return JSON.stringify(returnObject);
+ };
+
+ return this;
}
module['exports'] = Datatype;
diff --git a/test/datatype.unit.js b/test/datatype.unit.js index a0a6a281..2b81b66c 100644 --- a/test/datatype.unit.js +++ b/test/datatype.unit.js @@ -173,6 +173,8 @@ describe("datatype.js", function () { assert.ok(today.valueOf() === date.valueOf());
faker.datatype.number.restore();
});
+
+ //@TODO make seeding work exactly, not only on Dates (not Times)
it('check if date works with seeding', function (){
faker.seed(100);
var date = faker.datatype.date();
@@ -182,15 +184,44 @@ describe("datatype.js", function () { });
});
+ describe('string', function() {
+ it('should generate a string value', function() {
+ var generateString = faker.datatype.string();
+ assert.ok(typeof generateString === 'string');
+ assert.ok(generateString.length === 10);
+ });
+
+ it('should generate a string value, checks seeding', function() {
+ faker.seed(100);
+ var generateString = faker.datatype.string();
+ assert.ok(generateString === 'S_:GHQo.!/');
+ });
+
+ it('returns empty string if negative length is passed', function() {
+ const negativeValue = faker.datatype.number({min: -1000, max: -1});
+ var generateString = faker.datatype.string(negativeValue);
+ assert.ok(generateString === '')
+ assert.ok(generateString.length === 0);
+ });
+
+ it('returns string with length of 2^20 if bigger length value is passed', function() {
+ const overMaxValue = Math.pow(2, 28);
+ var generateString = faker.datatype.string(overMaxValue);
+ assert.ok(generateString.length === (Math.pow(2,20)));
+ });
+
+
+ });
+
describe('boolean', function() {
- it('should generate a boolean value', function() {
+ it('generates a boolean value', function() {
var bool = faker.datatype.boolean();
assert.ok(typeof bool == 'boolean');
});
});
describe('UUID', function() {
- it('should generate a valid UUID', function() {
+ it('generates a valid UUID', function() {
var UUID = faker.datatype.uuid();
var RFC4122 = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
assert.ok(RFC4122.test(UUID));
@@ -200,14 +231,23 @@ describe("datatype.js", function () { describe('hexaDecimal', function() {
var hexaDecimal = faker.datatype.hexaDecimal;
- it('should generate single hex character when no additional argument was provided', function() {
+ it('generates single hex character when no additional argument was provided', function() {
var hex = hexaDecimal();
assert.ok(hex.match(/^(0x)[0-9a-f]{1}$/i));
});
- it('should generate a random hex string', function() {
+ it('generates a random hex string', function() {
var hex = hexaDecimal(5);
assert.ok(hex.match(/^(0x)[0-9a-f]+$/i));
});
});
+
+ describe('json', function() {
+ it('generates a valid json object', function() {
+ const jsonObject = faker.datatype.json();
+ console.log(jsonObject);
+ assert.ok(typeof jsonObject == 'string');
+ assert.ok(JSON.parse(jsonObject));
+ });
+ });
});
\ No newline at end of file |
