Skip to content

Commit

Permalink
🏷️ [#445] Add some types to utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Mar 6, 2025
1 parent d7e2353 commit 7ba1be0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/env.mjs → src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const env = import.meta.env;
const envVarPrefix = 'VITE';
const DEBUG = env.MODE === 'development';

const getEnv = name => {
const getEnv = (name: string): string | undefined => {
const fullName = `${envVarPrefix}_${name}`;
return env[fullName];
};
Expand Down
11 changes: 6 additions & 5 deletions src/utils.js → src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import clsx from 'clsx';
import type {IntlShape} from 'react-intl';

import {getEnv} from './env';

Expand All @@ -8,30 +9,30 @@ const VERSION = getEnv('VERSION');

export const PREFIX = 'openforms';

export const getFormattedDateString = (intl, dateString) => {
export const getFormattedDateString = (intl: IntlShape, dateString: string) => {
if (!dateString) return '';
return intl.formatDate(new Date(dateString));
};

export const getFormattedTimeString = (intl, dateTimeString) => {
export const getFormattedTimeString = (intl: IntlShape, dateTimeString: string) => {
if (!dateTimeString) return '';
return intl.formatTime(new Date(dateTimeString));
};

/**
* Prefix a name/string/identifier with the Open Forms specific prefix.
*/
export const applyPrefix = name => {
export const applyPrefix = (name: string) => {
return `${PREFIX}-${name}`;
};

export const getBEMClassName = (base, modifiers = []) => {
export const getBEMClassName = (base: string, modifiers: string[] = []) => {
const prefixedBase = applyPrefix(base);
const prefixedModifiers = modifiers.map(mod => applyPrefix(`${base}--${mod}`));
return clsx(prefixedBase, ...prefixedModifiers);
};

// usage: await sleep(3000);
export const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

export const getVersion = () => VERSION || 'unknown';
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@/sb-decorators": ["../.storybook/decorators.tsx"]
},
"types": [
"vite/client",
"vitest/globals"
]
},
Expand Down

0 comments on commit 7ba1be0

Please sign in to comment.