mirror of
https://github.com/ascribe/onion.git
synced 2024-12-22 17:33:14 +01:00
Use Object.assign() instead of writing own merge function
This commit is contained in:
parent
49f20462f1
commit
fe4e337690
@ -12,8 +12,6 @@ import GlobalNotificationActions from '../../actions/global_notification_actions
|
|||||||
import requests from '../../utils/requests';
|
import requests from '../../utils/requests';
|
||||||
|
|
||||||
import { getLangText } from '../../utils/lang_utils';
|
import { getLangText } from '../../utils/lang_utils';
|
||||||
import { mergeOptionsWithDuplicates } from '../../utils/general_utils';
|
|
||||||
|
|
||||||
|
|
||||||
let Form = React.createClass({
|
let Form = React.createClass({
|
||||||
propTypes: {
|
propTypes: {
|
||||||
@ -124,12 +122,12 @@ let Form = React.createClass({
|
|||||||
getFormData() {
|
getFormData() {
|
||||||
let data = {};
|
let data = {};
|
||||||
|
|
||||||
for(let ref in this.refs) {
|
for (let ref in this.refs) {
|
||||||
data[this.refs[ref].props.name] = this.refs[ref].state.value;
|
data[this.refs[ref].props.name] = this.refs[ref].state.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof this.props.getFormData === 'function') {
|
if (typeof this.props.getFormData === 'function') {
|
||||||
data = mergeOptionsWithDuplicates(data, this.props.getFormData());
|
data = Object.assign(data, this.props.getFormData());
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
@ -76,8 +76,8 @@ export function formatText() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
Checks a list of objects for key duplicates and returns a boolean
|
* Checks a list of objects for key duplicates and returns a boolean
|
||||||
*/
|
*/
|
||||||
function _doesObjectListHaveDuplicates(l) {
|
function _doesObjectListHaveDuplicates(l) {
|
||||||
let mergedList = [];
|
let mergedList = [];
|
||||||
@ -115,35 +115,7 @@ export function mergeOptions(...l) {
|
|||||||
throw new Error('The objects you submitted for merging have duplicates. Merge aborted.');
|
throw new Error('The objects you submitted for merging have duplicates. Merge aborted.');
|
||||||
}
|
}
|
||||||
|
|
||||||
let newObj = {};
|
return Object.assign({}, ...l);
|
||||||
|
|
||||||
for(let i = 1; i < l.length; i++) {
|
|
||||||
newObj = _mergeOptions(newObj, _mergeOptions(l[i - 1], l[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
return newObj;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Merges a number of objects even if there're having duplicates.
|
|
||||||
*
|
|
||||||
* DOES NOT RETURN AN ERROR!
|
|
||||||
*
|
|
||||||
* Takes a list of object and merges their keys to one object.
|
|
||||||
* Uses mergeOptions for two objects.
|
|
||||||
* @param {[type]} l [description]
|
|
||||||
* @return {[type]} [description]
|
|
||||||
*/
|
|
||||||
export function mergeOptionsWithDuplicates(...l) {
|
|
||||||
// If the objects submitted in the list have duplicates,in their key names,
|
|
||||||
// abort the merge and tell the function's user to check his objects.
|
|
||||||
let newObj = {};
|
|
||||||
|
|
||||||
for(let i = 1; i < l.length; i++) {
|
|
||||||
newObj = _mergeOptions(newObj, _mergeOptions(l[i - 1], l[i]));
|
|
||||||
}
|
|
||||||
|
|
||||||
return newObj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -159,25 +131,6 @@ export function update(a, ...l) {
|
|||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Overwrites obj1's values with obj2's and adds obj2's if non existent in obj1
|
|
||||||
* @param obj1
|
|
||||||
* @param obj2
|
|
||||||
* @returns obj3 a new object based on obj1 and obj2
|
|
||||||
* Taken from: http://stackoverflow.com/a/171256/1263876
|
|
||||||
*/
|
|
||||||
function _mergeOptions(obj1, obj2) {
|
|
||||||
let obj3 = {};
|
|
||||||
|
|
||||||
for (let attrname in obj1) {
|
|
||||||
obj3[attrname] = obj1[attrname];
|
|
||||||
}
|
|
||||||
for (let attrname in obj2) {
|
|
||||||
obj3[attrname] = obj2[attrname];
|
|
||||||
}
|
|
||||||
return obj3;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escape HTML in a string so it can be injected safely using
|
* Escape HTML in a string so it can be injected safely using
|
||||||
* React's `dangerouslySetInnerHTML`
|
* React's `dangerouslySetInnerHTML`
|
||||||
|
@ -9,17 +9,6 @@ import { excludePropFromObject } from '../utils/general_utils';
|
|||||||
import { argsToQueryParams } from '../utils/url_utils';
|
import { argsToQueryParams } from '../utils/url_utils';
|
||||||
|
|
||||||
class Requests {
|
class Requests {
|
||||||
_merge(defaults, options) {
|
|
||||||
let merged = {};
|
|
||||||
for (let key in defaults) {
|
|
||||||
merged[key] = defaults[key];
|
|
||||||
}
|
|
||||||
for (let key in options) {
|
|
||||||
merged[key] = options[key];
|
|
||||||
}
|
|
||||||
return merged;
|
|
||||||
}
|
|
||||||
|
|
||||||
unpackResponse(response) {
|
unpackResponse(response) {
|
||||||
if (response.status >= 500) {
|
if (response.status >= 500) {
|
||||||
throw new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url);
|
throw new Error(response.status + ' - ' + response.statusText + ' - on URL:' + response.url);
|
||||||
@ -112,7 +101,7 @@ class Requests {
|
|||||||
|
|
||||||
request(verb, url, options) {
|
request(verb, url, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
let merged = this._merge(this.httpOptions, options);
|
let merged = Object.assign({}, this.httpOptions, options);
|
||||||
let csrftoken = getCookie(AppConstants.csrftoken);
|
let csrftoken = getCookie(AppConstants.csrftoken);
|
||||||
if (csrftoken) {
|
if (csrftoken) {
|
||||||
merged.headers['X-CSRFToken'] = csrftoken;
|
merged.headers['X-CSRFToken'] = csrftoken;
|
||||||
@ -124,23 +113,23 @@ class Requests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get(url, params) {
|
get(url, params) {
|
||||||
if (url === undefined){
|
if (url === undefined) {
|
||||||
throw new Error('Url undefined');
|
throw new Error('Url undefined');
|
||||||
}
|
}
|
||||||
let paramsCopy = this._merge(params);
|
let paramsCopy = Object.assign({}, params);
|
||||||
let newUrl = this.prepareUrl(url, paramsCopy, true);
|
let newUrl = this.prepareUrl(url, paramsCopy, true);
|
||||||
return this.request('get', newUrl);
|
return this.request('get', newUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(url, params) {
|
delete(url, params) {
|
||||||
let paramsCopy = this._merge(params);
|
let paramsCopy = Object.assign({}, params);
|
||||||
let newUrl = this.prepareUrl(url, paramsCopy, true);
|
let newUrl = this.prepareUrl(url, paramsCopy, true);
|
||||||
return this.request('delete', newUrl);
|
return this.request('delete', newUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
_putOrPost(url, paramsAndBody, method){
|
|
||||||
let paramsCopy = this._merge(paramsAndBody);
|
|
||||||
let params = excludePropFromObject(paramsAndBody, ['body']);
|
let params = excludePropFromObject(paramsAndBody, ['body']);
|
||||||
|
_putOrPost(url, paramsAndBody, method) {
|
||||||
|
let paramsCopy = Object.assign({}, paramsAndBody);
|
||||||
let newUrl = this.prepareUrl(url, params);
|
let newUrl = this.prepareUrl(url, params);
|
||||||
let body = null;
|
let body = null;
|
||||||
if (paramsCopy && paramsCopy.body) {
|
if (paramsCopy && paramsCopy.body) {
|
||||||
@ -153,11 +142,11 @@ class Requests {
|
|||||||
return this._putOrPost(url, params, 'post');
|
return this._putOrPost(url, params, 'post');
|
||||||
}
|
}
|
||||||
|
|
||||||
put(url, params){
|
put(url, params) {
|
||||||
return this._putOrPost(url, params, 'put');
|
return this._putOrPost(url, params, 'put');
|
||||||
}
|
}
|
||||||
|
|
||||||
patch(url, params){
|
patch(url, params) {
|
||||||
return this._putOrPost(url, params, 'patch');
|
return this._putOrPost(url, params, 'patch');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user