@beweco/utils-js
v0.0.11
Published
Utilidades de validación para BeWe
Downloads
406
Readme
@beweco/utils
Utility library for phone number validation and Tolgee localization management.
Installation
npm install @beweco/utils-jsUsage
Phone Validation
import { validatePhone } from '@beweco/utils-js';
// Validate a Chilean mobile number
const { isValid, formattedNumber, error } = validatePhone('912345678');
if (isValid) {
console.log(formattedNumber); // '+56912345678'
} else {
console.log(error);
}
// Validate with different country code
const result = validatePhone('912345678', '+1');Tolgee Localization
import {
getAllKeysTolgee,
downloadTranslations,
uploadOnlyNewKeysToTolgee,
uploadToTolgee,
getAvailableLanguages
} from '@beweco/utils-js';
// Configuration
const config = {
apiKey: 'your-tolgee-api-key',
projectId: 'your-project-id',
baseUrl: 'https://app.tolgee.io' // optional, defaults to this value
};
// Get all available languages
const languages = await getAvailableLanguages(config);
// Get all keys from project
const keys = await getAllKeysTolgee(config);
// Download all translations
const translations = await downloadTranslations(config);
// Upload new keys only
const uploadResult = await uploadOnlyNewKeysToTolgee({
...config,
language: 'es-AR',
namespace: 'common',
data: {
'welcome': 'Bienvenido',
'hello': 'Hola'
}
});
// Upload all keys (including duplicates)
const uploadAllResult = await uploadToTolgee({
...config,
language: 'es-AR',
namespace: 'common',
data: {
'welcome': 'Bienvenido',
'hello': 'Hola'
}
});API Reference
Phone Validation
validatePhone(phone: string, countryCode?: string): PhoneValidationResult
Validates and formats a phone number.
Parameters:
phone- The phone number to validatecountryCode- Optional country code (default: '+56' for Chile)
Returns: PhoneValidationResult
interface PhoneValidationResult {
isValid: boolean;
formattedNumber?: string;
error?: string;
}Validation Rules:
- Must be a mobile number (starts with 9)
- Must be 9 digits long (excluding country code)
- Can be provided with or without country code
- Automatically formats to international format (e.g., '+56912345678')
Tolgee Localization
Types
interface TolgeeConfig {
apiKey: string;
projectId: string;
baseUrl?: string;
}
interface Language {
tag: string;
name: string;
originalName: string;
}
interface TranslationData {
[key: string]: string;
}
interface DownloadTranslationsResult {
languages: Language[];
translations: {
[language: string]: {
[namespace: string]: {
[key: string]: string;
};
};
};
}
interface UploadResult {
uploaded?: number;
[key: string]: unknown;
}Functions
getAvailableLanguages(config: TolgeeConfig): Promise<Language[]>
Gets all available languages in the Tolgee project.
getAllKeysTolgee(config: TolgeeConfig): Promise<string[]>
Gets all keys from the Tolgee project with pagination support.
downloadTranslations(config: TolgeeConfig & { namespace?: string }): Promise
Downloads all translations organized by language and namespace.
Parameters:
config- Tolgee configurationnamespace- Optional namespace filter
uploadOnlyNewKeysToTolgee(config: TolgeeConfig & { language: string; namespace: string; data: TranslationData }): Promise
Uploads only new keys that don't exist in the project.
Parameters:
config- Tolgee configurationlanguage- Target language (e.g., 'es-AR')namespace- Namespace (e.g., 'common')data- Translation data object
uploadToTolgee(config: TolgeeConfig & { language: string; namespace: string; data: TranslationData }): Promise
Uploads all keys without checking for duplicates.
Parameters:
config- Tolgee configurationlanguage- Target language (e.g., 'es-AR')namespace- Namespace (e.g., 'common')data- Translation data object
Constants
const DEFAULT_BASE_URL = 'https://app.tolgee.io';
const DEFAULT_PAGE_SIZE = 1000;Development
Prerequisites
- Node.js >= 14
- npm
Available Scripts
npm run clean- Removes the dist directorynpm run build- Compiles TypeScript codenpm test- Runs Jest testsnpm run lint- Runs ESLintnpm run dev- Runs TypeScript compiler in watch mode
Contributing
- Create a new branch from
main - Make your changes
- Write/update tests if necessary
- Run linting and tests
- Create a pull request
License
MIT
