Nodejs generate short unique alphanumeric

前端 未结 6 642
借酒劲吻你
借酒劲吻你 2021-01-30 23:55

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-uuid but the

相关标签:
6条回答
  • 2021-01-31 00:27

    A little late on this one but it seems like hashids would work pretty well for this scenario.

    https://github.com/ivanakimov/hashids.node.js

    hashids (Hash ID's) creates short, unique, decryptable hashes from unsigned integers

    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.

    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.

    0 讨论(0)
  • 2021-01-31 00:36

    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:

    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:

    Shorten ObjectId in node.js and mongoose

    0 讨论(0)
  • 2021-01-31 00:39

    For this purpose I wrote a module that can do that and ever more. Look at its page: id-shorter

    0 讨论(0)
  • IF each online purchase associated to Unique URL , you can use the backend of simple-short package,which don't require database connection, nor web services :

    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 
    
    0 讨论(0)
  • 2021-01-31 00:46

    based on this 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'
    });
    
    0 讨论(0)
  • 2021-01-31 00:49

    10/23/15: See the hashids answer below, as well!

    You can borrow from the URL shortener model and do something like this:

    (100000000000).toString(36);
    // produces 19xtf1ts
    
    (200000000000).toString(36);
    // produces 2jvmu3nk
    

    Just increment the number to keep it unique:

    function(uniqueIndex) {
        return uniqueIndex.toString(36);
    }
    

    Note that 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.

    0 讨论(0)
提交回复
热议问题