11import { describe , expect , it , vi } from "vitest" ;
2- import { swap , defaultCompare , sortProducts , ProductTuple , newSortProducts } from "./sort" ;
2+ import { swap , defaultCompare , sortProducts , ProductTuple , newSortProducts , compareStrings , Compare } from "./sort" ;
33
44describe ( "swap" , ( ) => {
55 it ( "should swap two items in an array" , ( ) => {
@@ -217,7 +217,6 @@ describe("new sort products", () => {
217217 ] ;
218218 const [ , ...rows ] = data ;
219219 const result = newSortProducts ( rows , "ascending" ) ;
220- console . log ( "result" , result ) ;
221220 expect ( result ) . toEqual ( [
222221 [ "5" , "1" , "AB 7" ] ,
223222 [ "4" , "1" , "AB 9" ] ,
@@ -230,6 +229,124 @@ describe("new sort products", () => {
230229 } ) ;
231230
232231 it ( "should sort an array of products by pick location in ascending order from A 1 to AZ 10" , ( ) => {
233- //
232+ const data : ProductTuple [ ] = [
233+ [ "product_code" , "quantity" , "pick_location" ] ,
234+ [ "1" , "1" , "A 1" ] ,
235+ [ "2" , "1" , "Z 1" ] ,
236+ [ "3" , "1" , "AB 10" ] ,
237+ [ "9" , "1" , "AZ 7" ]
238+ ] ;
239+ const [ , ...rows ] = data ;
240+ const result = newSortProducts ( rows , "ascending" ) ;
241+ console . log ( "result" , result ) ;
242+ expect ( result ) . toEqual ( [
243+ [ "1" , "1" , "A 1" ] ,
244+ [ "2" , "1" , "Z 1" ] ,
245+ [ "3" , "1" , "AB 10" ] ,
246+ [ "9" , "1" , "AZ 7" ]
247+ ] ) ;
234248 } ) ;
235249} ) ;
250+
251+ describe ( "compareStrings" , ( ) => {
252+ // it("should return undefined if nothing matches", () => {
253+ // // TODO empty case
254+ // });
255+ describe ( "single char" , ( ) => {
256+ // Case 1: A, A - single chars, both match
257+ it ( "should return EQUALS if single chars, both match" , ( ) => {
258+ const data = [ "A" , "A" ] ;
259+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
260+ expect ( result ) . toEqual ( Compare . EQUALS ) ;
261+ } ) ;
262+
263+ // Case 2: A, Z || Z, A - single chars, no match
264+ it ( "should return BIGGER THAN if single chars, no match" , ( ) => {
265+ const data = [ "Z" , "A" ] ;
266+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
267+ expect ( result ) . toEqual ( Compare . BIGGER_THAN ) ;
268+ } ) ;
269+
270+ it ( "should return LESS THAN if single chars, no match" , ( ) => {
271+ const data = [ "A" , "Z" ] ;
272+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
273+ expect ( result ) . toEqual ( Compare . LESS_THAN ) ;
274+ } ) ;
275+ } ) ;
276+
277+ describe ( "multiple chars" , ( ) => {
278+ // Case 3: AA, AA - multiple chars, both match
279+ it ( "should return EQUALS if the first and second letters are the same" , ( ) => {
280+ const data = [ "AA" , "AA" ] ;
281+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
282+ expect ( result ) . toEqual ( Compare . EQUALS ) ;
283+ } ) ;
284+
285+ // Case 4: AA, AZ - multiple chars, only first letters match
286+ it ( "should return LESS THAN if the first letters match but the second char is lower" , ( ) => {
287+ const data = [ "AA" , "AB" ] ;
288+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
289+ expect ( result ) . toEqual ( Compare . LESS_THAN ) ;
290+ } ) ;
291+
292+ it ( "should return BIGGER THAN if the first letters match but the second char is higher" , ( ) => {
293+ const data = [ "AB" , "AA" ] ;
294+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
295+ expect ( result ) . toEqual ( Compare . BIGGER_THAN ) ;
296+ } ) ;
297+
298+ // Case 5: ZA, AA - multiple chars, only second letters match
299+ it ( "should return LESS THAN if the first letters dont match and only second letters match" , ( ) => {
300+ const data = [ "AA" , "BA" ] ;
301+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
302+ expect ( result ) . toEqual ( Compare . LESS_THAN ) ;
303+ } ) ;
304+
305+ it ( "should return BIGGER THAN if the first letters dont match and only second letters match" , ( ) => {
306+ const data = [ "BA" , "AA" ] ;
307+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
308+ expect ( result ) . toEqual ( Compare . BIGGER_THAN ) ;
309+ } ) ;
310+
311+ // Case 6: ZA, AZ - multiple chars, no match
312+ it ( "should return BIGGER THAN if no chars match and the first char is lower" , ( ) => {
313+ const data = [
314+ [ "ZA" , "AZ" ] ,
315+ [ "XY" , "AK" ]
316+ ] ;
317+ data . forEach ( ( [ a , b ] ) => {
318+ const result = compareStrings ( a , b ) ;
319+ expect ( result ) . toEqual ( Compare . BIGGER_THAN ) ;
320+ } ) ;
321+ } ) ;
322+
323+ it ( "should return LESS THAN if no chars match and the first char is higher" , ( ) => {
324+ const data = [
325+ [ "AZ" , "ZA" ] ,
326+ [ "KA" , "XY" ]
327+ ] ;
328+ data . forEach ( ( [ a , b ] ) => {
329+ const result = compareStrings ( a , b ) ;
330+ expect ( result ) . toEqual ( Compare . LESS_THAN ) ;
331+ } ) ;
332+ } ) ;
333+
334+ // Case 7: AA, Z - multiple chars, and single char
335+ it ( "should return LESS THAN if the first string is multiple chars and the second is a single char" , ( ) => {
336+ const data = [ "AA" , "Z" ] ;
337+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
338+ expect ( result ) . toEqual ( Compare . LESS_THAN ) ;
339+ } ) ;
340+
341+ // Case 8: Z, AA - single char, and multiple chars
342+ it ( "should return BIGGER THAN if the first string is a single char and second is multiple" , ( ) => {
343+ const data = [ "Z" , "AA" ] ;
344+ const result = compareStrings ( data [ 0 ] , data [ 1 ] ) ;
345+ expect ( result ) . toEqual ( Compare . BIGGER_THAN ) ;
346+ } ) ;
347+ } ) ;
348+
349+ // describe("numeric shelf height", () => {
350+ // //
351+ // });
352+ } ) ;
0 commit comments