Skip to content

Instantly share code, notes, and snippets.

@owise1
Last active April 25, 2017 18:12
Show Gist options
  • Save owise1/6bfc05c649308d664109 to your computer and use it in GitHub Desktop.
Save owise1/6bfc05c649308d664109 to your computer and use it in GitHub Desktop.
owise1-functions.js
// depends on ramda.js
var owise1 = {
// takes: object
// returns: array containing the keys that are truthy
trueKeys : R.compose(R.keys, R.pickBy(function(v,k){ return v })),
// takes: two arrays
// returns: bool
doArraysIntersect : R.curry(function(firstList, secondList){
return R.reduce(function(prev, curr){
return prev || firstList.indexOf(curr) > -1
}, false, secondList)
}),
// takes: path (str, arr), arr of objs
// returns: obj where keys are the path value and values are an array of objs w/that common key
groupedBy : R.curry(function(path, arr){
if (typeof path === 'string') path = path.split('.')
return arr.reduce(function(prev, curr){
var val = R.path(path, curr)
prev[val] = prev[val] || []
prev[val].push(curr)
return prev
}, {});
}),
stripTags : function (str) {
return str.replace(/(<([^>]+)>)/ig,"")
},
addPropToObj : R.curry(function (prop, fn){
return function (obj) {
return R.set(R.lensProp(prop), typeof fn === 'function' ? fn(obj) : fn, R.clone(obj))
}
}),
guid : function(){
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = crypto.getRandomValues(new Uint8Array(1))[0]%16|0, v = c == 'x' ? r : (r&0x3|0x8)
return v.toString(16)
})
},
removeFromArr: function (key, arr) {
return R.filter(R.pipe(R.equals(key), R.not), arr)
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment