Skip to content

Commit

Permalink
more details in to-calcit-data
Browse files Browse the repository at this point in the history
  • Loading branch information
tiye committed Aug 26, 2021
1 parent e449035 commit 044ecf0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
8 changes: 8 additions & 0 deletions calcit/snapshots/test-js.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
assert-detect identity $ instance? js/Number (new js/Number 1)
assert-detect not $ instance? js/String (new js/Number 1)

assert=
[] 1 ([] 2 3) (:: :quote ([] 'a 'b))
to-calcit-data $ js-array 1 ([] 2 3) (:: :quote ([] 'a 'b))

assert=
&{} |a 1 :b 2 |c $ [] 3 4
to-calcit-data $ &js-object |a 1 |:b 2 :c $ [] 3 4

|test-let-example $ quote
fn ()
log-title "|Testing code emitting of using let"
Expand Down
33 changes: 20 additions & 13 deletions ts-src/js-cirru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CalcitList } from "./js-list";
import { CalcitRecord } from "./js-record";
import { CalcitMap } from "./js-map";
import { CalcitSet } from "./js-set";
import { CalcitKeyword, CalcitSymbol, kwd } from "./calcit-data";
import { CalcitKeyword, CalcitSymbol, CalcitRecur, CalcitRef, kwd } from "./calcit-data";
import { CalcitTuple } from "./js-tuple";

type CirruEdnFormat = string | CirruEdnFormat[];
Expand Down Expand Up @@ -200,24 +200,20 @@ export let format_cirru_edn = (data: CalcitValue, useInline: boolean = true): st
};

export let to_calcit_data = (x: any, noKeyword: boolean = false): CalcitValue => {
if (x == null) {
return null;
}
if (typeof x === "number") {
return x;
}
if (x == null) return null;

if (typeof x === "number") return x;

if (typeof x === "string") {
if (!noKeyword && x[0] === ":" && x.slice(1).match(/^[\w\d_\?\!\-]+$/)) {
return kwd(x.slice(1));
}
return x;
}
if (x === true || x === false) {
return x;
}
if (typeof x === "function") {
return x;
}
if (x === true || x === false) return x;

if (typeof x === "function") return x;

if (Array.isArray(x)) {
var result: any[] = [];
x.forEach((v) => {
Expand All @@ -232,6 +228,17 @@ export let to_calcit_data = (x: any, noKeyword: boolean = false): CalcitValue =>
});
return new CalcitSet(result);
}

if (x instanceof CalcitList) return x;
if (x instanceof CalcitMap) return x;
if (x instanceof CalcitSet) return x;
if (x instanceof CalcitRecord) return x;
if (x instanceof CalcitRecur) return x;
if (x instanceof CalcitRef) return x;
if (x instanceof CalcitKeyword) return x;
if (x instanceof CalcitSymbol) return x;
if (x instanceof CalcitTuple) return x;

// detects object
if (x === Object(x)) {
let result: Array<[CalcitValue, CalcitValue]> = [];
Expand Down

0 comments on commit 044ecf0

Please sign in to comment.