These are chat archives for ramda/ramda

7th
Jul 2017
Nikola Lukic
@nikolal
Jul 07 2017 08:34
How can I handle null checks in this example using ramda:
beforeLoginData.ProjectInfo &&
beforeLoginData.ProjectInfo.uiAppSettings &&
beforeLoginData.ProjectInfo.uiAppSettings.navigation === 'defaultTemplate' ?
 <Tabs /> : <Login />
James Forbes
@JAForbes
Jul 07 2017 09:00
Hey @nikolal just answered your question over in sanctuary :D
Ironically... using ramda
Francisco
@franciscotln
Jul 07 2017 14:38
pathEq ?
@nikolal
Eugene Krayni
@eakray
Jul 07 2017 20:08
If one wanted to write a function that accepted a JSON object and returned an object containing arrays of primitives as well as a schema that allowed you to recreate that original JSON object from that collection of arrays...how would one do that?
Eugene Krayni
@eakray
Jul 07 2017 21:15
Would the solution be to write a modified recursive descent parser?
Kevin Wallace
@kedashoe
Jul 07 2017 22:01
hi @eakray , do you have some example input and output?
Eugene Krayni
@eakray
Jul 07 2017 23:11
the input can be any valid JSON object. I'm not sure about the exact form of the output because i'm not sure exactly how to structure the schema. Let's say you have a spec that requires some input to be an object that's comprised of primitives or arrays of primitives. This function would serve to convert a json object, however deeply nested, into an object that satisfies that spec. I guess the way the function would parse the json object into a collection of arrays that hold only primitives would determine the structure of the generated schema. I...don't know if that was helpful.
Kurt Milam
@kurtmilam
Jul 07 2017 23:17
@eakray can you come up with a simple example showing input and a proposed output?
It doesn't have to be perfect, but a short example is often worth many words. Another way you could go about it would be to describe what the output object will be used for, and why it needs to have the (as yet unknown) format it will need to have.
I'd like to know more about the arrays of primitives and where you expect to store the object keys. Also, what you plan to do with arrays that are nested inside the input object.
Eugene Krayni
@eakray
Jul 07 2017 23:23
i was thinking about storing nesting depth information and object keys are the initial values in the arrays
but i haven't thought that part through really
Kurt Milam
@kurtmilam
Jul 07 2017 23:23
From what I understand of your explanation, this recipe might be helpful. You could use the technique to end up with one flat array of object keys and one flat array of object values, and you could use the array of keys as your schema for transforming the array of values back to the original object.
Eugene Krayni
@eakray
Jul 07 2017 23:23
i mean, the whole issue is about the structure of the generated schema, exactly what you're asking about
that looks promising
thanks a lot
Kurt Milam
@kurtmilam
Jul 07 2017 23:26
so, for example:
const start =  { a: { b: { c: 3 } }, d: 4, e: [ 5, 6 ] }
const schema = [ 'a', 'a.b', 'a.b.c', 'd', 'e', 'e.0', 'e.1' ]
const values = [ {},  {},    3,       4,   [],  5,     6 ]
It wouldn't be too difficult to write a function that would take schema and values as its arguments and return start.
No problem. If you have questions, feel free to ping me. I've worked with a similar setup and helped the author of that recipe put it together.
Eugene Krayni
@eakray
Jul 07 2017 23:30
yeah, this looks like the right approach. Although, I'll need to include a way to disambiguate between nested objects and nested arrays
Kurt Milam
@kurtmilam
Jul 07 2017 23:32
See my edited example.
The nested arrays and objects are stored in the values array.
I don't think the recipe I linked to will do exactly this, but if the schema works, the recipe can be used as a basis for generating the schema and values with the correct structure.
Eugene Krayni
@eakray
Jul 07 2017 23:37
yeah
Kurt Milam
@kurtmilam
Jul 07 2017 23:40
a better structure for the schema is probably:
[ [ 'a' ], [ 'a', 'b' ], [ 'a', 'b', 'c' ], [ 'd' ], [ 'e' ], [ 'e', 0 ], [ 'e', 1 ] ]
Then you don't have to worry about collisions with object keys that contain periods, and the integer keys in the schema will always point to array indexes.
'0' would point to an object key, 0 would point to an array index.
Eugene Krayni
@eakray
Jul 07 2017 23:51
and when you're traversing the array, you know that if you reach an array who's length is >1 the last value is the object key that's nested at array.length from the object who's key is array[0]
Kurt Milam
@kurtmilam
Jul 07 2017 23:57
Yes. The function for reassembling the object from schema and values would be trivial, as well.
Note that in the 'better structure', the entries in schema can be used directly with R.path, R.assocPath, etc.
So you could zip( schema, values ) and reduce that using an empty object as your accumulator and assocPath to build the object back, one key/value pair at a time.