Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const chance = new require('chance')();
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const typeRands = {
- String: () => chance.string(),
- Number: () => chance.floating(),
- Date: () => 'ISODate("' + chance.date().toISOString() + '")',
- Boolean: () => chance.bool(),
- ObjectId: () => 'ObjectId("' + new ObjectId().toString() + '")',
- };
- const randVal = (v, k) => {
- try {
- return (typeRands[v.name] || typeRands[v.type.name])();
- } catch (e) {
- }
- };
- const formatObjectId = val => val.replace(/\"ObjectId\(\\"(.*?)\\"\)"/g, 'ObjectId("$1")');
- const formatISODate = val => val.replace(/\"ISODate\(\\"(.*?)\\"\)"/g, 'ISODate("$1")');
- const random = raw => _.chain(raw).mapValues(randVal).pickBy(_.identity).thru(JSON.stringify)
- .thru(formatObjectId)
- .thru(formatISODate).value();
- module.exports = { random };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement