common.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. const Mock = require('mockjs')
  2. const config = require('../src/utils/config')
  3. const queryArray = (array, key, keyAlias = 'key') => {
  4. if (!(array instanceof Array)) {
  5. return null
  6. }
  7. let data
  8. for (const item of array) {
  9. if (item[keyAlias] === key) {
  10. data = item
  11. break
  12. }
  13. }
  14. if (data) {
  15. return data
  16. }
  17. return null
  18. }
  19. const NOTFOUND = {
  20. message: 'Not Found',
  21. documentation_url: 'http://localhost:8000/request',
  22. }
  23. let postId = 0
  24. const posts = Mock.mock({
  25. 'data|100': [
  26. {
  27. id () {
  28. postId += 1
  29. return postId + 10000
  30. },
  31. 'status|1-2': 1,
  32. title: '@title',
  33. author: '@last',
  34. categories: '@word',
  35. tags: '@word',
  36. 'views|10-200': 1,
  37. 'comments|10-200': 1,
  38. visibility: () => {
  39. return Mock.mock('@pick(["Public",'
  40. + '"Password protected", '
  41. + '"Private"])')
  42. },
  43. date: '@dateTime',
  44. image () {
  45. return Mock.Random.image('100x100', Mock.Random.color(), '#757575', 'png', this.author.substr(0, 1))
  46. },
  47. },
  48. ],
  49. }).data
  50. module.exports = {
  51. queryArray,
  52. NOTFOUND,
  53. Mock,
  54. posts,
  55. config,
  56. }