all files / api/toJSON/ authorEndpoint.js

100% Statements 9/9
100% Branches 0/0
100% Functions 0/0
100% Lines 9/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                                     
'use strict';
const R = require('ramda');
 
// once merged to master this can be exported
const splitAndParse = R.compose(
    R.defaultTo(null),
    parseInt,
    R.takeLast(1),
    R.split('.'),
    String
);
 
module.exports = response => {
 
    const metadata = {
        type:'author',
        id: splitAndParse(response.id)
    };
 
    const attributes = R.assoc('attributes',
        R.pick(
            [
                'name',
                'description',
                'twitterId'
            ],
        response
        ),
    {});
 
    // links property could not be there
    // and should not be in the result object
    const links = R.ifElse(
        R.any(x => x !== undefined),
        () => R.assoc('links', R.pick(['url','imageUrl'], response), {}),
        R.defaultTo(null)
    )([response.url, response.imageUrl]);
 
    return R.assoc('data',
        R.mergeAll([metadata, attributes, links]),
        {}
    );
};