helpers.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. export const handleKeyDown = (e) => {
  2. if(e.keyCode === 13){
  3. document.getElementById("input_carry").focus();
  4. }
  5. };
  6. export const afterCommaLen = (number) => {
  7. let noStrList = number.toString().split(".");
  8. if (noStrList.length > 1){
  9. return noStrList[1].length;
  10. }
  11. return 0;
  12. };
  13. export function numbersToArrOfArr(numbers) {
  14. let befComma = Math.max(...numbers).toString().split(".")[0].length;
  15. let afterComma = Math.max(...numbers.map(x => afterCommaLen(x)));
  16. let withComma = Math.max(...numbers.map(x => x.toString().indexOf(".")));
  17. let arrLength;
  18. if(withComma < 0){ // no comma found
  19. arrLength = befComma;
  20. }else{
  21. arrLength = befComma + afterComma + 1; // add comma and after comma len
  22. }
  23. let numbersArr = numbers.map(x => x.toString().split(""));
  24. for (let numArr of numbersArr){
  25. let commaIdx = numArr.indexOf(".");
  26. // without comma, add before
  27. while(commaIdx===-1 && numArr.length < befComma){
  28. numArr.unshift("&nbsp;"); // add " " before comma
  29. }
  30. // without comma, add after
  31. while(commaIdx===-1 && numArr.length < arrLength){
  32. numArr.push("&nbsp;"); // add " " after comma
  33. }
  34. // with comma, add before
  35. while(commaIdx>-1 && commaIdx!==befComma){
  36. numArr.unshift("&nbsp;"); // add " " before comma
  37. commaIdx = numArr.indexOf(".");
  38. }
  39. // with comma, add after
  40. while(numArr.length < arrLength){
  41. numArr.push("&nbsp;"); // add " " after comma
  42. }
  43. numArr.unshift("&nbsp;"); // for carry
  44. }
  45. return [numbersArr, befComma+1];
  46. // TODO: assert all have same length and comma at same index?
  47. }