Skip to content

Commit 8659e43

Browse files
committed
docs: various improvements
- add demo section - involutory section - move examples behind function lists - move `charCase.invert` to "Involutory functions with negated arguments" - create section "Involutory functions with counter function" - unicode module - better description for `fixSurrogates` - add examples for `fixSurrogates` and `unfixSurrogates` - example strings and unicode codepoints as code
1 parent b62a272 commit 8659e43

File tree

7 files changed

+156
-48
lines changed

7 files changed

+156
-48
lines changed

readme.md

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ So I decided to combine these "strange" functions into one library.
2323
More or less just for fun.
2424

2525

26+
## Demo
27+
28+
A [comprehensive demo][pseudo-cipherer-demo] of all functions can be found in
29+
the demonstration of this library/package in the demo project
30+
[`pseudeo-chiperer`][pseudo-cipherer-repo].
31+
32+
2633
## Table of content
2734

2835
1. [Modules](#user-content-module)
@@ -52,23 +59,24 @@ More or less just for fun.
5259

5360
<dl>
5461
<dt><a href="#flipBits">flipBits(string)</a> ⇒ <code>string</code></dt>
55-
<dd><p>Flip the character bits of a string. The 16 character bits of &#39;A&#39; are
62+
<dd><p>Flip the character bits of a string. The 16 character bits of <code>&#39;A&#39;</code> are
5663
<code>00000000 01000001</code> - if we flip the bits (so every 0 becomes 1 and vice
57-
versa) they look like this <code>11111111 10111110</code>. This means that &#39;A&#39; (0x41)
58-
becomes &#39;&#39; (0xFFBE).</p>
64+
versa) they look like this <code>11111111 10111110</code>. This means that <code>&#39;A&#39;</code>
65+
(<code>U+0041</code>) becomes <code>&#39;&#39;</code> (<code>U+FFBE</code>).</p>
5966
</dd>
6067
<dt><a href="#gobbledygook">gobbledygook(string, [exclude])</a> ⇒ <code>string</code></dt>
6168
<dd><p>Applies <code>toMANS</code> to all characters with a random <code>type</code>.
62-
For example <code>Hello World</code> turns into <code>𝐇𝖾𝓵𝗹𝘰 𝔚𝗈𝒓𝔩𝔡</code>.</p>
69+
For example <code>&#39;Hello World&#39;</code> turns into <code>&#39;𝐇𝖾𝓵𝗹𝘰 𝔚𝗈𝒓𝔩𝔡&#39;</code>.</p>
6370
</dd>
6471
<dt><a href="#jumble">jumble(string, [runs])</a> ⇒ <code>string</code></dt>
6572
<dd><p>Jumble the letters of all words in a string,
6673
except the first and last one, to keep it readable.</p>
6774
</dd>
6875
<dt><a href="#reverseBits">reverseBits(string)</a> ⇒ <code>string</code></dt>
69-
<dd><p>Reverse the character bits of a string. The 16 character bits of &#39;A&#39; are
76+
<dd><p>Reverse the character bits of a string. The 16 character bits of <code>&#39;A&#39;</code> are
7077
<code>00000000 01000001</code> - if we reverse the bits they look like this
71-
<code>10000010 00000000</code>. This means that &#39;A&#39; (0x41) becomes &#39;&#39; (0x8200).</p>
78+
<code>10000010 00000000</code>. This means that <code>&#39;A&#39;</code> (<code>U+0041</code>) becomes <code>&#39;&#39;</code>
79+
(<code>U+8200</code>).</p>
7280
</dd>
7381
<dt><a href="#reverse">reverse(string)</a> ⇒ <code>string</code></dt>
7482
<dd><p>Reverse a string.</p>
@@ -83,9 +91,10 @@ except the first and last one, to keep it readable.</p>
8391
<dd><p>Randomize the order of the characters in a string.</p>
8492
</dd>
8593
<dt><a href="#shiftBits">shiftBits(string, [n])</a> ⇒ <code>string</code></dt>
86-
<dd><p>Rotate the character bits of a string. The 16 character bits of &#39;A&#39; are
94+
<dd><p>Rotate the character bits of a string. The 16 character bits of <code>&#39;A&#39;</code> are
8795
<code>00000000 01000001</code> - if we shift the bits by -4 <code>n</code> digits they look like
88-
this <code>00010000 00000100</code>. This means that &#39;A&#39; (0x41) becomes &#39;&#39; (0x1004).</p>
96+
this <code>00010000 00000100</code>. This means that <code>&#39;A&#39;</code> (<code>U+0041</code>) becomes <code>&#39;&#39;</code>
97+
(<code>U+1004</code>).</p>
8998
</dd>
9099
<dt><a href="#shift">shift(string, [n])</a> ⇒ <code>string</code></dt>
91100
<dd><p>Shift the characters of a string by <code>n</code> digits.</p>
@@ -257,14 +266,24 @@ Functions for handle unicode stuff.
257266

258267
### stringMutilator/unicode~fixSurrogates(string) ⇒ <code>string</code>
259268
Fix unpaired high/low surrogates by adding a blank high/low surrogate
260-
(U+D800 or U+DC00) to the required location.
269+
(`U+D800` or `U+DC00`) to the designated location. An unpaired surrogate can
270+
lead to problems, for example by copying it to the clipboard could result in
271+
a Replacement Character � (`U+FFFD`). For example if the string is
272+
`'\uD801'` it will be altered to `'\uD801\uDC00'` (`'𐐀'`) or `'\uDE80'` to
273+
`'\uD800\uDE80'` (`'𐊀'`).
261274

262275
**Kind**: inner method of [<code>stringMutilator/unicode</code>](#module_stringMutilator/unicode)
276+
**See**: https://en.wikipedia.org/wiki/UTF-16#U+D800_to_U+DFFF
263277

264278
| Param | Type | Description |
265279
| --- | --- | --- |
266280
| string | <code>string</code> | The input string |
267281

282+
**Example**
283+
```js
284+
stringMutilator.unicode.fixSurrogates('Test: \uD801 \uDE80');
285+
// > 'Test: 𐐀 𐊀'
286+
```
268287
<a name="module_stringMutilator/unicode..unfixSurrogates"></a>
269288

270289
### stringMutilator/unicode~unfixSurrogates(string) ⇒ <code>string</code>
@@ -276,13 +295,18 @@ Remove the by `fixSurrogates` added blank high/low surrogates.
276295
| --- | --- | --- |
277296
| string | <code>string</code> | The input string |
278297

298+
**Example**
299+
```js
300+
stringMutilator.unfixSurrogates('Test: 𐐀 𐊀');
301+
// > 'Test: \uD801 \uDE80'
302+
```
279303
<a name="flipBits"></a>
280304

281305
## flipBits(string) ⇒ <code>string</code>
282-
Flip the character bits of a string. The 16 character bits of 'A' are
306+
Flip the character bits of a string. The 16 character bits of `'A'` are
283307
`00000000 01000001` - if we flip the bits (so every 0 becomes 1 and vice
284-
versa) they look like this `11111111 10111110`. This means that 'A' (0x41)
285-
becomes 'ᄒ' (0xFFBE).
308+
versa) they look like this `11111111 10111110`. This means that `'A'`
309+
(`U+0041`) becomes `'ᄒ'` (`U+FFBE`).
286310

287311
**Kind**: global function
288312

@@ -299,7 +323,7 @@ stringMutilator.flipBits('Hello World!');
299323

300324
## gobbledygook(string, [exclude]) ⇒ <code>string</code>
301325
Applies `toMANS` to all characters with a random `type`.
302-
For example `Hello World` turns into `𝐇𝖾𝓵𝗹𝘰 𝔚𝗈𝒓𝔩𝔡`.
326+
For example `'Hello World'` turns into `'𝐇𝖾𝓵𝗹𝘰 𝔚𝗈𝒓𝔩𝔡'`.
303327

304328
**Kind**: global function
305329

@@ -335,9 +359,10 @@ stringMutilator.jumble('Hello World!');
335359
<a name="reverseBits"></a>
336360

337361
## reverseBits(string) ⇒ <code>string</code>
338-
Reverse the character bits of a string. The 16 character bits of 'A' are
362+
Reverse the character bits of a string. The 16 character bits of `'A'` are
339363
`00000000 01000001` - if we reverse the bits they look like this
340-
`10000010 00000000`. This means that 'A' (0x41) becomes '舀' (0x8200).
364+
`10000010 00000000`. This means that `'A'` (`U+0041`) becomes `'舀'`
365+
(`U+8200`).
341366

342367
**Kind**: global function
343368

@@ -424,9 +449,10 @@ stringMutilator.scramble('Hello World!');
424449
<a name="shiftBits"></a>
425450

426451
## shiftBits(string, [n]) ⇒ <code>string</code>
427-
Rotate the character bits of a string. The 16 character bits of 'A' are
452+
Rotate the character bits of a string. The 16 character bits of `'A'` are
428453
`00000000 01000001` - if we shift the bits by -4 `n` digits they look like
429-
this `00010000 00000100`. This means that 'A' (0x41) becomes 'င' (0x1004).
454+
this `00010000 00000100`. This means that `'A'` (`U+0041`) becomes `'င'`
455+
(`U+1004`).
430456

431457
**Kind**: global function
432458

@@ -496,6 +522,11 @@ stringMutilator.toMANS('Hello World!', 1);
496522
497523
### List of involutory functions
498524

525+
* [`flipBits`](#flipBits)
526+
* [`reverse`](#reverse)
527+
* [`reverseBits`](#reverseBits)
528+
* [`rot13`](#rot13)
529+
499530
**Example**
500531

501532
```js
@@ -505,25 +536,50 @@ rot13(rot13('Hello World!')) === 'Hello World!';
505536
// > true
506537
```
507538

508-
* [`charCase.invert`](#module_stringMutilator/charCase.invert)
509-
* [`flipBits`](#flipBits)
510-
* [`reverse`](#reverse)
511-
* [`reverseBits`](#reverseBits)
512-
* [`rot13`](#rot13)
513-
514539
### List of involutory functions with negated arguments
515540

541+
* [`charCase.invert`](#module_stringMutilator/charCase..invert)
542+
* [`shift`](#shift)
543+
* [`shiftBits`](#shiftBits)
544+
516545
**Example**
517546

518-
```
547+
```js
519548
import { shift } from '@0x04/string-mutilator';
520549

521550
shift(shift('Hello World!', 5), -5) === 'Hello World!';
522551
// > true
523552
```
524553

525-
* [`shift`](#shift)
526-
* [`shiftBits`](#shiftBits)
554+
### List of involutory functions with counter function
555+
556+
* [`compressor.pack`](#module_stringMutilator/compressor..pack) / [`compressor.unpack`](#module_stringMutilator/compressor..unpack)
557+
* [`unicode.fixSurrogates`](#module_stringMutilator/unicode..fixSurrogates) / [`unicode.unfixSurrogates`](#module_stringMutilator/unicode..unfixSurrogates)
558+
559+
**Examples**
560+
561+
```js
562+
import { compressor } from '@0x04/string-mutilator';
563+
564+
compressor.unpack(compressor.pack('Hello World!')) === 'Hello World!';
565+
// > true
566+
```
567+
568+
### List of non involutory functions
569+
570+
* [`compressor.signature`](#module_stringMutilator/compressor..signature)
571+
* [`gobbledygook`](#gobbledygook)
572+
* [`jumble`](#jumble)
573+
* [`rockdotize`](#rockdotize)
574+
* [`scramble`](#scramble)
575+
* [`toMANS`](#toMANS)
576+
577+
```js
578+
import { jumble } from '@0x04/string-mutilator';
579+
580+
jumble(jumble('Hello World!')) === 'Hello World!';
581+
// > false
582+
```
527583

528584

529585
## Using the CLI
@@ -590,7 +646,8 @@ $ string-mutilator --help
590646

591647
<!-- Links -->
592648
[0x04]: mailto:ok@0x04.de
593-
649+
[pseudo-cipherer-demo]: https://0x04.github.io/pseudo-cipherer
650+
[pseudo-cipherer-repo]: https://github.com/0x04/pseudo-cipherer
594651

595652
<!-- Appendix -->
596653
[involution]: https://en.wikipedia.org/wiki/Involution_(mathematics)

src/flip-bits.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* Flip the character bits of a string. The 16 character bits of 'A' are
2+
* Flip the character bits of a string. The 16 character bits of `'A'` are
33
* `00000000 01000001` - if we flip the bits (so every 0 becomes 1 and vice
4-
* versa) they look like this `11111111 10111110`. This means that 'A' (0x41)
5-
* becomes 'ᄒ' (0xFFBE).
4+
* versa) they look like this `11111111 10111110`. This means that `'A'`
5+
* (`U+0041`) becomes `'ᄒ'` (`U+FFBE`).
66
* @param {string} string The input string.
77
* @return {string}
88
* @example
@@ -26,4 +26,4 @@ const flipBits = string =>
2626
)
2727
.join('');
2828

29-
export default flipBits;
29+
export default flipBits;

src/gobbledygook.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const getRandomType = (exclude = []) => {
2020

2121
/**
2222
* Applies `toMANS` to all characters with a random `type`.
23-
* For example `Hello World` turns into `𝐇𝖾𝓵𝗹𝘰 𝔚𝗈𝒓𝔩𝔡`.
23+
* For example `'Hello World'` turns into `'𝐇𝖾𝓵𝗹𝘰 𝔚𝗈𝒓𝔩𝔡'`.
2424
* @param {string} string The input string
2525
* @param {array} [exclude=[ 1, 2 ]] Exclude specific `type` of `toMANS`
2626
* @returns {string}

src/reverse-bits.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import reverse from './reverse';
22

33
/**
4-
* Reverse the character bits of a string. The 16 character bits of 'A' are
4+
* Reverse the character bits of a string. The 16 character bits of `'A'` are
55
* `00000000 01000001` - if we reverse the bits they look like this
6-
* `10000010 00000000`. This means that 'A' (0x41) becomes '舀' (0x8200).
6+
* `10000010 00000000`. This means that `'A'` (`U+0041`) becomes `'舀'`
7+
* (`U+8200`).
78
* @param {string} string The input string.
89
* @returns {string}
910
* @example

src/shift-bits.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import shift from './shift';
22

33
/**
4-
* Rotate the character bits of a string. The 16 character bits of 'A' are
4+
* Rotate the character bits of a string. The 16 character bits of `'A'` are
55
* `00000000 01000001` - if we shift the bits by -4 `n` digits they look like
6-
* this `00010000 00000100`. This means that 'A' (0x41) becomes 'င' (0x1004).
6+
* this `00010000 00000100`. This means that `'A'` (`U+0041`) becomes `'င'`
7+
* (`U+1004`).
78
* @param {string} string The input string.
89
* @param {number} [n=1] Number of digits to rotate the character bit.
910
* Positive for right rotation,
@@ -26,4 +27,4 @@ const shiftBits = (string, n = 1) =>
2627
)
2728
);
2829

29-
export default shiftBits;
30+
export default shiftBits;

src/unicode.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ const isLowSurrogate = char => (/^[\uDC00-\uDFFF]$/.test(char));
2121

2222
/**
2323
* Fix unpaired high/low surrogates by adding a blank high/low surrogate
24-
* (U+D800 or U+DC00) to the required location.
24+
* (`U+D800` or `U+DC00`) to the designated location. An unpaired surrogate can
25+
* lead to problems, for example by copying it to the clipboard could result in
26+
* a Replacement Character � (`U+FFFD`). For example if the string is
27+
* `'\uD801'` it will be altered to `'\uD801\uDC00'` (`'𐐀'`) or `'\uDE80'` to
28+
* `'\uD800\uDE80'` (`'𐊀'`).
2529
* @param {string} string The input string
2630
* @returns {string}
31+
* @see https://en.wikipedia.org/wiki/UTF-16#U+D800_to_U+DFFF
32+
* @example
33+
* stringMutilator.unicode.fixSurrogates('Test: \uD801 \uDE80');
34+
* // > 'Test: 𐐀 𐊀'
2735
*/
2836
const fixSurrogates = string =>
2937
{
@@ -56,6 +64,9 @@ const fixSurrogates = string =>
5664
* Remove the by `fixSurrogates` added blank high/low surrogates.
5765
* @param {string} string The input string
5866
* @returns {string}
67+
* @example
68+
* stringMutilator.unfixSurrogates('Test: 𐐀 𐊀');
69+
* // > 'Test: \uD801 \uDE80'
5970
*/
6071
const unfixSurrogates = string =>
6172
string.replace(/([\uD800|\uDC00])/g, '');

0 commit comments

Comments
 (0)