Created
November 7, 2016 22:14
-
-
Save oconn/235de4106746bbec51a7d422d893d5d3 to your computer and use it in GitHub Desktop.
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
import { append, equals, map, merge, omit, pluck, prepend, reject, zipObj } from 'ramda'; | |
/** | |
* @method itemByKey | |
* | |
* pulls an items key and asigns itself to its key | |
*/ | |
export const itemByKey = (item, key = '_id') => { | |
return { [item[key]]: item }; | |
}; | |
/** | |
* @method toDictionary | |
* | |
* creates a dictionary from a given collection | |
*/ | |
export const toDictionary = (collection, key = '_id') => { | |
return zipObj(map(item => item[key], collection), collection); | |
}; | |
/** | |
* @method byIds | |
* | |
* creates an array of ids | |
*/ | |
export const byIds = (collection, key = '_id') => { | |
return pluck(key)(collection); | |
}; | |
/** | |
* @method normalizeCollection | |
* | |
* normalizes a collection | |
*/ | |
export const normalizeCollection = (collection, key = '_id') => { | |
return { | |
data: toDictionary(collection, key), | |
ids: byIds(collection, key) | |
}; | |
}; | |
/** | |
* @method addNormalizedItem | |
* | |
* adds an item to a normalized collection | |
*/ | |
const _addNormalizedItem = (normalizeCollection, item, key, method) => { | |
const { data, ids } = normalizeCollection; | |
return { | |
data: merge(data, itemByKey(item, key)), | |
ids: method(item[key], ids) | |
} | |
}; | |
/** | |
* @method appendNormalizedItem | |
* | |
* adds an item to the tail of a normalized collection | |
*/ | |
export const addNormalizedItem = (normalizedCollection, item, key = '_id') => { | |
return _addNormalizedItem(normalizeCollection, item, key, append); | |
}; | |
/** | |
* @method prependNormalizedItem | |
* | |
* adds an item to the head of a normalized collection | |
*/ | |
export const prependNormalizedItem = (normalizeCollection, item, key = '_id') => { | |
return _addNormalizedItem(normalizeCollection, item, key, prepend); | |
}; | |
/** | |
* @method removeNormalizedItem | |
* | |
* removes an item from a normalized collection | |
*/ | |
export const removeNormalizedItem = (normalizeCollection, itemId) => { | |
const { data, ids } = normalizeCollection; | |
return { | |
data: omit([ itemId ], data), | |
ids: reject(equals(itemId), ids) | |
}; | |
}; | |
/** | |
* @method toMap | |
* | |
* maps over a normalized collection turning | |
*/ | |
export const toMap = (normalizeCollection) => { | |
const { data, ids } = normalizeCollection; | |
return map(id => { | |
return { item: data[id], id: id } | |
}, ids); | |
}; | |
/** | |
* @constant | |
* | |
* empty normalized collection | |
*/ | |
export const emptyNormalizedCollection = { | |
data: {}, | |
ids: [] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment