blob: 32dc9ec1cff75954cb87d3c12cb6cee29c55b555 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Shallow merges the first argument with the second.
* Retrieves/updates the userid if userFromParams is provided.
* @param {Object} config Current configuration object to be merged into.
* @param {Object} newConfig Configuration object to merge into the current config.
*/
export function configure(config, newConfig) {
Object.keys(newConfig).forEach(function(option) {
if (option === 'userFromParams') {
var userId = getUserIdFromParams(newConfig[option]);
if (userId) {
config.userId = userId;
}
}
config[option] = newConfig[option];
});
}
/**
* Attempts to extract the userid from the query parameters of the URL.
* @param {string} param The name of the query parameter containing the userid.
* @return {string|null} The extracted/decoded userid, or null if none is found.
*/
export function getUserIdFromParams(param) {
var userField = param;
var regex = new RegExp('[?&]' + userField + '(=([^&#]*)|&|#|$)');
var results = window.location.href.match(regex);
if (results && results[2]) {
return decodeURIComponent(results[2].replace(/\+/g, ' '));
} else {
return null;
}
}