all files / ps-direct-sdk/lib/ stringify.js

94.74% Statements 18/19
91.67% Branches 11/12
100% Functions 3/3
100% Lines 17/17
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                          52× 52× 50× 14× 12× 18× 18×   20×   12× 36× 35×    
"use strict";
 
var R = require("ramda");
 
/*
 *
 * rewrites usages of { $ref: '${uri}' } into ${uri} and
 * returns a string for saving into the presentation-service
 * so that earlier code returns correct js objects
 *
 * $ref key is required so the object is valid json. Otherwise
 * this function would pass the whole resolved object in as a string
 *
 */
function stringify(presentation) {
    var strings;
    if (R.is(Array, presentation)) {
        strings = R.map(stringify, presentation);
        return '[' + strings.join(', ') + ']';
    } else if (R.is(Object, presentation)) {
        if (presentation && presentation.$ref) return presentation.$ref;
        strings = R.map(function (pair) {
            Iif (R.isNil(pair[1])) return null;
            else return pair.map(stringify).join(': ');
        }, R.reject(function (pair) {
            return R.isNil(pair[1]);
        }, R.toPairs(presentation)));
        return '{' + strings.join(', ') + '}';
    } else if (presentation === null) {
        return "null";
    } else return JSON.stringify(presentation);
}
 
module.exports = stringify;