Skip to content

Instantly share code, notes, and snippets.

@kwijibo
Created June 30, 2015 22:04
Show Gist options
  • Save kwijibo/f38953bea0b25744e569 to your computer and use it in GitHub Desktop.
Save kwijibo/f38953bea0b25744e569 to your computer and use it in GitHub Desktop.
Some code I wrote using ramda.js
//getFieldDefinition :: key -> [a] -> a
var getFieldDefinition = r.useWith(
r.find, // #final function :: Relation -> [a] -> a
r.propEq(1), //takes `key` where [1]==key #function for 1st param
r.identity //#placeholder func for 2nd param
)
// getFieldSpec :: String -> Object
// getFieldSpec('duration')
// > {type:'number', max:60, min: 1}
var getFieldSpec = r.memoize(
r.compose(r.defaultTo({}), r.prop(2), r.defaultTo([]), getFieldDefinition(r.__, textFields))
)
//getFieldLabel :: fieldName -> fieldLabel
var getFieldLabel = r.memoize(r.compose(r.head, getFieldDefinition(r.__,textFields)))
// fieldInvalid :: Object -> Array -> String -> Boolean
var fieldInvalid = r.curry(function(model,fieldDefinitions, key){
// modelKeyValue :: Model {someKey: { value: someValue }} -> String someValue
var modelKeyValue = r.pipe(r.prop(key), r.prop('value'))
var fieldDef = getFieldDefinition(key, fieldDefinitions)
if(!fieldDef) return false
var attrs = fieldDef[2] || {}
var value = modelKeyValue(model)
return isValid(value, attrs)===false
})
//invalidFields :: model -> Array
function invalidFields(model){
var fieldnames = getModelPropKeys(model)
return r.filter(fieldInvalid(model, textFields), fieldnames)
}
//wrapItem :: item -> ReactDomNode
var wrapItem = (i)=>{ return (<li>{i}</li>) }
var itemizeFieldLabel = r.compose(wrapItem, getFieldLabel)
@kwijibo
Copy link
Author

kwijibo commented Jun 30, 2015

not all that recognisable as javascript. It's 25% comments, I'm the one that wrote it, and it still looks foreign.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment