Skip to content

Commit

Permalink
use optional chaining, nullish coalescing and nullish coalescing assi…
Browse files Browse the repository at this point in the history
…gnment
  • Loading branch information
VIKTORVAV99 committed Jul 17, 2024
1 parent 08ba6ae commit 2bfa9cb
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 72 deletions.
70 changes: 35 additions & 35 deletions react-i18next.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,24 @@
}
};

function warn() {
if (console && console.warn) {
const warn = function () {
if (console?.warn) {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
if (isString(args[0])) args[0] = `react-i18next:: ${args[0]}`;
console.warn(...args);
}
}
};
const alreadyWarned = {};
function warnOnce() {
const warnOnce = function () {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
if (isString(args[0]) && alreadyWarned[args[0]]) return;
if (isString(args[0])) alreadyWarned[args[0]] = new Date();
warn(...args);
}
};
const loadedClb = (i18n, cb) => () => {
if (i18n.isInitialized) {
cb();
Expand All @@ -162,14 +162,14 @@
const oldI18nextHasLoadedNamespace = function (ns, i18n) {
let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
const lng = i18n.languages[0];
const fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
const fallbackLng = i18n.options?.fallbackLng ?? false;
const lastLng = i18n.languages[i18n.languages.length - 1];
if (lng.toLowerCase() === 'cimode') return true;
const loadNotPending = (l, n) => {
const loadState = i18n.services.backendConnector.state[`${l}|${n}`];
return loadState === -1 || loadState === 2;
};
if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false;
if (options.bindI18n?.indexOf('languageChanging') > -1 && i18n.services.backendConnector.backend && i18n.isLanguageChangingTo && !loadNotPending(i18n.isLanguageChangingTo, ns)) return false;
if (i18n.hasResourceBundle(lng, ns)) return true;
if (!i18n.services.backendConnector.backend || i18n.options.resources && !i18n.options.partialBundledLanguages) return true;
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
Expand All @@ -188,7 +188,7 @@
return i18n.hasLoadedNamespace(ns, {
lng: options.lng,
precheck: (i18nInstance, loadNotPending) => {
if (options.bindI18n && options.bindI18n.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
if (options.bindI18n?.indexOf('languageChanging') > -1 && i18nInstance.services.backendConnector.backend && i18nInstance.isLanguageChangingTo && !loadNotPending(i18nInstance.isLanguageChangingTo, ns)) return false;
}
});
};
Expand Down Expand Up @@ -249,14 +249,14 @@

const hasChildren = (node, checkLength) => {
if (!node) return false;
const base = node.props ? node.props.children : node.children;
const base = node.props?.children ?? node.children;
if (checkLength) return base.length > 0;
return !!base;
};
const getChildren = node => {
if (!node) return [];
const children = node.props ? node.props.children : node.children;
return node.props && node.props.i18nIsDynamicList ? getAsArray(children) : children;
const children = node.props?.children ?? node.children;
return node.props?.i18nIsDynamicList ? getAsArray(children) : children;
};
const hasValidReactChildren = children => Array.isArray(children) && children.every(react.isValidElement);
const getAsArray = data => Array.isArray(data) ? data : [data];
Expand All @@ -271,7 +271,7 @@
if (!children) return '';
let stringNode = '';
const childrenArray = getAsArray(children);
const keepArray = i18nOptions.transSupportBasicHtmlNodes && i18nOptions.transKeepBasicHtmlNodesFor ? i18nOptions.transKeepBasicHtmlNodesFor : [];
const keepArray = i18nOptions?.transSupportBasicHtmlNodes ? i18nOptions.transKeepBasicHtmlNodesFor ?? [] : [];
childrenArray.forEach((child, childIndex) => {
if (isString(child)) {
stringNode += `${child}`;
Expand Down Expand Up @@ -335,7 +335,7 @@
const renderInner = (child, node, rootReactNode) => {
const childs = getChildren(child);
const mappedChildren = mapAST(childs, node.children, rootReactNode);
return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props && child.props.i18nIsDynamicList ? childs : mappedChildren;
return hasValidReactChildren(childs) && mappedChildren.length === 0 || child.props?.i18nIsDynamicList ? childs : mappedChildren;
};
const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
if (child.dummy) {
Expand All @@ -361,7 +361,7 @@
const reactNodes = getAsArray(reactNode);
const astNodes = getAsArray(astNode);
return astNodes.reduce((mem, node, i) => {
const translationContent = node.children && node.children[0] && node.children[0].content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
const translationContent = node.children?.[0]?.content && i18n.services.interpolator.interpolate(node.children[0].content, opts, i18n.language);
if (node.type === 'tag') {
let tmp = reactNodes[parseInt(node.name, 10)];
if (rootReactNode.length === 1 && !tmp) tmp = rootReactNode[0][node.name];
Expand Down Expand Up @@ -454,17 +454,17 @@
const t = tFromProps || i18n.t.bind(i18n) || (k => k);
const reactI18nextOptions = {
...getDefaults(),
...(i18n.options && i18n.options.react)
...i18n.options?.react
};
let namespaces = ns || t.ns || i18n.options && i18n.options.defaultNS;
let namespaces = ns || t.ns || i18n.options?.defaultNS;
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
const nodeAsString = nodesToString(children, reactI18nextOptions);
const defaultValue = defaults || nodeAsString || reactI18nextOptions.transEmptyNodeValue || i18nKey;
const {
hashTransKey
} = reactI18nextOptions;
const key = i18nKey || (hashTransKey ? hashTransKey(nodeAsString || defaultValue) : nodeAsString || defaultValue);
if (i18n.options && i18n.options.interpolation && i18n.options.interpolation.defaultVariables) {
if (i18n.options?.interpolation?.defaultVariables) {
values = values && Object.keys(values).length > 0 ? {
...values,
...i18n.options.interpolation.defaultVariables
Expand Down Expand Up @@ -500,7 +500,7 @@
});
}
const content = renderNodes(components || children, translation, i18n, reactI18nextOptions, combinedTOpts, shouldUnescape);
const useAsParent = parent !== undefined ? parent : reactI18nextOptions.defaultTransParent;
const useAsParent = parent ?? reactI18nextOptions.defaultTransParent;
return useAsParent ? react.createElement(useAsParent, additionalProps, content) : content;
}

Expand All @@ -519,13 +519,13 @@
}
addUsedNamespaces(namespaces) {
namespaces.forEach(ns => {
if (!this.usedNamespaces[ns]) this.usedNamespaces[ns] = true;
this.usedNamespaces[ns] ??= true;
});
}
getUsedNamespaces = () => Object.keys(this.usedNamespaces);
}
const composeInitialProps = ForComponent => async ctx => {
const componentsInitialProps = ForComponent.getInitialProps ? await ForComponent.getInitialProps(ctx) : {};
const componentsInitialProps = (await ForComponent.getInitialProps?.(ctx)) ?? {};
const i18nInitialProps = getInitialProps();
return {
...componentsInitialProps,
Expand All @@ -534,7 +534,7 @@
};
const getInitialProps = () => {
const i18n = getI18n();
const namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : [];
const namespaces = i18n.reportNamespaces?.getUsedNamespaces() ?? [];
const ret = {};
const initialI18nStore = {};
i18n.languages.forEach(l => {
Expand Down Expand Up @@ -570,7 +570,7 @@
defaultNS: defaultNSFromContext
} = react.useContext(I18nContext) || {};
const i18n = i18nFromProps || i18nFromContext || getI18n();
const t = tFromProps || i18n && i18n.t.bind(i18n);
const t = tFromProps || i18n?.t.bind(i18n);
return Trans$1({
children,
count,
Expand All @@ -581,7 +581,7 @@
values,
defaults,
components,
ns: ns || t && t.ns || defaultNSFromContext || i18n && i18n.options && i18n.options.defaultNS,
ns: ns || t?.ns || defaultNSFromContext || i18n?.options?.defaultNS,
i18n,
t: tFromProps,
shouldUnescape,
Expand All @@ -592,7 +592,7 @@
const usePrevious = (value, ignore) => {
const ref = react.useRef();
react.useEffect(() => {
ref.current = ignore ? ref.current : value;
ref.current = value;
}, [value, ignore]);
return ref.current;
};
Expand Down Expand Up @@ -622,7 +622,7 @@
retNotReady.ready = false;
return retNotReady;
}
if (i18n.options.react && i18n.options.react.wait !== undefined) warnOnce('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
if (i18n.options.react?.wait) warnOnce('It seems you are still using the old wait option, you may migrate to the new useSuspense behaviour.');
const i18nOptions = {
...getDefaults(),
...i18n.options.react,
Expand All @@ -632,9 +632,9 @@
useSuspense,
keyPrefix
} = i18nOptions;
let namespaces = ns || defaultNSFromContext || i18n.options && i18n.options.defaultNS;
let namespaces = ns || defaultNSFromContext || i18n.options?.defaultNS;
namespaces = isString(namespaces) ? [namespaces] : namespaces || ['translation'];
if (i18n.reportNamespaces.addUsedNamespaces) i18n.reportNamespaces.addUsedNamespaces(namespaces);
i18n.reportNamespaces.addUsedNamespaces?.(namespaces);
const ready = (i18n.isInitialized || i18n.initializedStoreOnce) && namespaces.every(n => hasLoadedNamespace(n, i18n, i18nOptions));
const memoGetT = useMemoizedT(i18n, props.lng || null, i18nOptions.nsMode === 'fallback' ? namespaces : namespaces[0], keyPrefix);
const getT = () => memoGetT;
Expand Down Expand Up @@ -667,11 +667,11 @@
const boundReset = () => {
if (isMounted.current) setT(getNewT);
};
if (bindI18n && i18n) i18n.on(bindI18n, boundReset);
if (bindI18nStore && i18n) i18n.store.on(bindI18nStore, boundReset);
if (bindI18n) i18n?.on(bindI18n, boundReset);
if (bindI18nStore) i18n?.store.on(bindI18nStore, boundReset);
return () => {
isMounted.current = false;
if (bindI18n && i18n) bindI18n.split(' ').forEach(e => i18n.off(e, boundReset));
if (i18n) bindI18n?.split(' ').forEach(e => i18n.off(e, boundReset));
if (bindI18nStore && i18n) bindI18nStore.split(' ').forEach(e => i18n.store.off(e, boundReset));
};
}, [i18n, joinedNS]);
Expand Down Expand Up @@ -729,18 +729,18 @@
};
};

function Translation(props) {
const {
const Translation = _ref => {
let {
ns,
children,
...options
} = props;
} = _ref;
const [t, i18n, ready] = useTranslation(ns, options);
return children(t, {
i18n,
lng: i18n.language
}, ready);
}
};

function I18nextProvider(_ref) {
let {
Expand All @@ -766,7 +766,7 @@
i18n: i18nFromContext
} = react.useContext(I18nContext) || {};
const i18n = i18nFromProps || i18nFromContext || getI18n();
if (i18n.options && i18n.options.isClone) return;
if (i18n.options?.isClone) return;
if (initialI18nStore && !i18n.initializedStoreOnce) {
i18n.services.resourceStore.data = initialI18nStore;
i18n.options.ns = Object.values(initialI18nStore).reduce((mem, lngResources) => {
Expand Down
Loading

0 comments on commit 2bfa9cb

Please sign in to comment.