mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-22 07:46:16 -04:00
ESM: Ported SeqUtils operations
This commit is contained in:
parent
66c768fe31
commit
d327dd47b2
7 changed files with 447 additions and 0 deletions
65
src/core/operations/CountOccurrences.mjs
Normal file
65
src/core/operations/CountOccurrences.mjs
Normal file
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Operation from "../Operation";
|
||||
import Utils from "../Utils";
|
||||
|
||||
/**
|
||||
* Count occurrences operation
|
||||
*/
|
||||
class CountOccurrences extends Operation {
|
||||
|
||||
/**
|
||||
* CountOccurrences constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "Count occurrences";
|
||||
this.module = "Default";
|
||||
this.description = "Counts the number of times the provided string occurs in the input.";
|
||||
this.inputType = "string";
|
||||
this.outputType = "number";
|
||||
this.args = [
|
||||
{
|
||||
"name": "Search string",
|
||||
"type": "toggleString",
|
||||
"value": "",
|
||||
"toggleValues": ["Regex", "Extended (\\n, \\t, \\x...)", "Simple string"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {number}
|
||||
*/
|
||||
run(input, args) {
|
||||
let search = args[0].string;
|
||||
const type = args[0].option;
|
||||
|
||||
if (type === "Regex" && search) {
|
||||
try {
|
||||
const regex = new RegExp(search, "gi"),
|
||||
matches = input.match(regex);
|
||||
return matches.length;
|
||||
} catch (err) {
|
||||
return 0;
|
||||
}
|
||||
} else if (search) {
|
||||
if (type.indexOf("Extended") === 0) {
|
||||
search = Utils.parseEscapedChars(search);
|
||||
}
|
||||
return input.count(search);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default CountOccurrences;
|
Loading…
Add table
Add a link
Reference in a new issue