functions.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. export const API_PREFIX = '/api'
  2. export const API_ROUTES = {
  3. ping: () => `${API_PREFIX}/ping`,
  4. dataCollect: () => `${API_PREFIX}/dataCollect`,
  5. listScenes: () => `${API_PREFIX}/listScenes`,
  6. listSceneQualities: sceneName => `${API_PREFIX}/listSceneQualities?${new URLSearchParams({ sceneName })}`,
  7. getImage: (sceneName, imageQuality, nearestQuality = false) => `${API_PREFIX}/getImage?${new URLSearchParams({ sceneName, imageQuality, nearestQuality })}`,
  8. getImageExtracts: (sceneName, imageQuality, horizontalExtractCount, verticalExtractCount, nearestQuality = false) =>
  9. `${API_PREFIX}/getImageExtracts?${new URLSearchParams({
  10. sceneName,
  11. imageQuality,
  12. horizontalExtractCount,
  13. verticalExtractCount,
  14. nearestQuality
  15. })}`
  16. }
  17. export const delay = ms => new Promise(res => setTimeout(res, ms))
  18. export const buildURI = (ssl, host, port, route = '') => `${ssl ? 'https' : 'http'}://${host}:${port}${route}`
  19. export const buildWsURI = (ssl, host, port, uuid = '') => `${ssl ? 'wss' : 'ws'}://${host}:${port}?uuid=${uuid}`
  20. export const sortIntArray = intArray => intArray ? intArray.sort((a, b) => a - b) : null
  21. export const findNearestUpper = (value, arrInt) => {
  22. const arr = sortIntArray(arrInt)
  23. const index = arr.findIndex(x => value === x)
  24. if (index >= 0 && index <= arr.length - 1)
  25. return index === arr.length - 1
  26. ? arr[index]
  27. : arr[index + 1]
  28. }
  29. export const findNearestLower = (value, arrInt) => {
  30. const arr = sortIntArray(arrInt)
  31. const index = arr.findIndex(x => value === x)
  32. if (index >= 0 && index <= arr.length - 1)
  33. return index === 0
  34. ? arr[index]
  35. : arr[index - 1]
  36. }
  37. /**
  38. * Randomize array element order in-place.
  39. * Using Durstenfeld shuffle algorithm.
  40. * @param {any[]} array Array to randomize
  41. * @returns {any[]} The randomized array
  42. * @see https://stackoverflow.com/a/12646864
  43. */
  44. export const shuffleArray = array => {
  45. for (let i = array.length - 1; i > 0; i--) {
  46. const j = Math.floor(Math.random() * (i + 1));
  47. [array[i], array[j]] = [array[j], array[i]]
  48. }
  49. return array
  50. }
  51. /**
  52. * Build a configuration file by merging the default config with the asked scene.
  53. * The asked scene config will overwrite the default config.
  54. * @param {Object} defaultConfig The default configuration object
  55. * @param {Object} scenesConfig The scenes specific configuration
  56. * @returns {Function} A function that will return the scene configuration
  57. */
  58. export const buildConfig = (defaultConfig = {}, scenesConfig = {}) =>
  59. sceneName => Object.assign(defaultConfig, scenesConfig[sceneName])