Coder Social home page Coder Social logo

unicode_alphanumeric's Introduction

Unicode Alphanumeric

This Ruby gem removes, replaces, transforms, or detects non-alphanumeric Unicode characters in a string. Spaces optionally may be treated as alphanumeric.

Basic usage synopsis

Detect non-alphanumerics:

# ['+', '?', '¿']
UnicodeAlphanumeric.scan('abc Ω + 123 ? ¿ Ñ def', :spaces => true)

Remove non-alphanumerics:

# 'abc Ω  123   Ñ def'
UnicodeAlphanumeric.filter('abc Ω + 123 ? ¿ Ñ def', :spaces => true)

Replace non-alphanumerics with a string:

#'abc Ω _ 123 _ _ Ñ def'
UnicodeAlphanumeric.replace('abc Ω + 123 ? ¿ Ñ def', '_', :spaces => true)

Replace non-alphanumerics with the return value of a block:

# 'abc Ω ++ 123 ?? ¿¿ Ñ def'
UnicodeAlphanumeric.map('abc Ω + 123 ? ¿ Ñ def', :spaces => true) do |char|
  char * 2
end

If you want to, you can treat spaces as alphanumerics. By default, spaces are not considered alphanumeric.

There are two ways to search for spaces. If you pass spaces => true, then a space is defined as any Unicode code point which is a member of the "Separator, Space" category. Be warned that this definition includes a lot more than the ASCII space character.

If you only want the ASCII space to be considered alphanumeric, you can pass in spaces => :ascii.

Implementation

This gem relies on the Unicode gem to analyze non-ASCII characters. Specifically, it calls Unicode.categories, which returns the category metadata from the Unicode Character Database. That metadata indicates whether the code point is alphanumeric.

For ASCII characters, this gem relies on gsub with a regex. While the Unicode metadata would achieve the same results, gsub is faster for ASCII.

unicode_alphanumeric's People

Contributors

jarrett avatar

Watchers

 avatar James Cloos 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.