Javascript Nodejs生成简短的唯一字母数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10985872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nodejs generate short unique alphanumeric
提问by teggy
I would like to generate a short unique alphanumeric value to be used as confirmation codes for online purchases. I've looking into https://github.com/broofa/node-uuidbut their uuids are too long and I want to have them be around 8 characters long. What is the best way I can achieve this?
我想生成一个简短的唯一字母数字值,用作在线购买的确认代码。我正在查看https://github.com/broofa/node-uuid但它们的 uuid 太长,我希望它们的长度约为 8 个字符。我可以实现这一目标的最佳方法是什么?
回答by James Moore
A little late on this one but it seems like hashids would work pretty well for this scenario.
这个有点晚了,但似乎hashids在这种情况下效果很好。
https://github.com/ivanakimov/hashids.node.js
https://github.com/ivanakimov/hashids.node.js
hashids (Hash ID's) creates short, unique, decryptable hashes from unsigned integers
hashids(哈希 ID)从无符号整数创建简短、唯一、可解密的哈希
var Hashids = require('hashids'),
hashids = new Hashids('this is my salt');
var hash = hashids.encrypt(12345);
// hash is now 'ryBo'
var numbers = hashids.decrypt('ryBo');
// numbers is now [ 12345 ]
If you want to target ~ 8 characters you could do, the following that calls for a minimum of 8 chars.
如果你想定位 ~ 8 个字符,你可以做,以下要求至少 8 个字符。
hashids = new Hashids("this is my salt", 8);
then this:
那么这个:
hash = hashids.encrypt(1);
// hash is now 'b9iLXiAa'
The accepted answer would be predictable/guessable, this solution should be unique and unpredictable.
接受的答案是可预测/可猜测的,此解决方案应该是唯一且不可预测的。
回答by Wes Johnson
10/23/15: See the hashids answer below, as well!
2015 年 10 月 23 日:也请参阅下面的 hashids 答案!
You can borrow from the URL shortener model and do something like this:
您可以借鉴 URL 缩短器模型并执行以下操作:
(100000000000).toString(36);
// produces 19xtf1ts
(200000000000).toString(36);
// produces 2jvmu3nk
Just increment the number to keep it unique:
只需增加数字以保持其唯一性:
function(uniqueIndex) {
return uniqueIndex.toString(36);
}
Notethat this is only really useful for "single instance" services that don't mind a certain amount of predictability in the way this is ordered (via basic increment). If you need a truly unique value across a number of application / DB instances, you should really consider a more full featured option per some of the comments.
请注意,这仅对“单实例”服务真正有用,这些服务不介意以排序方式(通过基本增量)具有一定的可预测性。如果您需要跨多个应用程序/数据库实例的真正独特的价值,您真的应该根据一些评论考虑使用更全功能的选项。
回答by Drag0
Install shortId module (https://www.npmjs.com/package/shortid). By default, shortid generates 7-14 url-friendly characters: A-Z, a-z, 0-9, _- but you can replace - and _ with some other characters if you like. Now you need to somehow stick this shortId to your objects when they're being saved in the database. Best way is to stick them in Schema like this:
安装 shortId 模块 ( https://www.npmjs.com/package/shortid)。默认情况下,shortid 生成 7-14 个对 url 友好的字符:AZ、az、0-9、_- 但如果您愿意,您可以将 - 和 _ 替换为其他一些字符。现在,您需要以某种方式将此 shortId 粘贴到您的对象中,以便将它们保存在数据库中。最好的方法是像这样将它们粘贴在 Schema 中:
var shortId = require('shortid');
var PurchaseConfirmationSchema = mongoose.Schema({
/* _id will be added automatically by mongoose */
shortId: {type: String, unique: true, default: shortId.generate}, /* WE SHOULD ADD THIS */
name: {type: String},
address: {type: String}
});
I already answered similiar question to myself over here:
我已经在这里回答了类似的问题:
回答by Andrey Hohutkin
For this purpose I wrote a module that can do that and ever more. Look at its page: id-shorter
为此,我编写了一个可以做到这一点甚至更多的模块。看它的页面:id-shorter
回答by Abdennour TOUMI
IF each online purchase associated to Unique URL , you can use the backend of simple-shortpackage,which don't require database connection, nor web services :
如果每个在线购买都关联到唯一 URL,您可以使用简单短包的后端,不需要数据库连接,也不需要 Web 服务:
var shorten=require('simple-short');
shorten.conf({length:8}); // Default is 4.
code1=shorten('http://store.com/purchase1');
code2=shorten('http://store.com/purchase2');
//.. so on
回答by Kristian Heryanto iandeeph
based on this https://www.npmjs.com/package/randomstring
基于这个https://www.npmjs.com/package/randomstring
var randomstring = require("randomstring");
randomstring.generate();
// >> "XwPp9xazJ0ku5CZnlmgAx2Dld8SHkAeT"
randomstring.generate(7);
// >> "xqm5wXX"
randomstring.generate({
length: 12,
charset: 'alphabetic'
});
// >> "AqoTIzKurxJi"
randomstring.generate({
charset: 'abc'
});
// >> "accbaabbbbcccbccccaacacbbcbbcbbc"
you can do this
你可以这样做
randomstring.generate({
length: 8,
charset: 'alphabetic'
});