Giter Club home page Giter Club logo

nanoid-swift's Introduction

SwiftPM compatible Cocoapods compatible GitHub release Swift 5.1 platforms Build Status

Nano ID

Another Swift implementation of Nano ID. This borrows heavily from https://github.com/antiflasher/NanoID, which seems to have been abandoned.

Usage

// Create an ID generator using the default alphabet, size, and randomizer
let id = ID()

// Generate a 21 character id (21 is the default size)
id.generate() 

// Generate a 10 character id
id.generate(size: 10) 

// Generate a 5 character id containing only numeric characters
id.generate(alphabet: .numeric, size: 5) 

// Generate a 5 character id containing only symbols
id.generate(alphabet: .symbols, size: 5)

// Generate a secure 10 character id
id.generate(size: 5, randomizer: ID.Randomizers.secure)

Randomizers

There are two implementations provided: Secure and ARC4Random (aliased as Insecure). The Secure implementation uses CryptoKit to for randomization. If those don't satisfy your requirements, you can provide your own implementation of RandomizerProtocol.

Alphabets

There are several alphabets included: numeric, alphaLower, alphaUpper, alphaNumeric, symbols, urlSafe, and full. You can easily create others, which only requires initializing the Alphabet struct with one or more strings.

Example custom alphabet

extension ID.Alphabet {
  public static var abc123 = Self(.abc123)
}
extension String {
  internal static var abc123 = "abc123" 
}

nanoid-swift's People

Contributors

gcox avatar

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.