Mocker
0. Introduction
Mocker is a simple and lightweight library which is used for generating random js object as defined schema.
Features
- Lightweight: Only 4 basic type, pretty easy to get start.
- Extendable: String type support RegExp which allow your to generate custom type, but we support array and object too
- Non-invasive: Keep all properties key clean, do not modify it since you may need it anywhere else.
1. Installation
npm install @unpourtous/mocker --save
2. Usage
a) First, define your object, the object schema.
import { Types } from '@unpourtous/mocker'
const objectSchema = {
stringDate: Types.string('date'),
stringRange: Types.string().range(10, 100),
numberRange: Types.number().range(0, 100),
enum: Types.enum(['A', 'B', 'C']),
fixNumber: 520,
fixString: 'this is a fixString',
plainObject: {
far: Types.string(),
bar: Types.number()
},
stringArray: [Types.string()],
objectArray: [{
far: Types.string(),
bar: Types.number()
}]
}
b) Secondly, use Mocker to generate a random object.
import { Mocker } from '@unpourtous/mocker'
const mockerObject = Mocker.mockObject(objectSchema)
3. Detail Types & API
String
API | Description |
---|
string() | Create a instance of TPString. |
range(min, max) | Set min and max length for string, this is only used if this string dose not set regExp |
regExp(regExp) | Set regExp for this string, if this is set, the generated string match this regexp. |
fixValue(fixValue) | Set fix value |
Number
API | Description |
---|
number() | Create a instance of Number. |
range (min, max) | Set min value and max value for number |
format(format) | Set number format, get more detail from Numeral.js |
fixValue(fixValue) | Set fix value |
Enum
API | Description |
---|
enum(enumValues) | Create a instance of Enum. enumValues should be a array containers all possible value. |
Boolean
API | Description |
---|
boolean() | Create a instance of Boolean. |
fixValue(fixValue) | Set fix value |
APIS
4. Test & Demo
All test case was put in test/
.
Test result:
You can also clone it run the test by youself.
git clone https:
cd mocker
yarn
yarn run test
5. Thanks
- Randexp.js used to generate random string match a specified regexp
- Numeral.js as number format library
- Chai as test assert library
- Mocha as test framework
6. TODO
- Add browser support
7. License
MIT
Refs