Advertisement
Guest User

Untitled

a guest
May 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. const chance = new require('chance')();
  2. const _ = require('lodash');
  3. const { ObjectId } = require('mongoose').Types;
  4.  
  5. const typeRands = {
  6. String: () => chance.string(),
  7. Number: () => chance.floating(),
  8. Date: () => 'ISODate("' + chance.date().toISOString() + '")',
  9. Boolean: () => chance.bool(),
  10. ObjectId: () => 'ObjectId("' + new ObjectId().toString() + '")',
  11. };
  12.  
  13. const randVal = (v, k) => {
  14. try {
  15. return (typeRands[v.name] || typeRands[v.type.name])();
  16. } catch (e) {
  17. }
  18. };
  19.  
  20. const formatObjectId = val => val.replace(/\"ObjectId\(\\"(.*?)\\"\)"/g, 'ObjectId("$1")');
  21. const formatISODate = val => val.replace(/\"ISODate\(\\"(.*?)\\"\)"/g, 'ISODate("$1")');
  22. const random = raw => _.chain(raw).mapValues(randVal).pickBy(_.identity).thru(JSON.stringify)
  23. .thru(formatObjectId)
  24. .thru(formatISODate).value();
  25.  
  26. module.exports = { random };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement