Skip to content

Instantly share code, notes, and snippets.

@gilligan gilligan/contentMapper.js Secret
Last active Dec 12, 2015

Embed
What would you like to do?
import R from 'ramda';
const hasStringValue = R.propIs(String, 'value');
const hasNumberValue = R.propIs(Number, 'value');
const hasPrimitiveValue = R.either(hasStringValue, hasNumberValue);
const isArrayOrObject = R.either(R.isArrayLike, R.is(Object));
function flattenValueKeys(obj) {
return R.cond([
[ R.isNil, R.identity ],
[ hasPrimitiveValue, R.prop('value') ],
[ isArrayOrObject, R.map(flattenValueKeys) ],
[ R.T, R.identity ]
])(obj);
}
export default function (content) {
const removeTypePrefix = R.pipe(R.split('.'), R.last);
const adjustKeyPrefixes = R.map(R.over(R.lensIndex(0), removeTypePrefix));
const mapKeys = R.pipe(
R.toPairs,
adjustKeyPrefixes,
R.fromPairs
);
const map = mapKeys(content.fragments);
return flattenValueKeys(map);
}
//////////////////////////////////////////////////////////////
// test
//////////////////////////////////////////////////////////////
describe('prismicMapper', function () {
let prismicContent;
beforeEach(function () {
prismicContent = {
id: 1,
type: 'header-brand',
fragments: {
'header-brand.logo': {
main: { url: 'some url' }
},
'header-brand.claim': {
value: 'some value'
},
'header-brand.invalid': null,
'header-brand.nested': {
thing: {
value: 'some thing'
}
},
'header-brand.nestedArray': {
thing: [ {
value: 'some thing'
}, {
value: 100
} ]
}
}
};
});
it('should transform the result from prismic', function () {
const result = mapPrismicResult(prismicContent);
const expectedResult = {
logo: { main: { url: 'some url' } },
claim: 'some value',
invalid: null,
nested: {
thing: 'some thing'
},
nestedArray: {
thing: [ 'some thing', 100 ]
}
};
expect(result).to.deep.equal(expectedResult);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.