@0x04/string-mutilator

A JavaScript library for mutilate strings.

Usage no npm install needed!

<script type="module">
  import 0x04StringMutilator from 'https://cdn.skypack.dev/@0x04/string-mutilator';
</script>

README

string-mutilator

A JavaScript library for mutilate strings.

MIT license

Motivation background

Most of the code from this project was created during a personal codegolf competition to distort the appearance of strings as much as possible but without actually destroying their contents.

So the name mutilator come into my mind.

mutilator (plural mutilators)

  1. Agent noun of mutilate; one who mutilates.

This sounds unnecessarily brutal for now, but I assure you that no strings will be harmed permanently, as long as the functions are used correctly. :smirk:

So I decided to combine these "strange" functions into one library. More or less just for fun.

Demo

A comprehensive demo of all functions can be found in the demonstration of this library/package in the demo project pseudeo-chiperer.

Table of content

  1. Modules
  2. Functions
  3. Involutory functions
  4. Using the CLI

Modules

stringMutilator/charCase

Functions for manipulating the letter case of strings.

stringMutilator/compressor

Functions for compressing (pack/unpack etc.) strings.

stringMutilator

Functions for mutilating strings.

stringMutilator/unicode

Functions for handle unicode stuff.

Functions

flipBits(string) β‡’ string

Flip the character bits of a string. The 16 character bits of 'A' are 00000000 01000001 - if we flip the bits (so every 0 becomes 1 and vice versa) they look like this 11111111 10111110. This means that 'A' (U+0041) becomes 'οΎΎ' (U+FFBE).

gobbledygook(string, [exclude]) β‡’ string

Applies toMANS to all characters with a random type. For example 'Hello World' turns into 'π‡π–Ύπ“΅π—Ήπ˜° π”šπ—ˆπ’“π”©π”‘'.

jumble(string, [runs]) β‡’ string

Jumble the letters of all words in a string, except the first and last one, to keep it readable.

reverseBits(string) β‡’ string

Reverse the character bits of a string. The 16 character bits of 'A' are 00000000 01000001 - if we reverse the bits they look like this 10000010 00000000. This means that 'A' (U+0041) becomes 'θˆ€' (U+8200).

reverse(string) β‡’ string

Reverse a string.

rockdotize(string, [regexp]) β‡’ string

Create a heavy metal like rockdot string.

rot13(string) β‡’ string

Rotate the character indexes of a string by 13 digits.

scramble(string) β‡’ string

Randomize the order of the characters in a string.

shiftBits(string, [n]) β‡’ string

Rotate the character bits of a string. The 16 character bits of 'A' are 00000000 01000001 - if we shift the bits by -4 n digits they look like this 00010000 00000100. This means that 'A' (U+0041) becomes 'င' (U+1004).

shift(string, [n]) β‡’ string

Shift the characters of a string by n digits.

toMANS(string, [type]) β‡’ string

Convert A-Z to Mathematical Alphanumeric Symbols.

List of type values:

type Unicode name
0 Mathematical Bold *
1 Mathematical Italic *
2 Mathematical Bold Italic *
3 Mathematical Script Capital *
4 Mathematical Bold Script *
5 Mathematical Fraktur *
6 Mathematical Double-Struck *
7 Mathematical Bold Fraktur *
8 Mathematical Sans-Serif *
9 Mathematical Sans-Serif Bold *
10 Mathematical Sans-Serif Italic *
11 Mathematical Sans-Serif Bold Italic *
12 Mathematical Monospace *

stringMutilator/charCase

Functions for manipulating the letter case of strings.

stringMutilator/charCase~invert(string, [every]) β‡’ string

Invert the case of letters in a string.

Kind: inner method of stringMutilator/charCase

Param Type Default Description
string string The string to be case inverted.
[every] number 0 Only invert every n letter.

Example

stringMutilator.charCase.invert('Hello World!');
// > 'hELLO wORLD!'

stringMutilator/charCase~snakeCase(string) β‡’ string

Change the case of given string to "Snake Case".

Kind: inner method of stringMutilator/charCase

Param Type
string string

Example

stringMutilator.charCase.snakeCase('Hello World');
// > 'hello_world'

stringMutilator/charCase~camelCase(string) β‡’ string

Change the case of the given string to "Camel Case".

Kind: inner method of stringMutilator/charCase

Param Type
string string

Example

stringMutilator.charCase.camelCase('Hello World');
// > 'helloWorld'

stringMutilator/charCase~kebabCase(string) β‡’ string

Change the case of the given string to "Kebap Case".

Kind: inner method of stringMutilator/charCase

Param Type
string string

Example

stringMutilator.charCase.kebabCase('Hello World');
// > 'hello-world'

stringMutilator/charCase~dotCase(string) β‡’ string

Change the case of the given string to "Dot Case".

Kind: inner method of stringMutilator/charCase

Param Type
string string

Example

stringMutilator.charCase.dotCase('Hello World');
// > 'hello.world'

stringMutilator/charCase~pascalCase(string) β‡’ string

Change the case of the given string to "Pascal Case".

Kind: inner method of stringMutilator/charCase

Param Type
string string

Example

stringMutilator.charCase.pascalCase('Hello World');
// > 'HelloWorld'

stringMutilator/charCase~capitalize(string) β‡’ string

Change the case of the given string to "Capitalized".

Kind: inner method of stringMutilator/charCase

Param Type
string string

Example

stringMutilator.charCase.capitalize('hello world');
// > 'Hello World'

stringMutilator/compressor

Functions for compressing (pack/unpack etc.) strings.

stringMutilator/compressor~pack(string) β‡’ string

Pack all characters of a 8-bit string.

Kind: inner method of stringMutilator/compressor

Param Type Description
string string A 8-bit string to pack.

Example

stringMutilator.compressor.pack('Hello World!');
// > 'δ‘₯汬漠坯牬搑'

stringMutilator/compressor~unpack(string) β‡’ string

Unpack a packed string.

Kind: inner method of stringMutilator/compressor

Param Type Description
string string The packed string to unpack.

Example

stringMutilator.compressor.unpack('δ‘₯汬漠坯牬搑');
// > 'Hello World!'

stringMutilator/compressor~signature(string, [withEval]) β‡’ string

Return the given, packed string with the unpack signature.

Kind: inner method of stringMutilator/compressor

Param Type Default Description
string string The packed string to signature.
[withEval] boolean false Determine if the result includes a eval command for executing code.

Example

stringMutilator.compressor.signature('δ‘₯汬漠坯牬搑');
// > 'unescape(escape("δ‘₯汬漠坯牬搑").replace(/u(..)/g, "$1%");'

stringMutilator

Functions for mutilating strings.

stringMutilator/unicode

Functions for handle unicode stuff.

stringMutilator/unicode~fixSurrogates(string) β‡’ string

Fix unpaired high/low surrogates by adding a blank high/low surrogate (U+D800 or U+DC00) to the designated location. An unpaired surrogate can lead to problems, for example by copying it to the clipboard could result in a Replacement Character οΏ½ (U+FFFD). For example if the string is '\uD801' it will be altered to '\uD801\uDC00' ('𐐀') or '\uDE80' to '\uD800\uDE80' ('πŠ€').

Kind: inner method of stringMutilator/unicode
See: https://en.wikipedia.org/wiki/UTF-16#U+D800_to_U+DFFF

Param Type Description
string string The input string

Example

stringMutilator.unicode.fixSurrogates('Test: \uD801 \uDE80');
// > 'Test: 𐐀 πŠ€'

stringMutilator/unicode~unfixSurrogates(string) β‡’ string

Remove the by fixSurrogates added blank high/low surrogates.

Kind: inner method of stringMutilator/unicode

Param Type Description
string string The input string

Example

stringMutilator.unfixSurrogates('Test: 𐐀 πŠ€');
// > 'Test: \uD801 \uDE80'

flipBits(string) β‡’ string

Flip the character bits of a string. The 16 character bits of 'A' are 00000000 01000001 - if we flip the bits (so every 0 becomes 1 and vice versa) they look like this 11111111 10111110. This means that 'A' (U+0041) becomes 'οΎΎ' (U+FFBE).

Kind: global function

Param Type Description
string string The input string.

Example

stringMutilator.flipBits('Hello World!');
// > 'οΎ·οΎšοΎ“οΎ“οΎοΏŸοΎ¨οΎοΎοΎ“οΎ›οΏž'

gobbledygook(string, [exclude]) β‡’ string

Applies toMANS to all characters with a random type. For example 'Hello World' turns into 'π‡π–Ύπ“΅π—Ήπ˜° π”šπ—ˆπ’“π”©π”‘'.

Kind: global function

Param Type Default Description
string string The input string
[exclude] array [ 1, 2 ] Exclude specific type of toMANS

Example

stringMutilator.gobbledygook('Hello World');
// > π‡π–Ύπ“΅π—Ήπ˜° π”šπ—ˆπ’“π”©π”‘

jumble(string, [runs]) β‡’ string

Jumble the letters of all words in a string, except the first and last one, to keep it readable.

Kind: global function
See: https://www.mnn.com/lifestyle/arts-culture/stories/why-your-brain-can-read-jumbled-letters

Param Type Default Description
string string The input string.
[runs] number 3 The number of attempts to get a real randomized word string.

Example

stringMutilator.jumble('Hello World!');
// > 'Hlelo Wrlod!'

reverseBits(string) β‡’ string

Reverse the character bits of a string. The 16 character bits of 'A' are 00000000 01000001 - if we reverse the bits they look like this 10000010 00000000. This means that 'A' (U+0041) becomes 'θˆ€' (U+8200).

Kind: global function

Param Type Description
string string The input string.

Example

stringMutilator.reverseBits('Hello World!');
// > 'αˆ€κ˜€γ˜€γ˜€ο˜€Π€ξ¨€ο˜€δΈ€γ˜€β˜€θ€'

reverse(string) β‡’ string

Reverse a string.

Kind: global function

Param Type Description
string string The input string.

Example

stringMutilator.reverse('Hello World!');
// > '!dlroW olleH'

rockdotize(string, [regexp]) β‡’ string

Create a heavy metal like rockdot string.

Kind: global function
See

Param Type Default Description
string string The string to rockdotize.
[regexp] RegExp <RegExp /\w/gi> A RegExp to select every character that should get rockdotized.

Example

stringMutilator.rockdotize('Hello World!');
// > 'Ḧël̈l̈ö Ẅör̈l̈d̈!'

rot13(string) β‡’ string

Rotate the character indexes of a string by 13 digits.

Kind: global function
See: https://en.wikipedia.org/wiki/ROT13

Param Type Description
string string The input string.

Example

stringMutilator.rot13('Hello World!');
// > 'Uryyb Jbeyq!'

scramble(string) β‡’ string

Randomize the order of the characters in a string.

Kind: global function

Param Type Description
string string The string to scramble.

Example

stringMutilator.scramble('Hello World!');
// > 'WorH!llo led'

shiftBits(string, [n]) β‡’ string

Rotate the character bits of a string. The 16 character bits of 'A' are 00000000 01000001 - if we shift the bits by -4 n digits they look like this 00010000 00000100. This means that 'A' (U+0041) becomes 'င' (U+1004).

Kind: global function

Param Type Default Description
string string The input string.
[n] number 1 Number of digits to rotate the character bit. Positive for right rotation, negative for left rotation.

shift(string, [n]) β‡’ string

Shift the characters of a string by n digits.

Kind: global function

Param Type Default Description
string string The string to shift.
[n] number 1 Number of digits to rotate the characters. Positive for right rotation, negative for left rotation.

Example

stringMutilator.shift('Hello World!', 3);
// > 'lo World!Hel'

toMANS(string, [type]) β‡’ string

Convert A-Z to Mathematical Alphanumeric Symbols.

List of type values:

type Unicode name
0 Mathematical Bold *
1 Mathematical Italic *
2 Mathematical Bold Italic *
3 Mathematical Script Capital *
4 Mathematical Bold Script *
5 Mathematical Fraktur *
6 Mathematical Double-Struck *
7 Mathematical Bold Fraktur *
8 Mathematical Sans-Serif *
9 Mathematical Sans-Serif Bold *
10 Mathematical Sans-Serif Italic *
11 Mathematical Sans-Serif Bold Italic *
12 Mathematical Monospace *

Kind: global function
See: https://unicode-table.com/en/blocks/mathematical-alphanumeric-symbols

Param Type Default Description
string string The input string.
[type] number 0 A number between 0 and 12.

Example

stringMutilator.toMANS('Hello World!', 1);
// > 'π»π‘’π‘™π‘™π‘œ π‘Šπ‘œπ‘Ÿπ‘™π‘‘.'

Involutory functions

An involutory function, is a function f that is its own inverse.

List of involutory functions

Example

import { rot13 } from '@0x04/string-mutilator';

rot13(rot13('Hello World!')) === 'Hello World!';
// > true

List of involutory functions with negated arguments

Example

import { shift } from '@0x04/string-mutilator';

shift(shift('Hello World!', 5), -5) === 'Hello World!';
// > true

List of involutory functions with counter function

Examples

import { compressor } from '@0x04/string-mutilator';

compressor.unpack(compressor.pack('Hello World!')) === 'Hello World!';
// > true

List of non involutory functions

import { jumble } from '@0x04/string-mutilator';

jumble(jumble('Hello World!')) === 'Hello World!';
// > false

Using the CLI

Install

To use it in your project

$ npm install --save @0x04/string-mutilator

To use it globally

$ npm install --global @0x04/string-mutilator

General usage

$ string-mutilator --help

  Functions for mutilating strings.

  Usage
  $ string-mutilator <function> <string> [argument]...

    Functions
      charCase.invert
      compressor.pack
      compressor.unpack
      compressor.signature
      charCase.snakeCase
      charCase.camelCase
      charCase.kebabCase
      charCase.dotCase
      charCase.pascalCase
      charCase.capitalize
      flipBits
      gobbledygook
      jumble
      reverse
      reverseBits
      rockdotize
      rot13
      scramble
      shift
      shiftBits
      toMANS
      unicode.fixSurrogates
      unicode.unfixSurrogates

    Examples
      $ string-mutilator shift "Hello World!" 6
      $ string-mutilator charCase.invert "Hello World!"
      $ string-mutilator toMANS "Hello World!" 4

License

© 2019 by Oliver KΓΌhn. Released under the MIT license.