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

Option to change defaults? #99

Closed
smeijer opened this issue Nov 3, 2018 · 7 comments
Closed

Option to change defaults? #99

smeijer opened this issue Nov 3, 2018 · 7 comments

Comments

@smeijer
Copy link

smeijer commented Nov 3, 2018

I know there is a generate method, to change the alphabet and length of the id that will be generated.

The case is; I need to migrate my id generation away from Meteor/Random, while I also don't want my id's to start look different.

I can achieve this with:

const generate = require('nanoid/generate');
const alphabet = '23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz';
const length = 17;

generate(alphabet, length); // D8taKDubtMzi58jZn

But, that means that I need to specify this alphabet and length on every place where I need an ID.

It would be nice if there was a method to change the defaults so that the next require('nanoid')() uses custom settings.

// main.js
const nano = require('nanoid');
nano.config({
  alphabet: '23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz',
  length: 17,
});

// some-other-file.js
const nano = require('nanoid');
nano(); // Kg6gwCgG44zEK8u6g
@ai
Copy link
Owner

ai commented Nov 3, 2018

I understand you. But we can't suggest global settings API. What if your dependency use Nano ID too and new settings will create conflict there?

But there is more simple solution:

// random.js
import nanoid from 'nanoid'

module exports function random () {
  return nanoid(alphabet, size)
}

// main.js
import random from './random'

@smeijer
Copy link
Author

smeijer commented Nov 3, 2018

I do understand that work around, but I don't like that I need to have relative imports throughout my code.

The suggestion to set defaults was what I really was looking for.

I do understand that you're hesitant to implement this.

But I also think that library authors should be aware that they shouldn't change the defaults, and that anyone changing the defaults, should be aware that it can have side effects.

@ai
Copy link
Owner

ai commented Nov 3, 2018

anyone changing the defaults, should be aware that it can have side effects.

I like how optimistic you are about users 😄. Unfortunately, I know by my practice with shortid, that it doesn't work.

Developers do not read docs. Developers prefer to save few bytes instead of protecting system from long term problems.

How even you can be sure, that your dependency well not change defaults and broke your code? 😏

I don't like that I need to have relative imports throughout my code.

Sure. Can you tell me what alphabet, what size and why do you use them? Maybe I can find better solution.

@smeijer
Copy link
Author

smeijer commented Nov 3, 2018

Developers should be the better kind of users 😇 But unfortunately, I must agree. Point taken.

Sure; all info is in the opening post; but here again:

const ALPHABET = '23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz';
const LENGTH = 17;

Why? So that I can replace meteor/random with nanoid, without changing the way generated ids look:

import { Random } from 'meteor/random';
Random.id();

UNMISTAKABLE_CHARS

Random.id()

A little more on the "why": Meteor style import definitions suck, as it's not easy testable. I try to lessen my dependencies on the framework, and move more to Meteor as a build tool. Thereby, depending on Random is a no go.

In the long term, when I would leave Meteor entirely, I still want to have the database ids to look the same. Currently Random.id() (meteor) is responsible for id generation. This would become nanoid if it was possible.

So perhaps a nanoid/unmistakable or nanoid/readable or nanoid/meteor-compat?

@ai
Copy link
Owner

ai commented Nov 4, 2018

I have a better plan. Send this alphabet to nanoid-dictionary as unmistakable alphabet. It has no 1, l, 0 since they could be misread as different symbols.

Then you can use it as it. Or ask nanoid-dictionary to add shortcuts:

import unmistakable from 'nanoid-dictionary/unmistakable'

unmistakable(17)

@ai
Copy link
Owner

ai commented Nov 6, 2018

I added issues there:
CyberAP/nanoid-dictionary#1
CyberAP/nanoid-dictionary#2

@CyberAP
Copy link
Contributor

CyberAP commented Dec 22, 2018

Hope that solves your issue: https://github.com/CyberAP/nanoid-generate#nolookalikes

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

3 participants