1 | /*
|
2 | * Licensed under the Apache License, Version 2.0 (the "License");
|
3 | * you may not use this file except in compliance with the License.
|
4 | * You may obtain a copy of the License at
|
5 | *
|
6 | * http://www.apache.org/licenses/LICENSE-2.0
|
7 | *
|
8 | * Unless required by applicable law or agreed to in writing, software
|
9 | * distributed under the License is distributed on an "AS IS" BASIS,
|
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11 | * See the License for the specific language governing permissions and
|
12 | * limitations under the License.
|
13 | */
|
14 |
|
15 | ;
|
16 |
|
17 | const loremIpsum = require('lorem-ipsum');
|
18 | const Moment = require('moment-mini');
|
19 |
|
20 | /**
|
21 | * Empty value generator.
|
22 | * @private
|
23 | */
|
24 | class EmptyValueGenerator {
|
25 | /**
|
26 | * This constructor should not be called directly.
|
27 | * @private
|
28 | */
|
29 | constructor() {
|
30 | this.currentDate = new Moment();
|
31 | }
|
32 |
|
33 | /**
|
34 | * Get a default DateTime value.
|
35 | * @return {Moment} a date value.
|
36 | */
|
37 | getDateTime() {
|
38 | return this.currentDate;
|
39 | }
|
40 |
|
41 | /**
|
42 | * Get a default Integer value.
|
43 | * @return {number} an Integer value.
|
44 | */
|
45 | getInteger() {
|
46 | return 0;
|
47 | }
|
48 |
|
49 | /**
|
50 | * Get a default Long value.
|
51 | * @return {number} a Long value.
|
52 | */
|
53 | getLong() {
|
54 | return 0;
|
55 | }
|
56 |
|
57 | /**
|
58 | * Get a default Double value.
|
59 | * @return {number} a Double value.
|
60 | */
|
61 | getDouble() {
|
62 | return 0.000;
|
63 | }
|
64 |
|
65 | /**
|
66 | * Get a default Boolean value.
|
67 | * @return {boolean} a Boolean value.
|
68 | */
|
69 | getBoolean() {
|
70 | return false;
|
71 | }
|
72 |
|
73 | /**
|
74 | * Get a default String value.
|
75 | * @return {string} a String value.
|
76 | */
|
77 | getString() {
|
78 | return '';
|
79 | }
|
80 |
|
81 | /**
|
82 | * Get the first enum value from the supplied array.
|
83 | * @param {Array} enumValues Array of possible enum values.
|
84 | * @return {*} an enum value.
|
85 | */
|
86 | getEnum(enumValues) {
|
87 | return enumValues[0];
|
88 | }
|
89 |
|
90 | /**
|
91 | * Get an array using the supplied callback to obtain array values.
|
92 | * @param {Function} valueSupplier - callback to obtain values.
|
93 | * @return {Array} an array
|
94 | */
|
95 | getArray(valueSupplier) {
|
96 | return [];
|
97 | }
|
98 | }
|
99 |
|
100 | /**
|
101 | * Sample data value generator.
|
102 | * @private
|
103 | */
|
104 | class SampleValueGenerator extends EmptyValueGenerator {
|
105 | /**
|
106 | * This constructor should not be called directly.
|
107 | * @private
|
108 | */
|
109 | constructor() {
|
110 | super();
|
111 | }
|
112 |
|
113 | /**
|
114 | * Get a randomly generated sample Integer value.
|
115 | * @return {number} an Integer value.
|
116 | */
|
117 | getInteger() {
|
118 | return Math.round(Math.random() * Math.pow(2, 16));
|
119 | }
|
120 |
|
121 | /**
|
122 | * Get a randomly generated sample Long value.
|
123 | * @return {number} a Long value.
|
124 | */
|
125 | getLong() {
|
126 | return Math.round(Math.random() * Math.pow(2, 32));
|
127 | }
|
128 |
|
129 | /**
|
130 | * Get a randomly generated sample Double value.
|
131 | * @return {number} a Double value.
|
132 | */
|
133 | getDouble() {
|
134 | return Number((Math.random() * Math.pow(2, 8)).toFixed(3));
|
135 | }
|
136 |
|
137 | /**
|
138 | * Get a randomly generated sample Boolean value.
|
139 | * @return {boolean} a Boolean value.
|
140 | */
|
141 | getBoolean() {
|
142 | return Math.round(Math.random()) === 1;
|
143 | }
|
144 |
|
145 | /**
|
146 | * Get a randomly generated sample String value.
|
147 | * @return {string} a String value.
|
148 | */
|
149 | getString() {
|
150 |
|
151 | return loremIpsum({
|
152 | count: 1 // Number of words, sentences, or paragraphs to generate.
|
153 | , units: 'sentences' // Generate words, sentences, or paragraphs.
|
154 | , sentenceLowerBound: 1 // Minimum words per sentence.
|
155 | , sentenceUpperBound: 5 // Maximum words per sentence.
|
156 |
|
157 | });
|
158 | }
|
159 |
|
160 |
|
161 | /**
|
162 | * Get a randomly selected enum value from the supplied array.
|
163 | * @param {Array} enumValues Array of possible enum values.
|
164 | * @return {*} an enum value.
|
165 | */
|
166 | getEnum(enumValues) {
|
167 | return enumValues[Math.floor(Math.random() * enumValues.length)];
|
168 | }
|
169 |
|
170 | /**
|
171 | * Get an array using the supplied callback to obtain array values.
|
172 | * @param {Function} valueSupplier - callback to obtain values.
|
173 | * @return {Array} an array
|
174 | */
|
175 | getArray(valueSupplier) {
|
176 | return [valueSupplier()];
|
177 | }
|
178 | }
|
179 |
|
180 | /**
|
181 | * Factory providing static methods to create ValueGenerator instances.
|
182 | * @private
|
183 | */
|
184 | class ValueGeneratorFactory {
|
185 | /**
|
186 | * Create a value generator that supplies empty values.
|
187 | * @return {ValueGenerator} a value generator.
|
188 | */
|
189 | static empty() {
|
190 | return new EmptyValueGenerator();
|
191 | }
|
192 |
|
193 | /**
|
194 | * Create a value generator that supplies randomly generated sample values.
|
195 | * @return {ValueGenerator} a value generator.
|
196 | */
|
197 | static sample() {
|
198 | return new SampleValueGenerator();
|
199 | }
|
200 | }
|
201 |
|
202 | module.exports = ValueGeneratorFactory;
|