Skip to content

Commit

Permalink
feat: add strings utils from suite to utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
karliatto committed Jan 28, 2022
1 parent ab5f69f commit c92c4a7
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as protocolActions from '@suite-actions/protocolActions';
import { Translation } from '@suite-components';
import { CoinLogo } from '@trezor/components';
import { useActions, useSelector } from '@suite-hooks';
import { capitalizeFirstLetter } from '@suite-utils/string';
import { capitalizeFirstLetter } from '@trezor/utils';
import { PROTOCOL_TO_NETWORK } from '@suite-constants/protocol';
import ConditionalActionRenderer from './ConditionalActionRenderer';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import styled, { css } from 'styled-components';
import { Button, useTheme, variables, Input, Tooltip, Checkbox, Icon } from '@trezor/components';
import { Translation } from '@suite-components/Translation';
import { MAX_LENGTH } from '@suite-constants/inputs';
import { countBytesInString } from '@suite-utils/string';
import { countBytesInString } from '@trezor/utils';
import { OpenGuideFromTooltip } from '@guide-views';
import PasswordStrengthIndicator from '@suite-components/PasswordStrengthIndicator';
import { useTranslation } from '@suite-hooks';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Icon, Button, useTheme, variables } from '@trezor/components';
import { FormattedCryptoAmount, Sign, Translation, FormattedDate } from '@suite-components';
import { useRbfContext } from '@wallet-hooks/useRbfForm';
import { useLayoutSize } from '@suite-hooks/useLayoutSize';
import { truncateMiddle } from '@suite-utils/string';
import { truncateMiddle } from '@trezor/utils';
import GreyCard from '../GreyCard';
import WarnHeader from '../WarnHeader';

Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/reducers/suite/resizeReducer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import produce from 'immer';
import * as variables from '@trezor/components/lib/config/variables'; // can't import from index cause it would import all UI components
import { RESIZE } from '@suite-actions/constants';
import { getNumberFromPxString } from '@suite-utils/string';
import { getNumberFromPxString } from '@trezor/utils';
import { Action } from '@suite-types';

const sizes = {
Expand Down
2 changes: 1 addition & 1 deletion packages/suite/src/reducers/suite/suiteReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Action, TrezorDevice, Lock, SuiteThemeColors } from '@suite-types';
import type { Locale } from '@suite-config/languages';
import { isWeb, getWindowWidth } from '@suite-utils/env';
import { ensureLocale } from '@suite-utils/l10n';
import { getNumberFromPxString } from '@suite-utils/string';
import { getNumberFromPxString } from '@trezor/utils';

export interface DebugModeOptions {
invityAPIUrl?: string;
Expand Down
18 changes: 0 additions & 18 deletions packages/suite/src/utils/suite/__tests__/string.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/suite/src/views/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { FIAT } from '@suite-config';
import { useAnalytics, useDevice, useSelector, useActions } from '@suite-hooks';
import { Button, Tooltip, Switch, Link } from '@trezor/components';
import { capitalizeFirstLetter } from '@suite-utils/string';
import { capitalizeFirstLetter } from '@trezor/utils';

import * as suiteActions from '@suite-actions/suiteActions';
import * as walletSettingsActions from '@settings-actions/walletSettingsActions';
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/capitalizeFirstLetter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const capitalizeFirstLetter = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
1 change: 1 addition & 0 deletions packages/utils/src/countBytesInString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const countBytesInString = (input: string) => encodeURI(input).split(/%..|./).length - 1;
1 change: 1 addition & 0 deletions packages/utils/src/getNumberFromPxString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const getNumberFromPxString = (size: string): number => parseInt(size.replace('px', ''), 10);
4 changes: 4 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export * from './bytesToHumanReadable';
export * from './mergeObject';
export * from './getWeakRandomId';
export * from './getRandomNumberInRange';
export * from './capitalizeFirstLetter';
export * from './countBytesInString';
export * from './truncateMiddle';
export * from './getNumberFromPxString';
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
export const capitalizeFirstLetter = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);

export const countBytesInString = (input: string) => encodeURI(input).split(/%..|./).length - 1;

export const getNumberFromPxString = (size: string): number => parseInt(size.replace('px', ''), 10);

export const truncateMiddle = (text: string, startChars: number, endChars: number) => {
if (text.length <= startChars + endChars) return text;
const start = text.substring(0, startChars);
Expand Down
6 changes: 6 additions & 0 deletions packages/utils/tests/capitalizeFirstLetter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { capitalizeFirstLetter } from '../src/capitalizeFirstLetter';

it('capitalizeFirstLetter', () => {
expect(capitalizeFirstLetter('god')).toBe('God');
expect(capitalizeFirstLetter('dog')).toBe('Dog');
});
8 changes: 8 additions & 0 deletions packages/utils/tests/countBytesInString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { countBytesInString } from '../src/countBytesInString';

it('countBytesInString', () => {
expect(countBytesInString('aaa')).toBe(3);
expect(countBytesInString('č')).toBe(2);
expect(countBytesInString('😀')).toBe(4);
expect(countBytesInString('+ěčřěžšýžřšý')).toBe(23);
});
6 changes: 6 additions & 0 deletions packages/utils/tests/getNumberFromPxString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { getNumberFromPxString } from '../src/getNumberFromPxString';

it('getNumberFromPxString', () => {
expect(getNumberFromPxString('1px')).toBe(1);
expect(getNumberFromPxString('1')).toBe(1);
});

0 comments on commit c92c4a7

Please sign in to comment.