Skip to content

Commit

Permalink
use object deconstruction, optional chaining and hot path optimizatio…
Browse files Browse the repository at this point in the history
…ns (#287)
  • Loading branch information
VIKTORVAV99 authored May 12, 2024
1 parent 1affdb8 commit 9a3b014
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 77 deletions.
21 changes: 11 additions & 10 deletions src/browserLookups/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,21 @@ const cookie = {
export default {
name: 'cookie',

lookup(options) {
let found;

if (options.lookupCookie && typeof document !== 'undefined') {
const c = cookie.read(options.lookupCookie);
if (c) found = c;
// Deconstruct the options object and extract the lookupCookie property
lookup({ lookupCookie }) {
if (lookupCookie && typeof document !== 'undefined') {
return cookie.read(lookupCookie) || undefined;
}

return found;
return undefined;
},

cacheUserLanguage(lng, options) {
if (options.lookupCookie && typeof document !== 'undefined') {
cookie.create(options.lookupCookie, lng, options.cookieMinutes, options.cookieDomain, options.cookieOptions);
// Deconstruct the options object and extract the lookupCookie, cookieMinutes, cookieDomain, and cookieOptions properties
cacheUserLanguage(lng, {
lookupCookie, cookieMinutes, cookieDomain, cookieOptions
}) {
if (lookupCookie && typeof document !== 'undefined') {
cookie.create(lookupCookie, lng, cookieMinutes, cookieDomain, cookieOptions);
}
}
};
9 changes: 5 additions & 4 deletions src/browserLookups/htmlTag.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export default {
name: 'htmlTag',

lookup(options) {
// Deconstruct the options object and extract the htmlTag property
lookup({ htmlTag }) {
let found;
const htmlTag = options.htmlTag || (typeof document !== 'undefined' ? document.documentElement : null);
const internalHtmlTag = htmlTag || (typeof document !== 'undefined' ? document.documentElement : null);

if (htmlTag && typeof htmlTag.getAttribute === 'function') {
found = htmlTag.getAttribute('lang');
if (internalHtmlTag && typeof internalHtmlTag.getAttribute === 'function') {
found = internalHtmlTag.getAttribute('lang');
}

return found;
Expand Down
20 changes: 9 additions & 11 deletions src/browserLookups/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ const localStorageAvailable = () => {
export default {
name: 'localStorage',

lookup(options) {
let found;

if (options.lookupLocalStorage && localStorageAvailable()) {
const lng = window.localStorage.getItem(options.lookupLocalStorage);
if (lng) found = lng;
// Deconstruct the options object and extract the lookupLocalStorage property
lookup({ lookupLocalStorage }) {
if (lookupLocalStorage && localStorageAvailable()) {
return window.localStorage.getItem(lookupLocalStorage) || undefined; // Undefined ensures type consistency with the previous version of this function
}

return found;
return undefined;
},

cacheUserLanguage(lng, options) {
if (options.lookupLocalStorage && localStorageAvailable()) {
window.localStorage.setItem(options.lookupLocalStorage, lng);
// Deconstruct the options object and extract the lookupLocalStorage property
cacheUserLanguage(lng, { lookupLocalStorage }) {
if (lookupLocalStorage && localStorageAvailable()) {
window.localStorage.setItem(lookupLocalStorage, lng);
}
}
};
16 changes: 9 additions & 7 deletions src/browserLookups/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ export default {
const found = [];

if (typeof navigator !== 'undefined') {
if (navigator.languages) { // chrome only; not an array, so can't use .push.apply instead of iterating
for (let i = 0; i < navigator.languages.length; i++) {
found.push(navigator.languages[i]);
const { languages, userLanguage, language } = navigator;
if (languages) {
// chrome only; not an array, so can't use .push.apply instead of iterating
for (let i = 0; i < languages.length; i++) {
found.push(languages[i]);
}
}
if (navigator.userLanguage) {
found.push(navigator.userLanguage);
if (userLanguage) {
found.push(userLanguage);
}
if (navigator.language) {
found.push(navigator.language);
if (language) {
found.push(language);
}
}

Expand Down
26 changes: 10 additions & 16 deletions src/browserLookups/path.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
export default {
name: 'path',

lookup(options) {
let found;
if (typeof window !== 'undefined') {
const language = window.location.pathname.match(/\/([a-zA-Z-]*)/g);
if (language instanceof Array) {
if (typeof options.lookupFromPathIndex === 'number') {
if (typeof language[options.lookupFromPathIndex] !== 'string') {
return undefined;
}
found = language[options.lookupFromPathIndex].replace('/', '');
} else {
found = language[0].replace('/', '');
}
}
}
return found;
// Deconstruct the options object and extract the lookupFromPathIndex property
lookup({ lookupFromPathIndex }) {

if (typeof window === 'undefined') return undefined;

const language = window.location.pathname.match(/\/([a-zA-Z-]*)/g);
if (!Array.isArray(language)) return undefined;

const index = typeof lookupFromPathIndex === 'number' ? lookupFromPathIndex : 0;
return language[index]?.replace('/', '');
}
};
7 changes: 4 additions & 3 deletions src/browserLookups/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
export default {
name: 'querystring',

lookup(options) {
// Deconstruct the options object and extract the lookupQuerystring property
lookup({ lookupQuerystring }) {
let found;

if (typeof window !== 'undefined') {
let { search } = window.location;
if (!window.location.search && window.location.hash && window.location.hash.indexOf('?') > -1) {
if (!window.location.search && window.location.hash?.indexOf('?') > -1) {
search = window.location.hash.substring(window.location.hash.indexOf('?'));
}
const query = search.substring(1);
Expand All @@ -15,7 +16,7 @@ export default {
const pos = params[i].indexOf('=');
if (pos > 0) {
const key = params[i].substring(0, pos);
if (key === options.lookupQuerystring) {
if (key === lookupQuerystring) {
found = params[i].substring(pos + 1);
}
}
Expand Down
18 changes: 7 additions & 11 deletions src/browserLookups/sessionStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ const sessionStorageAvailable = () => {
export default {
name: 'sessionStorage',

lookup(options) {
let found;

if (options.lookupSessionStorage && sessionStorageAvailable()) {
const lng = window.sessionStorage.getItem(options.lookupSessionStorage);
if (lng) found = lng;
lookup({ lookupSessionStorage }) {
if (lookupSessionStorage && sessionStorageAvailable()) {
return window.sessionStorage.getItem(lookupSessionStorage) || undefined;
}

return found;
return undefined;
},

cacheUserLanguage(lng, options) {
if (options.lookupSessionStorage && sessionStorageAvailable()) {
window.sessionStorage.setItem(options.lookupSessionStorage, lng);
cacheUserLanguage(lng, { lookupSessionStorage }) {
if (lookupSessionStorage && sessionStorageAvailable()) {
window.sessionStorage.setItem(lookupSessionStorage, lng);
}
}
};
15 changes: 5 additions & 10 deletions src/browserLookups/subdomain.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
export default {
name: 'subdomain',

lookup(options) {
lookup({ lookupFromSubdomainIndex }) {
// If given get the subdomain index else 1
const lookupFromSubdomainIndex = typeof options.lookupFromSubdomainIndex === 'number'
? options.lookupFromSubdomainIndex + 1
: 1;
const internalLookupFromSubdomainIndex = typeof lookupFromSubdomainIndex === 'number' ? lookupFromSubdomainIndex + 1 : 1;
// get all matches if window.location. is existing
// first item of match is the match itself and the second is the first group macht which sould be the first subdomain match
// first item of match is the match itself and the second is the first group match which should be the first subdomain match
// is the hostname no public domain get the or option of localhost
const language = typeof window !== 'undefined'
&& window.location
&& window.location.hostname
&& window.location.hostname.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i);
const language = typeof window !== 'undefined' && window.location?.hostname?.match(/^(\w{2,5})\.(([a-z0-9-]{1,63}\.[a-z]{2,6})|localhost)/i);

// if there is no match (null) return undefined
if (!language) return undefined;
// return the given group match
return language[lookupFromSubdomainIndex];
return language[internalLookupFromSubdomainIndex];
}
};
8 changes: 3 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const arr = [];
const each = arr.forEach;
const { slice } = arr;
const { slice, forEach } = [];

export function defaults(obj) {
each.call(slice.call(arguments, 1), (source) => {
forEach.call(slice.call(arguments, 1), (source) => {
if (source) {
for (const prop in source) {
if (obj[prop] === undefined) obj[prop] = source[prop];
Expand All @@ -14,7 +12,7 @@ export function defaults(obj) {
}

export function extend(obj) {
each.call(slice.call(arguments, 1), (source) => {
forEach.call(slice.call(arguments, 1), (source) => {
if (source) {
for (const prop in source) {
obj[prop] = source[prop];
Expand Down

0 comments on commit 9a3b014

Please sign in to comment.