Git Product home page Git Product logo

Comments (1)

shiiiiiiji avatar shiiiiiiji commented on June 2, 2024
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;
	}
}

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.