Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gilligan
Last active December 12, 2015 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilligan/dc1409dee420798c73dc to your computer and use it in GitHub Desktop.
Save gilligan/dc1409dee420798c73dc to your computer and use it in GitHub Desktop.
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