Make functions generic so it can cast to opaque (branded) types (#521)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Make functions generic so it can cast to opaque types * Add TypeScript section translations to ru, zh-CN and id-ID
authored by Sasha Koss · committed by GitHub · parent 89f0ab4 · Browse files · 5 files·+100 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+100 added−5 removed
- other:+100 added−5 removed
Provenance
- Commit
ea64dc7- Tree
c645fc1- Author
Sasha Koss <koss@nocorp.me>- Committer
GitHub <noreply@github.com>- Parent
89f0ab4
5 files changed+100 added−5 removed
modified:index.d.ts+8 added-5 removed
8 unmodified lines910111213141515161718192021222324252627282930313233343335363736383940414216 unmodified lines59606162636465636667686967707172737420 unmodified lines8 unmodified lines * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL" * ``` * * @param size Size of the ID. The default size is 21. * @typeparam Type The type of the generated ID. * @returns A random string. */export function nanoid(size?: number): stringexport function nanoid<Type extends string>(size?: number): Type
/** * Generate secure unique ID with custom alphabet. * * Alphabet must contain 256 symbols or less. Otherwise, the generator * will not be secure. * * @param alphabet Alphabet used to generate the ID. * @param defaultSize Size of the ID. The default size is 21. * @typeparam Type The type of the generated ID. * @returns A random string generator. * * ```js * const { customAlphabet } = require('nanoid') * const nanoid = customAlphabet('0123456789абвгдеё', 5) * nanoid() //=> "8ё56а" * ``` */export function customAlphabet(export function customAlphabet<Type extends string>( alphabet: string, defaultSize?: number): (size?: number) => string): (size?: number) => Type
/** * Generate unique ID with custom random generator and alphabet. *16 unmodified lines * * @param alphabet Alphabet used to generate a random string. * @param size Size of the random string. * @param random A random bytes generator. * @typeparam Type The type of the generated ID. * @returns A random string generator. */export function customRandom(export function customRandom<Type extends string>( alphabet: string, size: number, random: (bytes: number) => Uint8Array): () => string): () => Type
/** * URL safe symbols. *20 unmodified linesmodified:README.id-ID.md+23 added
300 unmodified lines301 ```302 303 Bila ingin mengganti alfabet atau ukuran ID, dapat menggunakan [`nanoid-cli`](https://github.com/twhitbeck/nanoid-cli).304 305+### TypeScript306 307+Nano ID memungkinkan untuk mengubah string yang dihasilkan menjadi string opak308+dalam TypeScript. Sebagai contoh:309+310+```ts311+type UserId = string & { [userIdBrand]: true }312+declare const userIdBrand: unique symbol313+314+interface User {315+ id: UserId316+ name: string317+}318+319+const user: User = {320+ // Secara otomatis diubah menjadi UserId:321+ id: nanoid(),322+ name: 'Alice'323+}324+325+// Gunakan parameter tipe secara eksplisit:326+mockUser(nanoid<UserId>())327+```328+329 ### Bahasa Pemrograman Lainnya330 331 Nano ID telah bermigrasi ke berbagai macam bahasa. Seluruh versi dapat digunakan untuk mendapatkan ID generator yang sama pada sisi klien dan sisi penyedia layanan (_client-side_ dan _server-side_).332 39 unmodified linesmodified:README.md+23 added
414 unmodified lines415 $ npx nanoid --alphabet abc --size 15416 bccbcabaabaccab417 ```418 419+### TypeScript420 421+Nano ID allows casting generated strings into opaque strings in TypeScript.422+For example:423+424+```ts425+type UserId = string & { [userIdBrand]: true }426+declare const userIdBrand: unique symbol427+428+interface User {429+ id: UserId430+ name: string431+}432+433+const user: User = {434+ // Automatically casts to UserId:435+ id: nanoid(),436+ name: 'Alice'437+}438+439+// Use explicit type parameter:440+mockUser(nanoid<UserId>())441+```442+443 ### Other Programming Languages444 445 Nano ID was ported to many languages. You can use these ports to have446 the same ID generator on the client and server side.51 unmodified linesmodified:README.ru.md+24 added
394 unmodified lines395 $ npx nanoid --alphabet abc --size 15396 bccbcabaabaccab397 ```398 399+### TypeScript400+401+Nano ID позволяет приводить сгенерированные строки к непрозрачным строкам в402+TypeScript. Например:403+404+```ts405+type UserId = string & { [userIdBrand]: true }406+declare const userIdBrand: unique symbol407+408+interface User {409+ id: UserId410+ name: string411+}412+413+const user: User = {414+ // Автоматически приводится к типу UserId:415+ id: nanoid(),416+ name: 'Alice'417+}418+419+// Используйте явный параметр типа:420+mockUser(nanoid<UserId>())421+```422+423 ### Другие языки программирования424 425 Nano ID был портирован на множество языков. Это полезно, чтобы сервер и клиент426 генерировали ID по одной схеме.48 unmodified linesmodified:README.zh-CN.md+22 added
368 unmodified lines369 $ npx nanoid --alphabet abc --size 15370 bccbcabaabaccab371 ```372 373+### TypeScript374 375+Nano ID 允许将生成的字符串转换为 TypeScript 中的不透明字符串。 例如:376+377+```ts378+type UserId = string & { [userIdBrand]: true }379+declare const userIdBrand: unique symbol380+381+interface User {382+ id: UserId383+ name: string384+}385+386+const user: User = {387+ // 自动转换为 UserId:388+ id: nanoid(),389+ name: 'Alice'390+}391+392+// 使用显式类型参数:393+mockUser(nanoid<UserId>())394+```395+396 ### 其他编程语言397 398 Nano ID 已被移植到许多语言。 你可以使用下面这些移植,获取在客户端和服务器端相同的ID生成器。399 47 unmodified lines