dummy-json-js
Version:
Generate dummy json data. As file or object.
162 lines (129 loc) • 4.24 kB
Markdown
This is a npm package to create json dummy data.
First import the module.
```js
const {Type, generateJSONDummy, generateJSONDummyArray} = require("dummy-json-js")
```
To generate json dummy data you need to define the format the data should be. The format contains of the key you want to name the entry, as well as the type of data.
```js
let dummyDataConfig = {
"stringKey": {"type": Type.String},
"numberKey": {"type": Type.Number},
"booleanKey": {"type": Type.Boolean},
"nullKey": {"type": Type.Null}
}
```
You can also add explicit configurations to some Types.
These options are available:
| Type | options name | default-value | example |
| :--- | :--- | :--- | :--- |
| String | length | 10 | "length": 123 |
| Number | limit | 100 | "limit": 200 |
| Integer | limit | 100 | "limit": 200 |
| Boolean | chance | 0.5 | "chance": 0.1 |
| StreetNumber | includeLetter | false | "includeLetter": true|
Example:
```js
let dummyDataConfigExplicit = {
"stringKey": {"type": Type.String, "length": 123},
"numberKey": {"type": Type.Number, "limit": 200},
"integerKey": {"type": Type.Integer, "limit": 200},
"booleanKey": {"type": Type.Boolean, "chance": 0.1},
"streetNumberKey": {"type": Type.StreetNumber, "includeLetter": true}
}
```
To generate a single object use the function generateJSONDummy, and parse the data config object.
```js
const {Type, generateJSONDummy} = require("dummy-json-js")
let dummyDataConfigExplicit = {
"stringKey": {"type": Type.String, "length": 123},
"numberKey": {"type": Type.Number, "limit": 200},
"booleanKey": {"type": Type.Boolean, "chance": 0.1},
"nullKey": {"type": Type.Null}
}
dummyData = generateJSONDummy(dummyDataConfigExplicit)
```
To generate a array of json objects use the function generateJSONDummyArray, and parse the data config object.
You can also parse the number of elements the array should contain, default is 2.
```js
const {Type, generateJSONDummyArray} = require("dummy-json-js")
let dummyDataConfigExplicit = {
"stringKey": {"type": Type.String, "length": 123},
"numberKey": {"type": Type.Number, "limit": 200},
"booleanKey": {"type": Type.Boolean, "chance": 0.1},
"nullKey": {"type": Type.Null}
}
dummyDataArray = generateJSONDummyArray(dummyDataConfigExplicit)
```
Selector: Type.String
>Example output: 'cTaGzkrOFD'
**Options:**
| options name | default-value | example |
| :--- | :--- | :--- |
| length | 10 | "length": 123 |
Selector: Type.Number
>Example output: 34.969813298829244
**Options:**
| options name | default-value | example |
| :--- | :--- | :--- |
| limit | 10 | "limit": 200 |
Selector: Type.Integer
>Example output: 34
**Options:**
| options name | default-value | example |
| :--- | :--- | :--- |
| limit | 10 | "limit": 200 |
Selector: Type.Boolean
>Example output: true
**Options:**
| options name | default-value | example |
| :--- | :--- | :--- |
| chance | 0.5 | "chance": 0.1 |
0.6 = 60% percent chance to get true
Selector: Type.Null
>Output: null
Selector: Type.FirstName
>Example output: 'Noah'
Selector: Type.LastName
>Example output: 'Miller'
Selector: Type.Company
>Example output: 'Krani'
Selector: Type.Street
>Example output: 'Industrial Dr'
Selector: Type.Streetnumber
>Example output: 125
**Options:**
| options name | default-value | example |
| :--- | :--- | :--- |
| includeLetter | false | "includeLetter": true |
include letters at end of streetnumber
Selector: Type.City
>Example output: 'Miami'
Selector: Type.PostalCode
>Example output: '16057'
PostalCodes may also end with letters
Selector: Type.Country
>Example output: 'Sweden'
Selector: Type.Continent
>Example output: 'Europe'