Skip to content

Commit

Permalink
create a isObject helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
VIKTORVAV99 committed Jul 15, 2024
1 parent 92bc147 commit abe3851
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
14 changes: 6 additions & 8 deletions src/TransWithoutContext.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Fragment, isValidElement, cloneElement, createElement, Children } from 'react';
import HTML from 'html-parse-stringify';
import { isString, warn, warnOnce } from './utils.js';
import { isObject, isString, warn, warnOnce } from './utils.js';
import { getDefaults } from './defaults.js';
import { getI18n } from './i18nInstance.js';

Expand Down Expand Up @@ -75,7 +75,7 @@ export const nodesToString = (children, i18nOptions) => {
}
} else if (child === null) {
warn(`Trans: the passed in value is invalid - seems you passed in a null child.`);
} else if (typeof child === 'object') {
} else if (isObject(child)) {
// e.g. lorem {{ value, format }} ipsum
const { format, ...clone } = child;
const keys = Object.keys(clone);
Expand Down Expand Up @@ -121,7 +121,7 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
childrenArray.forEach((child) => {
if (isString(child)) return;
if (hasChildren(child)) getData(getChildren(child));
else if (typeof child === 'object' && !isValidElement(child)) Object.assign(data, child);
else if (isObject(child) && !isValidElement(child)) Object.assign(data, child);
});
};

Expand Down Expand Up @@ -200,12 +200,10 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s
isElement && hasChildren(node, true) && !node.voidElement;

const isEmptyTransWithHTML =
emptyChildrenButNeedsHandling && typeof child === 'object' && child.dummy && !isElement;
emptyChildrenButNeedsHandling && isObject(child) && child.dummy && !isElement;

const isKnownComponent =
typeof children === 'object' &&
children !== null &&
Object.hasOwnProperty.call(children, node.name);
isObject(children) && Object.hasOwnProperty.call(children, node.name);

if (isString(child)) {
const value = i18n.services.interpolator.interpolate(child, opts, i18n.language);
Expand Down Expand Up @@ -253,7 +251,7 @@ const renderNodes = (children, targetString, i18n, i18nOptions, combinedTOpts, s

mem.push(`<${node.name}>${inner}</${node.name}>`);
}
} else if (typeof child === 'object' && !isElement) {
} else if (isObject(child) && !isElement) {
const content = node.children[0] ? translationContent : null;

// v1
Expand Down
15 changes: 9 additions & 6 deletions src/useTranslation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { useState, useEffect, useContext, useRef, useCallback } from 'react';
import { getI18n, getDefaults, ReportNamespaces, I18nContext } from './context.js';
import { warnOnce, loadNamespaces, loadLanguages, hasLoadedNamespace, isString } from './utils.js';
import {
warnOnce,
loadNamespaces,
loadLanguages,
hasLoadedNamespace,
isString,
isObject,
} from './utils.js';

const usePrevious = (value, ignore) => {
const ref = useRef();
Expand Down Expand Up @@ -31,11 +38,7 @@ export const useTranslation = (ns, props = {}) => {
warnOnce('You will need to pass in an i18next instance by using initReactI18next');
const notReadyT = (k, optsOrDefaultValue) => {
if (isString(optsOrDefaultValue)) return optsOrDefaultValue;
if (
optsOrDefaultValue &&
typeof optsOrDefaultValue === 'object' &&
isString(optsOrDefaultValue.defaultValue)
)
if (isObject(optsOrDefaultValue) && isString(optsOrDefaultValue.defaultValue))
return optsOrDefaultValue.defaultValue;
return Array.isArray(k) ? k[k.length - 1] : k;
};
Expand Down
2 changes: 2 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ export const getDisplayName = (Component) =>
(isString(Component) && Component.length > 0 ? Component : 'Unknown');

export const isString = (obj) => typeof obj === 'string';

export const isObject = (obj) => typeof obj === 'object' && obj !== null;
18 changes: 17 additions & 1 deletion test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { isString } from '../src/utils.js';
import { isString, isObject } from '../src/utils.js';

describe('isString', () => {
it('should return true for strings', () => {
Expand All @@ -13,3 +13,19 @@ describe('isString', () => {
},
);
});

describe('isObject', () => {
it.each([[{}], [{ key: 'value' }], [[]]])(
'should return true for objects, testing %o',
(value) => {
expect(isObject(value)).toBe(true);
},
);

it.each([[undefined], [null], [1], ['string'], [() => {}]])(
'should return false for non-objects, testing %o',
(value) => {
expect(isObject(value)).toBe(false);
},
);
});

0 comments on commit abe3851

Please sign in to comment.