Giter Site home page Giter Site logo

kulid's Introduction



ulid


KUlid

Unit test coverage

Support me

ULID (Universally Unique Lexicographically Sortable Identifier) generator and parser for Kotlin.

Refer the ULID spec for a more detailed ULID specification.

Installation

Kotlin DSL

Add the JitPack repository to your build.gradle.kts:

allprojects {
   repositories {
      ...
      maven { url = uri("https://jitpack.io") }
   }
}

Add the dependency to your build.gradle.kts:

dependencies {
    implementation("com.github.guepardoapps:kulid:2.0.0.0")
}

Groovy DSL

Add the JitPack repository to your build.gradle:

allprojects {
   repositories {
      ...
      maven { url 'https://jitpack.io' }
   }
}

Add the dependency to your build.gradle:

dependencies {
    implementation 'com.github.guepardoapps:kulid:2.0.0.0'
}

Usage

ULID generation examples:

val randomUlid = ULID.random()
val generateUlid = ULID.generate(System.currentTimeMillis(), byteArrayOf(0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9))
val stringUlid = ULID.fromString("003JZ9J6G80123456789ABCDEF")

ULID parsing examples:

val ulid = "003JZ9J6G80123456789ABCDEF"
val isValid = ULID.isValid(ulid)        // returns a Boolean indicating if the ULID is valid
val timestamp = ULID.getTimestamp(ulid) // returns a Long
val entropy = ULID.getEntropy(ulid)     // returns a ByteArray

Prior Projects

Requirements

  • Use at least JVM 1.8

Contributors

JonasSchubert
Jonas Schubert

License

KUlid is distributed under the MIT license. See LICENSE for details.

MIT License

Copyright (c) 2019 - 2021 Jonas Schubert

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

kulid's People

Contributors

jonasschubert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

kulid's Issues

Is there any plan to support monotonically increasing randomness?

There are an issue when i generate ULID using random function continuously. it does not guarantee sequence.

val ulids = listOf(ULID.random(), ULID.random(), ULID.random(), ULID.random())
val sortedUlid = ulids.sorted()

println("ORIGIN : " + ulids)
println("SORTED : " + sortedUlid)
println("COMPARE : " + (ulids == sortedUlid))
ORIGIN : [01FMYXM17KAV8C0P3HQ5Z0034N, 01FMYXM17KM2D00727YE0Q1ZK9, 01FMYXM17K45BJ1RR9CMPN0HDV, 01FMYXM17K8ZMZ0NVY0HSP1GQ9]
SORTED : [01FMYXM17K45BJ1RR9CMPN0HDV, 01FMYXM17K8ZMZ0NVY0HSP1GQ9, 01FMYXM17KAV8C0P3HQ5Z0034N, 01FMYXM17KM2D00727YE0Q1ZK9]
COMPARE : false

So, Is there any plan to support high-resolution time? like ulid-py?

Generates only two-three unique ULIDs in a same millisecond

When generating ULIDs for a list of items of more than 2-3, it generates same ULIDs. Sometimes, it generates only two unique IDs, sometimes three, others are just copy of the last unique ID.

I use ULID to name files before uploading them on server. For example - if I'm uploading 10 items, it names 7-8 of them same which results in no error but only 2-3 files get uploaded as same name file overwrites other.

I've gone through the Wiki of ULID/spec but nothing is mentioned there to overcome this problem. And I can't find anything related to MonotoniFactory in this lib's methods.

Here's how I use it:

private fun generateFileName(): String {
return ULID.generate(
     System.currentTimeMillis(),
     byteArrayOf(0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9)
    ) + ".png"
}

Check the below image : Out of 14, only two are unique. 01EBTT93BM... and 01EBTT93BN...
image

Any workaround to fix it?

Edit: The workaround I've implemented it to pass the list index as well to the method generateFileName() and adding it to the generated Id as + "-${index+1}.png".

Convert to multiplatform

I tried the conversion (in a not too clean way :| ) and it seems to work, the unit tests are successful for JVM and JS.

Discontinued

Hi everyone,
as most of you might have figured out by now, I do not maintain this project anymore and will not in the future.
Please fork it and continue the work with the bugfixes there,
I will archive this project.

Entropy encoding is broken

Hello,

the entropy encoding to Base32 is broken. Consider the following example:

val testUlid = "01EA9F3000ZZZZZZZZZZZZZZZZ"
println(testUlid)
println(ULID.generate(ULID.getTimestamp(testUlid), ULID.getEntropy(testUlid)))

The output is:

01EA9F3000ZZZZZZZZZZZZZZZZ
01EA9F3000ZZZZ1ZZZZZZZ1ZZZ

It seems #8 aims to fix that. I did not try out the fix from the PR, but a short comparison with the python implementation looks promising.

Please fix :)

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.