Created
June 11, 2018 18:24
-
-
Save elycruz/05c58fe23a7b9158b124e056dd1907aa to your computer and use it in GitHub Desktop.
Idea for assoc_list_helpers (for going to and fro associated lists on specific keys and as a whole) (untested, and/or incomplete implementations)
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
/** | |
* Idea for assoc_list_helpers (for going to and fro associated lists on specific keys and as a whole) (untested, and/or incomplete implementations) | |
*/ | |
const | |
/** | |
* Returns an associated list on incoming's object type. | |
* @note Does deep conversion on all values of passed in type's type. | |
* @note Useful for working with object primitive (json and the like). | |
* @function module:objectOps._toAssocList | |
* @param obj {(Object|Array|*)} | |
* @param [TypeConstraint = undefined] {Constructor|Function|undefined}. | |
* @returns {Array.<*, *>} | |
*/ | |
toAssocList = (obj, TypeConstraint) => !obj ? [] : keys(obj).map(key => | |
TypeConstraint && _isType(TypeConstraint, obj[key]) ? | |
[key, toAssocList(obj[key])] : | |
[key, obj[key]] | |
), | |
/** | |
* From associated list to object. | |
* @note Considers array of arrays associated lists. | |
* @function module:objectOps.fromAssocList | |
* @param xs {Array.<Array>} - Associated list. | |
* @returns {Object} | |
*/ | |
fromAssocList = xs => !xs ? {} : xs.reduce((agg, [key, value]) => { | |
if (isArray(value) && isArray(value[0])) { | |
agg[key] = fromAssocList(value); | |
return agg; | |
} | |
agg[key] = value; | |
return agg; | |
}, {}), | |
/** | |
* Converts incoming object into an associated lists and all subsequently | |
* all objects found at `key` | |
* @function module:_objectOps._toAssocListOnKey | |
* @param key {*} - Usually a string. | |
* @param obj {*} - Object to convert on. | |
* @returns {any[]} - Associated list | |
*/ | |
_toAssocListOnKey = (key, obj) => _toAssocListOnKeys([key], obj), | |
/** | |
* Converts all objects found at keys contained in `ks` to associated lists. Additionally, only | |
* values found that match the given constraint-type will be converted. (Returns a copy of incoming object). | |
* @note This method is recursive and will search the given objects tree recursively. | |
* @note All objects in object tree are copies of original(s). | |
* @function module:_objectOps._toAssocListOnKeys | |
* @param ks {Array.<*>} - Keys. Usually `Array.<String>`. | |
* @param obj {*} - Object to convert on. | |
* @param [TypeConstraint=Object] {Constructor|Function} - Type constraint for key value. Default `Object`. | |
* @returns {Array} | |
*/ | |
_toAssocListOnKeys = (ks, obj, TypeConstraint = Object) => | |
keys(obj).reduce((agg, key) => { | |
const foundObj = obj[key], | |
matchesConstrainedType = (TypeConstraint && _isType(TypeConstraint, foundObj)), | |
keyValNeedsConversion = ks.includes(key) && matchesConstrainedType; | |
if (keyValNeedsConversion) { | |
agg[key] = keys(foundObj).map(k => { | |
return _isType(TypeConstraint, foundObj[k]) ? | |
[k, _toAssocListOnKeys(ks, foundObj[k], TypeConstraint)] : | |
[k, foundObj] | |
}); | |
} | |
return agg; | |
}, _assign(TypeConstraint ? new TypeConstraint() : {}, obj) | |
), | |
/** | |
* @note Considers array of arrays associated lists. | |
* @function module:objectOps.fromAssocList | |
* @param key {String|*} | |
* @param xs {Array.<Array>} - Associated list. | |
* @returns {*} | |
*/ | |
_fromAssocListOnKey = (key, xs) => _fromAssocListOnKeys([key], xs), | |
/** | |
* Converts an associated list into an object and any subsequent key matching `keys` | |
* @function module:objectOps.fromAssocListOnKeys | |
* @param ks {Array.<String>} - Property keys array. | |
* @param xs {Array|*} - Associated list. | |
* @returns {Object} | |
*/ | |
_fromAssocListOnKeys = (ks, xs) => !xs ? [] : xs.reduce((agg, [k, value]) => { | |
if (ks.includes(k) && isArray(value) && isArray(value[0])) { | |
agg[k] = _fromAssocListOnKeys(ks, value); | |
return agg; | |
} | |
agg[k] = value; | |
return agg; | |
}, {}), | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment