Code Monkey home page Code Monkey logo

enum's Introduction

Tests GitHub release MIT license codecov lint workflow help wanted

Enum Package

Some databases do not have enums and we ofter just store strings. This package provides functionality to encode and decode strings into Enum values. This package is designed for situations where you need to represent strings as unique integer values, often used for enumeration-like functionality. It goes with improved performance and memory usage.

This package has zero dependencies.

Installation

To install the enum package, simply run:

go get github.com/kaatinga/enum

Usage

Here's a quick guide on how to use the package:

Import the Package

import "github.com/kaatinga/enum"

Encoding a String

To encode a string into an Enum value, use the Encode function. It will convert a string of characters into an integer value:

s := "Hello"
encoded, err := enum.Encode(s)
if err != nil {
    fmt.Println("Error:", err)
} else {
    fmt.Println("Encoded:", encoded)
}

Decoding an Enum

To decode an Enum back into its original string representation, you can use the String method of the Enum type:

s := "Hello" // Replace with your actual encoded value
encoded, err := enum.Encode(s)
decoded := encoded.String()
fmt.Println("Decoded:", decoded)

Supported Characters

The package supports encoding and decoding using the following characters:

  • Digits: 0-9
  • Uppercase Letters: A-Z
  • Lowercase Letters: a-z
  • Underscore: _
  • Space: (whitespace)

String Length Limitation

The package enforces a maximum string length for encoding, supporting strings with lengths between 1 and 10 characters. If the input string length does not meet this criterion, the errInvalidLength error is returned.

Error Handling

In cases where an error occurs during encoding, the Encode function returns an error, allowing you to handle it gracefully.

License

This package is provided under the MIT License. Feel free to use and modify it as needed.

Contribution

Contributions to the package are welcome. If you encounter any issues or have ideas for improvements, please open an issue or a pull request on the GitHub repository.

enum's People

Contributors

kaatinga avatar

Watchers

 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.