From 56044488d7886be92714c074d82f33da92afc046 Mon Sep 17 00:00:00 2001 From: Brett Sun Date: Mon, 13 Jun 2016 16:25:56 +0200 Subject: [PATCH] Replace mergeOptions with js-utility-belt's safeMerge And avoid using safeMerge when a normal Object.assign() would do. --- js/components/app_base.js | 4 +- .../accordion_list_item_edition_widget.js | 4 +- .../accordion_list_item_table_editions.js | 4 +- .../accordion_list_item_wallet.js | 4 +- .../ascribe_buttons/create_editions_button.js | 6 +-- .../ascribe_detail/piece_container.js | 6 +-- .../form_send_contract_agreement.js | 4 +- .../input_contract_agreement_checkbox.js | 6 +-- js/components/global_notification.js | 4 +- js/components/piece_list.js | 4 +- .../ascribe_detail/wallet_action_panel.js | 4 +- .../wallet/components/cc/cc_register_piece.js | 4 +- .../cyland_detail/cyland_piece_container.js | 4 +- .../cyland/cyland_register_piece.js | 4 +- .../ikonotv_detail/ikonotv_piece_container.js | 4 +- .../ikonotv/ikonotv_register_piece.js | 4 +- .../market/market_register_piece.js | 4 +- js/fetchers/edition_list_fetcher.js | 4 +- js/fetchers/piece_list_fetcher.js | 4 +- js/stores/edition_store.js | 4 +- js/utils/general.js | 43 +------------------ 21 files changed, 43 insertions(+), 86 deletions(-) diff --git a/js/components/app_base.js b/js/components/app_base.js index 50589efd..8f3ba052 100644 --- a/js/components/app_base.js +++ b/js/components/app_base.js @@ -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() ); diff --git a/js/components/ascribe_accordion_list/accordion_list_item_edition_widget.js b/js/components/ascribe_accordion_list/accordion_list_item_edition_widget.js index f62ed58b..ef2526ea 100644 --- a/js/components/ascribe_accordion_list/accordion_list_item_edition_widget.js +++ b/js/components/ascribe_accordion_list/accordion_list_item_edition_widget.js @@ -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() ); diff --git a/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js b/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js index a9c42843..24022e07 100644 --- a/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js +++ b/js/components/ascribe_accordion_list/accordion_list_item_table_editions.js @@ -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 diff --git a/js/components/ascribe_accordion_list/accordion_list_item_wallet.js b/js/components/ascribe_accordion_list/accordion_list_item_wallet.js index 513b699e..08f7a035 100644 --- a/js/components/ascribe_accordion_list/accordion_list_item_wallet.js +++ b/js/components/ascribe_accordion_list/accordion_list_item_wallet.js @@ -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 diff --git a/js/components/ascribe_buttons/create_editions_button.js b/js/components/ascribe_buttons/create_editions_button.js index 4ad59194..d42d61f7 100644 --- a/js/components/ascribe_buttons/create_editions_button.js +++ b/js/components/ascribe_buttons/create_editions_button.js @@ -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, diff --git a/js/components/ascribe_detail/piece_container.js b/js/components/ascribe_detail/piece_container.js index 80a84182..95208181 100644 --- a/js/components/ascribe_detail/piece_container.js +++ b/js/components/ascribe_detail/piece_container.js @@ -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 diff --git a/js/components/ascribe_forms/form_send_contract_agreement.js b/js/components/ascribe_forms/form_send_contract_agreement.js index bc012a0a..af333083 100644 --- a/js/components/ascribe_forms/form_send_contract_agreement.js +++ b/js/components/ascribe_forms/form_send_contract_agreement.js @@ -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 diff --git a/js/components/ascribe_forms/input_contract_agreement_checkbox.js b/js/components/ascribe_forms/input_contract_agreement_checkbox.js index f049ac72..9bbcb7cb 100644 --- a/js/components/ascribe_forms/input_contract_agreement_checkbox.js +++ b/js/components/ascribe_forms/input_contract_agreement_checkbox.js @@ -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: diff --git a/js/components/global_notification.js b/js/components/global_notification.js index 3ab8ac61..7125e678 100644 --- a/js/components/global_notification.js +++ b/js/components/global_notification.js @@ -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 }, diff --git a/js/components/piece_list.js b/js/components/piece_list.js index 847acd8f..c25b5e12 100644 --- a/js/components/piece_list.js +++ b/js/components/piece_list.js @@ -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(), { diff --git a/js/components/whitelabel/wallet/components/ascribe_detail/wallet_action_panel.js b/js/components/whitelabel/wallet/components/ascribe_detail/wallet_action_panel.js index b15e1288..07a0f658 100644 --- a/js/components/whitelabel/wallet/components/ascribe_detail/wallet_action_panel.js +++ b/js/components/whitelabel/wallet/components/ascribe_detail/wallet_action_panel.js @@ -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; diff --git a/js/components/whitelabel/wallet/components/cc/cc_register_piece.js b/js/components/whitelabel/wallet/components/cc/cc_register_piece.js index 668ec4f4..52646b1c 100644 --- a/js/components/whitelabel/wallet/components/cc/cc_register_piece.js +++ b/js/components/whitelabel/wallet/components/cc/cc_register_piece.js @@ -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 diff --git a/js/components/whitelabel/wallet/components/cyland/cyland_detail/cyland_piece_container.js b/js/components/whitelabel/wallet/components/cyland/cyland_detail/cyland_piece_container.js index 13032baa..a42002e6 100644 --- a/js/components/whitelabel/wallet/components/cyland/cyland_detail/cyland_piece_container.js +++ b/js/components/whitelabel/wallet/components/cyland/cyland_detail/cyland_piece_container.js @@ -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() ); diff --git a/js/components/whitelabel/wallet/components/cyland/cyland_register_piece.js b/js/components/whitelabel/wallet/components/cyland/cyland_register_piece.js index 71260042..dbe97794 100644 --- a/js/components/whitelabel/wallet/components/cyland/cyland_register_piece.js +++ b/js/components/whitelabel/wallet/components/cyland/cyland_register_piece.js @@ -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(), { diff --git a/js/components/whitelabel/wallet/components/ikonotv/ikonotv_detail/ikonotv_piece_container.js b/js/components/whitelabel/wallet/components/ikonotv/ikonotv_detail/ikonotv_piece_container.js index 4d51edb4..11b01378 100644 --- a/js/components/whitelabel/wallet/components/ikonotv/ikonotv_detail/ikonotv_piece_container.js +++ b/js/components/whitelabel/wallet/components/ikonotv/ikonotv_detail/ikonotv_piece_container.js @@ -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() ); diff --git a/js/components/whitelabel/wallet/components/ikonotv/ikonotv_register_piece.js b/js/components/whitelabel/wallet/components/ikonotv/ikonotv_register_piece.js index c897a98e..eec443ea 100644 --- a/js/components/whitelabel/wallet/components/ikonotv/ikonotv_register_piece.js +++ b/js/components/whitelabel/wallet/components/ikonotv/ikonotv_register_piece.js @@ -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(), { diff --git a/js/components/whitelabel/wallet/components/market/market_register_piece.js b/js/components/whitelabel/wallet/components/market/market_register_piece.js index 2d636ff0..6ac6afa8 100644 --- a/js/components/whitelabel/wallet/components/market/market_register_piece.js +++ b/js/components/whitelabel/wallet/components/market/market_register_piece.js @@ -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(), { diff --git a/js/fetchers/edition_list_fetcher.js b/js/fetchers/edition_list_fetcher.js index 5b23a15f..37d69ef3 100644 --- a/js/fetchers/edition_list_fetcher.js +++ b/js/fetchers/edition_list_fetcher.js @@ -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, diff --git a/js/fetchers/piece_list_fetcher.js b/js/fetchers/piece_list_fetcher.js index ec0c4067..3ee6dc42 100644 --- a/js/fetchers/piece_list_fetcher.js +++ b/js/fetchers/piece_list_fetcher.js @@ -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, diff --git a/js/stores/edition_store.js b/js/stores/edition_store.js index ac7d553f..fc992f09 100644 --- a/js/stores/edition_store.js +++ b/js/stores/edition_store.js @@ -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) }); diff --git a/js/utils/general.js b/js/utils/general.js index cf6705e8..2d34521c 100644 --- a/js/utils/general.js +++ b/js/utils/general.js @@ -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 */