spa5k/uids-postgres

Question: Custom alphabet for nanoid

anli-xsigns opened this issue ยท 9 comments

Hello,
thank you for this great work. I'm interested to use this to generate nanoids. Unfortunately I need a custom alphabet for nanoid as explained in https://github.com/ai/nanoid#custom-alphabet-or-size. How can I achieve this?
Thanks a lot for a short answer in advance! Have a great day!

spa5k commented

It won't be just available, i will need to add this functionality, wait for an update

spa5k commented

Check now, I've added the function - generate_nanoid_c(), more in readme.

Wow, thanks a lot for this lightning-fast update! It works like charm! The only backdraft is that I have to use the second parameter all the time where I use the function (obviously!). Is there a chance to make another function - I only need to get rid a of the default included _ and -, so I can use A-Za-z0-9 - but giving parameter ABCDEF....4567890 to achieve that seems a little bit exhausting :-)

spa5k commented

lol mate, wait a min then

spa5k commented

It has been added in the latest c, generate_nanoid_c, it only takes one argument, and that is of the alphabets.
remember to star

Perfect, thanks a lot ๐Ÿ‘๐Ÿฅณ

Sorry - I had a quick look at the source and didn't find it - I need something like:

    pub(crate) fn generate_nanoid_length(length: i32) -> String {
        let length_as_usize = length as usize;
        let alphabets_vec = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".chars().collect::<Vec<char>>();
        let id = nanoid!(length_as_usize , &alphabets_vec);
        id
    }

Sorry for pseudo-code - I don't know Rust yet :-(
So I can only specifiy the length and then generate an ID only consisting of A-Za-z0-9. Do you think this is possible? Thanks a lot for your help!

spa5k commented

That's too specifically of a function to be added. You can follow the nanoid functions that takes alphabets and the length in order to generate your custom configuration. If you don't like adding it every time, create a new sql functionality that takes length as argument and interact with my function.

Ok, I understand that ๐Ÿ‘