diff options
| author | Akshay Nair <phenax5@gmail.com> | 2023-08-25 08:37:43 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2023-08-25 08:37:43 +0530 |
| commit | da25e0da579da33dffddd16b9a564f358205c43b (patch) | |
| tree | bf9c9986a9f03ed5c745d9b32a032bd2c7501a2c | |
| parent | 6e0f7c0a66117ea458c61bf18c4a721d90523448 (diff) | |
| download | css-everything-da25e0da579da33dffddd16b9a564f358205c43b.tar.gz css-everything-da25e0da579da33dffddd16b9a564f358205c43b.zip | |
feat: string concatentation + quote
Diffstat (limited to '')
| -rw-r--r-- | TODO.norg | 5 | ||||
| -rw-r--r-- | examples/todo-list/style.css | 4 | ||||
| -rw-r--r-- | src/eval.ts | 24 |
3 files changed, 28 insertions, 5 deletions
@@ -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}`)), }) } |
