Class: Unisec::Confusables — Documentation by YARD 0.9.44

# Class: Unisec::Confusables

Inherits:
  Object
  - Object
  - Unisec::Confusables
  show all

Defined in:
  lib/unisec/confusables.rb

## Overview

Operations about Unicode confusable characters (homoglyphs).

## Class Method Summary collapse

- .list(chr, map: true) ⇒ Array<String>

  List confusables characters for a given character.

- .list_display(chr, map: true) ⇒ Object

  Display a CLI-friendly output listing all confusables corresponding to a character (code point).

- .randomize(str) ⇒ String

  Replace all characters with random confusables when possible.

- .randomize_display(str) ⇒ Object

  Display a CLI-friendly output of a string where characters are replaces with random confusables.

## Class Method Details

### .list(chr, map: true) ⇒ `Array<String>`

List confusables characters for a given character

#### Examples:

    Unisec::Confusables.list('!') # => ["！", "ǃ", "ⵑ", "‼", "⁉", "⁈"]
    Unisec::Confusables.list('!', map: false) # => ["！", "ǃ", "ⵑ"]

Parameters:

- chr (`String`) —

  the character to search confusables for

- map (`Boolean`) (defaults to: `true`) —

  allows partial mapping, includes confusable where the given chart is a part of

Returns:

- (`Array<String>`) —

  list of confusables

    18
    19
    20
    # File 'lib/unisec/confusables.rb', line 18

    def self.list(chr, map: true)
      Unicode::Confusable.list(chr, map)
    end

### .list_display(chr, map: true) ⇒ `Object`

Display a CLI-friendly output listing all confusables corresponding to a character (code point)

Parameters:

- chr (`String`) —

  the character to search confusables for

- map (`Boolean`) (defaults to: `true`) —

  allows partial mapping, includes confusable where the given chart is a part of

    25
    26
    27
    28
    29
    30
    31
    # File 'lib/unisec/confusables.rb', line 25

    def self.list_display(chr, map: true)
      Confusables.list(chr, map: map).each do |confu|
        puts "#{Utils::String.char2codepoint(confu).ljust(9)} #{confu.ljust(4)} " \
             "#{TwitterCldr::Shared::CodePoint.get(confu.codepoints.first).name}"
      end
      nil
    end

### .randomize(str) ⇒ `String`

Replace all characters with random confusables when possible.

#### Examples:

    Unisec::Confusables.randomize('noraj') # => "𝓃ⲟ𝓇𝒶ｊ"
    Unisec::Confusables.randomize('noraj') # => "𝗻૦𝚛⍺𝐣"
    Unisec::Confusables.randomize('noraj') # => "𝔫𞺄𝕣⍺ｊ"

Parameters:

- str (`String`) —

  Unicode string

Returns:

- (`String`) —

  input randomized with confusables

    40
    41
    42
    43
    44
    45
    46
    47
    # File 'lib/unisec/confusables.rb', line 40

    def self.randomize(str)
      out = ''
      str.each_char do |chr|
        confu = Confusables.list(chr, map: false).sample
        out += confu.nil? ? chr : confu
      end
      out
    end

### .randomize_display(str) ⇒ `Object`

Display a CLI-friendly output of a string where characters are replaces with random confusables

Parameters:

- str (`String`) —

  Unicode string

    51
    52
    53
    54
    55
    # File 'lib/unisec/confusables.rb', line 51

    def self.randomize_display(str)
      display = ->(key, value) { puts Paint[key, :red, :bold].ljust(23) + " #{value}" }
      display.call('Original:', str)
      display.call('Transformed:', Unisec::Confusables.randomize(str))
    end