-
Notifications
You must be signed in to change notification settings - Fork 1
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
6 changed files
with
90 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Base from '../base'; | ||
|
||
export function envFn(v: string, fallback?: string | number) { | ||
const envVal = Base.envVariables[v.toLowerCase()]; | ||
return (typeof envVal === 'function' ? envVal() : envVal) ?? fallback; | ||
} |
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,16 @@ | ||
import { toLength } from '../utils/values'; | ||
|
||
export const minFn = Math.min; | ||
export const maxFn = Math.max; | ||
|
||
export function calcFn(equation = '') { | ||
const splitValues = equation.split(/([+\-*/])/g); | ||
const parsedEquation = splitValues.map((v) => v.trim()).map((v) => (v.match(/[+\-*/]/) ? v : toLength(v) || v)); | ||
|
||
// eslint-disable-next-line no-eval | ||
return eval(parsedEquation.join('')); | ||
} | ||
|
||
export function clampFn(val: number, min = 0, max = Number.MAX_VALUE) { | ||
return Math.min(Math.max(val, min), max); | ||
} |
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,57 @@ | ||
import { envFn } from '../functional-notations/env'; | ||
import { minFn, maxFn, calcFn, clampFn } from '../functional-notations/math'; | ||
import { toLength } from './values'; | ||
|
||
type FunctionalNotation = (...args: any) => string | number | undefined; | ||
|
||
const functions: { [key: string]: FunctionalNotation } = { | ||
min: minFn, | ||
max: maxFn, | ||
calc: (e: string) => calcFn(e), | ||
clamp: (min: number, val: number, max: number) => clampFn(val, min, max), | ||
env: (v: string, o_fallback?: string | number) => envFn(v, o_fallback), | ||
}; | ||
|
||
export function functionalNotation(value: string) { | ||
if (typeof value === 'string') { | ||
const funcName = value.split('(')[0]; | ||
if (Object.keys(functions).includes(funcName)) { | ||
const values = value.substring(value.indexOf('(') + 1, value.lastIndexOf(')')); | ||
|
||
if (!values) { | ||
return null; | ||
} | ||
|
||
const isArgs = values?.includes(','); | ||
const valueArgs = | ||
values | ||
?.replace(/,\s*(?=[^)^)]*(?:\(|\(|$))/g, '##') | ||
?.split('##') | ||
?.map((v) => toLength(v.trim())) || []; | ||
|
||
const func = functions?.[funcName as keyof typeof functions]; | ||
const args = getFunctionParams(func).filter((p) => !p.startsWith('o_')); | ||
|
||
if (args.length > 0 && args.length !== valueArgs.length) { | ||
console.log(func.prototype); | ||
console.error( | ||
`Function "${funcName}" is missing arguments. Expected ${func.length} but received ${valueArgs.length}`, | ||
); | ||
return null; | ||
} | ||
|
||
return isArgs ? func?.(...valueArgs) : func?.(values); | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm; | ||
const ARGUMENT_NAMES = /([^\s,]+)/g; | ||
|
||
function getFunctionParams(func: Function) { | ||
var fnStr = func.toString().replace(STRIP_COMMENTS, ''); | ||
var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARGUMENT_NAMES); | ||
if (result === null) result = []; | ||
return result; | ||
} |
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