-
-
Save gilligan/dc1409dee420798c73dc to your computer and use it in GitHub Desktop.
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 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