1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 09:23:13 +01:00

Replace mergeOptions with js-utility-belt's safeMerge

And avoid using safeMerge when a normal Object.assign() would do.
This commit is contained in:
Brett Sun 2016-06-13 16:25:56 +02:00
parent 06c02ceecb
commit 56044488d7
21 changed files with 43 additions and 86 deletions

View File

@ -9,7 +9,7 @@ import WhitelabelStore from '../stores/whitelabel_store';
import GlobalNotification from './global_notification';
import { currentUserShape, locationShape, whitelabelShape } from './prop_types';
import { mergeOptions } from '../utils/general';
import { safeMerge } from '../utils/general';
export default function AppBase(App) {
@ -29,7 +29,7 @@ export default function AppBase(App) {
},
getInitialState() {
return mergeOptions(
return safeMerge(
UserStore.getState(),
WhitelabelStore.getState()
);

View File

@ -11,7 +11,7 @@ import PieceListStore from '../../stores/piece_list_store';
import CreateEditionsButton from '../ascribe_buttons/create_editions_button';
import AscribeSpinner from '../ascribe_spinner';
import { mergeOptions } from '../../utils/general';
import { safeMerge } from '../../utils/general';
import { getLangText } from '../../utils/lang';
@ -25,7 +25,7 @@ let AccordionListItemEditionWidget = React.createClass({
},
getInitialState() {
return mergeOptions(
return safeMerge(
EditionListStore.getState(),
PieceListStore.getState()
);

View File

@ -17,7 +17,7 @@ import TableItemAclFiltered from '../ascribe_table/table_item_acl_filtered';
import AscribeSpinner from '../ascribe_spinner';
import { getLangText } from '../../utils/lang';
import { mergeOptions } from '../../utils/general';
import { safeMerge } from '../../utils/general';
let AccordionListItemTableEditions = React.createClass({
@ -28,7 +28,7 @@ let AccordionListItemTableEditions = React.createClass({
},
getInitialState() {
return mergeOptions(
return safeMerge(
EditionListStore.getState(),
{
showMoreLoading: false

View File

@ -23,7 +23,7 @@ import AclProxy from '../acl_proxy';
import withContext from '../context/with_context';
import { whitelabelShape } from '../prop_types';
import { mergeOptions } from '../../utils/general';
import { safeMerge } from '../../utils/general';
import { getLangText } from '../../utils/lang';
@ -43,7 +43,7 @@ let AccordionListItemWallet = React.createClass({
},
getInitialState() {
return mergeOptions(
return safeMerge(
PieceListStore.getState(),
{
showCreateEditionsDialog: false

View File

@ -46,9 +46,9 @@ let CreateEditionsButton = React.createClass({
startPolling() {
// start polling until editions are defined
let pollingIntervalIndex = setInterval(() => {
// requests, will try to merge the filterBy parameter with other parameters (mergeOptions).
// Therefore it can't but null but instead has to be an empty object
// TODO: re-evaluate whether this is necessary:
// requests, will try to merge the filterBy parameter with other parameters (safeMerge).
// Therefore it can't be null but instead has to be an empty object
EditionListActions
.fetchEditionList({
pieceId: this.props.piece.id,

View File

@ -42,7 +42,7 @@ import { routerShape } from '../prop_types';
import ApiUrls from '../../constants/api_urls';
import { setDocumentTitle } from '../../utils/dom';
import { mergeOptions } from '../../utils/general';
import { safeMerge } from '../../utils/general';
import { getLangText } from '../../utils/lang';
/**
@ -69,7 +69,7 @@ const PieceContainer = React.createClass({
},
getInitialState() {
return mergeOptions(
return safeMerge(
PieceListStore.getState(),
PieceStore.getInitialState(),
{
@ -121,7 +121,7 @@ const PieceContainer = React.createClass({
*/
if (state && state.piece && state.piece.acl && typeof state.piece.acl.acl_loan !== 'undefined') {
let pieceState = mergeOptions({}, state.piece);
const pieceState = Object.assign({}, state.piece);
pieceState.acl.acl_loan = false;
this.setState({
piece: pieceState

View File

@ -18,7 +18,7 @@ import { routerShape } from '../prop_types';
import ApiUrls from '../../constants/api_urls';
import { mergeOptions } from '../../utils/general';
import { safeMerge } from '../../utils/general';
import { getLangText } from '../../utils/lang';
@ -31,7 +31,7 @@ const SendContractAgreementForm = React.createClass({
},
getInitialState() {
return mergeOptions(
return safeMerge(
ContractListStore.getState(),
{
selectedContract: 0

View File

@ -8,7 +8,7 @@ import InputCheckbox from './input_checkbox';
import ContractAgreementListStore from '../../stores/contract_agreement_list_store';
import ContractAgreementListActions from '../../actions/contract_agreement_list_actions';
import { mergeOptions } from '../../utils/general';
import { safeMerge } from '../../utils/general';
import { getLangText } from '../../utils/lang';
import { isEmail } from '../../utils/regex';
@ -37,7 +37,7 @@ const InputContractAgreementCheckbox = React.createClass({
},
getInitialState() {
return mergeOptions(
return safeMerge(
ContractAgreementListStore.getState(),
{
value: {
@ -73,7 +73,7 @@ const InputContractAgreementCheckbox = React.createClass({
// If there is no contract available, hide this `Property` from the user
this.props.setExpanded(!!contractAgreement);
state = mergeOptions(state, {
state = Object.assign(state, {
value: {
// If `email` is defined in this component, `getContractAgreementsOrCreatePublic`
// is either:

View File

@ -8,7 +8,7 @@ import Col from 'react-bootstrap/lib/Col';
import GlobalNotificationStore from '../stores/global_notification_store';
import { mergeOptions } from '../utils/general';
import { safeMerge } from '../utils/general';
const MAX_NOTIFICATION_BUBBLE_CONTAINER_WIDTH = 768;
@ -17,7 +17,7 @@ let GlobalNotification = React.createClass({
getInitialState() {
const notificationStore = GlobalNotificationStore.getState();
return mergeOptions(
return safeMerge(
{
containerWidth: 0
},

View File

@ -26,7 +26,7 @@ import { locationShape, routerShape } from './prop_types';
import { getAvailableAcls } from '../utils/acl';
import { setDocumentTitle } from '../utils/dom';
import { mergeOptions, isShallowEqual } from '../utils/general';
import { safeMerge, isShallowEqual } from '../utils/general';
import { getLangText } from '../utils/lang';
@ -75,7 +75,7 @@ const PieceList = React.createClass({
getInitialState() {
const pieceListStore = PieceListStore.getState();
const stores = mergeOptions(
const stores = safeMerge(
pieceListStore,
EditionListStore.getState(),
{

View File

@ -10,8 +10,6 @@ import AclProxy from '../../../../acl_proxy';
import withContext from '../../../../context/with_context';
import { currentUserShape } from '../../../../prop_types';
import { mergeOptions } from '../../../../../utils/general';
let WalletActionPanel = React.createClass({
propTypes: {
@ -40,7 +38,7 @@ let WalletActionPanel = React.createClass({
if (piece && piece.acl && typeof piece.acl.acl_loan !== 'undefined') {
// make a copy to not have side effects
availableAcls = mergeOptions({}, piece.acl);
availableAcls = Object.assign({}, piece.acl);
availableAcls.acl_loan = false;
}
let SubmitButtonType = submitButtonType;

View File

@ -8,12 +8,12 @@ import LicenseActions from '../../../../../actions/license_actions';
import LicenseStore from '../../../../../stores/license_store';
import { setDocumentTitle } from '../../../../../utils/dom';
import { mergeOptions } from '../../../../../utils/general';
import { safeMerge } from '../../../../../utils/general';
import { getLangText } from '../../../../../utils/lang';
let CCRegisterPiece = React.createClass({
getInitialState() {
return mergeOptions(
return safeMerge(
LicenseStore.getState(),
{
selectedLicense: 0

View File

@ -24,7 +24,7 @@ import withContext from '../../../../../context/with_context';
import { routerShape } from '../../../../../prop_types';
import { setDocumentTitle } from '../../../../../../utils/dom';
import { mergeOptions } from '../../../../../../utils/general';
import { safeMerge } from '../../../../../../utils/general';
import { getLangText } from '../../../../../../utils/lang';
@ -38,7 +38,7 @@ const CylandPieceContainer = React.createClass({
},
getInitialState() {
return mergeOptions(
return safeMerge(
PieceStore.getInitialState(),
PieceListStore.getState()
);

View File

@ -30,7 +30,7 @@ import ApiUrls from '../../../../../constants/api_urls';
import { setDocumentTitle } from '../../../../../utils/dom';
import { getAclFormMessage } from '../../../../../utils/form';
import { mergeOptions } from '../../../../../utils/general';
import { safeMerge } from '../../../../../utils/general';
import { getLangText } from '../../../../../utils/lang';
@ -44,7 +44,7 @@ const CylandRegisterPiece = React.createClass({
},
getInitialState(){
return mergeOptions(
return safeMerge(
PieceListStore.getState(),
PieceStore.getInitialState(),
{

View File

@ -25,7 +25,7 @@ import withContext from '../../../../../context/with_context';
import { routerShape } from '../../../../../prop_types';
import { setDocumentTitle } from '../../../../../../utils/dom';
import { mergeOptions } from '../../../../../../utils/general';
import { safeMerge } from '../../../../../../utils/general';
import { getLangText } from '../../../../../../utils/lang';
@ -39,7 +39,7 @@ const IkonotvPieceContainer = React.createClass({
},
getInitialState() {
return mergeOptions(
return safeMerge(
PieceListStore.getState(),
PieceStore.getInitialState()
);

View File

@ -26,7 +26,7 @@ import { currentUserShape, locationShape, routerShape, whitelabelShape } from '.
import ApiUrls from '../../../../../constants/api_urls';
import { mergeOptions } from '../../../../../utils/general';
import { safeMerge } from '../../../../../utils/general';
import { getLangText } from '../../../../../utils/lang';
@ -45,7 +45,7 @@ const IkonotvRegisterPiece = React.createClass({
},
getInitialState() {
return mergeOptions(
return safeMerge(
PieceListStore.getState(),
PieceStore.getInitialState(),
{

View File

@ -20,7 +20,7 @@ import withContext from '../../../../context/with_context';
import { locationShape, routerShape, whitelabelShape } from '../../../../prop_types';
import { setDocumentTitle } from '../../../../../utils/dom';
import { mergeOptions } from '../../../../../utils/general';
import { safeMerge } from '../../../../../utils/general';
import { getLangText } from '../../../../../utils/lang';
@ -33,7 +33,7 @@ let MarketRegisterPiece = React.createClass({
},
getInitialState(){
return mergeOptions(
return safeMerge(
PieceListStore.getState(),
PieceStore.getInitialState(),
{

View File

@ -2,7 +2,7 @@
import requests from '../utils/requests';
import { mergeOptions } from '../utils/general';
import { safeMerge } from '../utils/general';
import { generateOrderingQueryParams } from '../utils/url';
let EditionListFetcher = {
@ -12,7 +12,7 @@ let EditionListFetcher = {
fetch({ pieceId, page, pageSize, orderBy, orderAsc, filterBy }) {
const ordering = generateOrderingQueryParams(orderBy, orderAsc);
const queryParams = mergeOptions(
const queryParams = safeMerge(
{
page,
pageSize,

View File

@ -2,7 +2,7 @@
import requests from '../utils/requests';
import { mergeOptions } from '../utils/general';
import { safeMerge } from '../utils/general';
import { generateOrderingQueryParams } from '../utils/url';
let PieceListFetcher = {
@ -15,7 +15,7 @@ let PieceListFetcher = {
// filterBy is an object of acl key-value pairs.
// The values are booleans
const queryParams = mergeOptions(
const queryParams = safeMerge(
{
page,
pageSize,

View File

@ -7,7 +7,7 @@ import EditionActions from '../actions/edition_actions';
import EditionSource from '../sources/edition_source';
import CoaSource from '../sources/coa_source';
import { mergeOptions } from '../utils/general';
import { safeMerge } from '../utils/general';
class EditionStore {
@ -15,7 +15,7 @@ class EditionStore {
this.getInitialState();
this.bindActions(EditionActions);
this.registerAsync(mergeOptions(EditionSource, CoaSource));
this.registerAsync(safeMerge(EditionSource, CoaSource));
this.exportPublicMethods({
getInitialState: this.getInitialState.bind(this)
});

View File

@ -8,6 +8,7 @@ export { default as isShallowEqual } from 'shallow-equals';
// Re-export general utilities from js-utility-belt for easier access
export {
safeMerge,
sanitize
} from 'js-utility-belt/es6';
@ -39,48 +40,6 @@ export function sumNumList(l) {
*/
/**
* Checks a list of objects for key duplicates and returns a boolean
*/
function _doesObjectListHaveDuplicates(l) {
let mergedList = [];
l = l.map((obj) => {
if(!obj) {
throw new Error('The object you are trying to merge is null instead of an empty object');
}
return Object.keys(obj);
});
// Taken from: http://stackoverflow.com/a/10865042
// How to flatten an array of arrays in javascript.
// If two objects contain the same key, then these two keys
// will actually be represented in the merged array
mergedList = mergedList.concat.apply(mergedList, l);
// Taken from: http://stackoverflow.com/a/7376645/1263876
// By casting the array to a set, and then checking if the size of the array
// shrunk in the process of casting, we can check if there were any duplicates
return new Set(mergedList).size !== mergedList.length;
}
/**
* 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 mergeOptions(...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.
if (_doesObjectListHaveDuplicates(l)) {
throw new Error('The objects you submitted for merging have duplicates. Merge aborted.');
}
return Object.assign({}, ...l);
}
/**
* In place update of a dictionary
*/