Skip to content

Commit

Permalink
optimise exports
Browse files Browse the repository at this point in the history
  • Loading branch information
eartharoid committed Jul 2, 2024
1 parent c335d76 commit 28c729f
Show file tree
Hide file tree
Showing 27 changed files with 259 additions and 81 deletions.
4 changes: 2 additions & 2 deletions packages/_vite-test/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { setupCounter } from './counter.ts';


// it won't let me import the plugin's I18n?
import { I18nLite } from '@eartharoid/i18n';
import { I18nCore } from '@eartharoid/i18n';
//@ts-ignore
import { ctom } from '@eartharoid/cif';
class I18n extends I18nLite {
class I18n extends I18nCore {
//@ts-ignore
constructor(options) {
super(options);
Expand Down
4 changes: 2 additions & 2 deletions packages/cif/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ function encode(messages) {
### Decoding

```js
import { I18nLite } from '@eartharoid/i18n';
import { I18nCore } from '@eartharoid/i18n';
import { ctom } from '@eartharoid/cif';

const i18n = new I18nLite();
const i18n = new I18nCore();
i18n.loadParsed('en', ctom(cif));

```
1 change: 1 addition & 0 deletions packages/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@typescript-eslint/parser": "^4.22.0",
"ava": "^5.3.1",
"eslint": "^7.25.0",
"rimraf": "^5.0.7",
"typescript": "^4.7.4"
}
}
15 changes: 11 additions & 4 deletions packages/i18n/src/I18n.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import type {
ExtractedMessageObject,
Fallen,
Getter,
I18nOptions,
MetaMessageObject,
ParsedMessage,
ParsedMessages,
RawMessages,
} from './types.js';
import type Locale from './Locale.js';
import I18nLite from './I18nLite.js';
import type Locale from './core/Locale.js';
import I18nCore from './core/I18nCore.js';
import $t from './parsers/$t.js';

export default class I18n extends I18nLite {
export default class I18n extends I18nCore {
public defer_extraction: boolean;
public placeholder_regex: RegExp;
public getters: Record<string, Getter>;

constructor(options?: Partial<I18nOptions>) {
constructor(options: Partial<I18nOptions> = {}) {
options.getters = {
$t,
...options?.getters,
};
super(options);
this.defer_extraction = options?.defer_extraction ?? true; // ?? not ||
this.placeholder_regex = options?.placeholder_regex || /\\?{\s?(?:(?<variable>[-a-z0-9._]+)|(?:(?<getter>[$a-z0-9_]+)(?:\((?<args>[-a-z0-9()!@:%_+.~#?&/= ,]*)\))?))\s?}/gi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import type {
ExtractedMessageObject,
FactoryLocaleInserter,
Getters,
I18nLiteOptions,
Getter,
I18nCoreOptions,
Locales,
NamedArg,
NamedArgs,
ParsedMessages,
Translator
} from './types.js';
import I18n from './I18n.js';
} from '../types.js';
import I18n from '../I18n.js';
import Locale from './Locale.js';
import $t from './getters/$t.js';
import $t from './functions/$t.js';

export default class I18nLite {
export default class I18nCore {
public default_locale_id: string;
public formatters: Record<string, FactoryLocaleInserter<unknown>>;
public getters: Getters;
public getters: Record<string, Omit<Getter, 'parse'>>;
public locales: Locales;
public nested_limit: number;

constructor(options?: Partial<I18nLiteOptions>) {
constructor(options?: Partial<I18nCoreOptions>) {
this.default_locale_id = options?.default_locale_id;
this.formatters = options?.formatters ?? {};
this.getters = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import type {
ParsedMessage,
ParsedMessages,
Translator
} from './types.js';
import type I18nLite from './I18nLite.js';
} from '../types.js';
import type I18nCore from './I18nCore.js';

export default class Locale extends Map<string, ParsedMessage> {
public formatters: Record<string, FormatterFactory>;
public readonly i18n: I18nLite;
public readonly i18n: I18nCore;
public readonly locale_id: string;

constructor(
i18n: I18nLite,
i18n: I18nCore,
locale_id: string,
messages: ParsedMessages,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Getter } from '../types';
import type { Getter } from '../../types.js';

type Parsed = {
export type Parsed = {
k: string,
o?: {
[key: string]: string
}
};

export default <Getter>{
export default <Omit<Getter, 'parse'>>{
get(locale, original, data: Parsed) {
const [, , args, cycle] = original;
return locale.i18n.t(
Expand All @@ -20,14 +20,4 @@ export default <Getter>{
cycle + 1
);
},
parse(args) {
const [key, options] = args.replace(/\s/g, '').split(',');
const d: Parsed = {
k: key
};
if (options) {
d.o = Object.fromEntries(new URLSearchParams(options).entries());
}
return d;
}
};
12 changes: 3 additions & 9 deletions packages/i18n/src/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import DateTime from './DateTime.js';
import List from './List.js';
import RelativeTime from './RelativeTime.js';

export {
DateTime,
List,
RelativeTime,
};
export { default as DateTime } from './DateTime.js';
export { default as List } from './List.js';
export { default as RelativeTime } from './RelativeTime.js';
10 changes: 4 additions & 6 deletions packages/i18n/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

'use strict';

import I18n from './I18n.js';
import I18nLite from './I18nLite.js';
export * from './types.js';

export {
I18n,
I18nLite,
};
export { default as I18n } from './I18n.js';
export { default as I18nCore } from './core/I18nCore.js';
export * as formatters from './formatters/index.js';
16 changes: 16 additions & 0 deletions packages/i18n/src/parsers/$t.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Getter } from '../types.js';
import $t, { type Parsed } from '../core/functions/$t.js';

export default <Getter>{
...$t,
parse(args) {
const [key, options] = args.replace(/\s/g, '').split(',');
const d: Parsed = {
k: key
};
if (options) {
d.o = Object.fromEntries(new URLSearchParams(options).entries());
}
return d;
}
};
10 changes: 4 additions & 6 deletions packages/i18n/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type Locale from './Locale.js';
import type Locale from './core/Locale.js';
export interface FormatterFactory {
get result(): string;
}
Expand All @@ -21,16 +21,14 @@ export type Getter = {
parse(args: string): unknown,
}

export type Getters = Record<string, Getter>

export interface I18nLiteOptions {
export interface I18nCoreOptions {
default_locale_id: string,
formatters: Record<string, FactoryLocaleInserter<unknown>>,
getters: Getters,
getters: Record<string, Omit<Getter, 'parse'>>,
nested_limit: number,
}

export interface I18nOptions extends I18nLiteOptions {
export interface I18nOptions extends I18nCoreOptions {
defer_extraction: boolean,
placeholder_regex: RegExp,
}
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/test/formatters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { I18n } from '../dist/index.js';
import * as formatters from '../dist/formatters/index.js';
import { formatters } from '../dist/index.js';
import fs from 'fs';
import test from 'ava';

Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test('getLocale no', t => {
// t.is(actual, expected);
// });

test('I18nLite#createTranslator', t => {
test('I18nCore#createTranslator', t => {
const translate = i18n.createTranslator('en');
const expected = 'This is as simple as it gets';
const actual = translate('simple');
Expand Down
9 changes: 5 additions & 4 deletions packages/i18n/types/I18n.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { ExtractedMessageObject, Fallen, I18nOptions, ParsedMessages, RawMessages } from './types.js';
import type Locale from './Locale.js';
import I18nLite from './I18nLite.js';
export default class I18n extends I18nLite {
import type { ExtractedMessageObject, Fallen, Getter, I18nOptions, ParsedMessages, RawMessages } from './types.js';
import type Locale from './core/Locale.js';
import I18nCore from './core/I18nCore.js';
export default class I18n extends I18nCore {
defer_extraction: boolean;
placeholder_regex: RegExp;
getters: Record<string, Getter>;
constructor(options?: Partial<I18nOptions>);
extract(message: string): ExtractedMessageObject;
fallback(fallback_map?: Record<string, string[]>): Fallen;
Expand Down
6 changes: 3 additions & 3 deletions packages/i18n/types/I18nLite.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { FactoryLocaleInserter, Getters, I18nLiteOptions, Locales, NamedArg, NamedArgs, ParsedMessages, Translator } from './types.js';
import type { FactoryLocaleInserter, Getters, I18nCoreOptions, Locales, NamedArg, NamedArgs, ParsedMessages, Translator } from './types.js';
import Locale from './Locale.js';
export default class I18nLite {
export default class I18nCore {
default_locale_id: string;
formatters: Record<string, FactoryLocaleInserter<unknown>>;
getters: Getters;
locales: Locales;
nested_limit: number;
constructor(options?: Partial<I18nLiteOptions>);
constructor(options?: Partial<I18nCoreOptions>);
createTranslator(locale_id: string): Translator;
loadParsed(locale_id: string, messages: ParsedMessages): Locale;
resolve(obj: NamedArgs, key: string): NamedArg | undefined;
Expand Down
6 changes: 3 additions & 3 deletions packages/i18n/types/Locale.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { FormatterFactory, NamedArgs, ParsedMessage, ParsedMessages, Translator } from './types.js';
import type I18nLite from './I18nLite.js';
import type I18nCore from './core/I18nCore.js';
export default class Locale extends Map<string, ParsedMessage> {
formatters: Record<string, FormatterFactory>;
readonly i18n: I18nLite;
readonly i18n: I18nCore;
readonly locale_id: string;
constructor(i18n: I18nLite, locale_id: string, messages: ParsedMessages);
constructor(i18n: I18nCore, locale_id: string, messages: ParsedMessages);
createTranslator(): Translator;
t(key: string, args?: NamedArgs): string;
}
14 changes: 14 additions & 0 deletions packages/i18n/types/core/I18nCore.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { FactoryLocaleInserter, Getter, I18nCoreOptions, Locales, NamedArg, NamedArgs, ParsedMessages, Translator } from '../types.js';
import Locale from './Locale.js';
export default class I18nCore {
default_locale_id: string;
formatters: Record<string, FactoryLocaleInserter<unknown>>;
getters: Record<string, Omit<Getter, 'parse'>>;
locales: Locales;
nested_limit: number;
constructor(options?: Partial<I18nCoreOptions>);
createTranslator(locale_id: string): Translator;
loadParsed(locale_id: string, messages: ParsedMessages): Locale;
resolve(obj: NamedArgs, key: string): NamedArg | undefined;
t(locale_id: string, key: string, args?: NamedArgs, nested?: number): string;
}
10 changes: 10 additions & 0 deletions packages/i18n/types/core/Locale.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { FormatterFactory, NamedArgs, ParsedMessage, ParsedMessages, Translator } from '../types.js';
import type I18nCore from './I18nCore.js';
export default class Locale extends Map<string, ParsedMessage> {
formatters: Record<string, FormatterFactory>;
readonly i18n: I18nCore;
readonly locale_id: string;
constructor(i18n: I18nCore, locale_id: string, messages: ParsedMessages);
createTranslator(): Translator;
t(key: string, args?: NamedArgs): string;
}
9 changes: 9 additions & 0 deletions packages/i18n/types/core/functions/$t.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Getter } from '../../types.js';
export declare type Parsed = {
k: string;
o?: {
[key: string]: string;
};
};
declare const _default: Omit<Getter, "parse">;
export default _default;
9 changes: 9 additions & 0 deletions packages/i18n/types/core/partial-getters/$t.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { Getter } from '../../types.js';
export declare type Parsed = {
k: string;
o?: {
[key: string]: string;
};
};
declare const _default: Omit<Getter, "parse">;
export default _default;
7 changes: 3 additions & 4 deletions packages/i18n/types/formatters/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import DateTime from './DateTime.js';
import List from './List.js';
import RelativeTime from './RelativeTime.js';
export { DateTime, List, RelativeTime, };
export { default as DateTime } from './DateTime.js';
export { default as List } from './List.js';
export { default as RelativeTime } from './RelativeTime.js';
2 changes: 1 addition & 1 deletion packages/i18n/types/getters/$t.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Getter } from '../types';
import type { Getter } from '../types.js';
declare const _default: Getter;
export default _default;
7 changes: 4 additions & 3 deletions packages/i18n/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import I18n from './I18n.js';
import I18nLite from './I18nLite.js';
export { I18n, I18nLite, };
export * from './types.js';
export { default as I18n } from './I18n.js';
export { default as I18nCore } from './core/I18nCore.js';
export * as formatters from './formatters/index.js';
3 changes: 3 additions & 0 deletions packages/i18n/types/parsers/$t.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Getter } from '../types.js';
declare const _default: Getter;
export default _default;
9 changes: 4 additions & 5 deletions packages/i18n/types/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type Locale from './Locale.js';
import type Locale from './core/Locale.js';
export interface FormatterFactory {
get result(): string;
}
Expand All @@ -14,14 +14,13 @@ export declare type Getter = {
], data: unknown): string;
parse(args: string): unknown;
};
export declare type Getters = Record<string, Getter>;
export interface I18nLiteOptions {
export interface I18nCoreOptions {
default_locale_id: string;
formatters: Record<string, FactoryLocaleInserter<unknown>>;
getters: Getters;
getters: Record<string, Omit<Getter, 'parse'>>;
nested_limit: number;
}
export interface I18nOptions extends I18nLiteOptions {
export interface I18nOptions extends I18nCoreOptions {
defer_extraction: boolean;
placeholder_regex: RegExp;
}
Expand Down
Loading

0 comments on commit 28c729f

Please sign in to comment.