-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
73 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { rot13 } from './rot13' | ||
import { runBenchmark } from '../dev' | ||
|
||
function rot13Arr(str: string) { | ||
const input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' | ||
const output = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm' | ||
const index = (x: string) => input.indexOf(x) | ||
const translate = (x: string) => (index(x) > -1 ? output[index(x)] : x) | ||
return str.split('').map(translate).join('') | ||
} | ||
|
||
function rot13Re(str: string) { | ||
return str.replace(/[a-z]/gi, char => { | ||
return String.fromCharCode( | ||
char.charCodeAt(0) + (char.toLowerCase() < 'n' ? 13 : -13), | ||
) | ||
}) | ||
} | ||
|
||
const str = `ROT13 is its own inverse. Meaning, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding.` | ||
|
||
runBenchmark({ | ||
['rot13!fastest']() { | ||
rot13(str) | ||
}, | ||
['rot13Arr']() { | ||
rot13Arr(str) | ||
}, | ||
['rot13Re']() { | ||
rot13Re(str) | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { rot13 } from './rot13' | ||
|
||
describe('rot13', () => { | ||
test('表现正常', () => { | ||
expect(rot13('hello world')).toBe('uryyb jbeyq') | ||
expect(rot13('uryyb jbeyq')).toBe('hello world') | ||
expect(rot13('foo-bar')).toBe('sbb-one') | ||
expect(rot13('foo123bar')).toBe('sbb123one') | ||
expect(rot13('foo!@bar')).toBe('sbb!@one') | ||
expect(rot13('sbb!@one')).toBe('foo!@bar') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('') | ||
const output = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'.split('') | ||
const lookup = input.reduce<Record<string, string>>((m, k, i) => { | ||
m[k] = output[i] | ||
return m | ||
}, Object.create(null)) | ||
|
||
/** | ||
* 回转 13 位替换式密码。 | ||
* | ||
* @param str 原文 | ||
* @see https://zh.wikipedia.org/wiki/ROT13 | ||
* @example | ||
* ```typescript | ||
* rot13('hello world') // => 'uryyb jbeyq' | ||
* ``` | ||
*/ | ||
export function rot13(str: string): string { | ||
let res = '' | ||
for (let i = 0, len = str.length; i < len; i++) { | ||
const char = str.charAt(i) | ||
res += lookup[char] || char | ||
} | ||
return res | ||
} |