-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'jarmovanlenthe-master'
- Loading branch information
Showing
5 changed files
with
166 additions
and
1 deletion.
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,63 @@ | ||
/** | ||
* @author Jarmo van Lenthe [github.com/jarmovanlenthe] | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation"; | ||
import Utils from "../Utils"; | ||
import {DELIM_OPTIONS} from "../lib/Delim"; | ||
import OperationError from "../errors/OperationError"; | ||
|
||
/** | ||
* A1Z26 Cipher Decode operation | ||
*/ | ||
class A1Z26CipherDecode extends Operation { | ||
|
||
/** | ||
* A1Z26CipherDecode constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "A1Z26 Cipher Decode"; | ||
this.module = "Ciphers"; | ||
this.description = "Converts alphabet order numbers into their corresponding alphabet character.<br><br>e.g. <code>1</code> becomes <code>a</code> and <code>2</code> becomes <code>b</code>."; | ||
this.infoURL = ""; | ||
this.inputType = "string"; | ||
this.outputType = "string"; | ||
this.args = [ | ||
{ | ||
name: "Delimiter", | ||
type: "option", | ||
value: DELIM_OPTIONS | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* @param {string} input | ||
* @param {Object[]} args | ||
* @returns {string} | ||
*/ | ||
run(input, args) { | ||
const delim = Utils.charRep(args[0] || "Space"); | ||
|
||
if (input.length === 0) { | ||
return []; | ||
} | ||
|
||
const bites = input.split(delim); | ||
let latin1 = ""; | ||
for (let i = 0; i < bites.length; i++) { | ||
if (bites[i] < 1 || bites[i] > 26) { | ||
throw new OperationError("Error: all numbers must be between 1 and 26."); | ||
} | ||
latin1 += Utils.chr(parseInt(bites[i], 10) + 96); | ||
} | ||
return latin1; | ||
} | ||
|
||
} | ||
|
||
export default A1Z26CipherDecode; |
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,61 @@ | ||
/** | ||
* @author Jarmo van Lenthe [github.com/jarmovanlenthe] | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation"; | ||
import Utils from "../Utils"; | ||
import {DELIM_OPTIONS} from "../lib/Delim"; | ||
|
||
/** | ||
* A1Z26 Cipher Encode operation | ||
*/ | ||
class A1Z26CipherEncode extends Operation { | ||
|
||
/** | ||
* A1Z26CipherEncode constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "A1Z26 Cipher Encode"; | ||
this.module = "Ciphers"; | ||
this.description = "Converts alphabet characters into their corresponding alphabet order number.<br><br>e.g. <code>a</code> becomes <code>1</code> and <code>b</code> becomes <code>2</code>.<br><br>Non-alphabet characters are dropped."; | ||
this.infoURL = ""; | ||
this.inputType = "string"; | ||
this.outputType = "string"; | ||
this.args = [ | ||
{ | ||
name: "Delimiter", | ||
type: "option", | ||
value: DELIM_OPTIONS | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* @param {string} input | ||
* @param {Object[]} args | ||
* @returns {string} | ||
*/ | ||
run(input, args) { | ||
const delim = Utils.charRep(args[0] || "Space"); | ||
let output = ""; | ||
|
||
const sanitizedinput = input.toLowerCase(), | ||
charcode = Utils.strToCharcode(sanitizedinput); | ||
|
||
for (let i = 0; i < charcode.length; i++) { | ||
const ordinal = charcode[i] - 96; | ||
|
||
if (ordinal > 0 && ordinal <= 26) { | ||
output += ordinal.toString(10) + delim; | ||
} | ||
} | ||
return output.slice(0, -delim.length); | ||
} | ||
|
||
} | ||
|
||
export default A1Z26CipherEncode; |
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