functions.js 2.5 KB

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