GithubHelp home page GithubHelp logo

emoji-sanitizer's Introduction

Go Emoji Sanitizer

tests Coverage Status GitHub Go Report Card

go library to detect unicode emoji runes and to sanitize them.

Usage

The usage is fairly simple. You generate a sanitizer and can sanitize passed strings.
A minimalistic usage:

sanitizer, err := NewSanitizer()
// this will return "Test string [e][e][e] [e]"
sanitizer.ReplaceUnicodeEmojis("Test string ๐Ÿ˜†๐Ÿ˜†๐Ÿ˜† ๐Ÿ˜›", "[e]")
// this will return: "Test string  "
sanitizer.StripUnicodeEmojis("Test string ๐Ÿ˜†๐Ÿ˜†๐Ÿ˜† ๐Ÿ˜›")

You can also set multiple options to further configure the sanitization.

Options

Set custom unicode version

You can simply set the unicode version with the UnicodeVersion option:

sanitizer, err := NewSanitizer(
    options.UnicodeVersion(VersionLatest),
)

currently offline available are following versions:
1.0, 2.0, 3.0, 4.0, 5.0, 11.0, 12.0, 12.1, 13.0, 13.1 All of those are taken from https://unicode.org/Public/emoji/

If you are using the online option you can also instantly use other versions when they get released.

By default the version is set to the currently newest version: 13.1

Emoji Data from custom path

You can load the emoji data from whatever path you want with the option:

sanitizer, err := NewSanitizer(
    options.LoadFromCustomPath("https://unicode.org/Public/13.0.0/ucd/emoji/emoji-data.txt"),
)

If no http or https is in the scheme of the parsed URL it'll try to load a local file.

Retrieve Emoji Data from offline/online

In case you always want to run the latest emoji data or possibly any updates you can use the option:

// offline, this is already the default option
sanitizer, err := NewSanitizer(
    options.LoadFromOnline(false),
)

// online, you'll need this option if you want to load the emoji data from online
sanitizer, err := NewSanitizer(
    options.LoadFromOnline(true),
)

To reduce the error proneness you can further set the option to fallback to the offline approach should an error occur while loading the emoji data.

sanitizer, err := NewSanitizer(
    options.LoadFromOnline(true),
    options.FallbackToOffline(true),
)

Allow specific unicode emoji runes

In case you want to allow specific unicode emoji runes you can allow specific runes/ranges.
Best shown in the example of basic runes which are categorized as emoji:

sanitizer, err := NewSanitizer(
    // general emoji codes which are normally allowed in most contexts
    // "#", "*", "[0-9]", "ยฉ", "ยฎ", "โ€ผ", "โ„ข"
    options.AllowEmojiCodes([]string{"0023", "002A", "0030..0039", "00A9", "00AE", "203C", "2122"}),
)

Development

Want to contribute? Great!
I'm always glad hearing about bugs or pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details

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.