UNPKG

jsongraph

Version:

powerful minimalist graph/dataflow programming based on jsonschema references (nodejs/coffeescript)

1 lines 9.26 kB
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){(function(process,__dirname){(function(){var clone,expr,fs,request;fs=typeof window==="undefined"||window===null?require("fs"):false;request=fs&&fs.existsSync(__dirname+"/../sync-request")?require("sync-request"):false;expr=require("property-expr");clone=function(obj){return JSON.parse(JSON.stringify(obj))};module.exports=function(){this.cache={};this.findIds=function(json,ids){var id,k,obj,v;id=false;obj={};for(k in json){v=json[k];if(json.id!=null){id=json.id}if(id&&k!=="id"){obj[k]=v}if(typeof v==="object"){this.findIds(v,ids)}}if(id){return ids[id]=obj}};this.get_json_pointer=function(ref,root){var err,evalstr,result;evalstr=ref.replace(/\\\//,"#SLASH#").replace(/\//g,".").replace(/#SLASH#/,"/");evalstr=evalstr.replace(/#\./,"");try{if(process.env.DEBUG!=null){console.log(evalstr)}result=expr.getter(evalstr)(root)}catch(_error){err=_error;result=""}return result};this.replace=function(json,ids,root){var jsonpointer,k,ref,results,str,v;results=[];for(k in json){v=json[k];if(v!=null&&v["$ref"]!=null){ref=v["$ref"];if(Array.isArray(ref)){results.push(ref=this.replace(ref,ids,root))}else if(ids[ref]!=null){results.push(json[k]=ids[ref])}else if(request&&String(ref).match(/^http/)){if(!this.cache[ref]){this.cache[ref]=JSON.parse(request("GET",ref).getBody().toString())}json[k]=this.cache[ref];if(ref.match("#")){jsonpointer=ref.replace(/.*#/,"#");if(jsonpointer.length){results.push(json[k]=this.get_json_pointer(jsonpointer,json[k]))}else{results.push(void 0)}}else{results.push(void 0)}}else if(fs&&fs.existsSync(ref)){str=fs.readFileSync(ref).toString();if(str.match(/module\.exports/)){results.push(json[k]=require(ref))}else{results.push(json[k]=JSON.parse(str))}}else if(String(ref).match(/^#\//)){results.push(json[k]=this.get_json_pointer(ref,root))}else{results.push(void 0)}}else{if(typeof v==="object"){results.push(this.replace(v,ids,root))}else{results.push(void 0)}}}return results};this.resolve=function(json){var ids;ids={};this.findIds(json,ids);this.replace(json,ids,json);return json};this.evaluate=function(json,data,cb){var k,ref1,v;if(cb==null){cb=this.evaluateStr}ref1=clone(json);for(k in ref1){v=ref1[k];if(typeof v==="string"){json[k]=cb(v,data)}if(typeof v==="object"){json[k]=this.evaluate(v,data)}}return json};this.evaluateStr=function(k,data){var itemstr;if(typeof k!=="string"){return k}itemstr=k.replace(/(\{)(.*?)(\})/g,function($0,$1,$2){var err,result;result="";if(data==null||$2==null){return result}if(data[$2]!=null&&typeof data[$2]==="function"){result=data[$2]()}else{if(data[$2]!=null){result=data[$2]}else{try{$2=$2.replace(/^#\//,"").replace(/\//g,".");result=expr.getter($2)(data)}catch(_error){err=_error;result=""}if(result==null){result=""}}}this.evaluateStr(result,data);return result});return itemstr};return this}()}).call(this)}).call(this,require("_process"),"/node_modules/json-ref-lite")},{_process:4,fs:3,"property-expr":2,"sync-request":3}],2:[function(require,module,exports){"use strict";var SPLIT_REGEX=/[^.^\]^[]+|(?=\[\]|\.\.)/g,DIGIT_REGEX=/^\d+$/;var setCache={},getCache={};module.exports={expr:expr,setter:function(path){return setCache[path]||(setCache[path]=new Function("data, value","return "+expr(path,"data")+" = value, data;"))},getter:function(path,safe){var k=path+"_"+safe;return getCache[k]||(getCache[k]=new Function("data","return "+expr(path,safe,"data")))},split:function(path){return path.match(SPLIT_REGEX)},join:function(segments){return segments.reduce(function(path,part){return path+(isQuoted(part)||DIGIT_REGEX.test(part)?"["+part+"]":(path?".":"")+part)},"")},forEach:function(path,cb,thisArg){forEach(path.match(SPLIT_REGEX),cb,thisArg)}};function expr(expression,safe,param){expression=expression||"";if(typeof safe==="string"){param=safe;safe=false}param=param||"data";if(expression&&expression.charAt(0)!=="[")expression="."+expression;return safe?makeSafe(expression,param):param+expression}function forEach(parts,iter,thisArg){var len=parts.length,part,idx,isArray,isBracket;for(idx=0;idx<len;idx++){part=parts[idx];if(part){isBracket=isQuoted(part);isArray=!isBracket&&/^\d+$/.test(part);iter.call(thisArg,part,isBracket,isArray,idx,parts)}}}function isQuoted(str){return typeof str==="string"&&str&&["'",'"'].indexOf(str.charAt(0))!==-1}function makeSafe(path,param){var result=param,parts=path.match(SPLIT_REGEX),isLast;forEach(parts,function(part,isBracket,isArray,idx,parts){isLast=idx===parts.length-1;part=isBracket||isArray?"["+part+"]":"."+part;result+=part+(!isLast?" || {})":")")});return new Array(parts.length+1).join("(")+result}},{}],3:[function(require,module,exports){},{}],4:[function(require,module,exports){var process=module.exports={};var queue=[];var draining=false;function drainQueue(){if(draining){return}draining=true;var currentQueue;var len=queue.length;while(len){currentQueue=queue;queue=[];var i=-1;while(++i<len){currentQueue[i]()}len=queue.length}draining=false}process.nextTick=function(fun){queue.push(fun);if(!draining){setTimeout(drainQueue,0)}};process.title="browser";process.browser=true;process.env={};process.argv=[];process.version="";process.versions={};function noop(){}process.on=noop;process.addListener=noop;process.once=noop;process.off=noop;process.removeListener=noop;process.removeAllListeners=noop;process.emit=noop;process.binding=function(name){throw new Error("process.binding is not supported")};process.cwd=function(){return"/"};process.chdir=function(dir){throw new Error("process.chdir is not supported")};process.umask=function(){return 0}},{}],jsongraph:[function(require,module,exports){(function(){var clone,jref;jref=require("json-ref-lite");clone=function(obj){return JSON.parse(JSON.stringify(obj))};module.exports=function(jg){jg.graph=false;jg.filters={global:{}};jg.bindings=[];jg.types={};jg.parsers={};jg.opts={verbose:0,maxrecurse:1,halt:function(node,data,processed){if(node.processed!=null&&node.processed===this.maxrecurse){return true}if(data==null){return true}return false}};jg.bind=function(nodenames,cb){return this.bindings.push({nodenames:nodenames,process:cb})};jg.register=function(type,cb){return this.types[type]=cb};jg.evaluate=function(data,opts){var g,i,len,parser,parsers;g=(opts!=null?opts.graph:void 0)!=null?opts.graph:jg.graph;g=clone(g);parsers=(opts!=null?opts.parsers:void 0)?opts.parsers:["expr","ref"];for(i=0,len=parsers.length;i<len;i++){parser=parsers[i];if(jg.parsers[parser]!=null){jg.parsers[parser](g,data)}}if((opts!=null?opts.graph:void 0)==null){jg.graph=g}return g};jg._run=function(node,data,cb){var _,err,process,result;_=this.opts;if(node!=null&&!_.halt(node,data)){try{if(this.opts.verbose>1){console.log("[ "+node.name+" ]\n ├ input : "+JSON.stringify(data))}process=cb=cb!=null?cb:function(node,data,next){return next(node,data)};if(node.type!=null&&jg.types[node.type]!=null){process=function(node,data,next){return jg.types[node.type](node,data,function(){return cb(node,data,next)})}}return result=process(node,data,function(node,data){var filter,filtername,i,len,o,ref,ref1,results;ref=jg.filters.global;for(filtername in ref){filter=ref[filtername];filter(node,data)}if(jg.opts.verbose>1){console.log(" ├ output: "+JSON.stringify(data))}node.processed=node.processed==null?1:++node.processed;if(node["$ref"]!=null){ref1=node["$ref"];results=[];for(i=0,len=ref1.length;i<len;i++){o=ref1[i];results.push(jg._run(o,clone(data),cb))}return results}})}catch(_error){err=_error;if(err==="flow-stop"){return}throw err}}};jg.run=function(startnode,data,cb){var graph,name,node,ref,ref1;if(typeof startnode!=="string"||typeof data!=="object"){throw"invalid args"}graph=jref.resolve(clone(jg.graph));ref=graph.graph;for(name in ref){node=ref[name];node.name=name}if((graph!=null?(ref1=graph.graph)!=null?ref1[startnode]:void 0:void 0)==null){throw'node "'+startnode+'" not found'}return jg._run(graph.graph[startnode],data,cb)};jg.utils={dump:function(){return console.log(JSON.stringify(this.get(),null,2))},get:function(nodename){if(nodename==null){return jg.graph}return jg.graph.graph[nodename]!=null?jg.graph.graph[nodename]:null},set:function(name,node){jg.graph.graph[name]=node==null?{name:name}:node;return jg.graph.graph[name]},link:function(a,b){var graph;graph=jg.graph.graph;if(typeof a!=="object"&&graph[a]!=null){a=graph[a]}if(typeof b!=="object"&&graph[b]!=null){b=graph[b]}if(a["$ref"]==null){a["$ref"]=[]}return a["$ref"].push(b)},run:jg.run,evaluate:jg.evaluate};jg.init=function(graph){if(typeof graph!=="object"||graph.graph==null){throw"invalid args"}this.graph=graph;return this.utils};jg.parsers.ref=function(graph,data){graph.data=data;jref.resolve(graph);delete graph.data;return graph};jg.parsers.expr=function(graph,data){return jref.evaluate(graph,data)};return jg}({})}).call(this)},{"json-ref-lite":1}]},{},[]);