@intlayer/core 1.2.0 → 2.0.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.
Files changed (76) hide show
  1. package/dist/cjs/index.cjs +2 -0
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs/index.d.ts +2 -0
  4. package/dist/cjs/transpiler/content_transformers/enumeration/enumeration.cjs +0 -4
  5. package/dist/cjs/transpiler/content_transformers/enumeration/enumeration.cjs.map +1 -1
  6. package/dist/cjs/transpiler/content_transformers/enumeration/enumeration.d.ts +4 -3
  7. package/dist/cjs/transpiler/content_transformers/enumeration/findMatchingCondition.cjs +42 -0
  8. package/dist/cjs/transpiler/content_transformers/enumeration/findMatchingCondition.cjs.map +1 -0
  9. package/dist/cjs/transpiler/content_transformers/enumeration/findMatchingCondition.d.ts +33 -0
  10. package/dist/cjs/transpiler/content_transformers/enumeration/getEnumerationContent.cjs +3 -12
  11. package/dist/cjs/transpiler/content_transformers/enumeration/getEnumerationContent.cjs.map +1 -1
  12. package/dist/cjs/transpiler/content_transformers/enumeration/getEnumerationContent.d.ts +8 -6
  13. package/dist/cjs/transpiler/content_transformers/enumeration/index.cjs +3 -1
  14. package/dist/cjs/transpiler/content_transformers/enumeration/index.cjs.map +1 -1
  15. package/dist/cjs/transpiler/content_transformers/enumeration/index.d.ts +2 -2
  16. package/dist/cjs/transpiler/content_transformers/index.d.ts +4 -3
  17. package/dist/cjs/transpiler/content_transformers/translation/getTranslationContent.cjs +1 -1
  18. package/dist/cjs/transpiler/content_transformers/translation/getTranslationContent.cjs.map +1 -1
  19. package/dist/cjs/transpiler/content_transformers/translation/getTranslationContent.d.ts +6 -5
  20. package/dist/cjs/transpiler/content_transformers/translation/index.d.ts +2 -2
  21. package/dist/cjs/transpiler/content_transformers/translation/translation.cjs +3 -5
  22. package/dist/cjs/transpiler/content_transformers/translation/translation.cjs.map +1 -1
  23. package/dist/cjs/transpiler/content_transformers/translation/translation.d.ts +5 -3
  24. package/dist/cjs/transpiler/content_transformers/translation/types.cjs.map +1 -1
  25. package/dist/cjs/transpiler/content_transformers/translation/types.d.ts +3 -4
  26. package/dist/cjs/transpiler/index.d.ts +4 -3
  27. package/dist/cjs/types/contentModule.cjs.map +1 -1
  28. package/dist/cjs/types/contentModule.d.ts +2 -1
  29. package/dist/cjs/types/index.cjs.map +1 -1
  30. package/dist/cjs/types/index.d.ts +1 -0
  31. package/dist/cjs/types/keyPath.cjs +17 -0
  32. package/dist/cjs/types/keyPath.cjs.map +1 -0
  33. package/dist/cjs/types/keyPath.d.ts +13 -0
  34. package/dist/esm/index.d.mts +2 -0
  35. package/dist/esm/index.mjs +2 -0
  36. package/dist/esm/index.mjs.map +1 -1
  37. package/dist/esm/transpiler/content_transformers/enumeration/enumeration.d.mts +4 -3
  38. package/dist/esm/transpiler/content_transformers/enumeration/enumeration.mjs +0 -6
  39. package/dist/esm/transpiler/content_transformers/enumeration/enumeration.mjs.map +1 -1
  40. package/dist/esm/transpiler/content_transformers/enumeration/findMatchingCondition.d.mts +33 -0
  41. package/dist/esm/transpiler/content_transformers/enumeration/findMatchingCondition.mjs +18 -0
  42. package/dist/esm/transpiler/content_transformers/enumeration/findMatchingCondition.mjs.map +1 -0
  43. package/dist/esm/transpiler/content_transformers/enumeration/getEnumerationContent.d.mts +8 -6
  44. package/dist/esm/transpiler/content_transformers/enumeration/getEnumerationContent.mjs +3 -12
  45. package/dist/esm/transpiler/content_transformers/enumeration/getEnumerationContent.mjs.map +1 -1
  46. package/dist/esm/transpiler/content_transformers/enumeration/index.d.mts +2 -2
  47. package/dist/esm/transpiler/content_transformers/enumeration/index.mjs +1 -0
  48. package/dist/esm/transpiler/content_transformers/enumeration/index.mjs.map +1 -1
  49. package/dist/esm/transpiler/content_transformers/index.d.mts +4 -3
  50. package/dist/esm/transpiler/content_transformers/translation/getTranslationContent.d.mts +6 -5
  51. package/dist/esm/transpiler/content_transformers/translation/getTranslationContent.mjs +2 -4
  52. package/dist/esm/transpiler/content_transformers/translation/getTranslationContent.mjs.map +1 -1
  53. package/dist/esm/transpiler/content_transformers/translation/index.d.mts +2 -2
  54. package/dist/esm/transpiler/content_transformers/translation/translation.d.mts +5 -3
  55. package/dist/esm/transpiler/content_transformers/translation/translation.mjs +4 -6
  56. package/dist/esm/transpiler/content_transformers/translation/translation.mjs.map +1 -1
  57. package/dist/esm/transpiler/content_transformers/translation/types.d.mts +3 -4
  58. package/dist/esm/transpiler/index.d.mts +4 -3
  59. package/dist/esm/types/contentModule.d.mts +2 -1
  60. package/dist/esm/types/index.d.mts +1 -0
  61. package/dist/esm/types/index.mjs.map +1 -1
  62. package/dist/esm/types/keyPath.d.mts +13 -0
  63. package/dist/esm/types/keyPath.mjs +1 -0
  64. package/dist/esm/types/keyPath.mjs.map +1 -0
  65. package/package.json +2 -3
  66. package/src/index.ts +4 -0
  67. package/src/transpiler/content_transformers/enumeration/enumeration.ts +6 -13
  68. package/src/transpiler/content_transformers/enumeration/findMatchingCondition.ts +64 -0
  69. package/src/transpiler/content_transformers/enumeration/getEnumerationContent.ts +11 -36
  70. package/src/transpiler/content_transformers/enumeration/index.ts +1 -0
  71. package/src/transpiler/content_transformers/translation/getTranslationContent.ts +7 -9
  72. package/src/transpiler/content_transformers/translation/translation.ts +6 -7
  73. package/src/transpiler/content_transformers/translation/types.ts +1 -2
  74. package/src/types/contentModule.ts +1 -0
  75. package/src/types/index.ts +5 -0
  76. package/src/types/keyPath.ts +13 -0
@@ -0,0 +1,64 @@
1
+ import type { EnterFormat, QuantityContent } from './enumeration';
2
+
3
+ /**
4
+ * Find the matching condition for a quantity.
5
+ *
6
+ * Usage:
7
+ *
8
+ * ```ts
9
+ * const key = findMatchingCondition({
10
+ * '<=-2.3': 'You have less than -2.3',
11
+ * '<1': 'You have less than one',
12
+ * '2': 'You have two',
13
+ * '>=3': 'You have three or more',
14
+ * }, 2);
15
+ * // '2'
16
+ * ```
17
+ *
18
+ * The order of the keys will define the priority of the content.
19
+ *
20
+ * ```ts
21
+ * const key = findMatchingCondition({
22
+ * '<4': 'You have less than four',
23
+ * '2': 'You have two',
24
+ * }, 2);
25
+ * // '<4'
26
+ * ```
27
+ *
28
+ * If no keys match, the default key is '1'.
29
+ */
30
+ export const findMatchingCondition = <Content>(
31
+ enumerationContent: QuantityContent<Content>,
32
+ quantity: number
33
+ ): EnterFormat => {
34
+ const numericKeys = Object.keys(enumerationContent);
35
+
36
+ for (const key of numericKeys) {
37
+ const isEqual =
38
+ (!key.startsWith('>') &&
39
+ !key.startsWith('<') &&
40
+ !key.startsWith('=') &&
41
+ parseFloat(key) === quantity) ||
42
+ (key.startsWith('=') && parseFloat(key.slice(1)) === quantity);
43
+ const isSuperior =
44
+ key.startsWith('>') && quantity > parseFloat(key.slice(1));
45
+ const isSuperiorOrEqual =
46
+ key.startsWith('>=') && quantity >= parseFloat(key.slice(2));
47
+ const isInferior =
48
+ key.startsWith('<') && quantity < parseFloat(key.slice(1));
49
+ const isInferiorOrEqual =
50
+ key.startsWith('<=') && quantity <= parseFloat(key.slice(2));
51
+
52
+ if (
53
+ isEqual ||
54
+ isSuperior ||
55
+ isSuperiorOrEqual ||
56
+ isInferior ||
57
+ isInferiorOrEqual
58
+ ) {
59
+ return key as EnterFormat;
60
+ }
61
+ }
62
+
63
+ return '1';
64
+ };
@@ -1,63 +1,38 @@
1
+ import { findMatchingCondition } from '../..';
1
2
  import type { QuantityContent } from './enumeration';
2
3
 
3
- type Key = keyof QuantityContent<string>;
4
-
5
4
  /**
6
5
  * Allow to pick a content based on a quantity.
7
6
  *
8
7
  * Usage:
9
8
  *
9
+ * ```ts
10
10
  * const content = getEnumerationContent({
11
11
  * '<=-2.3': 'You have less than -2.3',
12
12
  * '<1': 'You have less than one',
13
13
  * '2': 'You have two',
14
14
  * '>=3': 'You have three or more',
15
- * },
16
- * 2);
15
+ * }, 2);
17
16
  * // 'You have two'
17
+ * ```
18
18
  *
19
19
  * The order of the keys will define the priority of the content.
20
20
  *
21
+ * ```ts
21
22
  * const content = getEnumerationContent({
22
- * '<4': 'You have less than four',
23
- * '2': 'You have two',
24
- * });
23
+ * '<4': 'You have less than four',
24
+ * '2': 'You have two',
25
+ * }, 2);
25
26
  * // 'You have less than four'
27
+ * ```
26
28
  *
27
29
  */
28
30
  export const getEnumerationContent = <Content>(
29
31
  enumerationContent: QuantityContent<Content>,
30
32
  quantity: number
31
33
  ): Content => {
32
- const numericKeys = Object.keys(enumerationContent);
33
-
34
- for (const key of numericKeys) {
35
- const isEqual =
36
- (!key.startsWith('>') &&
37
- !key.startsWith('<') &&
38
- !key.startsWith('=') &&
39
- parseFloat(key) === quantity) ||
40
- (key.startsWith('=') && parseFloat(key.slice(1)) === quantity);
41
- const isSuperior =
42
- key.startsWith('>') && quantity > parseFloat(key.slice(1));
43
- const isSuperiorOrEqual =
44
- key.startsWith('>=') && quantity >= parseFloat(key.slice(2));
45
- const isInferior =
46
- key.startsWith('<') && quantity < parseFloat(key.slice(1));
47
- const isInferiorOrEqual =
48
- key.startsWith('<=') && quantity <= parseFloat(key.slice(2));
49
-
50
- if (
51
- isEqual ||
52
- isSuperior ||
53
- isSuperiorOrEqual ||
54
- isInferior ||
55
- isInferiorOrEqual
56
- ) {
57
- return enumerationContent[key as Key];
58
- }
59
- }
34
+ const key = findMatchingCondition<Content>(enumerationContent, quantity);
60
35
 
61
36
  // Default or error handling if no keys match
62
- return enumerationContent['1'];
37
+ return enumerationContent[key];
63
38
  };
@@ -1,2 +1,3 @@
1
1
  export * from './enumeration';
2
2
  export * from './getEnumerationContent';
3
+ export * from './findMatchingCondition';
@@ -1,10 +1,7 @@
1
- import {
2
- type Locales,
3
- intlayerIntlConfiguration,
4
- } from '@intlayer/config/client';
1
+ import { type Locales, getConfiguration } from '@intlayer/config/client';
5
2
  import type { CustomizableLanguageContent } from './types';
6
3
 
7
- const defaultLocale = intlayerIntlConfiguration.defaultLocale;
4
+ const defaultLocale = getConfiguration().internationalization.defaultLocale;
8
5
 
9
6
  type GetTranslationContent = <Content = string>(
10
7
  languageContent: CustomizableLanguageContent<Content>,
@@ -18,12 +15,13 @@ type GetTranslationContent = <Content = string>(
18
15
  *
19
16
  * Usage:
20
17
  *
18
+ * ```ts
21
19
  * const content = getTranslationContent({
22
- * en: 'Hello',
23
- * fr: 'Bonjour',
24
- * },
25
- * 'fr');
20
+ * en: 'Hello',
21
+ * fr: 'Bonjour',
22
+ * }, 'fr');
26
23
  * // 'Bonjour'
24
+ * ```
27
25
  *
28
26
  * Using TypeScript:
29
27
  * - this function will require each locale to be defined if defined in the project configuration.
@@ -1,10 +1,7 @@
1
- import { intlayerConfiguration } from '@intlayer/config/client';
1
+ import { getConfiguration } from '@intlayer/config/client';
2
2
  import { NodeType } from '../../../types/index';
3
- import { getStackTraceInfo } from '../../../utils/getStackTraceInfo';
4
3
  import type { CustomizableLanguageContent, TranslationContent } from './types';
5
4
 
6
- const { defaultLocale } = intlayerConfiguration.internationalization;
7
-
8
5
  /**
9
6
  *
10
7
  * Function intended to be used to build intlayer dictionaries.
@@ -13,11 +10,13 @@ const { defaultLocale } = intlayerConfiguration.internationalization;
13
10
  *
14
11
  * Usage:
15
12
  *
13
+ * ```ts
16
14
  * translation<string>({
17
15
  * "en": "Hello",
18
16
  * "fr": "Bonjour",
19
17
  * // ... any other available locale
20
18
  * })
19
+ * ```
21
20
  *
22
21
  * Using TypeScript:
23
22
  * - this function require each locale to be defined if defined in the project configuration.
@@ -26,12 +25,13 @@ const { defaultLocale } = intlayerConfiguration.internationalization;
26
25
  const translation = <Content = string>(
27
26
  content?: CustomizableLanguageContent<Content>
28
27
  ) => {
29
- const stackTraceInfo = getStackTraceInfo();
28
+ const {
29
+ internationalization: { defaultLocale },
30
+ } = getConfiguration();
30
31
 
31
32
  if (typeof content === 'string') {
32
33
  const result: TranslationContent<Content> = {
33
34
  nodeType: NodeType.Translation,
34
- ...stackTraceInfo,
35
35
  [defaultLocale]: content,
36
36
  } as TranslationContent<Content>;
37
37
 
@@ -40,7 +40,6 @@ const translation = <Content = string>(
40
40
 
41
41
  const result: TranslationContent<Content> = {
42
42
  nodeType: NodeType.Translation,
43
- ...stackTraceInfo,
44
43
  ...(content as unknown as object),
45
44
  } as TranslationContent<Content>;
46
45
 
@@ -32,7 +32,6 @@ export type CustomLocales = ConfigLocales extends never
32
32
  * }
33
33
  */
34
34
  export type LanguageContent<Content> = Partial<Record<LocalesValues, Content>>;
35
- export type ConfigLanguageContent<Content> = Record<ConfigLocales, Content>;
36
35
 
37
36
  /**
38
37
  * Valid
@@ -64,7 +63,7 @@ export type ConfigLanguageContent<Content> = Record<ConfigLocales, Content>;
64
63
  export type CustomizableLanguageContent<Content = string> =
65
64
  ConfigLocales extends never
66
65
  ? LanguageContent<Content>
67
- : ConfigLanguageContent<Content>;
66
+ : IConfigLocales<Content>;
68
67
 
69
68
  export type TranslationContent<Content> = LanguageContent<Content> &
70
69
  NoteStackTraceInfo & {
@@ -29,4 +29,5 @@ export type FlatContent = Record<string, FlatContentValue | undefined>;
29
29
 
30
30
  export type ContentModule = Content & {
31
31
  id: string;
32
+ filePath?: string;
32
33
  };
@@ -7,3 +7,8 @@ export type {
7
7
  ContentModule,
8
8
  } from './contentModule';
9
9
  export { NodeType } from './nodeType';
10
+ export type {
11
+ KeyPath,
12
+ ObjectExpressionNode,
13
+ TranslationOrEnumerationNode,
14
+ } from './keyPath';
@@ -0,0 +1,13 @@
1
+ import type { NodeType } from './nodeType';
2
+
3
+ export type ObjectExpressionNode = {
4
+ type: 'ObjectExpression';
5
+ key: string;
6
+ };
7
+
8
+ export type TranslationOrEnumerationNode = {
9
+ type: NodeType;
10
+ key: string;
11
+ };
12
+
13
+ export type KeyPath = ObjectExpressionNode | TranslationOrEnumerationNode;