Skip to content

Commit

Permalink
add js-object, js-array; bump 0.3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed May 5, 2021
1 parent 1f5163d commit ddcc070
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_runner"
version = "0.3.7"
version = "0.3.8"
authors = ["jiyinyiyong <jiyinyiyong@gmail.com>"]
edition = "2018"
license = "MIT"
Expand Down
19 changes: 19 additions & 0 deletions calcit/snapshots/test-js.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,31 @@
d 5
assert= 13 $ + a b c d

|test-collection $ quote
fn ()
log-title "|Testing quick collection syntax"

&let
a $ js-array 1 2 3 4
assert= 4 $ .-length a
assert= 1 $ aget a 0
assert= 4 $ aget a 3
assert= nil $ aget a 4
&let
b $ js-object (:a 1) (|b 2) (:c 3)
assert= 1 $ .-a b
assert= 2 $ .-b b
assert= 3 $ .-c b
assert= 2 $ aget b |b

|main! $ quote
defn main! ()
log-title "|Testing js"
test-js
test-let-example

test-collection

when (> 1 2)
raise (str "|error of math" 2 1)
raise "|base error"
Expand Down
25 changes: 25 additions & 0 deletions lib/calcit.procs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,31 @@ export let format_to_lisp = (x: CrDataValue): string => {
}
};

/** for quickly creating js Array */
export let js_array = (...xs: CrDataValue[]): CrDataValue[] => {
return xs;
};

export let _AND_js_object = (...xs: CrDataValue[]): Record<string, CrDataValue> => {
if (xs.length % 2 !== 0) {
throw new Error("&js-object expects even number of arguments");
}
var ret: Record<string, CrDataValue> = {}; // object
let halfLength = xs.length >> 1;
for (let idx = 0; idx < halfLength; idx++) {
let k = xs[idx << 1];
let v = xs[(idx << 1) + 1];
if (typeof k === "string") {
ret[k] = v;
} else if (k instanceof CrDataKeyword) {
ret[turn_string(k)] = v;
} else {
throw new Error("Invalid key for js Object");
}
}
return ret;
};

/** notice, Nim version of format-time takes format */
export let format_time = (timeSecNumber: number, format?: string) => {
if (format != null) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@calcit/procs",
"version": "0.3.7",
"version": "0.3.8",
"main": "./lib/calcit.procs.js",
"devDependencies": {
"@types/node": "^15.0.1",
Expand Down
2 changes: 2 additions & 0 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ pub fn is_js_syntax_procs(s: &str) -> bool {
| "extract-cirru-edn"
| "foldl"
| "instance?"
| "&js-object"
| "js-array"
| "load-console-formatter!"
| "printable"
| "new"
Expand Down
12 changes: 9 additions & 3 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@
let[] (k v) a
assoc xs k v
raise "|coll-append expected a collection"

|empty $ quote
defn empty (x)
if (list? x) ([])
Expand Down Expand Up @@ -650,6 +650,12 @@
ys $ concat & xs
quote-replace $ &{} ~@ys

|js-object $ quote
defmacro js-object (& xs)
&let
ys $ concat & xs
quote-replace $ &js-object ~@ys

|%{} $ quote
defmacro %{} (R & xs)
&let
Expand Down Expand Up @@ -898,7 +904,7 @@
and (list? result) (&= 2 (count result))
let[] (k2 v2) result
assoc acc k2 v2

|either $ quote
defmacro either (x y)
quote-replace $ if (nil? ~x) ~y ~x
Expand Down Expand Up @@ -1068,7 +1074,7 @@
|conj $ quote
defn conj (xs a)
append xs a

|turn-str $ quote
defn turn-str (x) (turn-string x)

Expand Down

0 comments on commit ddcc070

Please sign in to comment.