Swift 中的 cuid2

js 中的原始实现:https://github.com/paralleldrive/cuid2

cuid2 是一种可能的全局唯一标识符替代方案,例如 UUID 等。 这种 ID 的长度是可配置的,并且可以更容易地手动复制粘贴。

这是一个 cuid2 的示例,具有其默认长度:lnapog2xtrbfatrw5qtrpvs6

要了解更多信息,请访问 javascript 中的参考实现链接。

安装

请将此依赖项添加到您的 Package.swift 中以使用此库

.package(url: "https://github.com/RussBaz/cuid2.git", from: "0.0.1"),

然后将依赖项添加到目标中

.product(name: "cuid2", package: "cuid2"),

如何使用

作为库

import cuid2

let id = createId() // quickest way to create a one off id
isCuid2(id: id) // to verify if the string can possibly be cuid2

// Alternatively, you can customise how the ids a generated
// by providing a custom session counter, cuid length and a custom fingerprint
func createId(counter: Cuid2SessionCounter? = nil, length: Int? = nil, fingerprint: String? = nil) -> String { }

// If you need to create many ids
// 1. please create an instance of the generator
struct Cuid2Generator {
    // other code omitted

    init(counter: Cuid2SessionCounter? = nil, length: Int? = nil, fingerprint: String? = nil) { }

    // 2. and then use this method to create an array of ids
    func generate(times: Int) -> [String] { }

    // skipping more code
}

作为 CLI 工具

克隆存储库,从项目根目录使用 swift build -c release 构建发布版本。 cuid2cli 现在可以在 .build/<target-arch-platform>/release/ 文件夹中使用。 将其移动或复制到目标位置。

USAGE: tool [--count <count>] [--include-line-number] [--length <length>] [--fingerprint <fingerprint>]

OPTIONS:
  -c, --count <count>     The number of times to repeat 'cuid2'. (default: 1)
  -i, --include-line-number
                          Include a line number with each repetition.
  -l, --length <length>   The 'cuid2' length in characters.
  -f, --fingerprint <fingerprint>
                          The custom fingerprint.
  -h, --help              Show help information.