/**
 * Created by CSL on 2017-06-06.
 * public functions for web.
 */

// 忽略大小写判断字符串是否和参数指定的字符串相同
String.prototype.sameText = function (str) {
    return this.toLowerCase() == str.toLowerCase();
};

// 忽略大小写判断字符串是否有参数指定的子串
String.prototype.hasSubStr = function (str) {
    return this.toLowerCase().indexOf(str.toLowerCase()) > -1;
};

// 判断字符串是否是数字形式的字符串
String.prototype.isNumberStr = function () {
    return this == +this;
};

// 树结点计算时，取费会出现值为NaN的情况，导致往父节点汇总(递归相加)会出现错误。
function parseFloatPlus(value){
    let rst = parseFloat(value);
    return  isNaN(rst) ? 0 : rst;
};

// 数组合并，并去重复。
Array.prototype.merge = function (arr) {
    if (arr.length > 0){
        for (let e of arr){
            if (!this.includes(e)) this.push(e);
        };
    }
};

// 数组是否包含另一个数组。
Array.prototype.hasSubArr = function (subArr){
    for(var i = 0, len = subArr.length; i < len; i++){
        if(this.indexOf(subArr[i]) == -1) return false;
    }
    return true;
};

Array.prototype.delete = function (elem){
    let idx = this.findIndex(function (e){return e == elem});
    if (idx != -1) this.splice(idx, 1);
};

function seqString(num,length){
    var numstr = num.toString();
    var l=numstr.length;
    if (numstr.length>=length) {return numstr;}

    for(var  i = 0 ;i<length - l;i++){
        numstr = "0" + numstr;
    }
    return numstr;
};

function customRowHeader(sheet, dataLength) {
    for (let i = 0; i < dataLength; i++) {
        sheet.setValue(i, 0, `F${i + 1}`, GC.Spread.Sheets.SheetArea.rowHeader);
    }
};

function changePropNames(object, oldNames, newNames) {
    if (!object) return;
    for (let i = 0; i < oldNames.length; i++) {
        if (object[oldNames[i]]){
            object[newNames[i]] = object[oldNames[i]];
            delete object[oldNames[i]];
        };
    }
};

function changePropNames(object, oldNames, newNames) {
    if (!object) return;
    for (let i = 0; i < oldNames.length; i++) {
        if (object[oldNames[i]]){
            object[newNames[i]] = object[oldNames[i]];
            delete object[oldNames[i]];
        };
    }
};

function deletePropNames(object, namesArr) {
    if (!object) return;
    for (let name of namesArr){
        if (object[name]) delete object[name];
    };
};