Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to import this package? #2

Closed
Usama-Tahir opened this issue Oct 21, 2019 · 4 comments
Closed

how to import this package? #2

Usama-Tahir opened this issue Oct 21, 2019 · 4 comments

Comments

@Usama-Tahir
Copy link

Usama-Tahir commented Oct 21, 2019

right now, only
const randomToken = require('random-token');
syntax is supported I believe. But how can we use it in import like way?

@ashnur
Copy link
Owner

ashnur commented Oct 21, 2019

That's a very good question :)

ES modules (aka. export/import) is still only accessible under a --experimental-modules flag in nodejs. As you can see the repository is ~6 years old. At the time export/import was not something I had to consider seriously.

Even today, most people use babel to support export/import and a bundler to support require.

So to answer your question, it depends where you want to use it.

On the other hand, the whole module is quite a small code

  // return a number between 0 and max-1
    function r(max){ return Math.floor(Math.random()*max) }

    function generate(salt, size){
        var key = ''
        var sl = salt.length
        while ( size -- ) {
            var rnd = r(sl)
            key += salt[rnd]
        }
        return key
    }

    var rndtok = function(salt, size){
        return isNaN(size) ? undefined :
               size < 1    ? undefined : generate(salt, size)

    }

    rndtok.gen = createGenerator

    function createGenerator(salt){
        salt = typeof salt  == 'string' && salt.length > 0 ? salt :  'abcdefghijklmnopqrstuvwxzy0123456789'
        var temp = rndtok.bind(rndtok, salt)
        temp.salt = function(){ return salt }
        temp.create = createGenerator
        temp.gen = createGenerator
        return temp
    }

    module.exports = createGenerator()

You can copy just that and then no need to import.

@Usama-Tahir
Copy link
Author

thanks. I will use this code for now and will try to make a PR supporting import syntax.

@Usama-Tahir
Copy link
Author

import * as randomToken from 'random-token' also works in typescript

@ashnur
Copy link
Owner

ashnur commented Oct 22, 2019

Thanks, good to know.

@ashnur ashnur closed this as completed Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants