@gram-data/gram-builder
Advanced tools
Comparing version 0.3.8 to 0.3.9-alpha.2
@@ -252,32 +252,111 @@ 'use strict'; | ||
/** | ||
* Reduces an array of GramProperties into a map. | ||
* Create a new, empty GramRecord. | ||
* | ||
*/ | ||
var emptyRecord = function emptyRecord() { | ||
return new Map(); | ||
}; | ||
/** | ||
* Reduces an array of GramProperties into a GramRecord. | ||
* | ||
* @param properties | ||
*/ | ||
var recordToMap = function recordToMap(properties) { | ||
var propertiesToRecord = function propertiesToRecord(properties) { | ||
return properties.reduce(function (acc, p) { | ||
acc[p.name] = p.value; | ||
acc.set(p.name, p.value); | ||
return acc; | ||
}, {}); | ||
}, emptyRecord()); | ||
}; | ||
/** | ||
* Unfolds a property map<string,GramRecordValue> into a property list[GramProperty]. | ||
* Transforms a plain js object into a GramRecord. | ||
* | ||
* @param properties | ||
* @param o | ||
*/ | ||
var mapToRecord = function mapToRecord(properties) { | ||
return Object.entries(properties).reduce(function (acc, _ref) { | ||
var objectToRecord = function objectToRecord(o) { | ||
return Object.entries(o).reduce(function (acc, _ref) { | ||
var k = _ref[0], | ||
v = _ref[1]; | ||
acc.push(property(k, v)); | ||
acc.set(k, propertyValue(v)); | ||
return acc; | ||
}, []); | ||
}, emptyRecord()); | ||
}; | ||
var pluck = function pluck(properties, path) { | ||
return properties.reduce(function (acc, prop) { | ||
return prop.name === path ? prop : acc; | ||
}); | ||
/** | ||
* Extracts the value from a GramLiteral, if available. | ||
* | ||
* @param l | ||
*/ | ||
var getValue = function getValue(l) { | ||
return gramAst.isGramLiteral(l) ? l.value : undefined; | ||
}; | ||
/** | ||
* Produces a Lens into a literal value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
var getLiteral = function getLiteral(name) { | ||
return function (v) { | ||
var l = v.get(name); | ||
return getValue(l); | ||
}; | ||
}; | ||
/** | ||
* Produces a Lens into a record value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
var getRecord = function getRecord(name) { | ||
return function (r) { | ||
var v = r.get(name); | ||
return gramAst.isGramRecord(v) ? v : undefined; | ||
}; | ||
}; | ||
/** | ||
* Produces a Lens down into nested GramRecords. | ||
* | ||
* ### Examples: | ||
* | ||
* Descend using either an array of names, or dot notation. | ||
* | ||
* ``` | ||
* const o = g.objectToRecord({a:{b:{c:g.string("value")}}}) | ||
* | ||
* const getAbc1 = g.getDown(['a','b','c']); | ||
* const getAbc2 = g.getDown("a.b.c"); | ||
* | ||
* expect(getAbc1(o)).toStrictEqual(getAbc2(o)); | ||
* ``` | ||
* | ||
* Descend, then apply a function to extract the text value. | ||
* | ||
* ``` | ||
* const o = objectToRecord({a:{b:{c:string("value")}}}) | ||
* const getAbc = getDown("a.b.c", getValue); | ||
* | ||
* expect(getAbc(o)).toBe("value"); | ||
* ``` | ||
* | ||
* @param hierarchy array or dot-notation path to descend | ||
*/ | ||
var getDown = function getDown(hierarchy, f) { | ||
var pathDown = Array.isArray(hierarchy) ? hierarchy : hierarchy.split('.'); | ||
return function (r) { | ||
var bottom = pathDown.reduce(function (acc, name) { | ||
return gramAst.isGramRecord(acc) ? acc.get(name) : undefined; | ||
}, r); | ||
return bottom && (f ? f(bottom) : bottom); | ||
}; | ||
}; | ||
/** | ||
* Builds a GramProperty from a name | ||
* @param name | ||
* @param value | ||
*/ | ||
var property = function property(name, value) { | ||
@@ -287,7 +366,42 @@ var Node = { | ||
name: name, | ||
value: value | ||
value: gramAst.isGramLiteral(value) ? value : propertyValue(value) | ||
}; | ||
return Node; | ||
}; | ||
var propertyValue = function propertyValue(value) { | ||
if (Array.isArray(value)) { | ||
return value.map(function (v) { | ||
return propertyValue(v); | ||
}); | ||
} else if (typeof value === 'object') { | ||
if (value instanceof Date) { | ||
return date(value); | ||
} else if (gramAst.isGramLiteral(value)) { | ||
return value; | ||
} | ||
return objectToRecord(value); | ||
} else { | ||
switch (typeof value) { | ||
case 'string': | ||
return string(value); | ||
case 'bigint': | ||
return decimal(value.toString()); | ||
case 'boolean': | ||
return _boolean(value); | ||
case 'number': | ||
return decimal(value.toString()); | ||
case 'symbol': | ||
return string(value.toString()); | ||
default: | ||
throw new Error("Unsupported value: " + value); | ||
} | ||
} | ||
}; | ||
var _boolean = function _boolean(value) { | ||
@@ -388,4 +502,6 @@ return { | ||
flatten: flatten, | ||
recordToMap: recordToMap, | ||
mapToRecord: mapToRecord | ||
objectToRecord: objectToRecord, | ||
propertiesToRecord: propertiesToRecord, | ||
propertyValue: propertyValue, | ||
fromLiteral: getLiteral | ||
}; | ||
@@ -402,15 +518,20 @@ | ||
exports.empty = empty; | ||
exports.emptyRecord = emptyRecord; | ||
exports.flatten = flatten; | ||
exports.getDown = getDown; | ||
exports.getLiteral = getLiteral; | ||
exports.getRecord = getRecord; | ||
exports.getValue = getValue; | ||
exports.hexadecimal = hexadecimal; | ||
exports.integer = integer; | ||
exports.listToPath = listToPath; | ||
exports.mapToRecord = mapToRecord; | ||
exports.measurement = measurement; | ||
exports.node = node; | ||
exports.objectToRecord = objectToRecord; | ||
exports.octal = octal; | ||
exports.pair = pair; | ||
exports.path = path; | ||
exports.pluck = pluck; | ||
exports.propertiesToRecord = propertiesToRecord; | ||
exports.property = property; | ||
exports.recordToMap = recordToMap; | ||
exports.propertyValue = propertyValue; | ||
exports.seq = seq; | ||
@@ -417,0 +538,0 @@ exports.string = string; |
@@ -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,n,i,o){return t({type:"seq",id:n},i&&{labels:i},o&&{record:o},{children:r(e)})},i=function(e,t){return void 0===e&&(e="pair"),t.length>1?t.slice(0,t.length-1).reduceRight((function(t,r){return o([r,t],{kind:e})}),t[t.length-1]):t[0]},o=function(r,n){void 0===n&&(n={});var i=t({type:"path"},n.id&&{id:n.id},n.labels&&{labels:n.labels},n.record&&{record:n.record});if(void 0===r)return i.id&&i.id!==e.EMPTY_PATH_ID?(i.children=[],i):(i.children=void 0,a);if(0===r.length)return i.id===e.EMPTY_PATH_ID?a:(i.children=[],i);if(1===r.length){var o=r[0];return e.isGramEmptyPath(o)?(i.children=[],i):(i.children=[o],i)}if(2===r.length){if(n.kind&&"pair"!==n.kind&&e.isGramNode(r[0])&&e.isGramNode(r[1]))return i.kind=n.kind,i.children=[r[0],r[1]],i;if(e.isGramEmptyPath(r[0])&&e.isGramEmptyPath(r[1]))return i.kind=n.kind,i.children=[],i;i.children=[r[0],r[1]]}return i.kind=n.kind||"pair",i},a={type:"path",id:e.EMPTY_PATH_ID,labels:void 0,record:void 0,children:[]},u=function(){return a},c=function(e,r,n){return t({type:"path"},e&&{id:e},r&&{labels:r},n&&{record:n},{children:[]})},d=function(e,r,n,i,o){return t({type:"path",id:n},i&&{labels:i},o&&{record:o},{kind:r,children:e})},l=function(e,r,n,i,o){return t({type:"path",kind:e,id:n},i&&{labels:i},o&&{record:o},{children:r})},p=function(e,t,r,n){return l("pair",e,t,r,n)},s=function(e){return e.reduce((function(e,t){return e[t.name]=t.value,e}),{})},f=function(e){return Object.entries(e).reduce((function(e,t){return e.push(g(t[0],t[1])),e}),[])},g=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}},y=function(e,t){return{type:"tagged",value:t,tag:e}},x=function(e){return{type:"integer",value:String(e)}},v=function(e){return{type:"decimal",value:String(e)}},T=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}},k=function(e){return y("date",e instanceof Date?e.toISOString().slice(0,10):e)},M=function(e){return y("time",e instanceof Date?e.toTimeString():e)},S=function(e){return y("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)},D=function(e,t){return void 0===t&&(t=1),e.flat(t).filter((function(e){return null!==e}))},O={seq:n,empty:u,cons:o,pair:p,listToPath:i,node:c,edge:d,property:g,boolean:h,string:m,tagged:y,integer:x,decimal:v,hexadecimal:T,octal:b,measurement:P,date:k,time:M,duration:S,flatten:D,recordToMap:s,mapToRecord:f};exports.boolean=h,exports.cons=o,exports.date=k,exports.dayOfMonth=function(e){return y("date",e instanceof Date?"--"+e.toISOString().slice(5,10):e)},exports.decimal=v,exports.default=O,exports.duration=S,exports.edge=d,exports.empty=u,exports.flatten=D,exports.hexadecimal=T,exports.integer=x,exports.listToPath=i,exports.mapToRecord=f,exports.measurement=P,exports.node=c,exports.octal=b,exports.pair=p,exports.path=l,exports.pluck=function(e,t){return e.reduce((function(e,r){return r.name===t?r:e}))},exports.property=g,exports.recordToMap=s,exports.seq=n,exports.string=m,exports.tagged=y,exports.time=M,exports.year=function(e){return y("date",e instanceof Date?e.getFullYear().toString():e)}; | ||
"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,i,o){return t({type:"seq",id:n},i&&{labels:i},o&&{record:o},{children:r(e)})},i=function(e,t){return void 0===e&&(e="pair"),t.length>1?t.slice(0,t.length-1).reduceRight((function(t,r){return o([r,t],{kind:e})}),t[t.length-1]):t[0]},o=function(r,n){void 0===n&&(n={});var i=t({type:"path"},n.id&&{id:n.id},n.labels&&{labels:n.labels},n.record&&{record:n.record});if(void 0===r)return i.id&&i.id!==e.EMPTY_PATH_ID?(i.children=[],i):(i.children=void 0,a);if(0===r.length)return i.id===e.EMPTY_PATH_ID?a:(i.children=[],i);if(1===r.length){var o=r[0];return e.isGramEmptyPath(o)?(i.children=[],i):(i.children=[o],i)}if(2===r.length){if(n.kind&&"pair"!==n.kind&&e.isGramNode(r[0])&&e.isGramNode(r[1]))return i.kind=n.kind,i.children=[r[0],r[1]],i;if(e.isGramEmptyPath(r[0])&&e.isGramEmptyPath(r[1]))return i.kind=n.kind,i.children=[],i;i.children=[r[0],r[1]]}return i.kind=n.kind||"pair",i},a={type:"path",id:e.EMPTY_PATH_ID,labels:void 0,record:void 0,children:[]},u=function(){return a},c=function(e,r,n){return t({type:"path"},e&&{id:e},r&&{labels:r},n&&{record:n},{children:[]})},s=function(e,r,n,i,o){return t({type:"path",id:n},i&&{labels:i},o&&{record:o},{kind:r,children:e})},d=function(e,r,n,i,o){return t({type:"path",kind:e,id:n},i&&{labels:i},o&&{record:o},{children:r})},l=function(e,t,r,n){return d("pair",e,t,r,n)},p=function(){return new Map},f=function(e){return e.reduce((function(e,t){return e.set(t.name,t.value),e}),p())},g=function(e){return Object.entries(e).reduce((function(e,t){return e.set(t[0],x(t[1])),e}),p())},y=function(t){return e.isGramLiteral(t)?t.value:void 0},m=function(e){return function(t){var r=t.get(e);return y(r)}},h=function(t,r){return{type:"property",name:t,value:e.isGramLiteral(r)?r:x(r)}},x=function t(r){if(Array.isArray(r))return r.map((function(e){return t(e)}));if("object"==typeof r)return r instanceof Date?G(r):e.isGramLiteral(r)?r:g(r);switch(typeof r){case"string":return b(r);case"bigint":return P(r.toString());case"boolean":return v(r);case"number":return P(r.toString());case"symbol":return b(r.toString());default:throw new Error("Unsupported value: "+r)}},v=function(e){return{type:"boolean",value:e?"true":"false"}},b=function(e){return{type:"string",value:e}},T=function(e,t){return{type:"tagged",value:t,tag:e}},S=function(e){return{type:"integer",value:String(e)}},P=function(e){return{type:"decimal",value:String(e)}},D=function(e){return{type:"hexadecimal",value:"number"==typeof e?e.toString(16):e}},k=function(e){return{type:"octal",value:"number"==typeof e?e.toString(8):e}},M=function(e,t){return{type:"measurement",value:String(t),unit:e}},G=function(e){return T("date",e instanceof Date?e.toISOString().slice(0,10):e)},A=function(e){return T("time",e instanceof Date?e.toTimeString():e)},R=function(e){return T("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}))},_={seq:n,empty:u,cons:o,pair:l,listToPath:i,node:c,edge:s,property:h,boolean:v,string:b,tagged:T,integer:S,decimal:P,hexadecimal:D,octal:k,measurement:M,date:G,time:A,duration:R,flatten:O,objectToRecord:g,propertiesToRecord:f,propertyValue:x,fromLiteral:m};exports.boolean=v,exports.cons=o,exports.date=G,exports.dayOfMonth=function(e){return T("date",e instanceof Date?"--"+e.toISOString().slice(5,10):e)},exports.decimal=P,exports.default=_,exports.duration=R,exports.edge=s,exports.empty=u,exports.emptyRecord=p,exports.flatten=O,exports.getDown=function(t,r){var n=Array.isArray(t)?t:t.split(".");return function(t){var i=n.reduce((function(t,r){return e.isGramRecord(t)?t.get(r):void 0}),t);return i&&(r?r(i):i)}},exports.getLiteral=m,exports.getRecord=function(t){return function(r){var n=r.get(t);return e.isGramRecord(n)?n:void 0}},exports.getValue=y,exports.hexadecimal=D,exports.integer=S,exports.listToPath=i,exports.measurement=M,exports.node=c,exports.objectToRecord=g,exports.octal=k,exports.pair=l,exports.path=d,exports.propertiesToRecord=f,exports.property=h,exports.propertyValue=x,exports.seq=n,exports.string=b,exports.tagged=T,exports.time=A,exports.year=function(e){return T("date",e instanceof Date?e.getFullYear().toString():e)}; | ||
//# sourceMappingURL=gram-builder.cjs.production.min.js.map |
@@ -1,2 +0,2 @@ | ||
import { EMPTY_PATH_ID, isGramEmptyPath, isGramNode } from '@gram-data/gram-ast'; | ||
import { EMPTY_PATH_ID, isGramEmptyPath, isGramNode, isGramLiteral, isGramRecord } from '@gram-data/gram-ast'; | ||
@@ -248,32 +248,111 @@ function _extends() { | ||
/** | ||
* Reduces an array of GramProperties into a map. | ||
* Create a new, empty GramRecord. | ||
* | ||
*/ | ||
var emptyRecord = function emptyRecord() { | ||
return new Map(); | ||
}; | ||
/** | ||
* Reduces an array of GramProperties into a GramRecord. | ||
* | ||
* @param properties | ||
*/ | ||
var recordToMap = function recordToMap(properties) { | ||
var propertiesToRecord = function propertiesToRecord(properties) { | ||
return properties.reduce(function (acc, p) { | ||
acc[p.name] = p.value; | ||
acc.set(p.name, p.value); | ||
return acc; | ||
}, {}); | ||
}, emptyRecord()); | ||
}; | ||
/** | ||
* Unfolds a property map<string,GramRecordValue> into a property list[GramProperty]. | ||
* Transforms a plain js object into a GramRecord. | ||
* | ||
* @param properties | ||
* @param o | ||
*/ | ||
var mapToRecord = function mapToRecord(properties) { | ||
return Object.entries(properties).reduce(function (acc, _ref) { | ||
var objectToRecord = function objectToRecord(o) { | ||
return Object.entries(o).reduce(function (acc, _ref) { | ||
var k = _ref[0], | ||
v = _ref[1]; | ||
acc.push(property(k, v)); | ||
acc.set(k, propertyValue(v)); | ||
return acc; | ||
}, []); | ||
}, emptyRecord()); | ||
}; | ||
var pluck = function pluck(properties, path) { | ||
return properties.reduce(function (acc, prop) { | ||
return prop.name === path ? prop : acc; | ||
}); | ||
/** | ||
* Extracts the value from a GramLiteral, if available. | ||
* | ||
* @param l | ||
*/ | ||
var getValue = function getValue(l) { | ||
return isGramLiteral(l) ? l.value : undefined; | ||
}; | ||
/** | ||
* Produces a Lens into a literal value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
var getLiteral = function getLiteral(name) { | ||
return function (v) { | ||
var l = v.get(name); | ||
return getValue(l); | ||
}; | ||
}; | ||
/** | ||
* Produces a Lens into a record value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
var getRecord = function getRecord(name) { | ||
return function (r) { | ||
var v = r.get(name); | ||
return isGramRecord(v) ? v : undefined; | ||
}; | ||
}; | ||
/** | ||
* Produces a Lens down into nested GramRecords. | ||
* | ||
* ### Examples: | ||
* | ||
* Descend using either an array of names, or dot notation. | ||
* | ||
* ``` | ||
* const o = g.objectToRecord({a:{b:{c:g.string("value")}}}) | ||
* | ||
* const getAbc1 = g.getDown(['a','b','c']); | ||
* const getAbc2 = g.getDown("a.b.c"); | ||
* | ||
* expect(getAbc1(o)).toStrictEqual(getAbc2(o)); | ||
* ``` | ||
* | ||
* Descend, then apply a function to extract the text value. | ||
* | ||
* ``` | ||
* const o = objectToRecord({a:{b:{c:string("value")}}}) | ||
* const getAbc = getDown("a.b.c", getValue); | ||
* | ||
* expect(getAbc(o)).toBe("value"); | ||
* ``` | ||
* | ||
* @param hierarchy array or dot-notation path to descend | ||
*/ | ||
var getDown = function getDown(hierarchy, f) { | ||
var pathDown = Array.isArray(hierarchy) ? hierarchy : hierarchy.split('.'); | ||
return function (r) { | ||
var bottom = pathDown.reduce(function (acc, name) { | ||
return isGramRecord(acc) ? acc.get(name) : undefined; | ||
}, r); | ||
return bottom && (f ? f(bottom) : bottom); | ||
}; | ||
}; | ||
/** | ||
* Builds a GramProperty from a name | ||
* @param name | ||
* @param value | ||
*/ | ||
var property = function property(name, value) { | ||
@@ -283,7 +362,42 @@ var Node = { | ||
name: name, | ||
value: value | ||
value: isGramLiteral(value) ? value : propertyValue(value) | ||
}; | ||
return Node; | ||
}; | ||
var propertyValue = function propertyValue(value) { | ||
if (Array.isArray(value)) { | ||
return value.map(function (v) { | ||
return propertyValue(v); | ||
}); | ||
} else if (typeof value === 'object') { | ||
if (value instanceof Date) { | ||
return date(value); | ||
} else if (isGramLiteral(value)) { | ||
return value; | ||
} | ||
return objectToRecord(value); | ||
} else { | ||
switch (typeof value) { | ||
case 'string': | ||
return string(value); | ||
case 'bigint': | ||
return decimal(value.toString()); | ||
case 'boolean': | ||
return _boolean(value); | ||
case 'number': | ||
return decimal(value.toString()); | ||
case 'symbol': | ||
return string(value.toString()); | ||
default: | ||
throw new Error("Unsupported value: " + value); | ||
} | ||
} | ||
}; | ||
var _boolean = function _boolean(value) { | ||
@@ -384,8 +498,10 @@ return { | ||
flatten: flatten, | ||
recordToMap: recordToMap, | ||
mapToRecord: mapToRecord | ||
objectToRecord: objectToRecord, | ||
propertiesToRecord: propertiesToRecord, | ||
propertyValue: propertyValue, | ||
fromLiteral: getLiteral | ||
}; | ||
export default index; | ||
export { _boolean as boolean, cons, date, dayOfMonth, decimal, duration, edge, empty, flatten, hexadecimal, integer, listToPath, mapToRecord, measurement, node, octal, pair, path, pluck, property, recordToMap, seq, string, tagged, time, year }; | ||
export { _boolean as boolean, cons, date, dayOfMonth, decimal, duration, edge, empty, emptyRecord, flatten, getDown, getLiteral, getRecord, getValue, hexadecimal, integer, listToPath, measurement, node, objectToRecord, octal, pair, path, propertiesToRecord, property, propertyValue, seq, string, tagged, time, year }; | ||
//# sourceMappingURL=gram-builder.esm.js.map |
@@ -60,3 +60,25 @@ (function (global, factory) { | ||
}; | ||
/** | ||
* A type guard to narrow a GramRecordValue to a GramRecord. | ||
* | ||
* Warning: this is not a runtime guarantee | ||
* | ||
* @param v any GramRecordValue | ||
*/ | ||
var isGramRecord = function isGramRecord(v) { | ||
return typeof v == 'object' && v instanceof Map; | ||
}; | ||
/** | ||
* Type guard for GramLiteral. | ||
* | ||
* @param o any object | ||
*/ | ||
var isGramLiteral = function isGramLiteral(o) { | ||
return !!o.type && !!o.value && o.type !== 'property'; | ||
}; | ||
function normalizeChildren(children) { | ||
@@ -289,32 +311,111 @@ if (Array.isArray(children)) { | ||
/** | ||
* Reduces an array of GramProperties into a map. | ||
* Create a new, empty GramRecord. | ||
* | ||
*/ | ||
var emptyRecord = function emptyRecord() { | ||
return new Map(); | ||
}; | ||
/** | ||
* Reduces an array of GramProperties into a GramRecord. | ||
* | ||
* @param properties | ||
*/ | ||
var recordToMap = function recordToMap(properties) { | ||
var propertiesToRecord = function propertiesToRecord(properties) { | ||
return properties.reduce(function (acc, p) { | ||
acc[p.name] = p.value; | ||
acc.set(p.name, p.value); | ||
return acc; | ||
}, {}); | ||
}, emptyRecord()); | ||
}; | ||
/** | ||
* Unfolds a property map<string,GramRecordValue> into a property list[GramProperty]. | ||
* Transforms a plain js object into a GramRecord. | ||
* | ||
* @param properties | ||
* @param o | ||
*/ | ||
var mapToRecord = function mapToRecord(properties) { | ||
return Object.entries(properties).reduce(function (acc, _ref) { | ||
var objectToRecord = function objectToRecord(o) { | ||
return Object.entries(o).reduce(function (acc, _ref) { | ||
var k = _ref[0], | ||
v = _ref[1]; | ||
acc.push(property(k, v)); | ||
acc.set(k, propertyValue(v)); | ||
return acc; | ||
}, []); | ||
}, emptyRecord()); | ||
}; | ||
var pluck = function pluck(properties, path) { | ||
return properties.reduce(function (acc, prop) { | ||
return prop.name === path ? prop : acc; | ||
}); | ||
/** | ||
* Extracts the value from a GramLiteral, if available. | ||
* | ||
* @param l | ||
*/ | ||
var getValue = function getValue(l) { | ||
return isGramLiteral(l) ? l.value : undefined; | ||
}; | ||
/** | ||
* Produces a Lens into a literal value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
var getLiteral = function getLiteral(name) { | ||
return function (v) { | ||
var l = v.get(name); | ||
return getValue(l); | ||
}; | ||
}; | ||
/** | ||
* Produces a Lens into a record value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
var getRecord = function getRecord(name) { | ||
return function (r) { | ||
var v = r.get(name); | ||
return isGramRecord(v) ? v : undefined; | ||
}; | ||
}; | ||
/** | ||
* Produces a Lens down into nested GramRecords. | ||
* | ||
* ### Examples: | ||
* | ||
* Descend using either an array of names, or dot notation. | ||
* | ||
* ``` | ||
* const o = g.objectToRecord({a:{b:{c:g.string("value")}}}) | ||
* | ||
* const getAbc1 = g.getDown(['a','b','c']); | ||
* const getAbc2 = g.getDown("a.b.c"); | ||
* | ||
* expect(getAbc1(o)).toStrictEqual(getAbc2(o)); | ||
* ``` | ||
* | ||
* Descend, then apply a function to extract the text value. | ||
* | ||
* ``` | ||
* const o = objectToRecord({a:{b:{c:string("value")}}}) | ||
* const getAbc = getDown("a.b.c", getValue); | ||
* | ||
* expect(getAbc(o)).toBe("value"); | ||
* ``` | ||
* | ||
* @param hierarchy array or dot-notation path to descend | ||
*/ | ||
var getDown = function getDown(hierarchy, f) { | ||
var pathDown = Array.isArray(hierarchy) ? hierarchy : hierarchy.split('.'); | ||
return function (r) { | ||
var bottom = pathDown.reduce(function (acc, name) { | ||
return isGramRecord(acc) ? acc.get(name) : undefined; | ||
}, r); | ||
return bottom && (f ? f(bottom) : bottom); | ||
}; | ||
}; | ||
/** | ||
* Builds a GramProperty from a name | ||
* @param name | ||
* @param value | ||
*/ | ||
var property = function property(name, value) { | ||
@@ -324,7 +425,42 @@ var Node = { | ||
name: name, | ||
value: value | ||
value: isGramLiteral(value) ? value : propertyValue(value) | ||
}; | ||
return Node; | ||
}; | ||
var propertyValue = function propertyValue(value) { | ||
if (Array.isArray(value)) { | ||
return value.map(function (v) { | ||
return propertyValue(v); | ||
}); | ||
} else if (typeof value === 'object') { | ||
if (value instanceof Date) { | ||
return date(value); | ||
} else if (isGramLiteral(value)) { | ||
return value; | ||
} | ||
return objectToRecord(value); | ||
} else { | ||
switch (typeof value) { | ||
case 'string': | ||
return string(value); | ||
case 'bigint': | ||
return decimal(value.toString()); | ||
case 'boolean': | ||
return _boolean(value); | ||
case 'number': | ||
return decimal(value.toString()); | ||
case 'symbol': | ||
return string(value.toString()); | ||
default: | ||
throw new Error("Unsupported value: " + value); | ||
} | ||
} | ||
}; | ||
var _boolean = function _boolean(value) { | ||
@@ -425,4 +561,6 @@ return { | ||
flatten: flatten, | ||
recordToMap: recordToMap, | ||
mapToRecord: mapToRecord | ||
objectToRecord: objectToRecord, | ||
propertiesToRecord: propertiesToRecord, | ||
propertyValue: propertyValue, | ||
fromLiteral: getLiteral | ||
}; | ||
@@ -439,15 +577,20 @@ | ||
exports.empty = empty; | ||
exports.emptyRecord = emptyRecord; | ||
exports.flatten = flatten; | ||
exports.getDown = getDown; | ||
exports.getLiteral = getLiteral; | ||
exports.getRecord = getRecord; | ||
exports.getValue = getValue; | ||
exports.hexadecimal = hexadecimal; | ||
exports.integer = integer; | ||
exports.listToPath = listToPath; | ||
exports.mapToRecord = mapToRecord; | ||
exports.measurement = measurement; | ||
exports.node = node; | ||
exports.objectToRecord = objectToRecord; | ||
exports.octal = octal; | ||
exports.pair = pair; | ||
exports.path = path; | ||
exports.pluck = pluck; | ||
exports.propertiesToRecord = propertiesToRecord; | ||
exports.property = property; | ||
exports.recordToMap = recordToMap; | ||
exports.propertyValue = propertyValue; | ||
exports.seq = seq; | ||
@@ -454,0 +597,0 @@ exports.string = string; |
@@ -1,2 +0,2 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(((e=e||self).gram=e.gram||{},e.gram.builder={}))}(this,(function(e){"use strict";function n(){return(n=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])}return e}).apply(this,arguments)}var t=function(e){return!!e.type&&"path"===e.type},r=function(e){return t(e)&&"ø"===e.id},i=function(e){return t(e)&&e.children&&0===e.children.length&&"ø"!==e.id};function u(e){return Array.isArray(e)?e:e instanceof Function?u(e()):void 0===e?[]:[e]}var o=function(e,t,r,i){return n({type:"seq",id:t},r&&{labels:r},i&&{record:i},{children:u(e)})},a=function(e,n){return void 0===e&&(e="pair"),n.length>1?n.slice(0,n.length-1).reduceRight((function(n,t){return c([t,n],{kind:e})}),n[n.length-1]):n[0]},c=function(e,t){void 0===t&&(t={});var u=n({type:"path"},t.id&&{id:t.id},t.labels&&{labels:t.labels},t.record&&{record:t.record});if(void 0===e)return u.id&&"ø"!==u.id?(u.children=[],u):(u.children=void 0,d);if(0===e.length)return"ø"===u.id?d:(u.children=[],u);if(1===e.length){var o=e[0];return r(o)?(u.children=[],u):(u.children=[o],u)}if(2===e.length){if(t.kind&&"pair"!==t.kind&&i(e[0])&&i(e[1]))return u.kind=t.kind,u.children=[e[0],e[1]],u;if(r(e[0])&&r(e[1]))return u.kind=t.kind,u.children=[],u;u.children=[e[0],e[1]]}return u.kind=t.kind||"pair",u},d={type:"path",id:"ø",labels:void 0,record:void 0,children:[]},l=function(){return d},f=function(e,t,r){return n({type:"path"},e&&{id:e},t&&{labels:t},r&&{record:r},{children:[]})},p=function(e,t,r,i,u){return n({type:"path",id:r},i&&{labels:i},u&&{record:u},{kind:t,children:e})},s=function(e,t,r,i,u){return n({type:"path",kind:e,id:r},i&&{labels:i},u&&{record:u},{children:t})},g=function(e,n,t,r){return s("pair",e,n,t,r)},h=function(e){return e.reduce((function(e,n){return e[n.name]=n.value,e}),{})},y=function(e){return Object.entries(e).reduce((function(e,n){return e.push(m(n[0],n[1])),e}),[])},m=function(e,n){return{type:"property",name:e,value:n}},v=function(e){return{type:"boolean",value:e?"true":"false"}},b=function(e){return{type:"string",value:e}},T=function(e,n){return{type:"tagged",value:n,tag:e}},k=function(e){return{type:"integer",value:String(e)}},S=function(e){return{type:"decimal",value:String(e)}},M=function(e){return{type:"hexadecimal",value:"number"==typeof e?e.toString(16):e}},O=function(e){return{type:"octal",value:"number"==typeof e?e.toString(8):e}},D=function(e,n){return{type:"measurement",value:String(n),unit:e}},x=function(e){return T("date",e instanceof Date?e.toISOString().slice(0,10):e)},C=function(e){return T("time",e instanceof Date?e.toTimeString():e)},U=function(e){return T("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,n){return void 0===n&&(n=1),e.flat(n).filter((function(e){return null!==e}))},P={seq:o,empty:l,cons:c,pair:g,listToPath:a,node:f,edge:p,property:m,boolean:v,string:b,tagged:T,integer:k,decimal:S,hexadecimal:M,octal:O,measurement:D,date:x,time:C,duration:U,flatten:j,recordToMap:h,mapToRecord:y};e.boolean=v,e.cons=c,e.date=x,e.dayOfMonth=function(e){return T("date",e instanceof Date?"--"+e.toISOString().slice(5,10):e)},e.decimal=S,e.default=P,e.duration=U,e.edge=p,e.empty=l,e.flatten=j,e.hexadecimal=M,e.integer=k,e.listToPath=a,e.mapToRecord=y,e.measurement=D,e.node=f,e.octal=O,e.pair=g,e.path=s,e.pluck=function(e,n){return e.reduce((function(e,t){return t.name===n?t:e}))},e.property=m,e.recordToMap=h,e.seq=o,e.string=b,e.tagged=T,e.time=C,e.year=function(e){return T("date",e instanceof Date?e.getFullYear().toString():e)},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)&&"ø"===e.id},i=function(e){return n(e)&&e.children&&0===e.children.length&&"ø"!==e.id},o=function(e){return"object"==typeof e&&e instanceof Map},u=function(e){return!!e.type&&!!e.value&&"property"!==e.type};function a(e){return Array.isArray(e)?e:e instanceof Function?a(e()):void 0===e?[]:[e]}var c=function(e,n,r,i){return t({type:"seq",id:n},r&&{labels:r},i&&{record:i},{children:a(e)})},d=function(e,t){return void 0===e&&(e="pair"),t.length>1?t.slice(0,t.length-1).reduceRight((function(t,n){return l([n,t],{kind:e})}),t[t.length-1]):t[0]},l=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,f);if(0===e.length)return"ø"===o.id?f:(o.children=[],o);if(1===e.length){var u=e[0];return r(u)?(o.children=[],o):(o.children=[u],o)}if(2===e.length){if(n.kind&&"pair"!==n.kind&&i(e[0])&&i(e[1]))return o.kind=n.kind,o.children=[e[0],e[1]],o;if(r(e[0])&&r(e[1]))return o.kind=n.kind,o.children=[],o;o.children=[e[0],e[1]]}return o.kind=n.kind||"pair",o},f={type:"path",id:"ø",labels:void 0,record:void 0,children:[]},p=function(){return f},s=function(e,n,r){return t({type:"path"},e&&{id:e},n&&{labels:n},r&&{record:r},{children:[]})},g=function(e,n,r,i,o){return t({type:"path",id:r},i&&{labels:i},o&&{record:o},{kind:n,children:e})},y=function(e,n,r,i,o){return t({type:"path",kind:e,id:r},i&&{labels:i},o&&{record:o},{children:n})},h=function(e,t,n,r){return y("pair",e,t,n,r)},v=function(){return new Map},m=function(e){return e.reduce((function(e,t){return e.set(t.name,t.value),e}),v())},b=function(e){return Object.entries(e).reduce((function(e,t){return e.set(t[0],j(t[1])),e}),v())},S=function(e){return u(e)?e.value:void 0},T=function(e){return function(t){var n=t.get(e);return S(n)}},k=function(e,t){return{type:"property",name:e,value:u(t)?t:j(t)}},j=function e(t){if(Array.isArray(t))return t.map((function(t){return e(t)}));if("object"==typeof t)return t instanceof Date?C(t):u(t)?t:b(t);switch(typeof t){case"string":return M(t);case"bigint":return U(t.toString());case"boolean":return D(t);case"number":return U(t.toString());case"symbol":return M(t.toString());default:throw new Error("Unsupported value: "+t)}},D=function(e){return{type:"boolean",value:e?"true":"false"}},M=function(e){return{type:"string",value:e}},O=function(e,t){return{type:"tagged",value:t,tag:e}},R=function(e){return{type:"integer",value:String(e)}},U=function(e){return{type:"decimal",value:String(e)}},w=function(e){return{type:"hexadecimal",value:"number"==typeof e?e.toString(16):e}},x=function(e){return{type:"octal",value:"number"==typeof e?e.toString(8):e}},A=function(e,t){return{type:"measurement",value:String(t),unit:e}},C=function(e){return O("date",e instanceof Date?e.toISOString().slice(0,10):e)},P=function(e){return O("time",e instanceof Date?e.toTimeString():e)},q=function(e){return O("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)},F=function(e,t){return void 0===t&&(t=1),e.flat(t).filter((function(e){return null!==e}))},V={seq:c,empty:p,cons:l,pair:h,listToPath:d,node:s,edge:g,property:k,boolean:D,string:M,tagged:O,integer:R,decimal:U,hexadecimal:w,octal:x,measurement:A,date:C,time:P,duration:q,flatten:F,objectToRecord:b,propertiesToRecord:m,propertyValue:j,fromLiteral:T};e.boolean=D,e.cons=l,e.date=C,e.dayOfMonth=function(e){return O("date",e instanceof Date?"--"+e.toISOString().slice(5,10):e)},e.decimal=U,e.default=V,e.duration=q,e.edge=g,e.empty=p,e.emptyRecord=v,e.flatten=F,e.getDown=function(e,t){var n=Array.isArray(e)?e:e.split(".");return function(e){var r=n.reduce((function(e,t){return o(e)?e.get(t):void 0}),e);return r&&(t?t(r):r)}},e.getLiteral=T,e.getRecord=function(e){return function(t){var n=t.get(e);return o(n)?n:void 0}},e.getValue=S,e.hexadecimal=w,e.integer=R,e.listToPath=d,e.measurement=A,e.node=s,e.objectToRecord=b,e.octal=x,e.pair=h,e.path=y,e.propertiesToRecord=m,e.property=k,e.propertyValue=j,e.seq=c,e.string=M,e.tagged=O,e.time=P,e.year=function(e){return O("date",e instanceof Date?e.getFullYear().toString():e)},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=gram-builder.umd.production.min.js.map |
@@ -6,3 +6,3 @@ /** | ||
*/ | ||
import { GramSeq, GramPath, GramNode, GramEdge, GramProperty, GramRecordValue, BooleanLiteral, StringLiteral, IntegerLiteral, DecimalLiteral, HexadecimalLiteral, OctalLiteral, MeasurementLiteral, GramRecord, GramEmptyPath, GramPropertyMap, DateLiteral, TimeLiteral, DurationLiteral, PathKind, RelationshipKind, TaggedTextLiteral } from '@gram-data/gram-ast'; | ||
import { GramSeq, GramPath, GramNode, GramEdge, GramProperty, GramRecordValue, BooleanLiteral, StringLiteral, IntegerLiteral, DecimalLiteral, HexadecimalLiteral, OctalLiteral, MeasurementLiteral, GramRecord, GramEmptyPath, DateLiteral, TimeLiteral, DurationLiteral, PathKind, RelationshipKind, TaggedTextLiteral } from '@gram-data/gram-ast'; | ||
export declare type Children<T> = T | T[] | (() => T | T[]); | ||
@@ -81,15 +81,71 @@ /** | ||
/** | ||
* Reduces an array of GramProperties into a map. | ||
* Create a new, empty GramRecord. | ||
* | ||
* @param properties | ||
*/ | ||
export declare const recordToMap: (properties: GramRecord) => GramPropertyMap; | ||
export declare const emptyRecord: () => Map<string, GramRecordValue>; | ||
/** | ||
* Unfolds a property map<string,GramRecordValue> into a property list[GramProperty]. | ||
* Reduces an array of GramProperties into a GramRecord. | ||
* | ||
* @param properties | ||
*/ | ||
export declare const mapToRecord: (properties: GramPropertyMap) => GramRecord; | ||
export declare const pluck: (properties: GramRecord, path: string) => GramProperty; | ||
export declare const property: (name: string, value: GramRecordValue) => GramProperty; | ||
export declare const propertiesToRecord: (properties: GramProperty[]) => GramRecord; | ||
/** | ||
* Transforms a plain js object into a GramRecord. | ||
* | ||
* @param o | ||
*/ | ||
export declare const objectToRecord: (o: any) => GramRecord; | ||
/** | ||
* Extracts the value from a GramLiteral, if available. | ||
* | ||
* @param l | ||
*/ | ||
export declare const getValue: (l: GramRecordValue | undefined) => string | undefined; | ||
/** | ||
* Produces a Lens into a literal value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
export declare const getLiteral: (name: string) => (v: GramRecord) => string | undefined; | ||
/** | ||
* Produces a Lens into a record value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
export declare const getRecord: (name: string) => (r: GramRecord) => GramRecord | undefined; | ||
/** | ||
* Produces a Lens down into nested GramRecords. | ||
* | ||
* ### Examples: | ||
* | ||
* Descend using either an array of names, or dot notation. | ||
* | ||
* ``` | ||
* const o = g.objectToRecord({a:{b:{c:g.string("value")}}}) | ||
* | ||
* const getAbc1 = g.getDown(['a','b','c']); | ||
* const getAbc2 = g.getDown("a.b.c"); | ||
* | ||
* expect(getAbc1(o)).toStrictEqual(getAbc2(o)); | ||
* ``` | ||
* | ||
* Descend, then apply a function to extract the text value. | ||
* | ||
* ``` | ||
* const o = objectToRecord({a:{b:{c:string("value")}}}) | ||
* const getAbc = getDown("a.b.c", getValue); | ||
* | ||
* expect(getAbc(o)).toBe("value"); | ||
* ``` | ||
* | ||
* @param hierarchy array or dot-notation path to descend | ||
*/ | ||
export declare const getDown: (hierarchy: string[] | string, f?: ((r: GramRecordValue) => any) | undefined) => (r: GramRecord) => any; | ||
/** | ||
* Builds a GramProperty from a name | ||
* @param name | ||
* @param value | ||
*/ | ||
export declare const property: (name: string, value: any) => GramProperty; | ||
export declare const propertyValue: (value: any) => GramRecordValue; | ||
export declare const boolean: (value: boolean) => BooleanLiteral; | ||
@@ -117,3 +173,3 @@ export declare const string: (value: string) => StringLiteral; | ||
edge: (children: [GramNode, GramNode], kind: RelationshipKind, id?: string | undefined, labels?: string[] | undefined, record?: GramRecord | undefined) => GramEdge; | ||
property: (name: string, value: GramRecordValue) => GramProperty; | ||
property: (name: string, value: any) => GramProperty; | ||
boolean: (value: boolean) => BooleanLiteral; | ||
@@ -131,5 +187,7 @@ string: (value: string) => StringLiteral; | ||
flatten: (xs: any[], depth?: number) => any[]; | ||
recordToMap: (properties: GramRecord) => GramPropertyMap; | ||
mapToRecord: (properties: GramPropertyMap) => GramRecord; | ||
objectToRecord: (o: any) => GramRecord; | ||
propertiesToRecord: (properties: GramProperty[]) => GramRecord; | ||
propertyValue: (value: any) => GramRecordValue; | ||
fromLiteral: (name: string) => (v: GramRecord) => string | undefined; | ||
}; | ||
export default _default; |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "0.3.8", | ||
"version": "0.3.9-alpha.2", | ||
"license": "MIT", | ||
@@ -56,8 +56,8 @@ "repository": { | ||
"@types/chalk": "^2.2.0", | ||
"@types/jest": "^26.0.18", | ||
"@types/jest": "^26.0.19", | ||
"@types/unist": "^2.0.3", | ||
"@typescript-eslint/parser": "^4.9.1", | ||
"@typescript-eslint/parser": "^4.10.0", | ||
"chalk": "^4.1.0", | ||
"husky": "^4.3.5", | ||
"npm-check-updates": "^10.2.3", | ||
"husky": "^4.3.6", | ||
"npm-check-updates": "^10.2.5", | ||
"npm-run-all": "^4.1.5", | ||
@@ -69,3 +69,3 @@ "serve": "^11.3.2", | ||
"typedoc": "^0.19.2", | ||
"typescript": "^4.1.2", | ||
"typescript": "^4.1.3", | ||
"unist-util-inspect": "^6.0.1", | ||
@@ -75,5 +75,6 @@ "unist-util-size": "^2.0.1" | ||
"dependencies": { | ||
"@gram-data/gram-ast": "^0.3.8" | ||
"@gram-data/gram-ast": "^0.3.9-alpha.2", | ||
"fp-ts": "^2.9.1" | ||
}, | ||
"gitHead": "b1df9762a08f50e0f01f99c9e1ef7334aabd1c3b", | ||
"gitHead": "32528b415789ea165d93b2432a50322c977642eb", | ||
"publishConfig": { | ||
@@ -80,0 +81,0 @@ "access": "public" |
152
src/index.ts
@@ -27,3 +27,2 @@ /** | ||
EMPTY_PATH_ID, | ||
GramPropertyMap, | ||
DateLiteral, | ||
@@ -35,2 +34,4 @@ TimeLiteral, | ||
TaggedTextLiteral, | ||
isGramLiteral, | ||
isGramRecord, | ||
} from '@gram-data/gram-ast'; | ||
@@ -267,39 +268,114 @@ | ||
/** | ||
* Reduces an array of GramProperties into a map. | ||
* Create a new, empty GramRecord. | ||
* | ||
*/ | ||
export const emptyRecord = () => new Map<string, GramRecordValue>(); | ||
/** | ||
* Reduces an array of GramProperties into a GramRecord. | ||
* | ||
* @param properties | ||
*/ | ||
export const recordToMap = (properties: GramRecord): GramPropertyMap => { | ||
return properties.reduce((acc: GramPropertyMap, p: GramProperty) => { | ||
acc[p.name] = p.value; | ||
export const propertiesToRecord = (properties: GramProperty[]): GramRecord => { | ||
return properties.reduce((acc: GramRecord, p: GramProperty) => { | ||
acc.set(p.name, p.value); | ||
return acc; | ||
}, {} as GramPropertyMap); | ||
}, emptyRecord()); | ||
}; | ||
/** | ||
* Unfolds a property map<string,GramRecordValue> into a property list[GramProperty]. | ||
* Transforms a plain js object into a GramRecord. | ||
* | ||
* @param properties | ||
* @param o | ||
*/ | ||
export const mapToRecord = (properties: GramPropertyMap): GramRecord => { | ||
return Object.entries(properties).reduce((acc: GramRecord, [k, v]) => { | ||
acc.push(property(k, v)); | ||
export const objectToRecord = (o: any): GramRecord => { | ||
return Object.entries(o).reduce((acc: GramRecord, [k, v]) => { | ||
acc.set(k, propertyValue(v)); | ||
return acc; | ||
}, [] as GramRecord); | ||
}, emptyRecord()); | ||
}; | ||
export const pluck = (properties: GramRecord, path: string) => { | ||
return properties.reduce((acc, prop) => { | ||
return prop.name === path ? prop : acc; | ||
}); | ||
/** | ||
* Extracts the value from a GramLiteral, if available. | ||
* | ||
* @param l | ||
*/ | ||
export const getValue = (l: GramRecordValue | undefined) => | ||
isGramLiteral(l) ? l.value : undefined; | ||
/** | ||
* Produces a Lens into a literal value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
export const getLiteral = (name: string) => { | ||
return (v: GramRecord) => { | ||
const l = v.get(name); | ||
return getValue(l); | ||
}; | ||
}; | ||
export const property = ( | ||
name: string, | ||
value: GramRecordValue | ||
): GramProperty => { | ||
/** | ||
* Produces a Lens into a record value with a GramRecord. | ||
* | ||
* @param path | ||
*/ | ||
export const getRecord = (name: string) => { | ||
return (r: GramRecord) => { | ||
const v = r.get(name); | ||
return isGramRecord(v) ? v : undefined; | ||
}; | ||
}; | ||
/** | ||
* Produces a Lens down into nested GramRecords. | ||
* | ||
* ### Examples: | ||
* | ||
* Descend using either an array of names, or dot notation. | ||
* | ||
* ``` | ||
* const o = g.objectToRecord({a:{b:{c:g.string("value")}}}) | ||
* | ||
* const getAbc1 = g.getDown(['a','b','c']); | ||
* const getAbc2 = g.getDown("a.b.c"); | ||
* | ||
* expect(getAbc1(o)).toStrictEqual(getAbc2(o)); | ||
* ``` | ||
* | ||
* Descend, then apply a function to extract the text value. | ||
* | ||
* ``` | ||
* const o = objectToRecord({a:{b:{c:string("value")}}}) | ||
* const getAbc = getDown("a.b.c", getValue); | ||
* | ||
* expect(getAbc(o)).toBe("value"); | ||
* ``` | ||
* | ||
* @param hierarchy array or dot-notation path to descend | ||
*/ | ||
export const getDown = ( | ||
hierarchy: string[] | string, | ||
f?: (r: GramRecordValue) => any | ||
) => { | ||
const pathDown = Array.isArray(hierarchy) ? hierarchy : hierarchy.split('.'); | ||
return (r: GramRecord) => { | ||
const bottom = pathDown.reduce( | ||
(acc, name) => (isGramRecord(acc) ? acc.get(name) : undefined), | ||
r as GramRecordValue | undefined | ||
); | ||
return bottom && (f ? f(bottom) : bottom); | ||
}; | ||
}; | ||
/** | ||
* Builds a GramProperty from a name | ||
* @param name | ||
* @param value | ||
*/ | ||
export const property = (name: string, value: any): GramProperty => { | ||
const Node: GramProperty = { | ||
type: 'property', | ||
name, | ||
value, | ||
value: isGramLiteral(value) ? value : propertyValue(value), | ||
}; | ||
@@ -309,2 +385,30 @@ return Node; | ||
export const propertyValue = (value: any): GramRecordValue => { | ||
if (Array.isArray(value)) { | ||
return value.map(v => propertyValue(v)); | ||
} else if (typeof value === 'object') { | ||
if (value instanceof Date) { | ||
return date(value); | ||
} else if (isGramLiteral(value)) { | ||
return value; | ||
} | ||
return objectToRecord(value); | ||
} else { | ||
switch (typeof value) { | ||
case 'string': | ||
return string(value); | ||
case 'bigint': | ||
return decimal(value.toString()); | ||
case 'boolean': | ||
return boolean(value); | ||
case 'number': | ||
return decimal(value.toString()); | ||
case 'symbol': | ||
return string(value.toString()); | ||
default: | ||
throw new Error(`Unsupported value: ${value}`); | ||
} | ||
} | ||
}; | ||
export const boolean = (value: boolean): BooleanLiteral => ({ | ||
@@ -413,4 +517,6 @@ type: 'boolean', | ||
flatten, | ||
recordToMap, | ||
mapToRecord, | ||
objectToRecord, | ||
propertiesToRecord, | ||
propertyValue, | ||
fromLiteral: getLiteral, | ||
}; |
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
177298
2214
2
+ Addedfp-ts@^2.9.1
+ Addedfp-ts@2.16.9(transitive)