diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index a224d2e..2eb35c3 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -3,7 +3,7 @@ on: push: branches: - master - tags: '*' + tags: ['*'] pull_request: concurrency: # Skip intermediate builds: always. @@ -38,22 +38,3 @@ jobs: - uses: codecov/codecov-action@v2 with: files: lcov.info - docs: - name: Documentation - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@v1 - with: - version: '1' - - uses: julia-actions/julia-buildpkg@v1 - - uses: julia-actions/julia-docdeploy@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} - - run: | - julia --project=docs -e ' - using Documenter: DocMeta, doctest - using Nanoid - DocMeta.setdocmeta!(Nanoid, :DocTestSetup, :(using Nanoid); recursive=true) - doctest(Nanoid)' diff --git a/.github/workflows/CompatHelper.yml b/.github/workflows/CompatHelper.yml deleted file mode 100644 index cba9134..0000000 --- a/.github/workflows/CompatHelper.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: CompatHelper -on: - schedule: - - cron: 0 0 * * * - workflow_dispatch: -jobs: - CompatHelper: - runs-on: ubuntu-latest - steps: - - name: Pkg.add("CompatHelper") - run: julia -e 'using Pkg; Pkg.add("CompatHelper")' - - name: CompatHelper.main() - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }} - run: julia -e 'using CompatHelper; CompatHelper.main()' diff --git a/Project.toml b/Project.toml index e8cb816..b8a138a 100644 --- a/Project.toml +++ b/Project.toml @@ -3,6 +3,9 @@ uuid = "649b5ee6-231f-4350-86a5-2b05c0e3780e" authors = ["Reinaldy Rafli and contributors"] version = "0.1.0" +[deps] +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" + [compat] julia = "1.5" diff --git a/README.md b/README.md index 0f482ba..be34ed6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,34 @@ # Nanoid -[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://Reinaldy Rafli.github.io/Nanoid.jl/stable) -[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://Reinaldy Rafli.github.io/Nanoid.jl/dev) -[![Build Status](https://github.com/Reinaldy Rafli/Nanoid.jl/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/Reinaldy Rafli/Nanoid.jl/actions/workflows/CI.yml?query=branch%3Amaster) -[![Coverage](https://codecov.io/gh/Reinaldy Rafli/Nanoid.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/Reinaldy Rafli/Nanoid.jl) +[![Build Status](https://github.com/aldy505/Nanoid.jl/actions/workflows/CI.yml/badge.svg?branch=master)](https://github.com/aldy505/Nanoid.jl/actions/workflows/CI.yml?query=branch%3Amaster) +[![Coverage](https://codecov.io/gh/aldy505/Nanoid.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/aldy505/Nanoid.jl) + +Simple and minimalist Julia implementation of [nanoid](https://github.com/ai/nanoid). + +## Install + +```julia +Pkg.add("Nanoid") +``` + +## Usage + +```julia +import Nanoid + +id = Nanoid.nanoid() +# => "tcDIFxMJHw6UgUnbI_6za" + +id = Nanoid.nanoid(10) +# => "OutzQWa1SB" + +id = Nanoid.nanoid(; alphabet = "ABCDEFGHIJ") +# => "GEHCAGFJIAHCJGIHHIHFF" + +id = Nanoid.nanoid(10; alphabet = "abcdef") +# => "abeafebaab" +``` + +## License + +[MIT](./LICENSE) \ No newline at end of file diff --git a/docs/Manifest.toml b/docs/Manifest.toml deleted file mode 100644 index 4bf72a1..0000000 --- a/docs/Manifest.toml +++ /dev/null @@ -1,100 +0,0 @@ -# This file is machine-generated - editing it directly is not advised - -julia_version = "1.7.2" -manifest_format = "2.0" - -[[deps.ANSIColoredPrinters]] -git-tree-sha1 = "574baf8110975760d391c710b6341da1afa48d8c" -uuid = "a4c015fc-c6ff-483c-b24f-f7ea428134e9" -version = "0.0.1" - -[[deps.Base64]] -uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" - -[[deps.Dates]] -deps = ["Printf"] -uuid = "ade2ca70-3891-5945-98fb-dc099432e06a" - -[[deps.DocStringExtensions]] -deps = ["LibGit2"] -git-tree-sha1 = "b19534d1895d702889b219c382a6e18010797f0b" -uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" -version = "0.8.6" - -[[deps.Documenter]] -deps = ["ANSIColoredPrinters", "Base64", "Dates", "DocStringExtensions", "IOCapture", "InteractiveUtils", "JSON", "LibGit2", "Logging", "Markdown", "REPL", "Test", "Unicode"] -git-tree-sha1 = "7d9a46421aef53cbd6b8ecc40c3dcbacbceaf40e" -uuid = "e30172f5-a6a5-5a46-863b-614d45cd2de4" -version = "0.27.15" - -[[deps.IOCapture]] -deps = ["Logging", "Random"] -git-tree-sha1 = "f7be53659ab06ddc986428d3a9dcc95f6fa6705a" -uuid = "b5f81e59-6552-4d32-b1f0-c071b021bf89" -version = "0.2.2" - -[[deps.InteractiveUtils]] -deps = ["Markdown"] -uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" - -[[deps.JSON]] -deps = ["Dates", "Mmap", "Parsers", "Unicode"] -git-tree-sha1 = "3c837543ddb02250ef42f4738347454f95079d4e" -uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" -version = "0.21.3" - -[[deps.LibGit2]] -deps = ["Base64", "NetworkOptions", "Printf", "SHA"] -uuid = "76f85450-5226-5b5a-8eaa-529ad045b433" - -[[deps.Logging]] -uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" - -[[deps.Markdown]] -deps = ["Base64"] -uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" - -[[deps.Mmap]] -uuid = "a63ad114-7e13-5084-954f-fe012c677804" - -[[deps.Nanoid]] -path = ".." -uuid = "649b5ee6-231f-4350-86a5-2b05c0e3780e" -version = "0.1.0" - -[[deps.NetworkOptions]] -uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908" - -[[deps.Parsers]] -deps = ["Dates"] -git-tree-sha1 = "85b5da0fa43588c75bb1ff986493443f821c70b7" -uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0" -version = "2.2.3" - -[[deps.Printf]] -deps = ["Unicode"] -uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" - -[[deps.REPL]] -deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"] -uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" - -[[deps.Random]] -deps = ["SHA", "Serialization"] -uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" - -[[deps.SHA]] -uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce" - -[[deps.Serialization]] -uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b" - -[[deps.Sockets]] -uuid = "6462fe0b-24de-5631-8697-dd941f90decc" - -[[deps.Test]] -deps = ["InteractiveUtils", "Logging", "Random", "Serialization"] -uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" - -[[deps.Unicode]] -uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5" diff --git a/docs/Project.toml b/docs/Project.toml deleted file mode 100644 index d1b5217..0000000 --- a/docs/Project.toml +++ /dev/null @@ -1,3 +0,0 @@ -[deps] -Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" -Nanoid = "649b5ee6-231f-4350-86a5-2b05c0e3780e" diff --git a/docs/make.jl b/docs/make.jl deleted file mode 100644 index 95f613e..0000000 --- a/docs/make.jl +++ /dev/null @@ -1,24 +0,0 @@ -using Nanoid -using Documenter - -DocMeta.setdocmeta!(Nanoid, :DocTestSetup, :(using Nanoid); recursive=true) - -makedocs(; - modules=[Nanoid], - authors="Reinaldy Rafli and contributors", - repo="https://github.com/Reinaldy Rafli/Nanoid.jl/blob/{commit}{path}#{line}", - sitename="Nanoid.jl", - format=Documenter.HTML(; - prettyurls=get(ENV, "CI", "false") == "true", - canonical="https://Reinaldy Rafli.github.io/Nanoid.jl", - assets=String[], - ), - pages=[ - "Home" => "index.md", - ], -) - -deploydocs(; - repo="github.com/Reinaldy Rafli/Nanoid.jl", - devbranch="master", -) diff --git a/docs/src/index.md b/docs/src/index.md deleted file mode 100644 index bdd0f67..0000000 --- a/docs/src/index.md +++ /dev/null @@ -1,14 +0,0 @@ -```@meta -CurrentModule = Nanoid -``` - -# Nanoid - -Documentation for [Nanoid](https://github.com/Reinaldy Rafli/Nanoid.jl). - -```@index -``` - -```@autodocs -Modules = [Nanoid] -``` diff --git a/src/Nanoid.jl b/src/Nanoid.jl index 98caca8..edca69c 100644 --- a/src/Nanoid.jl +++ b/src/Nanoid.jl @@ -1,5 +1,61 @@ module Nanoid +using Random + # Write your package code here. +const urlalphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict" + +rng = MersenneTwister() + +function randomcharacter(from::String)::Char + return rand(rng, from) +end + +function generate(alphabet::String, length::Integer)::String + id = [] + + for _ = 1:length + randomchar = string(randomcharacter(alphabet)) + append!(id, randomchar) + end + + join(id, "") +end + +""" + nanoid(size::Integer = 21; alphabet::String = "") -> String + +Generate a random Nanoid with the given size and alphabet. +Size defaults to 21, and alphabet defaults to the URL-safe alphabet. + +## Examples + +```jldoctest +julia> nanoid() +"tcDIFxMJHw6UgUnbI_6za" + +julia> nanoid(10) +"OutzQWa1SB" + +julia> nanoid(; alphabet = "ABCDEFGHIJ") +"GEHCAGFJIAHCJGIHHIHFF" + +julia> nanoid(10; alphabet = "abcdef") +"abeafebaab" +``` +""" +function nanoid(size::Integer=21; alphabet::String = "")::String + if size < 1 + error("size must be at least 1") + end + + if alphabet == "" + alphabet = urlalphabet + end + + generate(alphabet, size) +end + +export nanoid end diff --git a/test/runtests.jl b/test/runtests.jl index c970f5f..79778f2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2,5 +2,25 @@ using Nanoid using Test @testset "Nanoid.jl" begin - # Write your tests here. + @testset "Randomness" begin + @test nanoid() != nanoid() + end + @testset "Length" begin + @test length(nanoid()) == 21 + @test length(nanoid(1)) == 1 + @test length(nanoid(255)) == 255 + @test length(nanoid(25)) == 25 + end + + @testset "Errors" begin + @test_throws ErrorException("size must be at least 1") nanoid(-1) + end + + @testset "Custom alphabet" begin + output = nanoid(;alphabet = "ABCDE") + + for chars in output + @test chars in "ABCDE" + end + end end