Skip to content

Commit

Permalink
refactor: move pickContext to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
pablopalacios committed Dec 28, 2021
1 parent 941523c commit 1036b96
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
43 changes: 4 additions & 39 deletions libs/fetcher.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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`.
Expand Down
40 changes: 40 additions & 0 deletions libs/util/pickContext.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 1036b96

Please sign in to comment.