CyberChef/src/node/apiUtils.mjs

87 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-04-20 13:23:20 +02:00
/**
2018-07-06 13:54:19 +02:00
* Utility functions for the node environment
2018-04-20 13:23:20 +02:00
*
* @author d98762625 [d98762625@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
2018-12-07 16:46:05 +01:00
/**
* someName => Somename
*
* @param {String} str = string to be altered
* @returns {String}
*/
const capitalise = function capitalise(str) {
// Don't edit names that start with 2+ caps
if (/^[A-Z0-9]{2,}/g.test(str)) {
return str;
}
// reserved. Don't change for now.
if (str === "Return") {
return str;
}
return `${str.charAt(0).toUpperCase()}${str.substr(1).toLowerCase()}`;
};
2018-06-19 17:41:16 +02:00
/**
* SomeName => someName
* @param {String} name - string to be altered
* @returns {String} decapitalised
*/
2018-12-07 16:46:05 +01:00
export function decapitalise(str) {
// Don't decapitalise str that start with 2+ caps
if (/^[A-Z0-9]{2,}/g.test(str)) {
return str;
2018-06-19 17:41:16 +02:00
}
// reserved. Don't change for now.
2018-12-07 16:46:05 +01:00
if (str === "Return") {
return str;
2018-06-19 17:41:16 +02:00
}
2018-12-07 16:46:05 +01:00
return `${str.charAt(0).toLowerCase()}${str.substr(1)}`;
2018-06-19 17:41:16 +02:00
}
2018-12-07 16:46:05 +01:00
/**
* Remove strings surrounded with [] from the given array.
*/
export function removeSubheadingsFromArray(array) {
if (Array.isArray(array)) {
return array.filter((i) => {
if (typeof i === "string") {
return !i.match(/^\[[\s\S]*\]$/);
}
return true;
});
}
}
2018-04-20 13:23:20 +02:00
/**
2018-07-20 14:59:52 +02:00
* Remove spaces, make lower case.
* @param str
2018-04-20 13:23:20 +02:00
*/
2018-07-06 13:54:19 +02:00
export function sanitise(str) {
return str.replace(/ /g, "").toLowerCase();
2018-04-20 13:23:20 +02:00
}
2018-12-07 16:46:05 +01:00
/**
2019-10-16 16:38:20 +02:00
* something like this => somethingLikeThis
2018-12-07 16:46:05 +01:00
* ABC a sentence => ABCASentence
*/
export function sentenceToCamelCase(str) {
return str.split(" ")
.map((s, index) => {
if (index === 0) {
return decapitalise(s);
}
return capitalise(s);
})
.reduce((prev, curr) => `${prev}${curr}`, "");
}