You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

2 years ago
  1. /**
  2. * A function that normalizes the various forms that the screens object can be
  3. * provided in.
  4. *
  5. * Input(s):
  6. * - ['100px', '200px'] // Raw strings
  7. * - { sm: '100px', md: '200px' } // Object with string values
  8. * - { sm: { min: '100px' }, md: { max: '100px' } } // Object with object values
  9. * - { sm: [{ min: '100px' }, { max: '200px' }] } // Object with object array (multiple values)
  10. *
  11. * Output(s):
  12. * - [{ name: 'sm', values: [{ min: '100px', max: '200px' }] }] // List of objects, that contains multiple values
  13. */ "use strict";
  14. Object.defineProperty(exports, "__esModule", {
  15. value: true
  16. });
  17. Object.defineProperty(exports, "normalizeScreens", {
  18. enumerable: true,
  19. get: ()=>normalizeScreens
  20. });
  21. function normalizeScreens(screens, root = true) {
  22. if (Array.isArray(screens)) {
  23. return screens.map((screen)=>{
  24. if (root && Array.isArray(screen)) {
  25. throw new Error("The tuple syntax is not supported for `screens`.");
  26. }
  27. if (typeof screen === "string") {
  28. return {
  29. name: screen.toString(),
  30. values: [
  31. {
  32. min: screen,
  33. max: undefined
  34. }
  35. ]
  36. };
  37. }
  38. let [name, options] = screen;
  39. name = name.toString();
  40. if (typeof options === "string") {
  41. return {
  42. name,
  43. values: [
  44. {
  45. min: options,
  46. max: undefined
  47. }
  48. ]
  49. };
  50. }
  51. if (Array.isArray(options)) {
  52. return {
  53. name,
  54. values: options.map((option)=>resolveValue(option))
  55. };
  56. }
  57. return {
  58. name,
  59. values: [
  60. resolveValue(options)
  61. ]
  62. };
  63. });
  64. }
  65. return normalizeScreens(Object.entries(screens !== null && screens !== void 0 ? screens : {}), false);
  66. }
  67. function resolveValue({ "min-width": _minWidth , min =_minWidth , max , raw } = {}) {
  68. return {
  69. min,
  70. max,
  71. raw
  72. };
  73. }