@gram-data/gram-builder
Advanced tools
Comparing version 0.1.10 to 0.2.3
@@ -58,4 +58,3 @@ 'use strict'; | ||
return _extends({ | ||
type: 'seq' | ||
}, id && { | ||
type: 'seq', | ||
id: id | ||
@@ -71,9 +70,10 @@ }, labels && { | ||
/** | ||
* Reduce paths into a single path composed using the given relation. | ||
* Reduce a list of paths into a single path composed using the given relation. | ||
* | ||
* @parm relation the relation to apply to all sub-paths | ||
* @param paths sub-paths to be paired | ||
* @param relation the relation to apply to all sub-paths | ||
* @param pathlist sub-paths to be paired | ||
* @param baseID the baseID from which path expressions will derive new IDs | ||
*/ | ||
var reduce = function reduce(relation, paths) { | ||
var reduce = function reduce(relation, pathlist, baseID) { | ||
if (relation === void 0) { | ||
@@ -83,20 +83,18 @@ relation = 'pair'; | ||
var pathlist = normalizeChildren(paths); | ||
var subID = 0; | ||
if (pathlist) { | ||
if (pathlist.length > 1) { | ||
return [pathlist.reduceRight(function (acc, curr) { | ||
return cons([curr, acc], { | ||
relation: relation | ||
}); | ||
}, UNIT)]; | ||
} else { | ||
return [pathlist[0]]; | ||
} | ||
if (pathlist.length > 1) { | ||
return pathlist.reduceRight(function (acc, curr) { | ||
var childID = baseID ? "" + baseID + subID : undefined; | ||
return cons([curr, acc], { | ||
relation: relation, | ||
id: childID | ||
}); | ||
}, EMPTY_PATH); | ||
} else { | ||
return pathlist[0]; | ||
} | ||
return []; | ||
}; | ||
/** | ||
* Build any path-like element | ||
* Build a path. | ||
* | ||
@@ -113,3 +111,4 @@ * @param members sub-paths to compose | ||
var element = _extends({ | ||
type: 'path', | ||
type: 'path' | ||
}, attributes.id && { | ||
id: attributes.id | ||
@@ -120,43 +119,43 @@ }, attributes.labels && { | ||
record: attributes.record | ||
}, { | ||
children: members.filter(function (child) { | ||
return child && !gramAst.isGramUnit(child); | ||
}) | ||
}); | ||
if (element.children.length === 0) { | ||
if (element.id || element.labels && element.labels.length > 0 || element.record) { | ||
element.type = 'node'; // element.id = element.id || identity.shortID(); | ||
if (members === undefined) { | ||
if (element.id && element.id !== gramAst.EMPTY_PATH_ID) { | ||
element.children = []; | ||
return element; | ||
} else { | ||
return UNIT; | ||
} | ||
} else if (element.children.length === 1) { | ||
var inner = element.children[0]; | ||
if (element.id || element.labels && element.labels.length > 0 || element.record) { | ||
if (gramAst.isGramUnit(inner)) { | ||
element.type = 'node'; | ||
element.children = []; | ||
return element; | ||
} | ||
element.children = undefined; | ||
return EMPTY_PATH; | ||
} else if (members.length === 0) { | ||
if (element.id === gramAst.EMPTY_PATH_ID) { | ||
return EMPTY_PATH; | ||
} | ||
element.children = []; | ||
return element; | ||
} else if (members.length === 1) { | ||
var lhs = members[0]; | ||
var rhs = EMPTY_PATH; | ||
if (gramAst.isGramEmptyPath(lhs)) { | ||
element.children = []; | ||
return element; | ||
} else { | ||
if (gramAst.isGramUnit(inner)) return inner; // element.id = identity.shortID(); | ||
if (gramAst.isGramNode(inner)) return inner; | ||
if (gramAst.isGramEdge(inner)) return inner; | ||
if (gramAst.isGramPath(inner)) return inner; | ||
element.children = [lhs, rhs]; | ||
return element; | ||
} | ||
} else if (element.children.length === 2) { | ||
if (attributes.relation && attributes.relation !== 'pair' && gramAst.isGramNode(element.children[0]) && gramAst.isGramNode(element.children[1])) { | ||
element.type = 'edge'; // element.id = element.id || identity.shortID(); | ||
} else if (members.length === 2) { | ||
if (attributes.relation && attributes.relation !== 'pair' && gramAst.isGramNode(members[0]) && gramAst.isGramNode(members[1])) { | ||
element.relation = attributes.relation; | ||
element.children = [members[0], members[1]]; | ||
return element; | ||
} else if (gramAst.isGramEmptyPath(members[0]) && gramAst.isGramEmptyPath(members[1])) { | ||
element.relation = attributes.relation; | ||
element.children = []; | ||
return element; | ||
} | ||
} // element.id = element.id || identity.shortID(); | ||
element.children = [members[0], members[1]]; | ||
} | ||
@@ -167,18 +166,18 @@ element.relation = attributes.relation || 'pair'; | ||
/** | ||
* Singleton instance of GramUnit | ||
* Singleton instance of GramEmptyPath | ||
*/ | ||
var UNIT = { | ||
type: 'unit', | ||
id: gramAst.UNIT_ID, | ||
var EMPTY_PATH = { | ||
type: 'path', | ||
id: gramAst.EMPTY_PATH_ID, | ||
labels: undefined, | ||
record: undefined, | ||
children: [] | ||
children: undefined | ||
}; | ||
/** | ||
* Convenience function for retrieving the singleton GramUnit. | ||
* Convenience function for retrieving the singleton GramEmptyPath. | ||
*/ | ||
var unit = function unit() { | ||
return UNIT; | ||
var empty = function empty() { | ||
return EMPTY_PATH; | ||
}; | ||
@@ -196,4 +195,4 @@ /** | ||
return _extends({ | ||
type: 'node', | ||
// id: id || identity.shortID(), | ||
type: 'path' | ||
}, id && { | ||
id: id | ||
@@ -219,9 +218,4 @@ }, labels && { | ||
var edge = function edge(children, relation, id, labels, record) { | ||
if (relation === void 0) { | ||
relation = 'right'; | ||
} | ||
return _extends({ | ||
type: 'edge', | ||
// id: id || identity.shortID(), | ||
type: 'path', | ||
id: id | ||
@@ -248,4 +242,3 @@ }, labels && { | ||
return _extends({ | ||
type: 'path' | ||
}, id && { | ||
type: 'path', | ||
id: id | ||
@@ -260,3 +253,9 @@ }, labels && { | ||
}; | ||
var record = function record(properties) { | ||
/** | ||
* Reduces an array of GramProperties into a map. | ||
* | ||
* @param properties | ||
*/ | ||
var reduceRecord = function reduceRecord(properties) { | ||
return properties.reduce(function (acc, p) { | ||
@@ -267,2 +266,16 @@ acc[p.name] = p.value; | ||
}; | ||
/** | ||
* Unfolds a property map<string,GramRecordValue> into a property list[GramProperty]. | ||
* | ||
* @param properties | ||
*/ | ||
var unfoldProperties = function unfoldProperties(properties) { | ||
return Object.entries(properties).reduce(function (acc, _ref) { | ||
var k = _ref[0], | ||
v = _ref[1]; | ||
acc.push(property(k, v)); | ||
return acc; | ||
}, []); | ||
}; | ||
var property = function property(name, value) { | ||
@@ -311,3 +324,3 @@ var Node = { | ||
type: 'hexadecimal', | ||
value: value | ||
value: typeof value === 'number' ? value.toString(16) : value | ||
}; | ||
@@ -318,3 +331,3 @@ }; | ||
type: 'octal', | ||
value: value | ||
value: typeof value === 'number' ? value.toString(8) : value | ||
}; | ||
@@ -330,13 +343,16 @@ }; | ||
var year = function year(value) { | ||
return tagged(value instanceof Date ? value.getFullYear().toString() : value, 'date'); | ||
return tagged('date', value instanceof Date ? value.getFullYear().toString() : value); | ||
}; | ||
var date = function date(value) { | ||
return tagged(value instanceof Date ? dateToYMD(value) : value, 'date'); | ||
return tagged('date', value instanceof Date ? dateToYMD(value) : value); | ||
}; | ||
var dayOfMonth = function dayOfMonth(value) { | ||
return tagged(value instanceof Date ? dateToDayOfMonth(value) : value, 'date'); | ||
return tagged('date', value instanceof Date ? dateToDayOfMonth(value) : value); | ||
}; | ||
var time = function time(value) { | ||
return tagged(value instanceof Date ? dateToYMD(value) : value, 'time'); | ||
return tagged('time', value instanceof Date ? value.toTimeString() : value); | ||
}; | ||
var duration = function duration(value) { | ||
return tagged('duration', value instanceof Date ? "P" + (value.getUTCFullYear() - 1970) + "Y" + value.getUTCMonth() + "M" + value.getUTCDate() + "DT" + value.getUTCHours() + "H" + value.getUTCMinutes() + "M" + value.getUTCMilliseconds() / 1000 + "S" : value); | ||
}; | ||
var flatten = function flatten(xs, depth) { | ||
@@ -353,3 +369,3 @@ if (depth === void 0) { | ||
seq: seq, | ||
unit: unit, | ||
empty: empty, | ||
cons: cons, | ||
@@ -359,3 +375,2 @@ path: path, | ||
edge: edge, | ||
record: record, | ||
property: property, | ||
@@ -372,6 +387,9 @@ "boolean": _boolean, | ||
time: time, | ||
flatten: flatten | ||
duration: duration, | ||
flatten: flatten, | ||
reduceRecord: reduceRecord, | ||
unfoldProperties: unfoldProperties | ||
}; | ||
var builder = { | ||
var gramBuilder$1 = { | ||
__proto__: null, | ||
@@ -381,8 +399,9 @@ seq: seq, | ||
cons: cons, | ||
UNIT: UNIT, | ||
unit: unit, | ||
EMPTY_PATH: EMPTY_PATH, | ||
empty: empty, | ||
node: node, | ||
edge: edge, | ||
path: path, | ||
record: record, | ||
reduceRecord: reduceRecord, | ||
unfoldProperties: unfoldProperties, | ||
property: property, | ||
@@ -401,2 +420,3 @@ boolean: _boolean, | ||
time: time, | ||
duration: duration, | ||
flatten: flatten, | ||
@@ -406,4 +426,5 @@ 'default': gramBuilder | ||
exports.UNIT = UNIT; | ||
exports.EMPTY_PATH = EMPTY_PATH; | ||
exports.boolean = _boolean; | ||
exports.builder = gramBuilder$1; | ||
exports.cons = cons; | ||
@@ -413,4 +434,5 @@ exports.date = date; | ||
exports.decimal = decimal; | ||
exports.default = builder; | ||
exports.duration = duration; | ||
exports.edge = edge; | ||
exports.empty = empty; | ||
exports.flatten = flatten; | ||
@@ -424,4 +446,4 @@ exports.hexadecimal = hexadecimal; | ||
exports.property = property; | ||
exports.record = record; | ||
exports.reduce = reduce; | ||
exports.reduceRecord = reduceRecord; | ||
exports.seq = seq; | ||
@@ -431,4 +453,4 @@ exports.string = string; | ||
exports.time = time; | ||
exports.unit = unit; | ||
exports.unfoldProperties = unfoldProperties; | ||
exports.year = year; | ||
//# sourceMappingURL=gram-builder.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@gram-data/gram-ast");function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function r(e){return Array.isArray(e)?e:e instanceof Function?r(e()):void 0===e?[]:[e]}var n=function(e){return e.toISOString().slice(0,10)},i=function(e,n,i,o){return t({type:"seq"},n&&{id:n},i&&{labels:i},o&&{record:o},{children:r(e)})},o=function(e,t){void 0===e&&(e="pair");var n=r(t);return n?n.length>1?[n.reduceRight((function(t,r){return a([r,t],{relation:e})}),l)]:[n[0]]:[]},a=function(r,n){void 0===n&&(n={});var i=t({type:"path",id:n.id},n.labels&&{labels:n.labels},n.record&&{record:n.record},{children:r.filter((function(t){return t&&!e.isGramUnit(t)}))});if(0===i.children.length)return i.id||i.labels&&i.labels.length>0||i.record?(i.type="node",i):l;if(1===i.children.length){var o=i.children[0];if(i.id||i.labels&&i.labels.length>0||i.record)return e.isGramUnit(o)?(i.type="node",i.children=[],i):i;if(e.isGramUnit(o))return o;if(e.isGramNode(o))return o;if(e.isGramEdge(o))return o;if(e.isGramPath(o))return o}else if(2===i.children.length&&n.relation&&"pair"!==n.relation&&e.isGramNode(i.children[0])&&e.isGramNode(i.children[1]))return i.type="edge",i.relation=n.relation,i;return i.relation=n.relation||"pair",i},l={type:"unit",id:e.UNIT_ID,labels:void 0,record:void 0,children:[]},u=function(){return l},c=function(e,r,n){return t({type:"node",id:e},r&&{labels:r},n&&{record:n},{children:[]})},d=function(e,r,n,i,o){return void 0===r&&(r="right"),t({type:"edge",id:n},i&&{labels:i},o&&{record:o},{relation:r,children:e})},s=function(e,r,n,i){return t({type:"path"},r&&{id:r},n&&{labels:n},i&&{record:i},{children:e})},p=function(e){return e.reduce((function(e,t){return e[t.name]=t.value,e}),{})},f=function(e,t){return{type:"property",name:e,value:t}},g=function(e){return{type:"boolean",value:e?"true":"false"}},h=function(e){return{type:"string",value:e}},m=function(e,t){return{type:"tagged",value:t,tag:e}},y=function(e){return{type:"integer",value:String(e)}},x=function(e){return{type:"decimal",value:String(e)}},v=function(e){return{type:"hexadecimal",value:e}},b=function(e){return{type:"octal",value:e}},G=function(e,t){return{type:"measurement",value:String(t),unit:e}},O=function(e){return m(e instanceof Date?e.getFullYear().toString():e,"date")},S=function(e){return m(e instanceof Date?n(e):e,"date")},_=function(e){return m(e instanceof Date?"--"+e.toISOString().slice(5,10):e,"date")},I=function(e){return m(e instanceof Date?n(e):e,"time")},N=function(e,t){return void 0===t&&(t=1),e.flat(t).filter((function(e){return null!==e}))},U={__proto__:null,seq:i,reduce:o,cons:a,UNIT:l,unit:u,node:c,edge:d,path:s,record:p,property:f,boolean:g,string:h,tagged:m,integer:y,decimal:x,hexadecimal:v,octal:b,measurement:G,year:O,date:S,dayOfMonth:_,time:I,flatten:N,default:{seq:i,unit:u,cons:a,path:s,node:c,edge:d,record:p,property:f,boolean:g,string:h,tagged:m,integer:y,decimal:x,hexadecimal:v,octal:b,measurement:G,date:S,time:I,flatten:N}};exports.UNIT=l,exports.boolean=g,exports.cons=a,exports.date=S,exports.dayOfMonth=_,exports.decimal=x,exports.default=U,exports.edge=d,exports.flatten=N,exports.hexadecimal=v,exports.integer=y,exports.measurement=G,exports.node=c,exports.octal=b,exports.path=s,exports.property=f,exports.record=p,exports.reduce=o,exports.seq=i,exports.string=h,exports.tagged=m,exports.time=I,exports.unit=u,exports.year=O; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@gram-data/gram-ast");function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function r(e){return Array.isArray(e)?e:e instanceof Function?r(e()):void 0===e?[]:[e]}var n=function(e,n,o,i){return t({type:"seq",id:n},o&&{labels:o},i&&{record:i},{children:r(e)})},o=function(e,t,r){return void 0===e&&(e="pair"),t.length>1?t.reduceRight((function(t,n){return i([n,t],{relation:e,id:r?""+r+0:void 0})}),a):t[0]},i=function(r,n){void 0===n&&(n={});var o=t({type:"path"},n.id&&{id:n.id},n.labels&&{labels:n.labels},n.record&&{record:n.record});if(void 0===r)return o.id&&o.id!==e.EMPTY_PATH_ID?(o.children=[],o):(o.children=void 0,a);if(0===r.length)return o.id===e.EMPTY_PATH_ID?a:(o.children=[],o);if(1===r.length){var i=r[0],u=a;return e.isGramEmptyPath(i)?(o.children=[],o):(o.children=[i,u],o)}if(2===r.length){if(n.relation&&"pair"!==n.relation&&e.isGramNode(r[0])&&e.isGramNode(r[1]))return o.relation=n.relation,o.children=[r[0],r[1]],o;if(e.isGramEmptyPath(r[0])&&e.isGramEmptyPath(r[1]))return o.relation=n.relation,o.children=[],o;o.children=[r[0],r[1]]}return o.relation=n.relation||"pair",o},a={type:"path",id:e.EMPTY_PATH_ID,labels:void 0,record:void 0,children:void 0},u=function(){return a},l=function(e,r,n){return t({type:"path"},e&&{id:e},r&&{labels:r},n&&{record:n},{children:[]})},d=function(e,r,n,o,i){return t({type:"path",id:n},o&&{labels:o},i&&{record:i},{relation:r,children:e})},c=function(e,r,n,o){return t({type:"path",id:r},n&&{labels:n},o&&{record:o},{children:e})},s=function(e){return e.reduce((function(e,t){return e[t.name]=t.value,e}),{})},p=function(e){return Object.entries(e).reduce((function(e,t){return e.push(f(t[0],t[1])),e}),[])},f=function(e,t){return{type:"property",name:e,value:t}},g=function(e){return{type:"boolean",value:e?"true":"false"}},h=function(e){return{type:"string",value:e}},m=function(e,t){return{type:"tagged",value:t,tag:e}},y=function(e){return{type:"integer",value:String(e)}},x=function(e){return{type:"decimal",value:String(e)}},v=function(e){return{type:"hexadecimal",value:"number"==typeof e?e.toString(16):e}},b=function(e){return{type:"octal",value:"number"==typeof e?e.toString(8):e}},P=function(e,t){return{type:"measurement",value:String(t),unit:e}},T=function(e){return m("date",e instanceof Date?e.getFullYear().toString():e)},_=function(e){return m("date",e instanceof Date?e.toISOString().slice(0,10):e)},M=function(e){return m("date",e instanceof Date?"--"+e.toISOString().slice(5,10):e)},S=function(e){return m("time",e instanceof Date?e.toTimeString():e)},D=function(e){return m("duration",e instanceof Date?"P"+(e.getUTCFullYear()-1970)+"Y"+e.getUTCMonth()+"M"+e.getUTCDate()+"DT"+e.getUTCHours()+"H"+e.getUTCMinutes()+"M"+e.getUTCMilliseconds()/1e3+"S":e)},O=function(e,t){return void 0===t&&(t=1),e.flat(t).filter((function(e){return null!==e}))},E={__proto__:null,seq:n,reduce:o,cons:i,EMPTY_PATH:a,empty:u,node:l,edge:d,path:c,reduceRecord:s,unfoldProperties:p,property:f,boolean:g,string:h,tagged:m,integer:y,decimal:x,hexadecimal:v,octal:b,measurement:P,year:T,date:_,dayOfMonth:M,time:S,duration:D,flatten:O,default:{seq:n,empty:u,cons:i,path:c,node:l,edge:d,property:f,boolean:g,string:h,tagged:m,integer:y,decimal:x,hexadecimal:v,octal:b,measurement:P,date:_,time:S,duration:D,flatten:O,reduceRecord:s,unfoldProperties:p}};exports.EMPTY_PATH=a,exports.boolean=g,exports.builder=E,exports.cons=i,exports.date=_,exports.dayOfMonth=M,exports.decimal=x,exports.duration=D,exports.edge=d,exports.empty=u,exports.flatten=O,exports.hexadecimal=v,exports.integer=y,exports.measurement=P,exports.node=l,exports.octal=b,exports.path=c,exports.property=f,exports.reduce=o,exports.reduceRecord=s,exports.seq=n,exports.string=h,exports.tagged=m,exports.time=S,exports.unfoldProperties=p,exports.year=T; | ||
//# sourceMappingURL=gram-builder.cjs.production.min.js.map |
@@ -1,2 +0,2 @@ | ||
import { isGramUnit, isGramNode, isGramEdge, isGramPath, UNIT_ID } from '@gram-data/gram-ast'; | ||
import { EMPTY_PATH_ID, isGramEmptyPath, isGramNode } from '@gram-data/gram-ast'; | ||
@@ -54,4 +54,3 @@ function _extends() { | ||
return _extends({ | ||
type: 'seq' | ||
}, id && { | ||
type: 'seq', | ||
id: id | ||
@@ -67,9 +66,10 @@ }, labels && { | ||
/** | ||
* Reduce paths into a single path composed using the given relation. | ||
* Reduce a list of paths into a single path composed using the given relation. | ||
* | ||
* @parm relation the relation to apply to all sub-paths | ||
* @param paths sub-paths to be paired | ||
* @param relation the relation to apply to all sub-paths | ||
* @param pathlist sub-paths to be paired | ||
* @param baseID the baseID from which path expressions will derive new IDs | ||
*/ | ||
var reduce = function reduce(relation, paths) { | ||
var reduce = function reduce(relation, pathlist, baseID) { | ||
if (relation === void 0) { | ||
@@ -79,20 +79,18 @@ relation = 'pair'; | ||
var pathlist = normalizeChildren(paths); | ||
var subID = 0; | ||
if (pathlist) { | ||
if (pathlist.length > 1) { | ||
return [pathlist.reduceRight(function (acc, curr) { | ||
return cons([curr, acc], { | ||
relation: relation | ||
}); | ||
}, UNIT)]; | ||
} else { | ||
return [pathlist[0]]; | ||
} | ||
if (pathlist.length > 1) { | ||
return pathlist.reduceRight(function (acc, curr) { | ||
var childID = baseID ? "" + baseID + subID : undefined; | ||
return cons([curr, acc], { | ||
relation: relation, | ||
id: childID | ||
}); | ||
}, EMPTY_PATH); | ||
} else { | ||
return pathlist[0]; | ||
} | ||
return []; | ||
}; | ||
/** | ||
* Build any path-like element | ||
* Build a path. | ||
* | ||
@@ -109,3 +107,4 @@ * @param members sub-paths to compose | ||
var element = _extends({ | ||
type: 'path', | ||
type: 'path' | ||
}, attributes.id && { | ||
id: attributes.id | ||
@@ -116,43 +115,43 @@ }, attributes.labels && { | ||
record: attributes.record | ||
}, { | ||
children: members.filter(function (child) { | ||
return child && !isGramUnit(child); | ||
}) | ||
}); | ||
if (element.children.length === 0) { | ||
if (element.id || element.labels && element.labels.length > 0 || element.record) { | ||
element.type = 'node'; // element.id = element.id || identity.shortID(); | ||
if (members === undefined) { | ||
if (element.id && element.id !== EMPTY_PATH_ID) { | ||
element.children = []; | ||
return element; | ||
} else { | ||
return UNIT; | ||
} | ||
} else if (element.children.length === 1) { | ||
var inner = element.children[0]; | ||
if (element.id || element.labels && element.labels.length > 0 || element.record) { | ||
if (isGramUnit(inner)) { | ||
element.type = 'node'; | ||
element.children = []; | ||
return element; | ||
} | ||
element.children = undefined; | ||
return EMPTY_PATH; | ||
} else if (members.length === 0) { | ||
if (element.id === EMPTY_PATH_ID) { | ||
return EMPTY_PATH; | ||
} | ||
element.children = []; | ||
return element; | ||
} else if (members.length === 1) { | ||
var lhs = members[0]; | ||
var rhs = EMPTY_PATH; | ||
if (isGramEmptyPath(lhs)) { | ||
element.children = []; | ||
return element; | ||
} else { | ||
if (isGramUnit(inner)) return inner; // element.id = identity.shortID(); | ||
if (isGramNode(inner)) return inner; | ||
if (isGramEdge(inner)) return inner; | ||
if (isGramPath(inner)) return inner; | ||
element.children = [lhs, rhs]; | ||
return element; | ||
} | ||
} else if (element.children.length === 2) { | ||
if (attributes.relation && attributes.relation !== 'pair' && isGramNode(element.children[0]) && isGramNode(element.children[1])) { | ||
element.type = 'edge'; // element.id = element.id || identity.shortID(); | ||
} else if (members.length === 2) { | ||
if (attributes.relation && attributes.relation !== 'pair' && isGramNode(members[0]) && isGramNode(members[1])) { | ||
element.relation = attributes.relation; | ||
element.children = [members[0], members[1]]; | ||
return element; | ||
} else if (isGramEmptyPath(members[0]) && isGramEmptyPath(members[1])) { | ||
element.relation = attributes.relation; | ||
element.children = []; | ||
return element; | ||
} | ||
} // element.id = element.id || identity.shortID(); | ||
element.children = [members[0], members[1]]; | ||
} | ||
@@ -163,18 +162,18 @@ element.relation = attributes.relation || 'pair'; | ||
/** | ||
* Singleton instance of GramUnit | ||
* Singleton instance of GramEmptyPath | ||
*/ | ||
var UNIT = { | ||
type: 'unit', | ||
id: UNIT_ID, | ||
var EMPTY_PATH = { | ||
type: 'path', | ||
id: EMPTY_PATH_ID, | ||
labels: undefined, | ||
record: undefined, | ||
children: [] | ||
children: undefined | ||
}; | ||
/** | ||
* Convenience function for retrieving the singleton GramUnit. | ||
* Convenience function for retrieving the singleton GramEmptyPath. | ||
*/ | ||
var unit = function unit() { | ||
return UNIT; | ||
var empty = function empty() { | ||
return EMPTY_PATH; | ||
}; | ||
@@ -192,4 +191,4 @@ /** | ||
return _extends({ | ||
type: 'node', | ||
// id: id || identity.shortID(), | ||
type: 'path' | ||
}, id && { | ||
id: id | ||
@@ -215,9 +214,4 @@ }, labels && { | ||
var edge = function edge(children, relation, id, labels, record) { | ||
if (relation === void 0) { | ||
relation = 'right'; | ||
} | ||
return _extends({ | ||
type: 'edge', | ||
// id: id || identity.shortID(), | ||
type: 'path', | ||
id: id | ||
@@ -244,4 +238,3 @@ }, labels && { | ||
return _extends({ | ||
type: 'path' | ||
}, id && { | ||
type: 'path', | ||
id: id | ||
@@ -256,3 +249,9 @@ }, labels && { | ||
}; | ||
var record = function record(properties) { | ||
/** | ||
* Reduces an array of GramProperties into a map. | ||
* | ||
* @param properties | ||
*/ | ||
var reduceRecord = function reduceRecord(properties) { | ||
return properties.reduce(function (acc, p) { | ||
@@ -263,2 +262,16 @@ acc[p.name] = p.value; | ||
}; | ||
/** | ||
* Unfolds a property map<string,GramRecordValue> into a property list[GramProperty]. | ||
* | ||
* @param properties | ||
*/ | ||
var unfoldProperties = function unfoldProperties(properties) { | ||
return Object.entries(properties).reduce(function (acc, _ref) { | ||
var k = _ref[0], | ||
v = _ref[1]; | ||
acc.push(property(k, v)); | ||
return acc; | ||
}, []); | ||
}; | ||
var property = function property(name, value) { | ||
@@ -307,3 +320,3 @@ var Node = { | ||
type: 'hexadecimal', | ||
value: value | ||
value: typeof value === 'number' ? value.toString(16) : value | ||
}; | ||
@@ -314,3 +327,3 @@ }; | ||
type: 'octal', | ||
value: value | ||
value: typeof value === 'number' ? value.toString(8) : value | ||
}; | ||
@@ -326,13 +339,16 @@ }; | ||
var year = function year(value) { | ||
return tagged(value instanceof Date ? value.getFullYear().toString() : value, 'date'); | ||
return tagged('date', value instanceof Date ? value.getFullYear().toString() : value); | ||
}; | ||
var date = function date(value) { | ||
return tagged(value instanceof Date ? dateToYMD(value) : value, 'date'); | ||
return tagged('date', value instanceof Date ? dateToYMD(value) : value); | ||
}; | ||
var dayOfMonth = function dayOfMonth(value) { | ||
return tagged(value instanceof Date ? dateToDayOfMonth(value) : value, 'date'); | ||
return tagged('date', value instanceof Date ? dateToDayOfMonth(value) : value); | ||
}; | ||
var time = function time(value) { | ||
return tagged(value instanceof Date ? dateToYMD(value) : value, 'time'); | ||
return tagged('time', value instanceof Date ? value.toTimeString() : value); | ||
}; | ||
var duration = function duration(value) { | ||
return tagged('duration', value instanceof Date ? "P" + (value.getUTCFullYear() - 1970) + "Y" + value.getUTCMonth() + "M" + value.getUTCDate() + "DT" + value.getUTCHours() + "H" + value.getUTCMinutes() + "M" + value.getUTCMilliseconds() / 1000 + "S" : value); | ||
}; | ||
var flatten = function flatten(xs, depth) { | ||
@@ -349,3 +365,3 @@ if (depth === void 0) { | ||
seq: seq, | ||
unit: unit, | ||
empty: empty, | ||
cons: cons, | ||
@@ -355,3 +371,2 @@ path: path, | ||
edge: edge, | ||
record: record, | ||
property: property, | ||
@@ -368,6 +383,9 @@ "boolean": _boolean, | ||
time: time, | ||
flatten: flatten | ||
duration: duration, | ||
flatten: flatten, | ||
reduceRecord: reduceRecord, | ||
unfoldProperties: unfoldProperties | ||
}; | ||
var builder = { | ||
var gramBuilder$1 = { | ||
__proto__: null, | ||
@@ -377,8 +395,9 @@ seq: seq, | ||
cons: cons, | ||
UNIT: UNIT, | ||
unit: unit, | ||
EMPTY_PATH: EMPTY_PATH, | ||
empty: empty, | ||
node: node, | ||
edge: edge, | ||
path: path, | ||
record: record, | ||
reduceRecord: reduceRecord, | ||
unfoldProperties: unfoldProperties, | ||
property: property, | ||
@@ -397,2 +416,3 @@ boolean: _boolean, | ||
time: time, | ||
duration: duration, | ||
flatten: flatten, | ||
@@ -402,4 +422,3 @@ 'default': gramBuilder | ||
export default builder; | ||
export { UNIT, _boolean as boolean, cons, date, dayOfMonth, decimal, edge, flatten, hexadecimal, integer, measurement, node, octal, path, property, record, reduce, seq, string, tagged, time, unit, year }; | ||
export { EMPTY_PATH, _boolean as boolean, gramBuilder$1 as builder, cons, date, dayOfMonth, decimal, duration, edge, empty, flatten, hexadecimal, integer, measurement, node, octal, path, property, reduce, reduceRecord, seq, string, tagged, time, unfoldProperties, year }; | ||
//# sourceMappingURL=gram-builder.esm.js.map |
@@ -36,12 +36,5 @@ (function (global, factory) { | ||
*/ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
// Pathlike types... | ||
/** | ||
* Identity of all units. | ||
*/ | ||
var UNIT_ID = '0'; | ||
/** | ||
* Type guard for GramUnit. | ||
* Type guard for a Path. | ||
* | ||
@@ -51,28 +44,31 @@ * @param o any object | ||
var isGramUnit = function isGramUnit(o) { | ||
return !!o.type && o.type === 'unit'; | ||
var isGramPath = function isGramPath(o) { | ||
return !!o.type && o.type === 'path'; | ||
}; | ||
/** | ||
* Type guard for GramNode. | ||
* | ||
* @param o any object | ||
*/ | ||
var isGramNode = function isGramNode(o) { | ||
return !!o.type && o.type === 'node'; | ||
}; | ||
var EMPTY_PATH_ID = 'ø'; | ||
/** | ||
* Type guard for GramEdge. | ||
* Type guard for GramEmptyPath. | ||
* | ||
* In practice both nodes and empty paths both have no children. | ||
* The distinguishing feature is that all empty paths have | ||
* no labels, no record, and always has the identity `ø`, while | ||
* a node may have labels, a record and always has an identity | ||
* other than `ø`. | ||
* | ||
* @param o any object | ||
*/ | ||
var isGramEdge = function isGramEdge(o) { | ||
return 'type' in o && 'relation' in o && o.type === 'edge'; | ||
var isGramEmptyPath = function isGramEmptyPath(o) { | ||
return isGramPath(o) && o.children === undefined && o.id === EMPTY_PATH_ID; | ||
}; | ||
/** | ||
* Type guard for a Path. | ||
* Type guard for GramNode. | ||
* | ||
* In practice both nodes and empty paths both have no children. | ||
* The distinguishing feature is that all empty paths have | ||
* no labels, no record, and always has the identity `ø`, while | ||
* a node may have labels, a record and always has an identity | ||
* other than `ø`. | ||
* | ||
* @param o any object | ||
@@ -82,4 +78,4 @@ */ | ||
var isGramPath = function isGramPath(o) { | ||
return !!o.type && o.type === 'path'; | ||
var isGramNode = function isGramNode(o) { | ||
return isGramPath(o) && o.children && o.children.length === 0 && o.id !== EMPTY_PATH_ID; | ||
}; | ||
@@ -120,4 +116,3 @@ | ||
return _extends({ | ||
type: 'seq' | ||
}, id && { | ||
type: 'seq', | ||
id: id | ||
@@ -133,9 +128,10 @@ }, labels && { | ||
/** | ||
* Reduce paths into a single path composed using the given relation. | ||
* Reduce a list of paths into a single path composed using the given relation. | ||
* | ||
* @parm relation the relation to apply to all sub-paths | ||
* @param paths sub-paths to be paired | ||
* @param relation the relation to apply to all sub-paths | ||
* @param pathlist sub-paths to be paired | ||
* @param baseID the baseID from which path expressions will derive new IDs | ||
*/ | ||
var reduce = function reduce(relation, paths) { | ||
var reduce = function reduce(relation, pathlist, baseID) { | ||
if (relation === void 0) { | ||
@@ -145,20 +141,18 @@ relation = 'pair'; | ||
var pathlist = normalizeChildren(paths); | ||
var subID = 0; | ||
if (pathlist) { | ||
if (pathlist.length > 1) { | ||
return [pathlist.reduceRight(function (acc, curr) { | ||
return cons([curr, acc], { | ||
relation: relation | ||
}); | ||
}, UNIT)]; | ||
} else { | ||
return [pathlist[0]]; | ||
} | ||
if (pathlist.length > 1) { | ||
return pathlist.reduceRight(function (acc, curr) { | ||
var childID = baseID ? "" + baseID + subID : undefined; | ||
return cons([curr, acc], { | ||
relation: relation, | ||
id: childID | ||
}); | ||
}, EMPTY_PATH); | ||
} else { | ||
return pathlist[0]; | ||
} | ||
return []; | ||
}; | ||
/** | ||
* Build any path-like element | ||
* Build a path. | ||
* | ||
@@ -175,3 +169,4 @@ * @param members sub-paths to compose | ||
var element = _extends({ | ||
type: 'path', | ||
type: 'path' | ||
}, attributes.id && { | ||
id: attributes.id | ||
@@ -182,43 +177,43 @@ }, attributes.labels && { | ||
record: attributes.record | ||
}, { | ||
children: members.filter(function (child) { | ||
return child && !isGramUnit(child); | ||
}) | ||
}); | ||
if (element.children.length === 0) { | ||
if (element.id || element.labels && element.labels.length > 0 || element.record) { | ||
element.type = 'node'; // element.id = element.id || identity.shortID(); | ||
if (members === undefined) { | ||
if (element.id && element.id !== EMPTY_PATH_ID) { | ||
element.children = []; | ||
return element; | ||
} else { | ||
return UNIT; | ||
} | ||
} else if (element.children.length === 1) { | ||
var inner = element.children[0]; | ||
if (element.id || element.labels && element.labels.length > 0 || element.record) { | ||
if (isGramUnit(inner)) { | ||
element.type = 'node'; | ||
element.children = []; | ||
return element; | ||
} | ||
element.children = undefined; | ||
return EMPTY_PATH; | ||
} else if (members.length === 0) { | ||
if (element.id === EMPTY_PATH_ID) { | ||
return EMPTY_PATH; | ||
} | ||
element.children = []; | ||
return element; | ||
} else if (members.length === 1) { | ||
var lhs = members[0]; | ||
var rhs = EMPTY_PATH; | ||
if (isGramEmptyPath(lhs)) { | ||
element.children = []; | ||
return element; | ||
} else { | ||
if (isGramUnit(inner)) return inner; // element.id = identity.shortID(); | ||
if (isGramNode(inner)) return inner; | ||
if (isGramEdge(inner)) return inner; | ||
if (isGramPath(inner)) return inner; | ||
element.children = [lhs, rhs]; | ||
return element; | ||
} | ||
} else if (element.children.length === 2) { | ||
if (attributes.relation && attributes.relation !== 'pair' && isGramNode(element.children[0]) && isGramNode(element.children[1])) { | ||
element.type = 'edge'; // element.id = element.id || identity.shortID(); | ||
} else if (members.length === 2) { | ||
if (attributes.relation && attributes.relation !== 'pair' && isGramNode(members[0]) && isGramNode(members[1])) { | ||
element.relation = attributes.relation; | ||
element.children = [members[0], members[1]]; | ||
return element; | ||
} else if (isGramEmptyPath(members[0]) && isGramEmptyPath(members[1])) { | ||
element.relation = attributes.relation; | ||
element.children = []; | ||
return element; | ||
} | ||
} // element.id = element.id || identity.shortID(); | ||
element.children = [members[0], members[1]]; | ||
} | ||
@@ -229,18 +224,18 @@ element.relation = attributes.relation || 'pair'; | ||
/** | ||
* Singleton instance of GramUnit | ||
* Singleton instance of GramEmptyPath | ||
*/ | ||
var UNIT = { | ||
type: 'unit', | ||
id: UNIT_ID, | ||
var EMPTY_PATH = { | ||
type: 'path', | ||
id: EMPTY_PATH_ID, | ||
labels: undefined, | ||
record: undefined, | ||
children: [] | ||
children: undefined | ||
}; | ||
/** | ||
* Convenience function for retrieving the singleton GramUnit. | ||
* Convenience function for retrieving the singleton GramEmptyPath. | ||
*/ | ||
var unit = function unit() { | ||
return UNIT; | ||
var empty = function empty() { | ||
return EMPTY_PATH; | ||
}; | ||
@@ -258,4 +253,4 @@ /** | ||
return _extends({ | ||
type: 'node', | ||
// id: id || identity.shortID(), | ||
type: 'path' | ||
}, id && { | ||
id: id | ||
@@ -281,9 +276,4 @@ }, labels && { | ||
var edge = function edge(children, relation, id, labels, record) { | ||
if (relation === void 0) { | ||
relation = 'right'; | ||
} | ||
return _extends({ | ||
type: 'edge', | ||
// id: id || identity.shortID(), | ||
type: 'path', | ||
id: id | ||
@@ -310,4 +300,3 @@ }, labels && { | ||
return _extends({ | ||
type: 'path' | ||
}, id && { | ||
type: 'path', | ||
id: id | ||
@@ -322,3 +311,9 @@ }, labels && { | ||
}; | ||
var record = function record(properties) { | ||
/** | ||
* Reduces an array of GramProperties into a map. | ||
* | ||
* @param properties | ||
*/ | ||
var reduceRecord = function reduceRecord(properties) { | ||
return properties.reduce(function (acc, p) { | ||
@@ -329,2 +324,16 @@ acc[p.name] = p.value; | ||
}; | ||
/** | ||
* Unfolds a property map<string,GramRecordValue> into a property list[GramProperty]. | ||
* | ||
* @param properties | ||
*/ | ||
var unfoldProperties = function unfoldProperties(properties) { | ||
return Object.entries(properties).reduce(function (acc, _ref) { | ||
var k = _ref[0], | ||
v = _ref[1]; | ||
acc.push(property(k, v)); | ||
return acc; | ||
}, []); | ||
}; | ||
var property = function property(name, value) { | ||
@@ -373,3 +382,3 @@ var Node = { | ||
type: 'hexadecimal', | ||
value: value | ||
value: typeof value === 'number' ? value.toString(16) : value | ||
}; | ||
@@ -380,3 +389,3 @@ }; | ||
type: 'octal', | ||
value: value | ||
value: typeof value === 'number' ? value.toString(8) : value | ||
}; | ||
@@ -392,13 +401,16 @@ }; | ||
var year = function year(value) { | ||
return tagged(value instanceof Date ? value.getFullYear().toString() : value, 'date'); | ||
return tagged('date', value instanceof Date ? value.getFullYear().toString() : value); | ||
}; | ||
var date = function date(value) { | ||
return tagged(value instanceof Date ? dateToYMD(value) : value, 'date'); | ||
return tagged('date', value instanceof Date ? dateToYMD(value) : value); | ||
}; | ||
var dayOfMonth = function dayOfMonth(value) { | ||
return tagged(value instanceof Date ? dateToDayOfMonth(value) : value, 'date'); | ||
return tagged('date', value instanceof Date ? dateToDayOfMonth(value) : value); | ||
}; | ||
var time = function time(value) { | ||
return tagged(value instanceof Date ? dateToYMD(value) : value, 'time'); | ||
return tagged('time', value instanceof Date ? value.toTimeString() : value); | ||
}; | ||
var duration = function duration(value) { | ||
return tagged('duration', value instanceof Date ? "P" + (value.getUTCFullYear() - 1970) + "Y" + value.getUTCMonth() + "M" + value.getUTCDate() + "DT" + value.getUTCHours() + "H" + value.getUTCMinutes() + "M" + value.getUTCMilliseconds() / 1000 + "S" : value); | ||
}; | ||
var flatten = function flatten(xs, depth) { | ||
@@ -415,3 +427,3 @@ if (depth === void 0) { | ||
seq: seq, | ||
unit: unit, | ||
empty: empty, | ||
cons: cons, | ||
@@ -421,3 +433,2 @@ path: path, | ||
edge: edge, | ||
record: record, | ||
property: property, | ||
@@ -434,6 +445,9 @@ "boolean": _boolean, | ||
time: time, | ||
flatten: flatten | ||
duration: duration, | ||
flatten: flatten, | ||
reduceRecord: reduceRecord, | ||
unfoldProperties: unfoldProperties | ||
}; | ||
var builder = { | ||
var gramBuilder$1 = { | ||
__proto__: null, | ||
@@ -443,8 +457,9 @@ seq: seq, | ||
cons: cons, | ||
UNIT: UNIT, | ||
unit: unit, | ||
EMPTY_PATH: EMPTY_PATH, | ||
empty: empty, | ||
node: node, | ||
edge: edge, | ||
path: path, | ||
record: record, | ||
reduceRecord: reduceRecord, | ||
unfoldProperties: unfoldProperties, | ||
property: property, | ||
@@ -463,2 +478,3 @@ boolean: _boolean, | ||
time: time, | ||
duration: duration, | ||
flatten: flatten, | ||
@@ -468,4 +484,5 @@ 'default': gramBuilder | ||
exports.UNIT = UNIT; | ||
exports.EMPTY_PATH = EMPTY_PATH; | ||
exports.boolean = _boolean; | ||
exports.builder = gramBuilder$1; | ||
exports.cons = cons; | ||
@@ -475,4 +492,5 @@ exports.date = date; | ||
exports.decimal = decimal; | ||
exports.default = builder; | ||
exports.duration = duration; | ||
exports.edge = edge; | ||
exports.empty = empty; | ||
exports.flatten = flatten; | ||
@@ -486,4 +504,4 @@ exports.hexadecimal = hexadecimal; | ||
exports.property = property; | ||
exports.record = record; | ||
exports.reduce = reduce; | ||
exports.reduceRecord = reduceRecord; | ||
exports.seq = seq; | ||
@@ -493,3 +511,3 @@ exports.string = string; | ||
exports.time = time; | ||
exports.unit = unit; | ||
exports.unfoldProperties = unfoldProperties; | ||
exports.year = year; | ||
@@ -496,0 +514,0 @@ |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((e=e||self).gram=e.gram||{},e.gram.builder={}))}(this,(function(e){"use strict";function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var n=function(e){return!!e.type&&"unit"===e.type},r=function(e){return!!e.type&&"node"===e.type};function i(e){return Array.isArray(e)?e:e instanceof Function?i(e()):void 0===e?[]:[e]}var o=function(e){return e.toISOString().slice(0,10)},a=function(e,n,r,o){return t({type:"seq"},n&&{id:n},r&&{labels:r},o&&{record:o},{children:i(e)})},u=function(e,t){void 0===e&&(e="pair");var n=i(t);return n?n.length>1?[n.reduceRight((function(t,n){return l([n,t],{relation:e})}),c)]:[n[0]]:[]},l=function(e,i){void 0===i&&(i={});var o,a=t({type:"path",id:i.id},i.labels&&{labels:i.labels},i.record&&{record:i.record},{children:e.filter((function(e){return e&&!n(e)}))});if(0===a.children.length)return a.id||a.labels&&a.labels.length>0||a.record?(a.type="node",a):c;if(1===a.children.length){var u=a.children[0];if(a.id||a.labels&&a.labels.length>0||a.record)return n(u)?(a.type="node",a.children=[],a):a;if(n(u))return u;if(r(u))return u;if("type"in(o=u)&&"relation"in o&&"edge"===o.type)return u;if(function(e){return!!e.type&&"path"===e.type}(u))return u}else if(2===a.children.length&&i.relation&&"pair"!==i.relation&&r(a.children[0])&&r(a.children[1]))return a.type="edge",a.relation=i.relation,a;return a.relation=i.relation||"pair",a},c={type:"unit",id:"0",labels:void 0,record:void 0,children:[]},d=function(){return c},f=function(e,n,r){return t({type:"node",id:e},n&&{labels:n},r&&{record:r},{children:[]})},p=function(e,n,r,i,o){return void 0===n&&(n="right"),t({type:"edge",id:r},i&&{labels:i},o&&{record:o},{relation:n,children:e})},s=function(e,n,r,i){return t({type:"path"},n&&{id:n},r&&{labels:r},i&&{record:i},{children:e})},g=function(e){return e.reduce((function(e,t){return e[t.name]=t.value,e}),{})},y=function(e,t){return{type:"property",name:e,value:t}},h=function(e){return{type:"boolean",value:e?"true":"false"}},m=function(e){return{type:"string",value:e}},v=function(e,t){return{type:"tagged",value:t,tag:e}},b=function(e){return{type:"integer",value:String(e)}},O=function(e){return{type:"decimal",value:String(e)}},S=function(e){return{type:"hexadecimal",value:e}},x=function(e){return{type:"octal",value:e}},_=function(e,t){return{type:"measurement",value:String(t),unit:e}},j=function(e){return v(e instanceof Date?e.getFullYear().toString():e,"date")},q=function(e){return v(e instanceof Date?o(e):e,"date")},D=function(e){return v(e instanceof Date?"--"+e.toISOString().slice(5,10):e,"date")},I=function(e){return v(e instanceof Date?o(e):e,"time")},M=function(e,t){return void 0===t&&(t=1),e.flat(t).filter((function(e){return null!==e}))},A={__proto__:null,seq:a,reduce:u,cons:l,UNIT:c,unit:d,node:f,edge:p,path:s,record:g,property:y,boolean:h,string:m,tagged:v,integer:b,decimal:O,hexadecimal:S,octal:x,measurement:_,year:j,date:q,dayOfMonth:D,time:I,flatten:M,default:{seq:a,unit:d,cons:l,path:s,node:f,edge:p,record:g,property:y,boolean:h,string:m,tagged:v,integer:b,decimal:O,hexadecimal:S,octal:x,measurement:_,date:q,time:I,flatten:M}};e.UNIT=c,e.boolean=h,e.cons=l,e.date=q,e.dayOfMonth=D,e.decimal=O,e.default=A,e.edge=p,e.flatten=M,e.hexadecimal=S,e.integer=b,e.measurement=_,e.node=f,e.octal=x,e.path=s,e.property=y,e.record=g,e.reduce=u,e.seq=a,e.string=m,e.tagged=v,e.time=I,e.unit=d,e.year=j,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(((e=e||self).gram=e.gram||{},e.gram.builder={}))}(this,(function(e){"use strict";function t(){return(t=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var n=function(e){return!!e.type&&"path"===e.type},r=function(e){return n(e)&&void 0===e.children&&"ø"===e.id},i=function(e){return n(e)&&e.children&&0===e.children.length&&"ø"!==e.id};function o(e){return Array.isArray(e)?e:e instanceof Function?o(e()):void 0===e?[]:[e]}var u=function(e,n,r,i){return t({type:"seq",id:n},r&&{labels:r},i&&{record:i},{children:o(e)})},a=function(e,t,n){return void 0===e&&(e="pair"),t.length>1?t.reduceRight((function(t,r){return d([r,t],{relation:e,id:n?""+n+0:void 0})}),l):t[0]},d=function(e,n){void 0===n&&(n={});var o=t({type:"path"},n.id&&{id:n.id},n.labels&&{labels:n.labels},n.record&&{record:n.record});if(void 0===e)return o.id&&"ø"!==o.id?(o.children=[],o):(o.children=void 0,l);if(0===e.length)return"ø"===o.id?l:(o.children=[],o);if(1===e.length){var u=e[0],a=l;return r(u)?(o.children=[],o):(o.children=[u,a],o)}if(2===e.length){if(n.relation&&"pair"!==n.relation&&i(e[0])&&i(e[1]))return o.relation=n.relation,o.children=[e[0],e[1]],o;if(r(e[0])&&r(e[1]))return o.relation=n.relation,o.children=[],o;o.children=[e[0],e[1]]}return o.relation=n.relation||"pair",o},l={type:"path",id:"ø",labels:void 0,record:void 0,children:void 0},c=function(){return l},f=function(e,n,r){return t({type:"path"},e&&{id:e},n&&{labels:n},r&&{record:r},{children:[]})},p=function(e,n,r,i,o){return t({type:"path",id:r},i&&{labels:i},o&&{record:o},{relation:n,children:e})},s=function(e,n,r,i){return t({type:"path",id:n},r&&{labels:r},i&&{record:i},{children:e})},g=function(e){return e.reduce((function(e,t){return e[t.name]=t.value,e}),{})},h=function(e){return Object.entries(e).reduce((function(e,t){return e.push(y(t[0],t[1])),e}),[])},y=function(e,t){return{type:"property",name:e,value:t}},m=function(e){return{type:"boolean",value:e?"true":"false"}},v=function(e){return{type:"string",value:e}},b=function(e,t){return{type:"tagged",value:t,tag:e}},S=function(e){return{type:"integer",value:String(e)}},T=function(e){return{type:"decimal",value:String(e)}},M=function(e){return{type:"hexadecimal",value:"number"==typeof e?e.toString(16):e}},P=function(e){return{type:"octal",value:"number"==typeof e?e.toString(8):e}},O=function(e,t){return{type:"measurement",value:String(t),unit:e}},_=function(e){return b("date",e instanceof Date?e.getFullYear().toString():e)},x=function(e){return b("date",e instanceof Date?e.toISOString().slice(0,10):e)},D=function(e){return b("date",e instanceof Date?"--"+e.toISOString().slice(5,10):e)},C=function(e){return b("time",e instanceof Date?e.toTimeString():e)},U=function(e){return b("duration",e instanceof Date?"P"+(e.getUTCFullYear()-1970)+"Y"+e.getUTCMonth()+"M"+e.getUTCDate()+"DT"+e.getUTCHours()+"H"+e.getUTCMinutes()+"M"+e.getUTCMilliseconds()/1e3+"S":e)},j=function(e,t){return void 0===t&&(t=1),e.flat(t).filter((function(e){return null!==e}))},Y={__proto__:null,seq:u,reduce:a,cons:d,EMPTY_PATH:l,empty:c,node:f,edge:p,path:s,reduceRecord:g,unfoldProperties:h,property:y,boolean:m,string:v,tagged:b,integer:S,decimal:T,hexadecimal:M,octal:P,measurement:O,year:_,date:x,dayOfMonth:D,time:C,duration:U,flatten:j,default:{seq:u,empty:c,cons:d,path:s,node:f,edge:p,property:y,boolean:m,string:v,tagged:b,integer:S,decimal:T,hexadecimal:M,octal:P,measurement:O,date:x,time:C,duration:U,flatten:j,reduceRecord:g,unfoldProperties:h}};e.EMPTY_PATH=l,e.boolean=m,e.builder=Y,e.cons=d,e.date=x,e.dayOfMonth=D,e.decimal=T,e.duration=U,e.edge=p,e.empty=c,e.flatten=j,e.hexadecimal=M,e.integer=S,e.measurement=O,e.node=f,e.octal=P,e.path=s,e.property=y,e.reduce=a,e.reduceRecord=g,e.seq=u,e.string=v,e.tagged=b,e.time=C,e.unfoldProperties=h,e.year=_,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=gram-builder.umd.production.min.js.map |
{ | ||
"name": "@gram-data/gram-builder", | ||
"version": "0.1.10", | ||
"version": "0.2.3", | ||
"license": "MIT", | ||
@@ -21,5 +21,5 @@ "main": "dist/index.js", | ||
"lint:fix": "tsdx lint --fix", | ||
"prepare": "run-s build", | ||
"build:demo": "shx cp dist/gram-builder.umd.development.* ./public", | ||
"demo": "serve ./public/" | ||
"demo": "serve ./public/", | ||
"prepare": "npm run build" | ||
}, | ||
@@ -40,7 +40,7 @@ "husky": { | ||
"devDependencies": { | ||
"@types/jest": "^26.0.4", | ||
"@types/unist": "^2.0.3", | ||
"@typescript-eslint/parser": "^3.5.0", | ||
"@typescript-eslint/parser": "^3.6.1", | ||
"husky": "^4.2.5", | ||
"npm-run-all": "^4.1.5", | ||
"rollup-plugin-node-builtins": "^2.1.2", | ||
"serve": "^11.3.2", | ||
@@ -50,3 +50,3 @@ "shx": "^0.3.2", | ||
"tslib": "^2.0.0", | ||
"typescript": "^3.9.6", | ||
"typescript": "^3.9.7", | ||
"unist-util-inspect": "^6.0.0", | ||
@@ -56,5 +56,5 @@ "unist-util-size": "^2.0.0" | ||
"dependencies": { | ||
"@gram-data/gram-ast": "^0.1.10" | ||
"@gram-data/gram-ast": "^0.2.3" | ||
}, | ||
"gitHead": "0df0d0a9641c40097de62219503f626d3913de9c", | ||
"gitHead": "d68a2b321b806f9c74b6446ead81d234ee85dff3", | ||
"publishConfig": { | ||
@@ -61,0 +61,0 @@ "access": "public" |
@@ -14,3 +14,3 @@ /** | ||
GramProperty, | ||
GramLiteral, | ||
GramRecordValue, | ||
Relation, | ||
@@ -28,8 +28,10 @@ Navigation, | ||
GramPathlike, | ||
GramUnit, | ||
GramEmptyPath, | ||
isGramNode, | ||
isGramUnit, | ||
isGramEdge, | ||
isGramPath, | ||
UNIT_ID, | ||
isGramEmptyPath, | ||
EMPTY_PATH_ID, | ||
GramPropertyMap, | ||
DateLiteral, | ||
TimeLiteral, | ||
DurationLiteral, | ||
} from '@gram-data/gram-ast'; | ||
@@ -66,3 +68,3 @@ | ||
export const seq = ( | ||
paths: Children<GramPathlike>, | ||
paths: Children<GramPath>, | ||
id?: string, | ||
@@ -73,9 +75,8 @@ labels?: string[], | ||
type: 'seq', | ||
...(id && { id }), | ||
id, | ||
...(labels && { labels }), | ||
...(record && { record }), | ||
children: normalizeChildren<GramPathlike>(paths), | ||
children: normalizeChildren<GramPath>(paths), | ||
}); | ||
export interface PathAttributes { | ||
@@ -85,30 +86,30 @@ id?: string; | ||
record?: GramRecord; | ||
relation?: Relation | ||
relation?: Relation; | ||
} | ||
/** | ||
* Reduce paths into a single path composed using the given relation. | ||
* Reduce a list of paths into a single path composed using the given relation. | ||
* | ||
* @parm relation the relation to apply to all sub-paths | ||
* @param paths sub-paths to be paired | ||
* @param relation the relation to apply to all sub-paths | ||
* @param pathlist sub-paths to be paired | ||
* @param baseID the baseID from which path expressions will derive new IDs | ||
*/ | ||
export const reduce = ( | ||
relation: Relation = 'pair', | ||
paths: Children<GramPathlike>, | ||
): [GramPathlike] | [] => { | ||
const pathlist = normalizeChildren(paths); | ||
if (pathlist) { | ||
if (pathlist.length > 1) { | ||
return [ | ||
pathlist.reduceRight( (acc, curr) => { return cons([curr,acc], {relation}) }, UNIT) | ||
]; | ||
} else { | ||
return [pathlist[0]]; | ||
} | ||
pathlist: GramPath[], | ||
baseID?: string, | ||
): GramPathlike => { | ||
let subID = 0; | ||
if (pathlist.length > 1) { | ||
return pathlist.reduceRight((acc:GramPathlike, curr) => { | ||
const childID = baseID ? `${baseID}${subID}` : undefined; | ||
return cons([curr, acc], { relation, id:childID }); | ||
}, EMPTY_PATH); | ||
} else { | ||
return pathlist[0]; | ||
} | ||
return []; | ||
} | ||
}; | ||
/** | ||
* Build any path-like element | ||
* Build a path. | ||
* | ||
@@ -119,3 +120,3 @@ * @param members sub-paths to compose | ||
export const cons = ( | ||
members: [] | [GramPathlike] | [GramPathlike, GramPathlike], | ||
members?: [] | [GramPathlike] | [GramPathlike, GramPathlike], | ||
attributes: PathAttributes = {} | ||
@@ -125,44 +126,51 @@ ): GramPathlike => { | ||
type: 'path', | ||
id: attributes.id, | ||
...(attributes.id && { id: attributes.id }), | ||
...(attributes.labels && { labels: attributes.labels }), | ||
...(attributes.record && { record: attributes.record }), | ||
children: members.filter(child => child && !isGramUnit(child)) | ||
// children: members ? members.filter(child => child && !isGramEmptyPath(child)) : undefined, | ||
}; | ||
if (element.children.length === 0) { | ||
if (element.id || (element.labels && element.labels.length > 0) || element.record ) { | ||
element.type = 'node'; | ||
// element.id = element.id || identity.shortID(); | ||
if (members === undefined) { | ||
if (element.id && (element.id !== EMPTY_PATH_ID)) { | ||
element.children = []; | ||
return element; | ||
} | ||
element.children = undefined; | ||
return EMPTY_PATH; | ||
} else if (members.length === 0) { | ||
if (element.id === EMPTY_PATH_ID) { | ||
return EMPTY_PATH; | ||
} | ||
element.children = []; | ||
return element as GramNode; | ||
} else if (members.length === 1) { | ||
const lhs = members[0]; | ||
const rhs = EMPTY_PATH; | ||
if (isGramEmptyPath(lhs)) { | ||
element.children = []; | ||
return element as GramNode; | ||
} else { | ||
return UNIT; | ||
} | ||
} else if (element.children.length === 1) { | ||
const inner = element.children[0]; | ||
if (element.id || (element.labels && element.labels.length > 0) || element.record ) { | ||
if (isGramUnit(inner)) { | ||
element.type = 'node'; | ||
element.children = []; | ||
return element as GramNode; | ||
} | ||
element.children = [lhs, rhs]; | ||
return element as GramPath; | ||
} else { | ||
if (isGramUnit(inner)) return inner as GramUnit; | ||
// element.id = identity.shortID(); | ||
if (isGramNode(inner)) return inner as GramNode; | ||
if (isGramEdge(inner)) return inner as GramEdge; | ||
if (isGramPath(inner)) return inner as GramPath; | ||
} | ||
} else if (element.children.length === 2) { | ||
} else if (members.length === 2) { | ||
if ( | ||
attributes.relation && attributes.relation !== 'pair' && | ||
isGramNode(element.children[0]) && | ||
isGramNode(element.children[1]) | ||
attributes.relation && | ||
attributes.relation !== 'pair' && | ||
isGramNode(members[0]) && | ||
isGramNode(members[1]) | ||
) { | ||
element.type = 'edge'; | ||
// element.id = element.id || identity.shortID(); | ||
element.relation = attributes.relation; | ||
element.children =[members[0], members[1]]; | ||
return element as GramEdge; | ||
} else if ( | ||
isGramEmptyPath(members[0]) && | ||
isGramEmptyPath(members[1]) | ||
) { | ||
element.relation = attributes.relation; | ||
element.children = []; | ||
return element as GramNode; | ||
} | ||
element.children = [members[0], members[1]]; | ||
} | ||
// element.id = element.id || identity.shortID(); | ||
element.relation = attributes.relation || 'pair'; | ||
@@ -173,16 +181,16 @@ return element as GramPath; | ||
/** | ||
* Singleton instance of GramUnit | ||
* Singleton instance of GramEmptyPath | ||
*/ | ||
export const UNIT: GramUnit = { | ||
type: 'unit', | ||
id: UNIT_ID, | ||
export const EMPTY_PATH: GramEmptyPath = { | ||
type: 'path', | ||
id: EMPTY_PATH_ID, | ||
labels: undefined, | ||
record: undefined, | ||
children: [], | ||
children: undefined | ||
}; | ||
/** | ||
* Convenience function for retrieving the singleton GramUnit. | ||
* Convenience function for retrieving the singleton GramEmptyPath. | ||
*/ | ||
export const unit = (): GramUnit => UNIT; | ||
export const empty = (): GramEmptyPath => EMPTY_PATH; | ||
@@ -202,5 +210,4 @@ /** | ||
): GramNode => ({ | ||
type: 'node', | ||
// id: id || identity.shortID(), | ||
id, | ||
type: 'path', | ||
...(id && { id }), | ||
...(labels && { labels }), | ||
@@ -222,3 +229,3 @@ ...(record && { record }), | ||
children: [GramNode, GramNode], | ||
relation: Navigation = 'right', | ||
relation: Navigation, | ||
id?: string, | ||
@@ -228,4 +235,3 @@ labels?: string[], | ||
): GramEdge => ({ | ||
type: 'edge', | ||
// id: id || identity.shortID(), | ||
type: 'path', | ||
id, | ||
@@ -247,3 +253,3 @@ ...(labels && { labels }), | ||
export const path = ( | ||
members: [GramPathlike] | [GramPathlike, GramPathlike], | ||
members: [GramPath] | [GramPath, GramPath], | ||
id?: string, | ||
@@ -254,3 +260,3 @@ labels?: string[], | ||
type: 'path', | ||
...(id && { id }), | ||
id, | ||
...(labels && { labels }), | ||
@@ -261,12 +267,29 @@ ...(record && { record }), | ||
export const record = (properties: GramProperty[]): GramRecord => { | ||
return properties.reduce((acc: GramRecord, p: GramProperty) => { | ||
/** | ||
* Reduces an array of GramProperties into a map. | ||
* | ||
* @param properties | ||
*/ | ||
export const reduceRecord = (properties: GramRecord): GramPropertyMap => { | ||
return properties.reduce((acc: GramPropertyMap, p: GramProperty) => { | ||
acc[p.name] = p.value; | ||
return acc; | ||
}, {} as GramRecord); | ||
}, {} as GramPropertyMap); | ||
}; | ||
/** | ||
* Unfolds a property map<string,GramRecordValue> into a property list[GramProperty]. | ||
* | ||
* @param properties | ||
*/ | ||
export const unfoldProperties = (properties: GramPropertyMap): GramRecord => { | ||
return Object.entries(properties).reduce((acc: GramRecord, [k, v]) => { | ||
acc.push(property(k, v)); | ||
return acc; | ||
}, [] as GramRecord); | ||
}; | ||
export const property = ( | ||
name: string, | ||
value: GramLiteral | GramLiteral[] | ||
value: GramRecordValue | ||
): GramProperty => { | ||
@@ -307,10 +330,10 @@ const Node: GramProperty = { | ||
export const hexadecimal = (value: string): HexadecimalLiteral => ({ | ||
export const hexadecimal = (value: string | number): HexadecimalLiteral => ({ | ||
type: 'hexadecimal', | ||
value, | ||
value: typeof value === 'number' ? value.toString(16) : value, | ||
}); | ||
export const octal = (value: string): OctalLiteral => ({ | ||
export const octal = (value: string | number): OctalLiteral => ({ | ||
type: 'octal', | ||
value, | ||
value: typeof value === 'number' ? value.toString(8) : value, | ||
}); | ||
@@ -327,17 +350,36 @@ | ||
export const year = (value: string | Date): TaggedLiteral => | ||
export const year = (value: string | Date): DateLiteral => | ||
tagged( | ||
value instanceof Date ? value.getFullYear().toString() : value, | ||
'date' | ||
); | ||
'date', | ||
value instanceof Date ? value.getFullYear().toString() : value | ||
) as DateLiteral; | ||
export const date = (value: string | Date): TaggedLiteral => | ||
tagged(value instanceof Date ? dateToYMD(value) : value, 'date'); | ||
export const date = (value: string | Date): DateLiteral => | ||
tagged( | ||
'date', | ||
value instanceof Date ? dateToYMD(value) : value | ||
) as DateLiteral; | ||
export const dayOfMonth = (value: string | Date): TaggedLiteral => | ||
tagged(value instanceof Date ? dateToDayOfMonth(value) : value, 'date'); | ||
export const dayOfMonth = (value: string | Date): DateLiteral => | ||
tagged( | ||
'date', | ||
value instanceof Date ? dateToDayOfMonth(value) : value | ||
) as DateLiteral; | ||
export const time = (value: string | Date): TaggedLiteral => | ||
tagged(value instanceof Date ? dateToYMD(value) : value, 'time'); | ||
export const time = (value: string | Date): TimeLiteral => | ||
tagged( | ||
'time', | ||
value instanceof Date ? value.toTimeString() : value | ||
) as TimeLiteral; | ||
export const duration = (value: string | Date): DurationLiteral => | ||
tagged( | ||
'duration', | ||
value instanceof Date | ||
? `P${value.getUTCFullYear() - | ||
1970}Y${value.getUTCMonth()}M${value.getUTCDate()}DT${value.getUTCHours()}H${value.getUTCMinutes()}M${value.getUTCMilliseconds() / | ||
1000}S` | ||
: value | ||
) as DurationLiteral; | ||
export const flatten = (xs: any[], depth = 1) => | ||
@@ -348,3 +390,3 @@ xs.flat(depth).filter(x => x !== null); | ||
seq, | ||
unit, | ||
empty, | ||
cons, | ||
@@ -354,3 +396,2 @@ path, | ||
edge, | ||
record, | ||
property, | ||
@@ -367,3 +408,6 @@ boolean, | ||
time, | ||
duration, | ||
flatten, | ||
reduceRecord, | ||
unfoldProperties, | ||
}; |
@@ -5,2 +5,2 @@ import * as builder from './gram-builder'; | ||
export default builder; | ||
export {builder}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
141284
1758
+ Added@gram-data/gram-ast@0.2.18(transitive)
- Removed@gram-data/gram-ast@0.1.10(transitive)
Updated@gram-data/gram-ast@^0.2.3