diff --git a/libs/fetcher.client.js b/libs/fetcher.client.js index 26e13590..8e1fbf0b 100644 --- a/libs/fetcher.client.js +++ b/libs/fetcher.client.js @@ -10,12 +10,14 @@ * @module Fetcher */ var REST = require('./util/http.client'); +var defaultConstructGetUri = require('./util/defaultConstructGetUri'); +var forEach = require('./util/forEach'); +var pickContext = require('./util/pickContext'); + var DEFAULT_PATH = '/api'; var DEFAULT_TIMEOUT = 3000; var MAX_URI_LEN = 2048; var OP_READ = 'read'; -var defaultConstructGetUri = require('./util/defaultConstructGetUri'); -var forEach = require('./util/forEach'); function parseResponse(response) { if (response && response.responseText) { @@ -28,43 +30,6 @@ function parseResponse(response) { return null; } -/** - * Pick keys from the context object - * @method pickContext - * @param {Object} context - context object - * @param {Function|Array|String} picker - key, array of keys or - * function that return keys to be extracted from context. - * @param {String} method - method name, GET or POST - */ -function pickContext(context, picker, method) { - if (!picker || !picker[method]) { - return context; - } - - var p = picker[method]; - var result = {}; - - if (typeof p === 'string') { - result[p] = context[p]; - } else if (Array.isArray(p)) { - p.forEach(function (key) { - result[key] = context[key]; - }); - } else if (typeof p === 'function') { - forEach(context, function (value, key) { - if (p(value, key, context)) { - result[key] = context[key]; - } - }); - } else { - throw new TypeError( - 'picker must be an string, an array, or a function.' - ); - } - - return result; -} - /** * A RequestClient instance represents a single fetcher request. * The constructor requires `operation` (CRUD) and `resource`. diff --git a/libs/util/pickContext.js b/libs/util/pickContext.js new file mode 100644 index 00000000..b77a1da2 --- /dev/null +++ b/libs/util/pickContext.js @@ -0,0 +1,40 @@ +var forEach = require('./forEach'); + +/** + * Pick keys from the context object + * @method pickContext + * @param {Object} context - context object + * @param {Function|Array|String} picker - key, array of keys or + * function that return keys to be extracted from context. + * @param {String} method - method name, GET or POST + */ +function pickContext(context, picker, method) { + if (!picker || !picker[method]) { + return context; + } + + var p = picker[method]; + var result = {}; + + if (typeof p === 'string') { + result[p] = context[p]; + } else if (Array.isArray(p)) { + p.forEach(function (key) { + result[key] = context[key]; + }); + } else if (typeof p === 'function') { + forEach(context, function (value, key) { + if (p(value, key, context)) { + result[key] = context[key]; + } + }); + } else { + throw new TypeError( + 'picker must be an string, an array, or a function.' + ); + } + + return result; +} + +module.exports = pickContext;