Guest User

Untitled

a guest
Jan 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. /**
  2. * JS utility function to sort an array of clothing sizes (["M", "S", "XL", "XXS"])
  3. * by size and not alphabetically.
  4. *
  5. * Usage:
  6.  
  7. ```
  8. clothingSizesSort(["S", "M", "XL"]) // should === ["XXS", "S", "M", "XL")
  9. ```
  10.  
  11. */
  12.  
  13. /*************
  14. * FUNCTIONS *
  15. *************/
  16.  
  17. /**
  18. * sort an array of sizes
  19. */
  20. function clothingSizesSort(sizesRaw) {
  21. // pair sizes with their numerical values
  22. const sizesWithNumValue = sizesRaw.map(size => {
  23. return {
  24. raw: size,
  25. num: numValue(size)
  26. };
  27. });
  28.  
  29. // and sort them ascending
  30. const sizesSortedByNumValue = sizesWithNumValue.sort(
  31. (a, b) => a.num - b.num
  32. );
  33.  
  34. return sizesSortedByNumValue.map(el => el.raw);
  35. }
  36.  
  37. /**
  38. * parse a size independent from type
  39. */
  40. function numValue(raw) {
  41. const num = parseInt(raw);
  42. if (typeof raw === "string") {
  43. // raw is numerical string, e.g "45"
  44. if (raw !== num.toString()) {
  45. return sizeStrValue(raw);
  46. }
  47. }
  48. return num;
  49. }
  50.  
  51. /**
  52. * convert a size string into a number value for sorting.
  53. * XS = 0.1, S = 1, M = 10, L = 100, XL = 10000 and so forth
  54. */
  55. function sizeStrValue(sizeStr) {
  56. sizeStr = sizeStr.toLowerCase();
  57.  
  58. // special case SM = 5
  59. if (sizeStr === "sm") {
  60. return 5;
  61. }
  62.  
  63. const sizePcs = sizeStr.split("");
  64. const sizeName = sizePcs.pop();
  65. let multiplier = Math.pow(10, sizePcs.length);
  66. let sizeValue = 0;
  67.  
  68. switch (sizeName) {
  69. case "s":
  70. sizeValue = 1;
  71. // [...X]S inverts the multiplier
  72. multiplier = 1 / multiplier;
  73. break;
  74. case "m":
  75. sizeValue = 10;
  76. break;
  77. case "l":
  78. sizeValue = 100;
  79. break;
  80. }
  81.  
  82. return sizeValue * multiplier;
  83. }
  84.  
  85. /*************
  86. * "TESTS" *
  87. *************/
  88.  
  89. const tests = [
  90. {
  91. in: ["45", "42", "38", "46"],
  92. expect: ["38", "42", "45", "46"]
  93. },
  94. {
  95. in: [45, 42, 38, 46],
  96. expect: [38, 42, 45, 46]
  97. },
  98. ,
  99. {
  100. in: ["s", "xxl", "m"],
  101. expect: ["s", "m", "xxl"]
  102. },
  103. {
  104. in: ["m", "s", "xl", "xxl", "xs", "xxs", "sm", "xxxxxxl", "xxxxxxxxxs"],
  105. expect: [
  106. "xxxxxxxxxs",
  107. "xxs",
  108. "xs",
  109. "s",
  110. "sm",
  111. "m",
  112. "xl",
  113. "xxl",
  114. "xxxxxxl"
  115. ]
  116. },
  117. {
  118. in: ["foo"],
  119. expect: ["foo"]
  120. },
  121. {
  122. in: [],
  123. expect: []
  124. }
  125. ];
  126.  
  127. tests.forEach(test => {
  128. const result = clothingSizesSort(test.in);
  129. const equals = JSON.stringify(result) === JSON.stringify(test.expect);
  130.  
  131. if (equals) {
  132. console.log("- ok -");
  133. } else {
  134. console.log("- fail -");
  135. console.log(result);
  136. console.log("- expected -");
  137. console.log(test.expect);
  138. }
  139. });
Add Comment
Please, Sign In to add comment