Created
November 14, 2017 14:56
-
-
Save david-hodgetts/ce08537aeeeaa410f81403747e1ca719 to your computer and use it in GitHub Desktop.
from data-nugget to data-scalars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// from data-nugget to data-scalars, | |
// a first end to end look at the problem by example | |
const Immutable = require('immutable'); | |
// let's define some record structure to represent simple type schemas. | |
// we need some composite types… | |
let ListType = Immutable.Record({type:'List', kind:'Collection', fields: Immutable.Map()}, 'ListType'); | |
let MapType = Immutable.Record({type:'Map', kind:'Collection', fields: Immutable.Map()}, 'MapType'); | |
// … and some scalars | |
let PercentType = Immutable.Record({type:'Percent', kind:'Scalar'}, 'PercentType'); | |
let StringType = Immutable.Record({type:'string', kind:'Scalar'}, 'StringType'); | |
// next we define a sample data-nugget. | |
// i.e some data, and a schema representing its type structure | |
let dataNugget = Immutable.Map({ | |
data: Immutable.Map({ | |
description: "some text", | |
percents: Immutable.List([0, 0.2, 0.5]) | |
}), | |
schema: new MapType({fields: Immutable.Map({ | |
"description": new StringType(), | |
"percents": new ListType({fields: Immutable.Map({"0": new PercentType()})}) | |
})}) | |
}); | |
// implementation detail ignore this | |
// intersperses value in array | |
// given array ['foo', 'bar'] and value 'inserted' return ['foo', 'inserted, 'bar'] | |
let intersperse = (array, value) => array.reduce((result, current, index) =>{ | |
let next = index == array.length - 1 ? current : [current].concat(value); | |
return result.concat(next); | |
}, []); | |
// provide a mecanism to mine a nugget so as to produce a smaller data-nugget/egg (data+schema) | |
// | |
// given a data-nugget and a path returns a subfield of the data-egg/nugget. | |
// the path represents the path from the root of the nugget's value to the subfield. | |
// path can be string or [string] | |
let extractEggFromNugget = (nugget, path) => { | |
let dataPath = ['data'].concat(path); | |
let schemaPath = ['schema'].concat(path); | |
schemaPath = intersperse(schemaPath, 'fields'); | |
return Immutable.Map({ | |
data: nugget.getIn(dataPath), | |
schema: nugget.getIn(schemaPath) | |
}); | |
}; | |
// let's extract some eggs to be fed to data-scalar polymer elements | |
let descriptionEgg = extractEggFromNugget(dataNugget, 'description'); | |
let firstPercentEgg = extractEggFromNugget(dataNugget, ['percents', '0']); | |
console.log(descriptionEgg); | |
// Map { "data": "some text", "schema": StringType { type: "string", kind: "Scalar" } } | |
console.log(firstPercentEgg); | |
// Map { "data": 0, "schema": PercentType { type: "Percent", kind: "Scalar" } } | |
// finally in some polymer element | |
// <data-scalars data-egg={{descriptionEgg}}></data-scalars> | |
// <data-scalars data-egg={{firstPercentEgg}}></data-scalars> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment