All files / fabric-contract-api/lib/annotations utils.js

100% Statements 38/38
100% Branches 23/23
100% Functions 11/11
100% Lines 38/38

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108            1x   1x 11x 6x 3x 3x       11x 8x 8x 8x       1x 6x 5x 1x       5x     1x 30x 9x     21x 8x   8x       13x 4x   4x           9x       1x           30x 30x       9x     21x           29x         15x           21x       8x 6x     2x       13x       4x    
/*
 * Copyright IBM Corp. All Rights Reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
 
const refPath = '#/components/schemas/';
 
module.exports.appendOrUpdate = function appendOrUpdate(arr, primaryField, id, data) {
    const objExists = arr.some((obj) => {
        if (obj[primaryField] === id) {
            Object.assign(obj, data);
            return true;
        }
    });
 
    if (!objExists) {
        const obj = data;
        data[primaryField] = id;
        arr.push(obj);
    }
};
 
module.exports.findByValue = function findByValue(arr, primaryField, id) {
    for (const el of arr) {
        if (el[primaryField] === id) {
            return el;
        }
    }
 
    return null;
};
 
const generateSchema = (type, fullPath = true) => {
    if (isPrimitive(type)) {
        return {
            type: type.toLowerCase()
        };
    } else if (isArray(type)) {
        const subType = getSubArray(type);
 
        return  {
            type: 'array',
            items: generateSchema(subType, fullPath)
        };
    } else if (isMap(type)) {
        const subType = getSubMap(type);
 
        return {
            type: 'object',
            additionalProperties: generateSchema(subType, fullPath)
        };
    }
 
    return {
        $ref: (fullPath ? refPath : '') + type
    };
};
module.exports.generateSchema = generateSchema;
 
// there appears to be confusions within the meta data handling
// whether string or String is correct.
// string is the preferred for JSON Schema
function isPrimitive(type) {
    const lowerCase = type.toLowerCase();
    switch (lowerCase) {
        case 'string':
        case 'number':
        case 'boolean':
            return lowerCase;
 
        default:
            return undefined;
    }
}
 
// Like Array<number>
function isArrowedArray(type) {
    return /^Array<[A-z].*>$/.test(type);
}
 
// Like number[]
function isBracketArray(type) {
    return /^[A-z].*(\[\])+?/.test(type);
}
 
// determine if string representation of type
// is in format of an arrray descriptor
function isArray(type) {
    return isArrowedArray(type) || isBracketArray(type);
}
 
function getSubArray(type) {
    if (isArrowedArray(type)) {
        return type.replace('Array<', '').replace('>', '');
    }
 
    return type.replace('[]', '');
}
 
function isMap(type) {
    return /^Map<[A-z].*,\s?[A-z].*>$/.test(type);
}
 
function getSubMap(type) {
    return type.replace(/^Map<[A-z].*?,\s?/, '').replace('>', '');
}