1
0
mirror of https://github.com/ascribe/onion.git synced 2025-01-03 10:25:08 +01:00

Modify excludePropFromObject to be similar to lodash.omit()

This commit is contained in:
Brett Sun 2015-11-02 18:31:01 +01:00
parent 955e20d6b6
commit 1a3dffe8bc
2 changed files with 35 additions and 8 deletions

View File

@ -143,14 +143,41 @@ export function escapeHTML(s) {
return document.createElement('div').appendChild(document.createTextNode(s)).parentNode.innerHTML;
}
export function excludePropFromObject(obj, propList){
let clonedObj = mergeOptions({}, obj);
for (let item in propList){
if (clonedObj[propList[item]]){
delete clonedObj[propList[item]];
/**
* Returns a copy of the given object's own and inherited enumerable
* properties, omitting any keys that pass the given filter function.
*/
function filterObjOnFn(obj, filterFn) {
const filteredObj = {};
for (let key in obj) {
const val = obj[key];
if (filterFn == null || !filterFn(val, key)) {
filteredObj[key] = val;
}
}
return clonedObj;
return filteredObj;
}
/**
* Similar to lodash's _.omit(), this returns a copy of the given object's
* own and inherited enumerable properties, omitting any keys that are
* in the given array or whose value pass the given filter function.
* @param {object} obj Source object
* @param {array|function} filter Array of key names to omit or function to invoke per iteration
* @return {object} The new object
*/
export function omitFromObject(obj, filter) {
if (filter && filter.constructor === Array) {
return filterObjOnFn(obj, (_, key) => {
return filter.indexOf(key) >= 0;
});
} else if (filter && typeof filter === 'function') {
return filterObjOnFn(obj, filter);
} else {
throw new Error('The given filter is not an array or function. Exclude aborted');
}
}
/**

View File

@ -5,7 +5,7 @@ import Q from 'q';
import AppConstants from '../constants/application_constants';
import { getCookie } from '../utils/fetch_api_utils';
import { excludePropFromObject } from '../utils/general_utils';
import { omitFromObject } from '../utils/general_utils';
import { argsToQueryParams } from '../utils/url_utils';
class Requests {
@ -127,9 +127,9 @@ class Requests {
return this.request('delete', newUrl);
}
let params = excludePropFromObject(paramsAndBody, ['body']);
_putOrPost(url, paramsAndBody, method) {
let paramsCopy = Object.assign({}, paramsAndBody);
let params = omitFromObject(paramsAndBody, ['body']);
let newUrl = this.prepareUrl(url, params);
let body = null;
if (paramsCopy && paramsCopy.body) {