英文:
Best way to shorten a URL in javascript *without* a database
问题
I'm a Java dev that's new to Typescript/Javascript. I have a directive to make my app's URL line shorter by coding up some sort of hash. So instead of:
https://www.someurl.com?urgency=all&impact=widespread&contentType=mine&...
I need something like:
https://www.someurl.com?params=somethingShort
How is this problem typically solved for in Javascript? I don't have any storage to use, so this will have to be an in-memory solution.
When I look up "hash URL params", I get something about the "#" sign in the location, and I don't think that's my use-case... Can someone point me to a solution?
英文:
I'm a Java dev that's new to Typescript/Javascript. I have a directive to make my app's URL line shorter by coding up some sort of hash. So instead of:
https://www.someurl.com?urgency=all&impact=widespread&contentType=mine&...
I need something like:
https://www.someurl.com?params=somethingShort
How is this problem typically solved for in Javascript? I don't have any storage to use, so this will have to be an in-memory solution.
When I look up "hash URL params", I get something about the "#" sign in the location, and I don't think that's my use-case... Can someone point me to a solution??
答案1
得分: 2
I have a directive to make my app's URL line shorter by coding up some sort of hash. So instead of:
我收到了一个指令,要通过编写某种哈希来缩短我的应用程序的URL行。所以不是:
A hash is irreversible, so you cannot use a hash to shorten your URLs without resorting to storage.
哈希是不可逆的,因此您不能仅依靠哈希来缩短URL,除非使用存储。
How is this problem typically solved for in Javascript?
这个问题在JavaScript中通常是如何解决的?
I don't think this is a "typical" problem for JavaScript and I can tell you it has nothing to do with JavaScript itself.
我认为这不是JavaScript的“典型”问题,我可以告诉您它与JavaScript本身无关。
This is because you want to use compression, which is a language agnostic problem.
这是因为您想使用压缩,这是一种与编程语言无关的问题。
However, I don't think compression will do much for shortening the URLs (depending on the input characteristics of the URL, this might be a different story).
但是,我认为压缩对于缩短URL不会有太大帮助(取决于URL的输入特性,情况可能会有所不同)。
Instead of using compression, I highly suggest you use storage since then you'll be able to use hashes.
我强烈建议您使用存储,而不是使用压缩,因为这样您将能够使用哈希。
If you insist on using compression, search the internet for a suitable (depending on your needs) compression algorithm, then find an implementation of that algorithm in JavaScript (shouldn't be too hard for popular compression algorithms).
如果您坚持要使用压缩,请在互联网上搜索一个适合您需求的压缩算法,然后找到JavaScript中该算法的实现(对于流行的压缩算法来说,这应该不难)。
Last resort would be to develop your own compression algorithm, but since you're asking this pretty basic question here, I highly doubt you have the skill required to do that.
最后的选择是开发自己的压缩算法,但由于您在这里提出了这个相当基础的问题,我非常怀疑您具备开发所需技能。
英文:
> I have a directive to make my app's URL line shorter by coding up some sort of hash. So instead of:
A hash is irreversible, so you cannot use a hash to shorten your URLs without resorting to storage.
> How is this problem typically solved for in Javascript?
I don't think this is a "typical" problem for JavaScript and I can tell you it has nothing to do with JavaScript itself.
This is because you want to use compression, which is a language agnostic problem.
However, I don't think compression will do much for shortening the URLs (depending on the input characteristics of the URL, this might be a different story).
Instead of using compression, I highly suggest you use storage since then you'll be able to use hashes.
If you insist on using compression, search the internet for a suitable (depending on your needs) compression algorithm, then find an implementation of that algorithm in JavaScript (should't be too hard for popular compression algorithms).
Last resort would be to develop your own compression algorithm, but since you're asking this pretty basic question here, I highly doubt you have the skill required to do that.
答案2
得分: 2
典型的URL缩短服务使用数据库将短ID与实际URL配对。
没有数据库的情况下,您必须创建一个允许相同数据以更短形式存在的算法。如何做到这一点完全取决于您。
第一个想法是对参数进行gzip压缩,然后对二进制数据进行base64编码,以使其在URL中起作用。但这可能不会有帮助。
urgency=all&impact=widespread&contentType=mine
被gzip压缩,然后base64编码为
eNorLUpPzUuutE3MyVHLzC1ITC6xLc9MSS0uKEpNTFFLzs8rSc0rCaksSLXNzcxLBQCqJxIm
(见https://bugdays.com/gzip-base64)
其中几乎没有重复的内容,并且使用base64编码会添加额外的字节。
因此,这不是一个通用问题。这是一个特定问题,很可能会以适合您的应用程序的方式解决。
更好的选择是自己创建一个压缩编码。
例如,您可以定义一个压缩格式,其中对于要按特定顺序传递的每个值,都有一个单独的字母。
urgency=all&impact=widespread&contentType=image/jpg
可以编码为:
awj
其中:
- 位置
0
的a
扩展为urgency=all
- 位置
1
的w
扩展为impact=widespread
- 位置
2
的j
扩展为contentType=image/jpeg
然后,您可以为该格式编写一个encode()
/ decode()
函数,以处理您在应用程序中想要的真实数据。
您选择的格式取决于您的数据和正在编码的数据。这些参数是否都是“有限值列表中的一个”(即全部或无)?还是有数值参数?长度未知的自由文本字符串?
您可以尽量对您的数据进行假设,以使其更小。您只需提出适合您的数据的编码方案即可。
英文:
Typical URL shorteners use a database to pair a short ID with the real URL.
Without a database you have to create an algorithm that allows the same data to exist in a shorter form. How you do that is really up to you.
The first thought is to gzip the parameters, and then base64 encode that binary data to make it work in a URL. But this probably wont help.
urgency=all&impact=widespread&contentType=mine
Gets gzipped and then base64 encoded as
eNorLUpPzUuutE3MyVHLzC1ITC6xLc9MSS0uKEpNTFFLzs8rSc0rCaksSLXNzcxLBQCqJxIm
(see https://bugdays.com/gzip-base64)
There's just not a lot of repetition in that, and using base64 encoding adds extra bytes.
So this is not a general problem. It is a specific problem that will most likely be solved in a way that is specific to your application.
A better option is to create a compressed encoding yourself.
For example, you could define a compressed format that has a single letter for each value you want to pass in a specific order.
urgency=all&impact=widespread&contentType=image/jpg
Could encode as:
awj
Where:
a
at position0
expands tourgency=all
w
at position1
expands toimpact=widespread
j
at position2
expands tocontentType=image/jpeg
You then write an encode()
/ decode()
function for that format that works the real data you want in your application.
What format you choose is up to you and what data is being encoded. Are these all "one of a finite list of values" kind of parameters (i.e. all or none)? Or are there numeric parameters? Free text strings of unknown length?
The more you can make assumptions about your data, the more you can compress it down. You'll just have to come with an encoding scheme that works for what your data is.
答案3
得分: 0
可能会对“哈希”一词的含义产生混淆。哈希可以表示URL中的片段标识符,通常由#符号表示,如#fragmentName。
对于我的回答,我使用了JavaScript地图(map)来存储你所需的完整数据,但你也可以使用基本对象。
这将允许你获取名为keyID的URL参数,你可以随时更改它。URL的格式可能类似于:https://example.com/?keyID=test1
然后我们检查该键是否存在于我们的数据地图中,如果存在,则你可以使用该页面的数据。
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
//const keyID = urlParams.get('keyID'); //实际上使用这一行
const keyID = "test1"; //用于测试
let data = new Map();
data.set("test1",{ "urgency" : "all", "impact" : "widespread", "contentType":"mine"});
let pageData;
if(data.has(keyID)){
pageData = data.get(keyID);
}
console.log(pageData)
英文:
There could be confusion on what you mean by "hash". As hash could represent a fragment identifier in the URL, usually represented by the # like #fragmentName.
For my answer, I'm using a javascript map to store the full data that you would need, but you can use a basic object also.
This will allow you to get a URL parameter called keyID which you can always change. The URL format would be something like: https://example.com/?keyID=test1
Then we check for that key to exist in our data map and if it does, you have data to use for that page.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
//const keyID = urlParams.get('keyID'); //actually use this line
const keyID = "test1"; //for testing
let data = new Map();
data.set("test1",{"urgency" : "all","impact" : "widespread","contentType":"mine"});
let pageData;
if(data.has(keyID)){
pageData = data.get(keyID);
}
console.log(pageData)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论