1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-28 16:48:04 +02: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 GlobalNotification from './global_notification';
import { currentUserShape, locationShape, whitelabelShape } from './prop_types'; import { currentUserShape, locationShape, whitelabelShape } from './prop_types';
import { mergeOptions } from '../utils/general'; import { safeMerge } from '../utils/general';
export default function AppBase(App) { export default function AppBase(App) {
@ -29,7 +29,7 @@ export default function AppBase(App) {
}, },
getInitialState() { getInitialState() {
return mergeOptions( return safeMerge(
UserStore.getState(), UserStore.getState(),
WhitelabelStore.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 CreateEditionsButton from '../ascribe_buttons/create_editions_button';
import AscribeSpinner from '../ascribe_spinner'; import AscribeSpinner from '../ascribe_spinner';
import { mergeOptions } from '../../utils/general'; import { safeMerge } from '../../utils/general';
import { getLangText } from '../../utils/lang'; import { getLangText } from '../../utils/lang';
@ -25,7 +25,7 @@ let AccordionListItemEditionWidget = React.createClass({
}, },
getInitialState() { getInitialState() {
return mergeOptions( return safeMerge(
EditionListStore.getState(), EditionListStore.getState(),
PieceListStore.getState() PieceListStore.getState()
); );

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,7 +7,7 @@ import EditionActions from '../actions/edition_actions';
import EditionSource from '../sources/edition_source'; import EditionSource from '../sources/edition_source';
import CoaSource from '../sources/coa_source'; import CoaSource from '../sources/coa_source';
import { mergeOptions } from '../utils/general'; import { safeMerge } from '../utils/general';
class EditionStore { class EditionStore {
@ -15,7 +15,7 @@ class EditionStore {
this.getInitialState(); this.getInitialState();
this.bindActions(EditionActions); this.bindActions(EditionActions);
this.registerAsync(mergeOptions(EditionSource, CoaSource)); this.registerAsync(safeMerge(EditionSource, CoaSource));
this.exportPublicMethods({ this.exportPublicMethods({
getInitialState: this.getInitialState.bind(this) 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 // Re-export general utilities from js-utility-belt for easier access
export { export {
safeMerge,
sanitize sanitize
} from 'js-utility-belt/es6'; } 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 * In place update of a dictionary
*/ */