Skip to content

Commit

Permalink
add one or two funcs
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
metal0 authored Oct 9, 2023
1 parent e1996e3 commit 13565e2
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 0 deletions.
14 changes: 14 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"@silvia-odwyer/photon-node": "^0.3.1",
"baseroo": "^1.2.0",
"bufferutil": "^4.0.3",
"clinic": "^13.0.0",
"cors": "^2.8.5",
Expand Down
121 changes: 121 additions & 0 deletions backend/src/plugins/Tags/templateFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ export const TemplateFunctions: TemplateFunction[] = [
arguments: ["number"],
examples: ["floor(1.2345)"],
},
{
name: "abs",
description: "Returns the absolute of a number",
returnValue: "number",
arguments: ["number"],
examples: ["abs(-1.2345)"],
},
{
name: "add",
description: "Adds two or more numbers",
Expand Down Expand Up @@ -177,13 +184,120 @@ export const TemplateFunctions: TemplateFunction[] = [
arguments: ["number1", "number2", "..."],
examples: ["div(6, 2)"],
},
{
name: "sqrt",
description: "Calculates the square root of a number",
returnValue: "number",
arguments: ["number"],
examples: ["sqrt(5)"],
},
{
name: "cbrt",
description: "Calculates the cubic root of a number",
returnValue: "number",
arguments: ["number"],
examples: ["cbrt(50)"],
},
{
name: "exp",
description: "Raises a number to the power of another number",
returnValue: "number",
arguments: ["base", "power"],
examples: ["exp(2, 3)"],
},
{
name: "sin",
description: "Returns the sine of a number in radians",
returnValue: "number",
arguments: ["radians"],
examples: ["sin(2)"],
},
{
name: "sinh",
description: "Returns the hyperbolic sine of a number",
returnValue: "number",
arguments: ["number"],
examples: ["sinh(1)"],
},
{
name: "tan",
description: "Returns the tangent of a number in radians",
returnValue: "number",
arguments: ["radians"],
examples: ["tan(1.5)"],
},
{
name: "tanh",
description: "Returns the hyperbolic tangent of a number in radians",
returnValue: "number",
arguments: ["radians"],
examples: ["tanh(1.5)"],
},
{
name: "cos",
description: "Returns the cosine of a number in radians",
returnValue: "number",
arguments: ["radians"],
examples: ["cos(1.5)"],
},
{
name: "cosh",
description: "Returns the hyperbolic cosine of a number in radians",
returnValue: "number",
arguments: ["radians"],
examples: ["cosh(1.5)"],
},
{
name: "hypot",
description: "Returns the square root of the sum of squares of it's arguments",
returnValue: "number",
arguments: ["number1", "number2", "..."],
examples: ["hypot(3, 4, 5, 6)"],
},
{
name: "log",
description: "Returns the base e logarithm of a number",
returnValue: "number",
arguments: ["number"],
examples: ["log(3)"],
},
{
name: "log2",
description: "Returns the base 2 logarithm of a number",
returnValue: "number",
arguments: ["number"],
examples: ["log2(3)"],
},
{
name: "log10",
description: "Returns the base 10 logarithm of a number",
returnValue: "number",
arguments: ["number"],
examples: ["log10(3)"],
},
{
name: "log1p",
description: "Returns the base e logarithm of a 1 + number",
returnValue: "number",
arguments: ["number"],
examples: ["log1p(3)"],
},
{
name: "const",
description: "Get value of math constants",
returnValue: "number",
arguments: ["constant_name"],
examples: [
"const(pi)",
"const(e)",
"const(sqrt2)",
"const(sqrt0.5)",
"const(ln10)",
"const(ln2)",
"const(log10e)",
"const(log2e)",
],
},
{
name: "cases",
description: "Returns the argument at position",
Expand Down Expand Up @@ -212,6 +326,13 @@ export const TemplateFunctions: TemplateFunction[] = [
arguments: ["string"],
examples: ['trim_text("<@!344837487526412300>")'],
},
{
name: "convert_base",
description: "Converts a value from <origin> base to <dest> base",
returnValue: "string",
arguments: ["value", "origin", "dest"],
examples: ['convert_base("256", "10", "2")'],
},
{
name: "tag",
description: "Gets the value of another defined tag",
Expand Down
80 changes: 80 additions & 0 deletions backend/src/templateFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { convertBase } from "baseroo";
import seedrandom from "seedrandom";
import { get, has } from "./utils";

Expand Down Expand Up @@ -434,6 +435,10 @@ const baseValues = {
if (isNaN(arg)) return 0;
return Math.ceil(parseFloat(arg));
},
abs(arg) {
if (isNaN(arg)) return 0;
return Math.abs(parseFloat(arg));
},
add(...args) {
return args.reduce((result, arg) => {
if (isNaN(arg)) return result;
Expand Down Expand Up @@ -465,6 +470,73 @@ const baseValues = {
if (isNaN(base) || isNaN(power)) return 0;
return Math.pow(parseFloat(base), parseFloat(power));
},
sqrt(arg) {
if (isNaN(arg)) return 0;
return Math.sqrt(parseFloat(arg));
},
cbrt(arg) {
if (isNaN(arg)) return 0;
return Math.cbrt(parseFloat(arg));
},
sin(radians) {
if (isNaN(radians)) return 0;
return Math.sin(parseFloat(radians));
},
sinh(arg) {
if (isNaN(arg)) return 0;
return Math.sinh(parseFloat(arg));
},
tan(arg) {
if (isNaN(arg)) return 0;
return Math.tan(parseFloat(arg));
},
tanh(arg) {
if (isNaN(arg)) return 0;
return Math.tanh(parseFloat(arg));
},
log(arg) {
if (isNaN(arg)) return 0;
return Math.log(parseFloat(arg));
},
log2(arg) {
if (isNaN(arg)) return 0;
return Math.log2(parseFloat(arg));
},
log10(arg) {
if (isNaN(arg)) return 0;
return Math.log10(parseFloat(arg));
},
log1p(arg) {
if (isNaN(arg)) return 0;
return Math.log1p(parseFloat(arg));
},
hypot(...args) {
if (!args.every((e) => !isNaN(e))) return ""; // TODO: Improve validation
return Math.hypot(...args.map((e) => parseFloat(e)));
},
cos(arg) {
if (isNaN(arg)) return 0;
return Math.cos(parseFloat(arg));
},
cosh(arg) {
if (isNaN(arg)) return 0;
return Math.cosh(parseFloat(arg));
},
const(str) {
// math constants lmao :joy:
const math_constants = {
pi: Math.PI,
e: Math.E,
sqrt2: Math.SQRT2,
"sqrt0.5": Math.SQRT1_2,
ln10: Math.LN10,
ln2: Math.LN2,
log10e: Math.LOG10E,
log2e: Math.LOG2E,
};
if (typeof str !== "string") return "";
return math_constants[str.toLowerCase()] ?? "";
},
map(obj, key) {
if (Array.isArray(obj)) {
return obj.map((tobj) => tobj[key]);
Expand All @@ -485,6 +557,14 @@ const baseValues = {
if (!str || typeof str !== "string") return "";
return str.replaceAll(/[^\d]+/g, "");
},
convert_base(value, from, to) {
try {
// :joy:
return convertBase(value, from, to);
} catch (_) {
return "";
}
},
};

export async function renderTemplate(
Expand Down

0 comments on commit 13565e2

Please sign in to comment.