Skip to content

Commit 19e1a7f

Browse files
committed
fix(api): expose helpers and types
2 parents 8ba6f25 + 30b9d05 commit 19e1a7f

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

projects/ngneat/transloco/src/lib/helpers.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export function getValue(obj: object, path: string) {
22
/* For cases where the key is like: 'general.something.thing' */
3-
if (obj && obj.hasOwnProperty(path)) {
3+
if( obj && obj.hasOwnProperty(path) ) {
44
return obj[path];
55
}
66
return path.split('.').reduce((p, c) => (p && p[c]) || null, obj);
@@ -13,7 +13,7 @@ export function setValue(obj: any, prop: string, val: any) {
1313
const lastIndex = split.length - 1;
1414

1515
split.reduce((acc, part, index) => {
16-
if (index === lastIndex) {
16+
if( index === lastIndex ) {
1717
acc[part] = val;
1818
} else {
1919
acc[part] = Array.isArray(acc[part]) ? acc[part].slice() : { ...acc[part] };
@@ -26,15 +26,15 @@ export function setValue(obj: any, prop: string, val: any) {
2626
}
2727

2828
export function size(collection) {
29-
if (!collection) {
29+
if( !collection ) {
3030
return 0;
3131
}
3232

33-
if (Array.isArray(collection)) {
33+
if( Array.isArray(collection) ) {
3434
return collection.length;
3535
}
3636

37-
if (isObject(collection)) {
37+
if( isObject(collection) ) {
3838
return Object.keys(collection).length;
3939
}
4040

@@ -62,13 +62,13 @@ export function coerceArray(val) {
6262
}
6363

6464
export function mergeDeep(target: Object, ...sources: Object[]) {
65-
if (!sources.length) return target;
65+
if( !sources.length ) return target;
6666
const source = sources.shift();
6767

68-
if (isObject(target) && isObject(source)) {
69-
for (const key in source) {
70-
if (isObject(source[key])) {
71-
if (!target[key]) Object.assign(target, { [key]: {} });
68+
if( isObject(target) && isObject(source) ) {
69+
for( const key in source ) {
70+
if( isObject(source[key]) ) {
71+
if( !target[key] ) Object.assign(target, { [key]: {} });
7272
mergeDeep(target[key], source[key]);
7373
} else {
7474
Object.assign(target, { [key]: source[key] });
@@ -110,7 +110,7 @@ export function getLangFromScope(lang: string): string {
110110
* given: path-to_happiness => pathToHappiness
111111
*
112112
*/
113-
export function dashCaseToCamelCase(str: string): string {
113+
export function toCamelCase(str: string): string {
114114
return str
115115
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => (index == 0 ? word.toLowerCase() : word.toUpperCase()))
116116
.replace(/\s+|_|-|\//g, '');

projects/ngneat/transloco/src/lib/transloco.service.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { Inject, Injectable } from '@angular/core';
22
import { BehaviorSubject, combineLatest, from, Observable, Subject } from 'rxjs';
3-
import { catchError, map, retry, shareReplay, tap } from 'rxjs/operators';
3+
import { catchError, map, retry, shareReplay, tap, distinctUntilChanged } from 'rxjs/operators';
44
import { TRANSLOCO_LOADER, TranslocoLoader } from './transloco.loader';
55
import { TRANSLOCO_TRANSPILER, TranslocoTranspiler } from './transloco.transpiler';
66
import { HashMap, Translation, TranslationCb, TranslocoEvents } from './types';
77
import {
8-
dashCaseToCamelCase,
8+
toCamelCase,
99
getLangFromScope,
1010
getScopeFromLang,
11-
getValue,
11+
getValue, isEmpty,
1212
isFunction,
1313
mergeDeep,
1414
setValue,
1515
size,
16-
isEmpty
1716
} from './helpers';
1817
import { defaultConfig, TRANSLOCO_CONFIG, TranslocoConfig } from './transloco.config';
1918
import { TRANSLOCO_MISSING_HANDLER, TranslocoMissingHandler } from './transloco-missing-handler';
@@ -61,13 +60,13 @@ export class TranslocoService {
6160
this.mergedConfig = { ...defaultConfig, ...this.userConfig };
6261
this.setDefaultLang(this.mergedConfig.defaultLang);
6362
this.lang = new BehaviorSubject<string>(this.getDefaultLang());
64-
this.langChanges$ = this.lang.asObservable();
63+
this.langChanges$ = this.lang.asObservable().pipe(distinctUntilChanged());
6564

6665
/**
6766
* When we have a failure, we want to define the next language that succeeded as the active
6867
*/
6968
this.events$.subscribe(e => {
70-
if (e.type === 'translationLoadSuccess' && e.wasFailure) {
69+
if( e.type === 'translationLoadSuccess' && e.wasFailure ) {
7170
// Handle scoped lang
7271
const lang = getLangFromScope(e.payload.lang);
7372
this.setActiveLang(lang);
@@ -101,7 +100,7 @@ export class TranslocoService {
101100
}
102101

103102
load(lang: string, options?: { fallbackLangs: string[] | null }): Observable<Translation> {
104-
if (this.cache.has(lang) === false) {
103+
if( this.cache.has(lang) === false ) {
105104
const mergedOptions = { ...{ fallbackLangs: null }, ...(options || {}) };
106105

107106
const load$ = from(this.loader.getTranslation(lang)).pipe(
@@ -140,22 +139,22 @@ export class TranslocoService {
140139
params: HashMap = {},
141140
lang?: string
142141
): string | string[] {
143-
if (Array.isArray(key)) {
142+
if( Array.isArray(key) ) {
144143
return key.map(k => this.translate(k, params, lang));
145144
}
146145

147-
if (!key) {
146+
if( !key ) {
148147
return this.missingHandler.handle(key as string, params, this.config);
149148
}
150149

151150
const translation = this.translations.get(lang || this.getActiveLang());
152-
if (!translation) {
151+
if( !translation ) {
153152
return '';
154153
}
155154

156155
const value = isFunction(key) ? key(translation as T, params) : getValue(translation, key);
157156

158-
if (!value) {
157+
if( !value ) {
159158
return this.missingHandler.handle(key, params, this.config);
160159
}
161160

@@ -232,7 +231,7 @@ export class TranslocoService {
232231
*/
233232
setTranslationKey(key: string, value: string, lang = this.getActiveLang()) {
234233
const translation = this.getTranslation(lang);
235-
if (!isEmpty(translation)) {
234+
if( !isEmpty(translation) ) {
236235
const withHook = this.interceptor.preSaveTranslationKey(key, value, lang);
237236
const newValue = setValue(translation, key, withHook);
238237
this.setTranslation(newValue, lang);
@@ -247,9 +246,10 @@ export class TranslocoService {
247246
_loadDependencies(langName: string): Observable<Translation | Translation[]> {
248247
const split = langName.split('/');
249248
const [lang] = split.slice(-1);
250-
if (split.length > 1 && this.isSharedScope && !size(this.getTranslation(lang))) {
249+
if( split.length > 1 && this.isSharedScope && !size(this.getTranslation(lang)) ) {
251250
return combineLatest(this.load(lang), this.load(langName));
252251
}
252+
253253
return this.load(langName);
254254
}
255255

@@ -259,18 +259,18 @@ export class TranslocoService {
259259
const { scopeStrategy, scopeMapping = {} } = this.config;
260260
const currLang = getLangFromScope(lang);
261261
const scope = getScopeFromLang(lang);
262-
if (scope && scopeStrategy === 'shared') {
262+
if( scope && scopeStrategy === 'shared' ) {
263263
const activeLang = this.getTranslation(currLang);
264-
const key = dashCaseToCamelCase(scopeMapping[scope] || scope);
264+
const key = toCamelCase(scopeMapping[scope] || scope);
265265
const merged = setValue(activeLang, key, withHook);
266266
this.translations.set(currLang, merged);
267267
}
268268
}
269269

270270
private handleSuccess(lang: string, translation: Translation) {
271271
this.setTranslation(translation, lang, { emitChange: false });
272-
if (this.failedLangs.has(lang) === false) {
273-
if (!this.config.prodMode) {
272+
if( this.failedLangs.has(lang) === false ) {
273+
if( !this.config.prodMode ) {
274274
console.log(`%c 🍻 Translation Load Success: ${lang}`, 'background: #fff; color: hotpink;');
275275
}
276276

@@ -298,9 +298,9 @@ export class TranslocoService {
298298

299299
const isFallbackLang = nextLang === splitted[splitted.length - 1];
300300

301-
if (!nextLang || isFallbackLang) {
301+
if( !nextLang || isFallbackLang ) {
302302
let msg = `Unable to load translation and all the fallback languages`;
303-
if (splitted.length > 1) {
303+
if( splitted.length > 1 ) {
304304
msg += `, did you misspelled the scope name?`;
305305
}
306306

@@ -309,7 +309,7 @@ export class TranslocoService {
309309

310310
let resolveLang = nextLang;
311311
// if it's scoped lang
312-
if (splitted.length > 1) {
312+
if( splitted.length > 1 ) {
313313
// We need to resolve it to:
314314
// todos/langNotExists => todos/nextLang
315315
splitted[splitted.length - 1] = nextLang;

projects/ngneat/transloco/src/lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export type FailedEvent = {
1818
export type TranslocoEvents = LoadedEvent | FailedEvent;
1919
export type Translation = HashMap<any>;
2020
export type TranslationCb<T> = (translation: T, params?: HashMap) => string;
21+
export type PersistStorage = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;

projects/ngneat/transloco/src/public-api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ export {
2020
} from './lib/transloco-fallback-strategy';
2121
export { MessageFormatTranspiler } from './lib/transpiler-strategies/messageformat.transpiler';
2222
export { getBrowserCultureLang, getBrowserLang } from './lib/browser-lang';
23+
export * from './lib/types';
24+
export * from './lib/helpers';

0 commit comments

Comments
 (0)