diff options
| author | LBuerstmayr <[email protected]> | 2021-03-01 21:00:50 +0100 |
|---|---|---|
| committer | Marak <[email protected]> | 2021-03-03 20:14:45 -0500 |
| commit | 58c61afb1b8baa160add593e5af7c110de011968 (patch) | |
| tree | 4cc8174e0809bc186ec833b85b5babeecce577a4 /lib | |
| parent | 36d8644d95fc4c99af5c80e120d97678a3f0ced5 (diff) | |
| download | faker-58c61afb1b8baa160add593e5af7c110de011968.tar.xz faker-58c61afb1b8baa160add593e5af7c110de011968.zip | |
WIP: issue-1114-datatypes-module
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/datatype.js | 184 | ||||
| -rw-r--r-- | lib/index.js | 3 | ||||
| -rw-r--r-- | lib/random.js | 12 |
3 files changed, 189 insertions, 10 deletions
diff --git a/lib/datatype.js b/lib/datatype.js new file mode 100644 index 00000000..0a5228c0 --- /dev/null +++ b/lib/datatype.js @@ -0,0 +1,184 @@ +var mersenne = require('../vendor/mersenne');
+
+/**
+ * Method to reduce array of characters
+ * @param arr existing array of characters
+ * @param values array of characters which should be removed
+ * @return {*} new array without banned characters
+ */
+var arrayRemove = function (arr, values) {
+ values.forEach(function(value){
+ arr = arr.filter(function(ele){
+ return ele !== value;
+ });
+ });
+ return arr;
+};
+
+/**
+ *
+ * @namespace faker.dataType
+ */
+function DataType (faker, seed) {
+ // Use a user provided seed if it is an array or number
+ if (Array.isArray(seed) && seed.length) {
+ mersenne.seed_array(seed);
+ }
+ else if(!isNaN(seed)) {
+ mersenne.seed(seed);
+ }
+
+ /**
+ * returns a single random number based on a max number or range
+ *
+ * @method faker.random.number
+ * @param {mixed} options {min, max, precision}
+ */
+ this.number = function (options) {
+
+ if (typeof options === "number") {
+ options = {
+ max: options
+ };
+ }
+
+ options = options || {};
+
+ if (typeof options.min === "undefined") {
+ options.min = 0;
+ }
+
+ if (typeof options.max === "undefined") {
+ options.max = 99999;
+ }
+ if (typeof options.precision === "undefined") {
+ options.precision = 1;
+ }
+
+ // Make the range inclusive of the max value
+ var max = options.max;
+ if (max >= 0) {
+ max += options.precision;
+ }
+
+ var 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);
+
+ return randomNumber;
+
+ };
+
+ /**
+ * returns a single random floating-point number based on a max number or range
+ *
+ * @method faker.random.float
+ * @param {mixed} options
+ */
+ this.float = function (options) {
+ if (typeof options === "number") {
+ options = {
+ precision: options
+ };
+ }
+ options = options || {};
+ var opts = {};
+ for (var p in options) {
+ opts[p] = options[p];
+ }
+ if (typeof opts.precision === 'undefined') {
+ opts.precision = 0.01;
+ }
+ return faker.random.number(opts);
+ };
+
+ /**
+ * takes an array and returns a random element of the array
+ *
+ * @method faker.random.arrayElement
+ * @param {array} array
+ */
+ this.arrayElement = function (array) {
+ array = array || ["a", "b", "c"];
+ var r = faker.random.number({ max: array.length - 1 });
+ return array[r];
+ };
+
+ /**
+ * takes an array and returns a subset with random elements of the array
+ *
+ * @method faker.random.arrayElements
+ * @param {array} array
+ * @param {number} count number of elements to pick
+ */
+ this.arrayElements = function (array, count) {
+ array = array || ["a", "b", "c"];
+
+ if (typeof count !== 'number') {
+ count = faker.random.number({ min: 1, max: array.length });
+ } else if (count > array.length) {
+ count = array.length;
+ } else if (count < 0) {
+ count = 0;
+ }
+
+ var arrayCopy = array.slice();
+ var countToRemove = arrayCopy.length - count;
+ for (var i = 0; i < countToRemove; i++) {
+ var indexToRemove = faker.random.number({ max: arrayCopy.length - 1 });
+ arrayCopy.splice(indexToRemove, 1);
+ }
+
+ return arrayCopy;
+ };
+
+ /**
+ * uuid
+ *
+ * @method faker.random.uuid
+ */
+ this.uuid = function () {
+ var RFC4122_TEMPLATE = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
+ var replacePlaceholders = function (placeholder) {
+ var random = faker.random.number({ min: 0, max: 15 });
+ var value = placeholder == 'x' ? random : (random &0x3 | 0x8);
+ return value.toString(16);
+ };
+ return RFC4122_TEMPLATE.replace(/[xy]/g, replacePlaceholders);
+ };
+
+ /**
+ * boolean
+ *
+ * @method faker.random.boolean
+ */
+ this.boolean = function () {
+ return !!faker.random.number(1)
+ };
+
+
+ /**
+ * hexaDecimal
+ *
+ * @method faker.random.hexaDecimal
+ * @param {number} count defaults to 1
+ */
+ this.hexaDecimal = function hexaDecimal(count) {
+ if (typeof count === "undefined") {
+ count = 1;
+ }
+
+ var wholeString = "";
+ for(var 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;
+
+}
+
+module['exports'] = DataType;
diff --git a/lib/index.js b/lib/index.js index 433a6b81..25123e51 100644 --- a/lib/index.js +++ b/lib/index.js @@ -104,6 +104,9 @@ function Faker (opts) { var Music = require('./music'); self.music = new Music(self); + var DataType = require('./datatype'); + self.datatype = new DataType(self); + var _definitions = { "name": ["first_name", "last_name", "prefix", "suffix", "binary_gender", "gender", "title", "male_prefix", "female_prefix", "male_first_name", "female_first_name", "male_middle_name", "female_middle_name", "male_last_name", "female_last_name"], "address": ["city_name", "city_prefix", "city_suffix", "street_suffix", "county", "country", "country_code", "country_code_alpha_3", "state", "state_abbr", "street_prefix", "postcode", "postcode_by_state", "direction", "direction_abbr", "time_zone"], diff --git a/lib/random.js b/lib/random.js index f815c94f..5c585236 100644 --- a/lib/random.js +++ b/lib/random.js @@ -333,16 +333,8 @@ function Random (faker, seed) { * @param {number} count defaults to 1 */ this.hexaDecimal = function hexaDecimal(count) { - if (typeof count === "undefined") { - count = 1; - } - - var wholeString = ""; - for(var 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; + console.log("DeprecationWarning: Method is now located in faker.datatype.hexaDecimal"); + return faker.datatype.hexaDecimal(count); }; return this; |
