sortAlphanumericCaseInsensitive.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { defaultOrderByFn, Row } from 'react-table';
  20. import { sortAlphanumericCaseInsensitive } from '../src/DataTable/utils/sortAlphanumericCaseInsensitive';
  21. type RecursivePartial<T> = {
  22. [P in keyof T]?: T[P] | RecursivePartial<T[P]>;
  23. };
  24. const testData = [
  25. {
  26. values: {
  27. col: 'test value',
  28. },
  29. },
  30. {
  31. values: {
  32. col: 'a lowercase test value',
  33. },
  34. },
  35. {
  36. values: {
  37. col: '5',
  38. },
  39. },
  40. {
  41. values: {
  42. col: NaN,
  43. },
  44. },
  45. {
  46. values: {
  47. col: '1234',
  48. },
  49. },
  50. {
  51. values: {
  52. col: Infinity,
  53. },
  54. },
  55. {
  56. values: {
  57. col: '.!# value starting with non-letter characters',
  58. },
  59. },
  60. {
  61. values: {
  62. col: 'An uppercase test value',
  63. },
  64. },
  65. {
  66. values: {
  67. col: undefined,
  68. },
  69. },
  70. {
  71. values: {
  72. col: null,
  73. },
  74. },
  75. ];
  76. describe('sortAlphanumericCaseInsensitive', () => {
  77. it('Sort rows', () => {
  78. const sorted = [...testData].sort((a, b) =>
  79. // @ts-ignore
  80. sortAlphanumericCaseInsensitive(a, b, 'col'),
  81. );
  82. expect(sorted).toEqual([
  83. {
  84. values: {
  85. col: null,
  86. },
  87. },
  88. {
  89. values: {
  90. col: undefined,
  91. },
  92. },
  93. {
  94. values: {
  95. col: Infinity,
  96. },
  97. },
  98. {
  99. values: {
  100. col: NaN,
  101. },
  102. },
  103. {
  104. values: {
  105. col: '.!# value starting with non-letter characters',
  106. },
  107. },
  108. {
  109. values: {
  110. col: '1234',
  111. },
  112. },
  113. {
  114. values: {
  115. col: '5',
  116. },
  117. },
  118. {
  119. values: {
  120. col: 'a lowercase test value',
  121. },
  122. },
  123. {
  124. values: {
  125. col: 'An uppercase test value',
  126. },
  127. },
  128. {
  129. values: {
  130. col: 'test value',
  131. },
  132. },
  133. ]);
  134. });
  135. });
  136. const testDataMulti: Array<RecursivePartial<Row<object>>> = [
  137. {
  138. values: {
  139. colA: 'group 1',
  140. colB: '10',
  141. },
  142. },
  143. {
  144. values: {
  145. colA: 'group 1',
  146. colB: '15',
  147. },
  148. },
  149. {
  150. values: {
  151. colA: 'group 1',
  152. colB: '20',
  153. },
  154. },
  155. {
  156. values: {
  157. colA: 'group 2',
  158. colB: '10',
  159. },
  160. },
  161. {
  162. values: {
  163. colA: 'group 3',
  164. colB: '10',
  165. },
  166. },
  167. {
  168. values: {
  169. colA: 'group 3',
  170. colB: '15',
  171. },
  172. },
  173. {
  174. values: {
  175. colA: 'group 3',
  176. colB: '10',
  177. },
  178. },
  179. ];
  180. describe('sortAlphanumericCaseInsensitiveMulti', () => {
  181. it('Sort rows', () => {
  182. const sorted = defaultOrderByFn(
  183. [...testDataMulti] as Array<Row<object>>,
  184. [
  185. (a, b) => sortAlphanumericCaseInsensitive(a, b, 'colA'),
  186. (a, b) => sortAlphanumericCaseInsensitive(a, b, 'colB'),
  187. ],
  188. [true, false],
  189. );
  190. expect(sorted).toEqual([
  191. {
  192. values: {
  193. colA: 'group 1',
  194. colB: '20',
  195. },
  196. },
  197. {
  198. values: {
  199. colA: 'group 1',
  200. colB: '15',
  201. },
  202. },
  203. {
  204. values: {
  205. colA: 'group 1',
  206. colB: '10',
  207. },
  208. },
  209. {
  210. values: {
  211. colA: 'group 2',
  212. colB: '10',
  213. },
  214. },
  215. {
  216. values: {
  217. colA: 'group 3',
  218. colB: '15',
  219. },
  220. },
  221. {
  222. values: {
  223. colA: 'group 3',
  224. colB: '10',
  225. },
  226. },
  227. {
  228. values: {
  229. colA: 'group 3',
  230. colB: '10',
  231. },
  232. },
  233. ]);
  234. });
  235. });