All files / moveRule coordinate.js

100% Statements 49/49
100% Branches 16/16
100% Functions 9/9
100% Lines 49/49

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 5015x 15x 15x 15x 15x 15x 15x 20457x 20457x 20457x 20457x 20457x 20457x 20457x 20457x 7394x 7394x 7394x 20457x 39503x 39503x 20457x 6336x 6336x 6336x 20457x 20457x 20457x 6301x 6301x 6301x 6301x 20457x 20457x 20457x 5211x 20457x 20457x 4978x 4978x 20457x 20457x 20457x 20457x 20457x 20457x 20457x 20457x 20457x  
function createCoordinate(rowIndex, columnIndex) {
    let rowTotal = 8;
    let columns = "abcdefgh";
    let position = initPosition(rowIndex, columnIndex);
 
    function setPosition(newPosition) {
        position = newPosition;
    }
 
    function getPosition() {
        return position;
    }
 
    function getColumn() {
        return columns.indexOf(getColumnLetter()) + 1;
    }
 
    function getRow() {
        return position.slice(1, 2);
    }
 
    function getColumnLetter() {
        return position.slice(0, 1);
    }
 
    function getNextCoordinate(direction) {
        let originCoordinate = createCoordinate();
        originCoordinate.setPosition(getPosition());
        return direction.getNextCoordinate(originCoordinate);
    }
 
    function initPosition(row, column){
        if(row && row > 0 && row <= rowTotal && column && column > 0 && column <= columns.length)
            return columns[columnIndex-1] + rowIndex;
    }
    
    return {
        setPosition,
        getPosition,
        getColumn,
        getColumnLetter,
        getRow,
        getNextCoordinate
    }
}
 
export {
    createCoordinate
}