Code Monkey home page Code Monkey logo

spotflake's Introduction

SpotFlake

Swift version snowflake (unique ID generator).

As an application developer, you create an instance of 'SpotFlake.Node' and then generate unique ID.

Swift version

The latest version of Health works with the 4.2 version of the Swift binaries. You can download this version of the Swift binaries by following this link.

Usage

To leverage the SpotFlake package in your Swift application, you should specify a dependency for it in your Package.swift file:

import PackageDescription

let package = Package(name: "MyAwesomeSwiftProject",
     ...
    dependencies: [
      .package(url: "https://github.com/shawnclovie/SpotFlake.git", .from: "0.1.0"),
    ],

And this is how you create a SpotFlake.Node instance and generate ID:

import SpotFlake

...

// the index would to mark ID generated source, e.g. server ID or process ID
let nodeIndex: Int64 = 1
// the index should between 0...(-1 ^ (-1 << SpotFlake.nodeBits))
let node = SpotFlake.Node(node: nodeIndex)!

...

// Generate new ID
let id = node.generate()
// Get date from the ID if needed
node.time(of: id) // ID generating time
node.node(of: id) // ID generator's node index

Epoch

SpotFlake use epoch as genrating base, also you can change the base epoch:

SpotFlake.epoch = 1514764800000

To calculate the epoch:

import Foundation

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd ZZZ"
print("epoch of custom date:", SpotFlake.Time(formatter.date(from: "2018-01-01 UTC")!).flakeTime)

ID

The type of ID is Int64.

If you want to convert an ID to JSON, it should convert and pass as string, since JavaScript would loss precision for big integer.

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.