// ${name: password}
// ${title: 密码生成器}

class password{
    ctor(){
        this.upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        this.lower = "abcdefghijklmnopqrstuvwxyz";
        this.num = "0123456789";  
        this.symbol = "()`~!@#$%^&*-+=_|{}[]:;'<>,.?/]";
        this.config = {upper = 2; lower = 2; num = 2; symbol = 2;}
    };
    getStr = function(len){
        config = this.config;
        var tab = {}; 
        var num = 0;
        var str = "";   
        for(k,v in config){
            var arr = ..string.split(this[[k]]);
                arr = ..table.shuffle(arr);
                v = tonumber(v); 
                if(!v or (#arr < v)) continue;
                str = str ++ this[[k]];
                ..table.push(tab,..table.pop(arr,v));
                num = num + v;
        }
        len  = len||num;
        if(len > num){
            str = ..string.random((len-num),str);
            if(#str==0) return "请至少指定一个长度>=每种字符个数的最大值的列表"; 
            ..table.push(tab,..table.pop(..string.split(str),#str))
        }
        if(len < num){
            ..table.pop(tab,(num-len))
        }	
        str = ..string.join(..table.shuffle(tab))
        return str; 	
    }
    
}

/**intellisense()
password = 密码生成器
password() = 创建密码生成器\n!password.
end intellisense**/

/**intellisense(!password.)
upper 设置大写字母列表 = @.upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
lower 设置小写字母列表 = @.lower = "abcdefghijklmnopqrstuvwxyz";
num 设置数字列表 = @.num = "0123456789";
symbol 设置符号列表 = @.symbol = "()`~!@#$%^&*-+=_|{}[]:;'<>,.?/]";
config 配置每种类型最小个数(不重复) = @.config={upper = 2; lower = 2; num = 2; symbol = 2};
getStr(.(len) = 获取指定长度的密码字符串\n默认为config配置每种字符类型个数总和
end intellisense**/

if(owner) return ;
 
import console;
var pwd = password();

//默认配置每种字符类型大于等于2位(不相同)
var str = pwd.getStr(20); 
console.dump("默认配置",str)

pwd.config={upper = 2; lower = 2; num = 0; symbol = 2};
str = pwd.getStr(20);
console.dump("排除数字",str)	

pwd.config={upper = 2; lower = 2; num = 2; symbol = 0};
str = pwd.getStr(20);
console.dump("排除符号",str)

pwd.config={upper = 2; lower = 2; num = 2; symbol = 2};
pwd.num = "186";
str = pwd.getStr(20);
console.dump("设置num范围",str)

console.pause(true)

// https://suiang.cn/aardio/code/password