Mocker is a simple and lightweight library which is used for generating random js object as defined schema.
- 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.
npm install @unpourtous/mocker --save
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, // If a valid type was set, the valid type will be the value
fixString: 'this is a fixString',
plainObject: { // define a object array
far: Types.string(),
bar: Types.number()
},
stringArray: [Types.string()], // define a primary type array
objectArray: [{ // define a object array
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)
// the mockerObject should like
// {
// stringDate: '1941-10-03',
// stringRange: 'PKlDGzTY10pMZjYDIMtEWThTlXGcQE1gp2rYcStO2n52vw',
// numberRange: 62.61,
// enum: 'B',
// default: 'GXvmNy',
// fixNumber: 520,
// fixString: 'this is a fixString',
// plainObject: { far: 'O0qBn', bar: 464255.58 },
// stringArray: [ 'X5iH5' ],
// objectArray: [ { far: '', bar: 574378.47 } ]
// }
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 |