Skip to content

Instantly share code, notes, and snippets.

@refset
Created March 20, 2013 13:24
Show Gist options
  • Save refset/5204604 to your computer and use it in GitHub Desktop.
Save refset/5204604 to your computer and use it in GitHub Desktop.
function binRelX(elA, elB){
return elA > elB ? true : false
}
function binRelXObj(elA, elB, opts){
//opts, accessor,
var value = opts.value || "value"
return elA[value] > elB[value] ? true : false
}
function binRelXObjHash(elA, elB, opts){
//opts , accessor, hash
var value = opts.value || "value"
var hash = opts.hash || {}
return opts.hash[elA[value]] > opts.hash[elB[value]] ? true : false
}
function binRelXObjHashLookup(elA, elB, opts){
//opts..., accessor, hashLookup
var key = opts.key || "key"
var hashLookup = opts.hashLookup || {}
return hashLookup[elA[key]] && hashLookup[elA[key]][elB[key]] ? true : false
}
function binRelWrapperInline(opts){
//opts...elA, elB, binRelFunc, binRelFuncParams, binRelsAccessor
if(!opts.elA[opts.binRelsAccessor]) opts.elA[opts.binRelsAccessor] = {} //could seperate this into some kind of optimised method
if(!opts.elB[opts.binRelsAccessor]) opts.elB[opts.binRelsAccessor] = {} //needed by binRelIteratorLookup to add empty accessor ns to leaves
if(opts.binRelFunc(opts.elA,opts.elB,opts.binRelFuncParams)) opts.elA[opts.binRelsAccessor][opts.elB.key] = opts.elB//.push(opts.elB) //add params
return opts.elA //not strictly necessary
}
//function binRelWrapperExtObj(binRelFunc, binRelFuncParams){
//}
function binRelIterator(setObj, binRelWrapperFunc, binRelWrapperOpts){
//setObj.map(function(elA){ //map? if setObj is a hash, then how to map against vals
Object.keys(setObj).forEach(function(key){
elA = setObj[key]
Object.keys(setObj).forEach(function(key){
elB = setObj[key]
binRelWrapperOpts["elA"] = elA
binRelWrapperOpts["elB"] = elB
binRelWrapperFunc(binRelWrapperOpts) //how to add the params to the end? wrap it all in []?
})
})
return setObj //not strictly necessary
}
//, binRelFunc,binRelFuncParams, binRelsAccessor
function binRelIteratorLookup(setObj, binRelWrapperFunc, binRelWrapperOpts, lookup, binRelsAccessor){//need to make this setObj have a hash of itself according to an accessor, perhaps another function
//binRelFuncParams, accessor, binRelsAccessor binRelFunc,
//binRelFunc,binRelWrapperFunc, binRelFuncParams, setObj, accessor, lookup, binRelsAccessor
//also need to set up _binRels somewhere
function recurse (elA){
if(!elA[lookup]) return false
Object.keys(elA[lookup]).forEach(function(key){
elBAccessor = key //elA[lookup][key] --not sure why this was here?!
elB = setObj[elBAccessor]
binRelWrapperOpts["elA"] = elA
binRelWrapperOpts["elB"] = elB
binRelWrapperFunc(binRelWrapperOpts)
if(!elB[binRelsAccessor]) recurse(elB)
})
}
Object.keys(setObj).forEach(function(key){
elA = setObj[key]
if(!elA[binRelsAccessor]) recurse(elA)//|| !elA[binRelsAccessor].length()
})
return setObj
}
//elA,elB,binRelFunc,binRelFuncParams, binRelsAccessor)
function setObjToBinRelHash (setObj,accessor){
hash = {}
Object.keys(setObj).forEach(function(key){
hash[key] = setObj[key][accessor]
})
return hash
}
testSetObj = {
"asdf":{"value":21,"key":"asdf","children":{"asdf2":true,"asdf5":true}},
"asdf2":{"value":18,"key":"asdf2","children":{"asdf3":true}},
"asdf3":{"value":15,"key":"asdf3","children":{"asdf4":true, "asdf5":true}},
"asdf4":{"value":11,"key":"asdf4","children":{}},
"asdf5":{"value":9,"key":"asdf5","children":{}}
}
testSetObjHash = setObjToBinRelHash(testSetObj,"children")
console.log(binRelX( //direct value comparison
testSetObj["asdf"]["value"],testSetObj["asdf2"]["value"])
)
console.log(binRelXObj( //object value comparison
testSetObj["asdf"],testSetObj["asdf2"],{})
)
console.log(binRelXObjHash( //hash object value comparison, using object names
testSetObj["asdf"],testSetObj["asdf2"],{"hash":{"21":"10","18":"1"}})
)
console.log(binRelXObjHashLookup( //hash object ref lookup (basically a stored binRel))
testSetObj["asdf"],testSetObj["asdf2"],{"hashLookup":testSetObjHash})
)
console.log(true || binRelWrapperInline( //inline the result if binRelXObjHashLookup=true
{
binRelFunc: binRelXObjHashLookup,
elA: testSetObj["asdf"],
elB: testSetObj["asdf2"],
binRelFuncParams: {"hashLookup":testSetObjHash},
binRelsAccessor: "aGreaterThanB"
})//{"asdf":testSetObj["asdf"]["children"]}})
)
console.log(true || binRelIterator(
testSetObj,
binRelWrapperInline,
{
binRelFunc: binRelXObjHashLookup,
binRelFuncParams: {"hashLookup":testSetObjHash},
binRelsAccessor: "aGreaterThanB"
})
)
console.log(true || binRelIteratorLookup(
testSetObj,
binRelWrapperInline,
{
binRelFunc: binRelXObjHashLookup,
binRelFuncParams: {"hashLookup":testSetObjHash},//could insert "key":"key"
binRelsAccessor: "aGreaterThanB"
},
"children",
"aGreaterThanB")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment