Skip to content

Commit 46e8ea5

Browse files
authored
use arrow functions where possible (#1762)
1 parent 5c3c3eb commit 46e8ea5

9 files changed

+67
-82
lines changed

src/TransWithoutContext.js

+21-23
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,34 @@ import { warn, warnOnce } from './utils.js';
44
import { getDefaults } from './defaults.js';
55
import { getI18n } from './i18nInstance.js';
66

7-
function hasChildren(node, checkLength) {
7+
const hasChildren = (node, checkLength) => {
88
if (!node) return false;
99
const base = node.props ? node.props.children : node.children;
1010
if (checkLength) return base.length > 0;
1111
return !!base;
12-
}
12+
};
1313

14-
function getChildren(node) {
14+
const getChildren = (node) => {
1515
if (!node) return [];
1616
const children = node.props ? node.props.children : node.children;
1717
return node.props && node.props.i18nIsDynamicList ? getAsArray(children) : children;
18-
}
18+
};
1919

20-
function hasValidReactChildren(children) {
20+
const hasValidReactChildren = (children) => {
2121
if (Object.prototype.toString.call(children) !== '[object Array]') return false;
2222
return children.every((child) => isValidElement(child));
23-
}
23+
};
2424

25-
function getAsArray(data) {
26-
return Array.isArray(data) ? data : [data];
27-
}
25+
const getAsArray = (data) => (Array.isArray(data) ? data : [data]);
2826

29-
function mergeProps(source, target) {
27+
const mergeProps = (source, target) => {
3028
const newTarget = { ...target };
3129
// overwrite source.props when target.props already set
3230
newTarget.props = Object.assign(source.props, target.props);
3331
return newTarget;
34-
}
32+
};
3533

36-
export function nodesToString(children, i18nOptions) {
34+
export const nodesToString = (children, i18nOptions) => {
3735
if (!children) return '';
3836
let stringNode = '';
3937

@@ -103,9 +101,9 @@ export function nodesToString(children, i18nOptions) {
103101
});
104102

105103
return stringNode;
106-
}
104+
};
107105

108-
function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) {
106+
const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, shouldUnescape) => {
109107
if (targetString === '') return [];
110108

111109
// check if contains tags we need to replace from html string to react nodes
@@ -119,15 +117,15 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, s
119117
// v2 -> interpolates upfront no need for "some <0>{{var}}</0>"" -> will be just "some {{var}}" in translation file
120118
const data = {};
121119

122-
function getData(childs) {
120+
const getData = (childs) => {
123121
const childrenArray = getAsArray(childs);
124122

125123
childrenArray.forEach((child) => {
126124
if (typeof child === 'string') return;
127125
if (hasChildren(child)) getData(getChildren(child));
128126
else if (typeof child === 'object' && !isValidElement(child)) Object.assign(data, child);
129127
});
130-
}
128+
};
131129

132130
getData(children);
133131

@@ -136,7 +134,7 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, s
136134
const ast = HTML.parse(`<0>${targetString}</0>`);
137135
const opts = { ...data, ...combinedTOpts };
138136

139-
function renderInner(child, node, rootReactNode) {
137+
const renderInner = (child, node, rootReactNode) => {
140138
const childs = getChildren(child);
141139
const mappedChildren = mapAST(childs, node.children, rootReactNode);
142140
// `mappedChildren` will always be empty if using the `i18nIsDynamicList` prop,
@@ -145,9 +143,9 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, s
145143
(child.props && child.props.i18nIsDynamicList)
146144
? childs
147145
: mappedChildren;
148-
}
146+
};
149147

150-
function pushTranslatedJSX(child, inner, mem, i, isVoid) {
148+
const pushTranslatedJSX = (child, inner, mem, i, isVoid) => {
151149
if (child.dummy) {
152150
child.children = inner; // needed on preact!
153151
mem.push(cloneElement(child, { key: i }, isVoid ? undefined : inner));
@@ -169,12 +167,12 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, s
169167
}),
170168
);
171169
}
172-
}
170+
};
173171

174172
// reactNode (the jsx root element or child)
175173
// astNode (the translation string as html ast)
176174
// rootReactNode (the most outer jsx children array or trans components prop)
177-
function mapAST(reactNode, astNode, rootReactNode) {
175+
const mapAST = (reactNode, astNode, rootReactNode) => {
178176
const reactNodes = getAsArray(reactNode);
179177
const astNodes = getAsArray(astNode);
180178

@@ -291,7 +289,7 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, s
291289
}
292290
return mem;
293291
}, []);
294-
}
292+
};
295293

296294
// call mapAST with having react nodes nested into additional node like
297295
// we did for the string ast from translation
@@ -302,7 +300,7 @@ function renderNodes(children, targetString, i18n, i18nOptions, combinedTOpts, s
302300
getAsArray(children || []),
303301
);
304302
return getChildren(result[0]);
305-
}
303+
};
306304

307305
export function Trans({
308306
children,

src/context.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,23 @@ export class ReportNamespaces {
1818
});
1919
}
2020

21-
getUsedNamespaces() {
22-
return Object.keys(this.usedNamespaces);
23-
}
21+
getUsedNamespaces = () => Object.keys(this.usedNamespaces);
2422
}
2523

26-
export function composeInitialProps(ForComponent) {
27-
return async (ctx) => {
28-
const componentsInitialProps = ForComponent.getInitialProps
29-
? await ForComponent.getInitialProps(ctx)
30-
: {};
24+
export const composeInitialProps = (ForComponent) => async (ctx) => {
25+
const componentsInitialProps = ForComponent.getInitialProps
26+
? await ForComponent.getInitialProps(ctx)
27+
: {};
3128

32-
const i18nInitialProps = getInitialProps();
29+
const i18nInitialProps = getInitialProps();
3330

34-
return {
35-
...componentsInitialProps,
36-
...i18nInitialProps,
37-
};
31+
return {
32+
...componentsInitialProps,
33+
...i18nInitialProps,
3834
};
39-
}
35+
};
4036

41-
export function getInitialProps() {
37+
export const getInitialProps = () => {
4238
const i18n = getI18n();
4339
const namespaces = i18n.reportNamespaces ? i18n.reportNamespaces.getUsedNamespaces() : [];
4440

@@ -55,4 +51,4 @@ export function getInitialProps() {
5551
ret.initialLanguage = i18n.language;
5652

5753
return ret;
58-
}
54+
};

src/defaults.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ let defaultOptions = {
1313
unescape,
1414
};
1515

16-
export function setDefaults(options = {}) {
16+
export const setDefaults = (options = {}) => {
1717
defaultOptions = { ...defaultOptions, ...options };
18-
}
18+
};
1919

20-
export function getDefaults() {
21-
return defaultOptions;
22-
}
20+
export const getDefaults = () => defaultOptions;

src/i18nInstance.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
let i18nInstance;
22

3-
export function setI18n(instance) {
3+
export const setI18n = (instance) => {
44
i18nInstance = instance;
5-
}
5+
};
66

7-
export function getI18n() {
8-
return i18nInstance;
9-
}
7+
export const getI18n = () => i18nInstance;

src/useSSR.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useContext } from 'react';
22
import { getI18n, I18nContext } from './context.js';
33

4-
export function useSSR(initialI18nStore, initialLanguage, props = {}) {
4+
export const useSSR = (initialI18nStore, initialLanguage, props = {}) => {
55
const { i18n: i18nFromProps } = props;
66
const { i18n: i18nFromContext } = useContext(I18nContext) || {};
77
const i18n = i18nFromProps || i18nFromContext || getI18n();
@@ -30,4 +30,4 @@ export function useSSR(initialI18nStore, initialLanguage, props = {}) {
3030
i18n.changeLanguage(initialLanguage);
3131
i18n.initializedLanguageOnce = true;
3232
}
33-
}
33+
};

src/useTranslation.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,18 @@ const usePrevious = (value, ignore) => {
1010
return ref.current;
1111
};
1212

13-
function alwaysNewT(i18n, language, namespace, keyPrefix) {
14-
return i18n.getFixedT(language, namespace, keyPrefix);
15-
}
13+
const alwaysNewT = (i18n, language, namespace, keyPrefix) =>
14+
i18n.getFixedT(language, namespace, keyPrefix);
1615

17-
function useMemoizedT(i18n, language, namespace, keyPrefix) {
18-
return useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [
16+
const useMemoizedT = (i18n, language, namespace, keyPrefix) =>
17+
useCallback(alwaysNewT(i18n, language, namespace, keyPrefix), [
1918
i18n,
2019
language,
2120
namespace,
2221
keyPrefix,
2322
]);
24-
}
2523

26-
export function useTranslation(ns, props = {}) {
24+
export const useTranslation = (ns, props = {}) => {
2725
// assert we have the needed i18nInstance
2826
const { i18n: i18nFromProps } = props;
2927
const { i18n: i18nFromContext, defaultNS: defaultNSFromContext } = useContext(I18nContext) || {};
@@ -114,9 +112,9 @@ export function useTranslation(ns, props = {}) {
114112
setT(getNewT);
115113
}
116114

117-
function boundReset() {
115+
const boundReset = () => {
118116
if (isMounted.current) setT(getNewT);
119-
}
117+
};
120118

121119
// bind events to trigger change, like languageChanged
122120
if (bindI18n && i18n) i18n.on(bindI18n, boundReset);
@@ -161,4 +159,4 @@ export function useTranslation(ns, props = {}) {
161159
loadNamespaces(i18n, namespaces, () => resolve());
162160
}
163161
});
164-
}
162+
};

src/utils.js

+14-15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Do not use arrow function here as it will break optimizations of arguments
12
export function warn(...args) {
23
if (console && console.warn) {
34
if (typeof args[0] === 'string') args[0] = `react-i18next:: ${args[0]}`;
@@ -6,6 +7,7 @@ export function warn(...args) {
67
}
78

89
const alreadyWarned = {};
10+
// Do not use arrow function here as it will break optimizations of arguments
911
export function warnOnce(...args) {
1012
if (typeof args[0] === 'string' && alreadyWarned[args[0]]) return;
1113
if (typeof args[0] === 'string') alreadyWarned[args[0]] = new Date();
@@ -37,22 +39,22 @@ const loadedClb = (i18n, cb) => () => {
3739
}
3840
};
3941

40-
export function loadNamespaces(i18n, ns, cb) {
42+
export const loadNamespaces = (i18n, ns, cb) => {
4143
i18n.loadNamespaces(ns, loadedClb(i18n, cb));
42-
}
44+
};
4345

4446
// should work with I18NEXT >= v22.5.0
45-
export function loadLanguages(i18n, lng, ns, cb) {
47+
export const loadLanguages = (i18n, lng, ns, cb) => {
4648
// eslint-disable-next-line no-param-reassign
4749
if (typeof ns === 'string') ns = [ns];
4850
ns.forEach((n) => {
4951
if (i18n.options.ns.indexOf(n) < 0) i18n.options.ns.push(n);
5052
});
5153
i18n.loadLanguages(lng, loadedClb(i18n, cb));
52-
}
54+
};
5355

5456
// WAIT A LITTLE FOR I18NEXT BEING UPDATED IN THE WILD, before removing this old i18next version support
55-
function oldI18nextHasLoadedNamespace(ns, i18n, options = {}) {
57+
const oldI18nextHasLoadedNamespace = (ns, i18n, options = {}) => {
5658
const lng = i18n.languages[0];
5759
const fallbackLng = i18n.options ? i18n.options.fallbackLng : false;
5860
const lastLng = i18n.languages[i18n.languages.length - 1];
@@ -91,9 +93,9 @@ function oldI18nextHasLoadedNamespace(ns, i18n, options = {}) {
9193
if (loadNotPending(lng, ns) && (!fallbackLng || loadNotPending(lastLng, ns))) return true;
9294

9395
return false;
94-
}
96+
};
9597

96-
export function hasLoadedNamespace(ns, i18n, options = {}) {
98+
export const hasLoadedNamespace = (ns, i18n, options = {}) => {
9799
if (!i18n.languages || !i18n.languages.length) {
98100
warnOnce('i18n.languages were undefined or empty', i18n.languages);
99101
return true;
@@ -120,12 +122,9 @@ export function hasLoadedNamespace(ns, i18n, options = {}) {
120122
return false;
121123
},
122124
});
123-
}
125+
};
124126

125-
export function getDisplayName(Component) {
126-
return (
127-
Component.displayName ||
128-
Component.name ||
129-
(typeof Component === 'string' && Component.length > 0 ? Component : 'Unknown')
130-
);
131-
}
127+
export const getDisplayName = (Component) =>
128+
Component.displayName ||
129+
Component.name ||
130+
(typeof Component === 'string' && Component.length > 0 ? Component : 'Unknown');

src/withSSR.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { useSSR } from './useSSR.js';
33
import { composeInitialProps } from './context.js';
44
import { getDisplayName } from './utils.js';
55

6-
export function withSSR() {
7-
return function Extend(WrappedComponent) {
6+
export const withSSR = () =>
7+
function Extend(WrappedComponent) {
88
function I18nextWithSSR({ initialI18nStore, initialLanguage, ...rest }) {
99
useSSR(initialI18nStore, initialLanguage);
1010

@@ -19,4 +19,3 @@ export function withSSR() {
1919

2020
return I18nextWithSSR;
2121
};
22-
}

src/withTranslation.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { createElement, forwardRef as forwardRefReact } from 'react';
22
import { useTranslation } from './useTranslation.js';
33
import { getDisplayName } from './utils.js';
44

5-
export function withTranslation(ns, options = {}) {
6-
return function Extend(WrappedComponent) {
5+
export const withTranslation = (ns, options = {}) =>
6+
function Extend(WrappedComponent) {
77
function I18nextWithTranslation({ forwardedRef, ...rest }) {
88
const [t, i18n, ready] = useTranslation(ns, { ...rest, keyPrefix: options.keyPrefix });
99

@@ -33,4 +33,3 @@ export function withTranslation(ns, options = {}) {
3333

3434
return options.withRef ? forwardRefReact(forwardRef) : I18nextWithTranslation;
3535
};
36-
}

0 commit comments

Comments
 (0)