Skip to content

Instantly share code, notes, and snippets.

@stuf
Created January 15, 2018 10:06
Show Gist options
  • Save stuf/a0f01bd341b737cb7ed978110e9b8bae to your computer and use it in GitHub Desktop.
Save stuf/a0f01bd341b737cb7ed978110e9b8bae to your computer and use it in GitHub Desktop.
Isomorphisms with partial lenses for consuming OBS WebSocket data
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