Guest User

Untitled

a guest
May 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. //function to get latest ABno;
  2. //function will return distinct latest version from an array of strings in the format 'ABxxx-vx',
  3. //where AB is the starting of the string,
  4. //xxx represents the number followed by AB,
  5. //-vx respresents the version and x at the end is the version number
  6.  
  7. var ABNOLatest = function(ABarray){
  8. ABarray = ABarray.sort();
  9. //console.log(ABarray);
  10. var baseAB;
  11. var PrevAB = {};
  12. var maxver;
  13. var finalABList=[];
  14. ABarray.map(function(AB){
  15. var ABsplit = AB.split("-");
  16. if(ABsplit.length>1) maxver=parseInt(ABsplit[1].replace('V',''));
  17. else maxver=0;
  18.  
  19. baseAB = ABsplit[0];
  20. //console.log(PrevAB,baseAB,maxver);
  21. //conditionals to check and update list of latest ABNo
  22. if(Object.keys(PrevAB).length === 0 && PrevAB.constructor === Object){ //checks whether object is empty or not
  23. //console.log("if loop");
  24. PrevAB.baseAB = baseAB;
  25. PrevAB.ver = maxver;
  26. PrevAB.AB=AB;
  27. finalABList.push(PrevAB.AB) //Initial push to finallist array
  28. //console.log(PrevAB);
  29. }
  30. else if(PrevAB.baseAB==baseAB){
  31. //console.log("else if loop ==> baseAB");
  32. finalABList[finalABList.length-1] = (PrevAB.ver<maxver? AB:PrevAB.AB); //sets Last ABNo based on version number
  33. //console.log(finalABList[finalABList.length-1]);
  34. PrevAB.ver= (PrevAB.ver<maxver? maxver:PrevAB.ver); //sets version number for Prev.ver
  35. //console.log("PrevAB.ve",PrevAB.ver);
  36. PrevAB.AB=AB;
  37. }
  38. else if (PrevAB.AB!=undefined && finalABList.indexOf(AB)==-1){//if current ABNo is not in the list
  39. //console.log("else if loop ==> finalABList");
  40. finalABList.push(AB);
  41. //console.log(AB);
  42. PrevAB.baseAB = baseAB;
  43. PrevAB.ver = maxver;
  44. PrevAB.AB=AB;
  45. }
  46.  
  47. });
  48. console.log('finalABList',finalABList);
  49. return finalABList;
  50.  
  51. }
  52.  
  53. //Following is a sample array that you can use for test.
  54. var POar = ["AB1914411-V3", "AB2583130-V2", "AB2583130", "AB2017312-V3","AB2017312-V20", "AB2017312-V2", "AB1985437-V3", "AB1985437-V2"];
  55. ABNOLatest(POar);
Add Comment
Please, Sign In to add comment