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 /lib | |
| 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
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/datatype.js | 73 |
1 files changed, 60 insertions, 13 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;
|
