Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
module.exports=((R,T,Z,A,B,C,D,E,F,G,H,I,J)=>{
R={};T=()=>{throw ""};Y=(r,c)=>r.test(c);Z=x=>(typeof x)[0]=="f"
A=c=>$=>$(c||R,c=>Y(/\s/,c)?A("")($):c)
B=c=>$=>$(c||R,c=>Y(/[0-9]/,c)?$(B("")($),s=>[s[0],c+s[1]]):[c,""])
C=c=>$=>$(c||R,c=>$(c=="-"?["","-"]:[c,""],([c,s])=>$(c||R,c=>$(c=="0"?["","0"]:Y(/[1-9]/,c)?B(c)($):T(),([c,b])=>$(c||R,c=>$(c=="."?$(B("")($),([c,s])=>[c,"."+s]):[c,""],([c,f])=>$(c||R,c=>$(Y(/[eE]/,c)?$($(R,c=>Y(/[+-]/,c)?["","e"+c]:[c,"e"]),([c,es])=>$(B(c)($),([c,s])=>[c,es+s])):[c,""],([c,e])=>[c,Number(s+b+f+e)]))))))))
D=$=>$(R,h=>Y(/[0-9a-fA-F]/,h)?parseInt(h,16):T())
E=$=>$($(R,c=>c!=="\\"?c:$(R,c=>Y(/[\\"\/bfnrt]/,c)?({b:"\b",f:"\f",n:"\n",r:"\r",t:"\t","\\":"\\","/":"/",'"':''})[c]:Y(/u/,c)?$(D($),a=>$(D($),b=>$(D($),c=>$(D($),d=>String.fromCharCode(a*4096+b*256+c*16+d))))):T())),c=>c=='"'?"":$(E($),s=>(c||'"')+s))
F=$=>{var a=[],g=c=>$=>$(c||R,c=>c=="]"?a:$(H(c)($),([c,v])=>(a.push(v),$(A(c)($),c=>Y(/[,\]]/,c)?g(c==","?"":c)($):T()))));return g("")($)}
G=$=>{var o={},g=c=>$=>$(c||R,c=>c=="}"?o:$(A(c)($),c=>c!=='"'?T():$(E($),k=>$(A("")($),c=>c!==':'?T():$(H("")($),([c,v])=>(o[k]=v,$(A(c)($),c=>Y(/[,}]/,c)?g(c==","?"":c)($):T())))))));return g("")($)}
H=c=>$=>$(A(c)($),c=>c=="["?$(F($),v=>["",v]):c=="{"?$(G($),v=>["",v]):c=='"'?$(E($),v=>["",v]):c=='t'?$(K("rue")(true)($),v=>["",v]):c=='f'?$(K("alse")(false)($),v=>["",v]):C(c)($))
I=p=>{var $=(a,b)=>a==R?c=>b(c):Z(a)?c=>$(a(c),b):b(a);return p($)}
J=p=>j=>{var s=I(p),f=k=>{for(var i=0;k[i];++i){try{s=s(k[i])}catch(e){s=I(p),k=""}!Z(s)&&(j(s[1]),s=I(p))}return f};return f}
K=s=>r=>$=>!s?r:$(R,c=>c!==s[0]?T():K(s.slice(1))(r)($));
return J(H(""))
})()
+20
View File
@@ -0,0 +1,20 @@
const njsp = require("./../dist/index.js");
// Callback is called when there is a complete JSON
const parse = njsp((json) => console.log(json));
parse('[1,2,3,4]');
parse('[1,2');
parse(',3,4]');
parse('[::invalid_json_is_ignored::]');
parse('{"pos": {"x":');
parse('1.70, "y": 2.');
parse('49, "z": 2e3}}');
parse('[ "aaaa\\"abcd\\u0123\\\\aa\\/aa" ]')
parse('[true,0,false,0')
parse(',false,0,true]');
+27
View File
@@ -0,0 +1,27 @@
{
"name": "nano-json-stream-parser",
"version": "0.1.2",
"description": "Lightweight, streamed JSON parser in 748 bytes",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/maiavictor/nano-json-stream-parser.git"
},
"keywords": [
"json",
"parse"
],
"author": "Victor Maia",
"license": "MIT",
"bugs": {
"url": "https://github.com/maiavictor/nano-json-stream-parser/issues"
},
"homepage": "https://github.com/maiavictor/nano-json-stream-parser#readme"
,"_resolved": "https://registry.npmjs.org/nano-json-stream-parser/-/nano-json-stream-parser-0.1.2.tgz"
,"_integrity": "sha1-DMj20OK2IrR5xA1JnEbWS3Vcb18="
,"_from": "nano-json-stream-parser@^0.1.2"
}
+136
View File
@@ -0,0 +1,136 @@
// NJSP is just a straightforward monadic parser for JSON.
const Read = {};
const error = () => {
throw "NoParse";
}
const readNonWhite = c => bind =>
bind(c || Read, c => /\s/.test(c)
? readNonWhite("")(bind)
: c);
const parseDigits = c => bind =>
bind(c || Read, c => /[0-9]/.test(c)
? bind(parseDigits("")(bind), cs => [cs[0], c + cs[1]])
: [c, ""]);
// Well fuck, this is why the do-notation exists
const parseNumber = c => bind =>
// Parses sign
bind(c || Read, c => bind(c === "-"
? ["","-"]
: [c,""],
([c,sign]) =>
// Parses the base digits
bind(c || Read, c => bind(c === "0"
? ["","0"]
: /[1-9]/.test(c)
? parseDigits(c)(bind)
: error(),
([c, base]) =>
// Parses the decimal digits
bind(c || Read, c => bind(c === "."
? bind(parseDigits("")(bind), ([c,ds]) => [c, "." + ds])
: [c, ""],
([c, frac]) =>
// Parses the exponent
bind(c || Read, c => bind(/[eE]/.test(c)
? bind(bind(Read, c => /[+-]/.test(c) ? ["", "e" + c] : [c, "e"]), ([c,es]) =>
bind(parseDigits(c)(bind), ([c,ds]) => [c, es + ds]))
: [c, ""],
([c, exp]) =>
// Returns the number
[c, Number(sign + base + frac + exp)]))))))));
const parseHex = bind =>
bind(Read, h =>
/[0-9a-fA-F]/.test(h)
? parseInt(h,16)
: error());
const parseString = bind =>
bind(bind(Read, c => c !== "\\" ? c :
bind(Read, c =>
/[\\"\/bfnrt]/.test(c)
? ({b:"\b",f:"\f",n:"\n",r:"\r",t:"\t","\\":"\\","/":"/",'"':''})[c]
: /u/.test(c)
? bind(parseHex(bind), a =>
bind(parseHex(bind), b =>
bind(parseHex(bind), c =>
bind(parseHex(bind), d =>
String.fromCharCode(a*4096+b*256+c*16+d)))))
: error())), c =>
c === '"' ? "" : bind(parseString(bind), s => (c||'"') + s));
const parseArray = bind => {
let array = [];
const go = c => bind =>
bind(c || Read, c => c === "]" ? array :
bind(parseValue(c)(bind), ([c,value]) => (array.push(value),
bind(readNonWhite(c)(bind), c => /[,\]]/.test(c)
? go(c === "," ? "" : c)(bind)
: error()))));
return go("")(bind);
}
const parseObject = bind => {
let object = {};
const go = c => bind =>
bind(c || Read, c => c === "}" ? object :
bind(readNonWhite(c)(bind), c => c !== '"' ? error() :
bind(parseString(bind), key =>
bind(readNonWhite("")(bind), c => c !== ':' ? error() :
bind(parseValue("")(bind), ([c,val]) => (object[key] = val,
bind(readNonWhite(c)(bind), c => /[,}]/.test(c)
? go(c === "," ? "" : c)(bind)
: error())))))));
return go("")(bind);
}
const parseExact = str => ret => bind => !str
? ret : bind(Read, c => c !== str[0]
? error()
: parseExact(str.slice(1))(ret)(bind));
const parseValue = c => bind =>
bind(readNonWhite(c)(bind), c =>
c === "[" ? bind(parseArray(bind), v => ["",v]) :
c === "{" ? bind(parseObject(bind), v => ["",v]) :
c === '"' ? bind(parseString(bind), v => ["",v]) :
c === 't' ? bind(parseExact("rue")(true)(bind), v => ["",v]) :
c === 'f' ? bind(parseExact("alse")(false)(bind), v => ["",v]) :
parseNumber(c)(bind));
const parseStream = parser => {
const bind = (a, b) =>
a === Read
? c => b(c)
: typeof a === "function"
? c => bind(a(c), b)
: b(a);
return parser(bind);
}
const parser = parser => getJSON => {
let s = parseStream(parser);
const feed = str => {
for (let i = 0, l = str.length; i < l; ++i) {
try {
s = s(str[i]);
} catch (e) {
s = parseStream(parser);
break;
}
if (typeof s !== "function") {
getJSON(s[1]);
s = parseStream(parser);
}
}
return feed;
}
return feed;
}
module.exports = parser(parseValue(""));