Coder Social home page Coder Social logo

Option to change defaults? about nanoid HOT 7 CLOSED

smeijer avatar smeijer commented on September 27, 2024
Option to change defaults?

from nanoid.

Comments (7)

ai avatar ai commented on September 27, 2024 1

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'

from nanoid.

smeijer avatar smeijer commented on September 27, 2024

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.

from nanoid.

ai avatar ai commented on September 27, 2024

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.

from nanoid.

smeijer avatar smeijer commented on September 27, 2024

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?

from nanoid.

ai avatar ai commented on September 27, 2024

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)

from nanoid.

ai avatar ai commented on September 27, 2024

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

from nanoid.

CyberAP avatar CyberAP commented on September 27, 2024

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

from nanoid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.