Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS 代码片段汇总 #64

Open
shiiiiiiji opened this issue Dec 18, 2019 · 1 comment
Open

JS 代码片段汇总 #64

shiiiiiiji opened this issue Dec 18, 2019 · 1 comment
Labels

Comments

@shiiiiiiji
Copy link
Owner

/**
 * 生成一个len+1的随机数,包含最后一位交验位
 * @param len
 * @returns {string}
 */
const randomArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const randomArrayLength = randomArray.length;
function rand(len, check = true) {
    let sum = '';
    let index;
    let count = 0;
    for (let i = 0; i < len; i++) {
        index = Math.floor(Math.random() * randomArrayLength);
        sum += randomArray.charAt(index);
        count += randomArray.charCodeAt(index);
    }
    if(check){
        //是否添加校验位
        sum += randomArray.charAt(count % randomArrayLength);
    }
    return sum;
}
@shiiiiiiji shiiiiiiji changed the title 生成随机数 代码片段-js Dec 18, 2019
@shiiiiiiji shiiiiiiji changed the title 代码片段-js JS 代码片段汇总 Nov 3, 2020
@shiiiiiiji shiiiiiiji added the js label Nov 3, 2020
@shiiiiiiji
Copy link
Owner Author

shiiiiiiji commented Nov 3, 2020

id2url(id, version) {
	if (!version) version = 1;
	if (typeof id === 'string' && id.length === 24) {
		return id;
	}
	if (id instanceof Array) {
		for (let i = 0; i < id.length; i++) {
			const value = id[i];
			id[i] = this.id2url(value);
		}
		return id;
	} else if (parseInt(id, 10) > 0) {
		let url;
		switch (version) {
			case 1:
				url = version + parseInt(`${id * 2 + 56}`, 10 | 0).toString(36 | 0);
				break;
			default:
				url = false;
				break;
		}
		return url;
	}
	return id;
}
url2id(url, check) {
	if (!check) check = false;
	if (url.length === 24 && url.match(/^[0-9a-f]+$/)) return url;  //兼容MongoId

	if (url instanceof Array) {
		for (var i = 0; i < url.length; i++) {
			var value = url[i];
			url[i] = this.url2id(value);
		}
		return url;
	}
	else if (url.indexOf(',') > 0) {
		url = url.split(',');
		for (var i = 0; i < url.length; i++) {
			var value = url[i];
			if (!value) {
				delete url[i];
				continue;
			}
			url[i] = this.url2id(value);
		}
		return url.join(',');
	}
	else {
		if (check && url.match(/^(null|undefined)$/i)) {
			return false;
		}
		const version = parseInt(url.substr(0, 1));
		let id = 0;
		switch (version) {
			case 1:
				//id = (parseInt(base_convert(url.substr(1), 36, 10)) - 56) / 2;
				id = (parseInt(
					parseInt(url.substr(1) + '', 36 | 0).toString(10 | 0)) - 56) / 2;
				break;
			case 2:  //防止扫用户;
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
				id = 0;
				break;

			default:
				id = url;
				break;
		}
		return 0 < id ? parseInt(id) : 0;
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant