@gholk/tsjson
Advanced tools
Comparing version 3.0.3 to 3.1.0
@@ -5,2 +5,13 @@ #!/usr/bin/env node | ||
let strf = x => JSON.stringify(x, null, ' ') | ||
let stro = null | ||
const stroLoad = import('stringify-object') | ||
.then(x => { | ||
stro = x.default | ||
strf = x => stro(x, { | ||
indent: ' ', inlineCharacterLimit: 40 | ||
}) | ||
}) | ||
.catch(e => {}) | ||
export function isMain(meta = import.meta, p = process) { | ||
@@ -29,3 +40,3 @@ return meta.url.replace(/^file:../, '') == p.argv[1] | ||
if (isMain()) { | ||
getInput().then(s => { | ||
stroLoad.then(() => getInput()).then(s => { | ||
s = trimChar(s, '`') | ||
@@ -36,7 +47,7 @@ let json = null | ||
catch (err1) {parseError = err1} | ||
if (json) return console.log(JSON.stringify(json, null, ' ')) | ||
if (json) return console.log(strf(json)) | ||
tsj.bracket = '[ ]'.split(' ') | ||
try {json = eval('tsj.j `' + s + '`')} | ||
catch (err2) {throw parseError} | ||
console.log(JSON.stringify(json, null, ' ')) | ||
console.log(strf(json)) | ||
}).catch(e => { | ||
@@ -43,0 +54,0 @@ console.error(e) |
{ | ||
"name": "@gholk/tsjson", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "A Template literal converts s-expression to json which support variable embedding.", | ||
@@ -13,10 +13,10 @@ "main": "index.esm.js", | ||
"devDependencies": { | ||
"@gholk/tt-logger": "^0.3.1", | ||
"cheerio": "^1.0.0-rc.10" | ||
"@gholk/tt-logger": "^0.3.1" | ||
}, | ||
"scripts": { | ||
"test": "node test/index.js && node test/html.js", | ||
"test": "node test/index.js", | ||
"build": "bin/build.js index.js tsjson lib/macro-more.js macroMore;\n bin/tsj-html.js < playground.sxml.js > playground.html", | ||
"clean": "rm *.browser.js *.esm.js *.cjs;\n cd lib && rm *.browser.js *.esm.js *.cjs", | ||
"bench": "echo no cache; env COUNT=1000 node test/time.js; echo with cache; env COUNT=1000 CACHE=1 node test/time.js" | ||
"bench": "echo no cache; env COUNT=1000 node test/time.js; echo with cache; env COUNT=1000 CACHE=1 node test/time.js", | ||
"build-stro": "(echo 'var stringifyObject='; asbundle node_modules/stringify-object/index.js) > stringify-object.browser.js" | ||
}, | ||
@@ -35,3 +35,7 @@ "type": "module", | ||
"author": "gholk", | ||
"license": "AGPL-3.0-or-later" | ||
"license": "AGPL-3.0-or-later", | ||
"optionalDependencies": { | ||
"stringify-object": "^5.0.0", | ||
"cheerio": "^1.0.0-rc.10" | ||
} | ||
} |
@@ -5,4 +5,2 @@ `[doctype html] | ||
[h1 "tsjson play ground"] | ||
[script [:src lib/macro-more.browser.js]] | ||
[script [:src index.browser.js]] | ||
[macro checkbox ${l => { | ||
@@ -19,2 +17,62 @@ const t = tsj.domTool | ||
}}] | ||
[macro radio ${l => { | ||
const t = tsj.domTool | ||
let i = 1 | ||
if (t.isAttributeDict(l[i])) i++ | ||
const group = l.splice(i, 1) | ||
t['macro:checkbox'](l) | ||
const d = l[1][1] | ||
d.type = 'radio' | ||
d.value = d.name | ||
d.name = group | ||
}}] | ||
[style | ||
["textarea:not([hidden])" | ||
display block | ||
width 100% | ||
height 8em] | ||
[textarea.sexp height 15em] | ||
[.warn color red]] | ||
[textarea [:class sexp :name sexp]] | ||
[textarea [:hidden '' :class [example sxml]] " | ||
(input (:placeholder username)) | ||
(button (:onclick \${e => { | ||
const b = e.target | ||
let s = \`hey \${b.previousSibling.value || 'unnamed'}!\` | ||
if (!b.nextElementSibling.firstChild.checked) s += '\\nplease enable cookie' | ||
alert(s)}} | ||
:style (color red background yellow | ||
margin 0.5em | ||
border (solid 1px black))) | ||
greet) | ||
" | ||
[do ${() => | ||
'(macro checkbox ${' + tsj.domTool['macro:checkbox'].toString() + '})' | ||
}] | ||
" | ||
(checkbox accept-cookie) | ||
"] | ||
[textarea [:hidden '' :class [example tabular]] | ||
"(name id summary) | ||
book1 1 'the first book' | ||
book2 2 'another book'"] | ||
[textarea [:hidden '' :class [example json]] | ||
'1 2 a b ("str1" "str2") (:ok false :val null :re \${/[Yy]es/}) | ||
\${x => x*2} @\${[1, 2, {aaa: function () { return "a"}}]}'] | ||
[button [:onclick "ui.transform()"] transform] | ||
[button [:onclick "ui.loadExample()"] example] | ||
[radio input-type json 'json mode'] | ||
[radio [:checked ''] input-type sxml 'sxml mode'] | ||
[radio input-type tabular 'unzip object table'] | ||
[label [input [:type checkbox :name square-bracket]] | ||
"square bracket"] | ||
[checkbox [:checked ''] append-html "append html directly"] | ||
[output [textarea '<input placeholder="username"><button>greet</button><label><input type="checkbox" name="accept-cookie">accept cookie</label>'] | ||
[div]] | ||
[script [:src lib/macro-more.browser.js]] | ||
[script [:src index.browser.js]] | ||
[script [:src stringify-object.browser.js]] | ||
[script ${() => { | ||
@@ -29,3 +87,11 @@ var tsj = tsjson | ||
} | ||
function stringify(x) { | ||
if (typeof stringifyObject == 'function') { | ||
return stringifyObject(x, { | ||
indent: ' ', | ||
inlineCharacterLimit: 40 | ||
}) | ||
} | ||
return JSON.stringify(x, null, ' ') | ||
} | ||
const ui = { | ||
@@ -35,3 +101,4 @@ transform(option) { | ||
let m = 'j' | ||
if (option.html) m = 'html' | ||
if (option.type == 'sxml') m = 'html' | ||
if (option.type == 'tabular') m = 'jtable' | ||
const source = $('textarea.sexp').value | ||
@@ -41,3 +108,3 @@ tsjson.bracket = option.bracket | ||
$('output textarea').value = this.xToString(res) | ||
if (option.html && option.append) { | ||
if (option.type == 'sxml' && option.append) { | ||
const div = $('output div') | ||
@@ -50,3 +117,3 @@ div.textContent = '' | ||
const {domTool} = tsjson | ||
if (!domTool.isNode(x)) return JSON.stringify(x, null, ' ') | ||
if (!domTool.isNode(x)) return stringify(x) | ||
const div = domTool.createNode('div') | ||
@@ -58,3 +125,3 @@ domTool.append(div, x.cloneNode(true)) | ||
const o = {} | ||
o.html = $('[name=sxml-mode]').checked | ||
o.type = $('[name=input-type]:checked').value | ||
const sq = $('[name=square-bracket]').checked | ||
@@ -64,39 +131,20 @@ o.bracket = (sq ? '[ ]' : '( )').split(' ') | ||
return o | ||
}, | ||
loadExample() { | ||
const o = this.getOption() | ||
$('textarea.sexp').value = $(`.example.${o.type}`).value | ||
}, | ||
init() { | ||
this.loadExample() | ||
if (typeof stringifyObject == 'undefined') { | ||
$('output').appendChild(tsjson.html ` | ||
(p (:class warn) " | ||
npm package stringify-object (stringify-object.browser.js) not found; | ||
stringify with JSON.stringify; non-json value will not stringify.")`) | ||
} | ||
} | ||
} | ||
ui.init() | ||
}}] | ||
[style | ||
[textarea | ||
display block | ||
width 100% | ||
height 8em] | ||
[textarea.sexp height 15em]] | ||
[textarea [:class sexp] " | ||
(input (:placeholder username)) | ||
(button (:onclick \${e => { | ||
const b = e.target | ||
let s = \`hey \${b.previousSibling.value || 'unnamed'}!\` | ||
if (!b.nextElementSibling.firstChild.checked) s += '\\nplease enable cookie' | ||
alert(s)}} | ||
:style (color red background yellow | ||
margin 0.5em | ||
border (solid 1px black))) | ||
greet) | ||
" | ||
[do ${() => | ||
'(macro checkbox ${' + tsj.domTool['macro:checkbox'].toString() + '})' | ||
}] | ||
" | ||
(checkbox accept-cookie) | ||
"] | ||
[button [:onclick "ui.transform()"] transform] | ||
[label [input [:type checkbox :checked '' :name sxml-mode]] | ||
sxml] | ||
[label [input [:type checkbox :name square-bracket]] | ||
"square bracket"] | ||
[checkbox [:checked ''] append-html "append html directly"] | ||
[output [textarea '<input placeholder="username"><button>greet</button><label><input type="checkbox" name="accept-cookie">accept cookie</label>'] | ||
[div]]]] | ||
]] | ||
` |
@@ -659,5 +659,5 @@ # Template S-expression to JSON | ||
to import as es-module, import the `*.esm.js` file if possible. | ||
to run in browser, load the `*.browser.js` if exists, | ||
to run in browser without es-module, load the `*.browser.js` (if exists), | ||
and you will have a `tsjson` global variable. | ||
the `*.js` is es-module or common-js module if `*.esm.js` exist. | ||
the `*.js` is es-module, or common-js module if `*.esm.js` exist. | ||
@@ -676,3 +676,11 @@ static js file on gitlab: | ||
cli tools accept arguments or read from stdin if no argument. | ||
to enable print non-json value (like function or regexp), | ||
you can install the optional dependency [stringify-object]. | ||
[stringify-object]: https://www.npmjs.com/package/stringify-object | ||
(to enable stringify-object in [playground.html] , | ||
install and `npm run build-stro`, | ||
or just wget it from online playground.) | ||
if the inputs first non-space char is backquote `` ` ``, | ||
@@ -703,3 +711,3 @@ then it will chop the first and the last non-space chars. | ||
so you can use `macro` and others macro. | ||
you can install cheerio with `npm i -D` | ||
cheerio is a optional dependency for tsjson. | ||
@@ -745,3 +753,2 @@ ```term | ||
so user can patch them | ||
* to js literal npm package | ||
* explicit cache |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115303
1
2949
751
2