UNPKG

@axway/api-builder-runtime

Version:

API Builder Runtime

47 lines (43 loc) 1.05 kB
var fs = require('fs'), path = require('path'), obj = {}; /** * Converts the provided object in to a field, setting some default properties. * @param obj * @returns {*|{}} */ function toField(obj) { obj = obj || {}; Object.keys(obj).forEach(function (key) { const value = obj[key], type = typeof(value); if (type === 'function') { delete obj[key]; } else if (value instanceof RegExp) { let flags = ''; value.ignoreCase && (flags += 'i'); value.global && (flags += 'g'); value.multiline && (flags += 'm'); obj[key] = { value: value.source, flags: flags, type: 'regexp' }; } }); return obj; } /** * Reads through the child files and loads them as fields. */ fs.readdirSync(__dirname).forEach(function (name) { const fn = path.basename(name); if (/\.js$/.test(fn) && fn !== 'index.js') { const metadata = require('./' + name); obj[metadata.name] = function () { const result = metadata.apply(metadata, arguments); return toField(result); }; } }); exports = module.exports = obj;