Created
January 15, 2018 10:06
-
-
Save stuf/a0f01bd341b737cb7ed978110e9b8bae to your computer and use it in GitHub Desktop.
Isomorphisms with partial lenses for consuming OBS WebSocket data
This file contains 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 * as R from 'ramda'; | |
import * as L from 'partial.lenses'; | |
// | |
const splitCamelCase = R.match(/(^[a-z]+|[A-Z][a-z]+)/g); | |
const flatJoin = R.compose(R.join(''), R.flatten); | |
// | |
const camelToKebab = | |
R.pipe(R.toLower, | |
R.split('-'), | |
R.map(R.adjust(R.toUpper, 0)), | |
flatJoin)) | |
const kebabCaseCamel = | |
R.compose(R.toLower, | |
R.join('-'), | |
splitCamelCase); | |
// | |
export const obsI = | |
L.iso(L.modify(L.keys, kebabToCamel), | |
L.modify(L.keys, camelToKebab)); | |
// The data we'll use | |
const data = { | |
'message-id': 'foo-123', | |
'request-type': 'GetFoo' | |
}; | |
// When querying data, objects will have camelCased keys | |
const eventData = L.get(obsI, data); | |
const eq1 = R.equals(eventData, | |
{ messageId: 'foo-123', | |
requestType: 'GetFoo' }); | |
// When modifying data, objects will have kebab-cased keys | |
const obsData = L.modify(obsI, R.identity, eventData); | |
const eq2 = R.equals(obsData, | |
{ 'message-id': 'foo-123', | |
'request-type': 'GetFoo' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment