generaltranslation 8.0.6 → 8.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/cache/IntlCache.d.ts +19 -1
- package/dist/cache/types.d.ts +32 -0
- package/dist/errors/formattingErrors.d.ts +1 -0
- package/dist/formatting/custom-formats/CutoffFormat/CutoffFormat.d.ts +59 -0
- package/dist/formatting/custom-formats/CutoffFormat/constants.d.ts +4 -0
- package/dist/formatting/custom-formats/CutoffFormat/types.d.ts +48 -0
- package/dist/index.cjs.min.cjs +3 -3
- package/dist/index.cjs.min.cjs.map +1 -1
- package/dist/index.d.ts +91 -2
- package/dist/index.esm.min.mjs +3 -3
- package/dist/index.esm.min.mjs.map +1 -1
- package/dist/internal.cjs.min.cjs +1 -1
- package/dist/internal.cjs.min.cjs.map +1 -1
- package/dist/internal.esm.min.mjs +1 -1
- package/dist/internal.esm.min.mjs.map +1 -1
- package/dist/translate/submitUserEditDiffs.d.ts +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# generaltranslation
|
|
2
2
|
|
|
3
|
+
## 8.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#872](https://github.com/generaltranslation/gt/pull/872) [`3e8ceb4`](https://github.com/generaltranslation/gt/commit/3e8ceb4526530d38eae469b05e8bf273d5ca05ac) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - feat: formatCutoff()
|
|
8
|
+
|
|
3
9
|
## 8.0.6
|
|
4
10
|
|
|
5
11
|
### Patch Changes
|
|
@@ -1,8 +1,26 @@
|
|
|
1
|
+
import { ConstructorType, CustomIntlConstructors } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Cache for Intl and custom format instances to avoid repeated instantiation
|
|
4
|
+
* Uses a two-level structure: constructor name -> cache key -> instance
|
|
5
|
+
*/
|
|
1
6
|
declare class IntlCache {
|
|
2
7
|
private cache;
|
|
3
8
|
constructor();
|
|
9
|
+
/**
|
|
10
|
+
* Generates a consistent cache key from locales and options
|
|
11
|
+
* Handles all LocalesArgument types (string, Locale, array, undefined)
|
|
12
|
+
*/
|
|
4
13
|
private _generateKey;
|
|
5
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Gets a cached Intl instance or creates a new one if not found
|
|
16
|
+
* @param constructor The name of the Intl constructor to use
|
|
17
|
+
* @param args Constructor arguments (locales, options)
|
|
18
|
+
* @returns Cached or newly created Intl instance
|
|
19
|
+
*/
|
|
20
|
+
get<K extends keyof CustomIntlConstructors>(constructor: K, ...args: ConstructorParameters<CustomIntlConstructors[K]>): InstanceType<ConstructorType<K>>;
|
|
6
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Global instance of the Intl cache for use throughout the application
|
|
24
|
+
*/
|
|
7
25
|
export declare const intlCache: IntlCache;
|
|
8
26
|
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { CutoffFormatConstructor } from '../formatting/custom-formats/CutoffFormat/CutoffFormat';
|
|
2
|
+
/**
|
|
3
|
+
* Extracts only the constructor functions from the Intl namespace,
|
|
4
|
+
* filtering out static methods like getCanonicalLocales and supportedValuesOf
|
|
5
|
+
*/
|
|
6
|
+
type IntlConstructors = {
|
|
7
|
+
[K in keyof typeof Intl as (typeof Intl)[K] extends new (...args: any[]) => any ? K : never]: (typeof Intl)[K];
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Extended interface that includes all native Intl constructors plus custom ones
|
|
11
|
+
*/
|
|
12
|
+
export interface CustomIntlConstructors extends IntlConstructors {
|
|
13
|
+
CutoffFormat: typeof CutoffFormatConstructor;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Helper type to represent a constructor function for a given Intl constructor key
|
|
17
|
+
*/
|
|
18
|
+
export type ConstructorType<K extends keyof CustomIntlConstructors> = new (...args: ConstructorParameters<CustomIntlConstructors[K]>) => InstanceType<CustomIntlConstructors[K]>;
|
|
19
|
+
/**
|
|
20
|
+
* Type for the cache object structure - each constructor gets its own Record
|
|
21
|
+
* mapping cache keys to instances of that specific constructor type
|
|
22
|
+
*/
|
|
23
|
+
export type IntlCacheObject = {
|
|
24
|
+
[K in keyof CustomIntlConstructors]?: Record<string, InstanceType<ConstructorType<K>>>;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Type for the CustomIntl object that maps constructor names to constructor functions
|
|
28
|
+
*/
|
|
29
|
+
export type CustomIntlType = {
|
|
30
|
+
[K in keyof CustomIntlConstructors]: ConstructorType<K>;
|
|
31
|
+
};
|
|
32
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createInvalidCutoffStyleError: (style: string) => string;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { CutoffFormat, CutoffFormatOptions, PostpendedCutoffParts, PrependedCutoffParts, ResolvedCutoffFormatOptions } from './types';
|
|
2
|
+
export declare class CutoffFormatConstructor implements CutoffFormat {
|
|
3
|
+
private locale;
|
|
4
|
+
private options;
|
|
5
|
+
private additionLength;
|
|
6
|
+
/**
|
|
7
|
+
* Constructor
|
|
8
|
+
* @param {string | string[]} locales - The locales to use for formatting.
|
|
9
|
+
* @param {CutoffFormatOptions} options - The options for formatting.
|
|
10
|
+
* @param {number} [option.maxChars] - The maximum number of characters to display.
|
|
11
|
+
* - Undefined values are treated as no cutoff.
|
|
12
|
+
* - Negative values follow .slice() behavior and terminator will be added before the value.
|
|
13
|
+
* - 0 will result in an empty string.
|
|
14
|
+
* - If cutoff results in an empty string, no terminator is added.
|
|
15
|
+
* @param {CutoffFormatStyle} [option.style='ellipsis'] - The style of the terminator.
|
|
16
|
+
* @param {string} [option.terminator] - Optional override the terminator to use.
|
|
17
|
+
* @param {string} [option.separator] - Optional override the separator to use between the terminator and the value.
|
|
18
|
+
* - If no terminator is provided, then separator is ignored.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* const format = new CutoffFormat('en', { maxChars: 5 });
|
|
22
|
+
* format.format('Hello, world!'); // 'Hello...'
|
|
23
|
+
*
|
|
24
|
+
* const format = new CutoffFormat('en', { maxChars: -3 });
|
|
25
|
+
* format.format('Hello, world!'); // '...ld!'
|
|
26
|
+
*/
|
|
27
|
+
constructor(locales: Intl.LocalesArgument, options?: CutoffFormatOptions);
|
|
28
|
+
/**
|
|
29
|
+
* Format a value according to the cutoff options, returning a formatted string.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} value - The string value to format with cutoff behavior.
|
|
32
|
+
* @returns {string} The formatted string with terminator applied if cutoff occurs.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const formatter = new CutoffFormatConstructor('en', { maxChars: 8, style: 'ellipsis' });
|
|
36
|
+
* formatter.format('Hello, world!'); // Returns 'Hello, w...'
|
|
37
|
+
*/
|
|
38
|
+
format(value: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Format a value to parts according to the cutoff options, returning an array of string parts.
|
|
41
|
+
* This method breaks down the formatted result into individual components for more granular control.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} value - The string value to format with cutoff behavior.
|
|
44
|
+
* @returns {PrependedCutoffParts | PostpendedCutoffParts} An array of string parts representing the formatted result.
|
|
45
|
+
* - For positive maxChars: [cutoffValue, separator?, terminator?]
|
|
46
|
+
* - For negative maxChars: [terminator?, separator?, cutoffValue]
|
|
47
|
+
* - For no cutoff: [originalValue]
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* const formatter = new CutoffFormatConstructor('en', { maxChars: 5, style: 'ellipsis' });
|
|
51
|
+
* formatter.formatToParts('Hello, world!'); // Returns ['Hello', '...']
|
|
52
|
+
*/
|
|
53
|
+
formatToParts(value: string): PrependedCutoffParts | PostpendedCutoffParts;
|
|
54
|
+
/**
|
|
55
|
+
* Get the resolved options
|
|
56
|
+
* @returns {ResolvedCutoffFormatOptions} The resolved options.
|
|
57
|
+
*/
|
|
58
|
+
resolvedOptions(): ResolvedCutoffFormatOptions;
|
|
59
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { CutoffFormatStyle, ResolvedTerminatorOptions } from './types';
|
|
2
|
+
export declare const DEFAULT_CUTOFF_FORMAT_STYLE: CutoffFormatStyle;
|
|
3
|
+
export declare const DEFAULT_TERMINATOR_KEY = "DEFAULT_TERMINATOR_KEY";
|
|
4
|
+
export declare const TERMINATOR_MAP: Record<CutoffFormatStyle, Record<string | typeof DEFAULT_TERMINATOR_KEY, ResolvedTerminatorOptions>>;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** Type of terminator */
|
|
2
|
+
export type CutoffFormatStyle = 'none' | 'ellipsis';
|
|
3
|
+
/** Terminator options */
|
|
4
|
+
export interface TerminatorOptions {
|
|
5
|
+
/** The terminator to use */
|
|
6
|
+
terminator?: string;
|
|
7
|
+
/** An optional separator between the terminator and the value */
|
|
8
|
+
separator?: string;
|
|
9
|
+
}
|
|
10
|
+
/** Input formatting options (for constructor) */
|
|
11
|
+
export interface CutoffFormatOptions extends TerminatorOptions {
|
|
12
|
+
/** Cutoff length */
|
|
13
|
+
maxChars?: number;
|
|
14
|
+
/** Type of terminator */
|
|
15
|
+
style?: CutoffFormatStyle;
|
|
16
|
+
}
|
|
17
|
+
/** Resolved terminator options */
|
|
18
|
+
export interface ResolvedTerminatorOptions extends TerminatorOptions {
|
|
19
|
+
terminator: string | undefined;
|
|
20
|
+
separator: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
/** Resolved options (after constructor) */
|
|
23
|
+
export interface ResolvedCutoffFormatOptions extends CutoffFormatOptions, ResolvedTerminatorOptions {
|
|
24
|
+
maxChars: number | undefined;
|
|
25
|
+
style: CutoffFormatStyle | undefined;
|
|
26
|
+
terminator: string | undefined;
|
|
27
|
+
separator: string | undefined;
|
|
28
|
+
}
|
|
29
|
+
/** Prepended cutoff list */
|
|
30
|
+
export type PrependedCutoffParts = [string] | [ResolvedTerminatorOptions['terminator'], string] | [
|
|
31
|
+
ResolvedTerminatorOptions['terminator'],
|
|
32
|
+
ResolvedTerminatorOptions['separator'],
|
|
33
|
+
string
|
|
34
|
+
];
|
|
35
|
+
/** Postpended cutoff list */
|
|
36
|
+
export type PostpendedCutoffParts = [string] | [string, ResolvedTerminatorOptions['terminator']] | [
|
|
37
|
+
string,
|
|
38
|
+
ResolvedTerminatorOptions['separator'],
|
|
39
|
+
ResolvedTerminatorOptions['terminator']
|
|
40
|
+
];
|
|
41
|
+
/**
|
|
42
|
+
* Cutoff Class interface
|
|
43
|
+
*/
|
|
44
|
+
export interface CutoffFormat {
|
|
45
|
+
format: (value: string) => string;
|
|
46
|
+
resolvedOptions: () => ResolvedCutoffFormatOptions;
|
|
47
|
+
formatToParts: (value: string) => PrependedCutoffParts | PostpendedCutoffParts;
|
|
48
|
+
}
|