aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--TODO.norg5
-rw-r--r--examples/todo-list/style.css4
-rw-r--r--src/eval.ts24
3 files changed, 28 insertions, 5 deletions
diff --git a/TODO.norg b/TODO.norg
index ba5412d..59fadbf 100644
--- a/TODO.norg
+++ b/TODO.norg
@@ -1,11 +1,12 @@
* Now
- (x) Documentation
- - ( ) string concatenation/interpolation
+ - (x) string concatenation/interpolation
+ - (x) some way to de-quotify values?
+ - (x) re-quotify value
- ( ) analog + digital clock example
- ( ) `--cssx-use-properties: --parent-prop;` to trigger nested property update
- ( ) Update `--cssx-text` on update
- ( ) FFI interface to declare functions
- - ( ) some way to de-quotify values?
- ( ) `has-class`
- ( ) `add-class` & `remove-class` should use self if id is not specified?
diff --git a/examples/todo-list/style.css b/examples/todo-list/style.css
index cda7003..ed9875c 100644
--- a/examples/todo-list/style.css
+++ b/examples/todo-list/style.css
@@ -18,6 +18,10 @@ body * { box-sizing: border-box; }
border-radius: 5px;
overflow: hidden;
+ --some-str: "Hello";
+
+ --cssx-on-mount: js-eval(string("alert('", quotify(get-var(--some-str)), "')"));
+
/* --my-func: */
/* js-eval(get-var(--js)) */
/* delay(1s) */
diff --git a/src/eval.ts b/src/eval.ts
index 8112353..e8bacb9 100644
--- a/src/eval.ts
+++ b/src/eval.ts
@@ -80,9 +80,12 @@ export const evalExpr = async (
_: async _ => EvalValue.Void(),
})
+const QUOTE_REGEX = /^['"](.*)(?=['"]$)['"]$/g
+const dequotify = (s: string) => s.replace(QUOTE_REGEX, '$1')
+
export const evalValueToString = (val: EvalValue): string | undefined =>
match<string | undefined, EvalValue>(val, {
- String: s => s.replace(/(^'|")|('|"$)/g, ''),
+ String: s => dequotify(s),
Boolean: b => `${b}`,
Number: n => `${n}`,
_: () => undefined,
@@ -98,7 +101,7 @@ const evalValueToNumber = (val: EvalValue): number | undefined =>
const evalValueToBoolean = (val: EvalValue): boolean =>
match<boolean, EvalValue>(val, {
- String: s => !['false', '', '0'].includes(s.replace(/(^'|")|('|"$)/g, '')),
+ String: s => !['false', '', '0'].includes(dequotify(s)),
Boolean: b => b,
Number: n => !!n,
_: () => false,
@@ -154,7 +157,9 @@ const getFunctions = (
return EvalValue.Void()
},
'js-eval': async () => {
- const js = evalValueToString(await evalExpr(args[0], actions))
+ console.log(args[0])
+ const js = await evalExprAsString(args[0], actions)
+ console.log(js)
return js && (await actions.jsEval(js))
},
@@ -297,6 +302,19 @@ const getFunctions = (
return EvalValue.Void()
},
+ string: async () => {
+ const str = await Promise.all(args.map(a => evalExprAsString(a, actions)))
+ return EvalValue.String(str.filter(Boolean).join(''))
+ },
+ quotify: async () => {
+ const str = await evalExprAsString(args[0], actions)
+ return EvalValue.String(`'${str}'`)
+ },
+ dequotify: async () => {
+ const str = await evalExprAsString(args[0], actions)
+ return EvalValue.String(dequotify(str || ''))
+ },
+
_: () => Promise.reject(new Error(`Not implemented: ${name}`)),
})
}