utils_dataset_check-duplicate-rows.js

import { nexaAlert } from "../../adapters/ezworks/nexa-alert";

/**
 * @callback CheckDuplicateRowsMessageFormatFunc
 * @param {number} row1
 * 중복 행 1.
 * @param {number} row2
 * 중복 행 2.
 */

/**
 * 데이터 셋의 중복을 검사합니다.
 *
 * @function checkDuplicateRows
 * @param {nexacro.Dataset} ds
 * 대상 데이터셋
 * @param {string} cols
 * 대상 컬럼 목록(,로 구분)
 * @param {string | CheckDuplicateRowsMessageFormatFunc} duplTextString
 * 메시지 텍스트 혹은 함수
 * @returns {boolean}
 * @memberof $f
 * @example
 * // 기본 템플릿 사용 방식
 * $f.checkDuplicateRows(this.ds_main, "PK1,PK2", "메시지 텍스트");
 *
 * // 커스텀 템플릿 방식
 * $f.checkDuplicateRows(
 *     this.ds_main,
 *     "PK1,PK2",
 *     (row1, row2) => `Lines ${row1} and ${row2} are duplicated.`,
 * );
 *
 */
export function checkDuplicateRows(ds, cols, duplTextString) {
    const arrCols = cols.split(",");
    for (let i = 0; i < ds.rowcount; i++) {
        // 노말은 무시 즉 새롭게 변경된 행만 확인한다.
        if (ds.getRowType(i) === Dataset.ROWTYPE_NORMAL) continue;
        let exprString = "";
        for (let j = 0; j < arrCols.length; j++) {
            exprString +=
                " && " +
                arrCols[j] +
                " == '" +
                ds.getColumn(i, arrCols[j]) +
                "'";
        }
        exprString = exprString.substr(4, exprString.length - 1);
        let rowIdx = ds.findRowExpr(exprString);
        if (i === rowIdx) {
            rowIdx = ds.findRowExpr(exprString, i + 1);
        }

        if (rowIdx > -1) {
            ds.rowposition = i;

            if (typeof duplTextString === "string") {
                // 문자열 방식
                nexaAlert(
                    "[" +
                        (Number(i) + 1) +
                        "번째 줄] " +
                        duplTextString +
                        " [" +
                        (Number(rowIdx) + 1) +
                        "번째 줄]과 중복되었습니다.",
                );
            } else if (typeof duplTextString === "function") {
                // 포맷 함수 방식
                nexaAlert(duplTextString(Number(i) + 1, Number(rowIdx) + 1));
            }

            return false;
        }
    }
    return true;
}