@koine/i18n 2.0.0-beta.166 → 2.0.0-beta.167

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.
@@ -93,9 +93,7 @@ var t = createAdapter.createGenerator("js", (a)=>{
93
93
  ext: "ts",
94
94
  index: !1,
95
95
  content: ()=>formatTo(t, !0).$out("ts", {
96
- imports: {
97
- folderUp: 0
98
- },
96
+ imports: {},
99
97
  exports: "named"
100
98
  })
101
99
  }
@@ -89,9 +89,7 @@ var t = createGenerator("js", (a)=>{
89
89
  ext: "ts",
90
90
  index: !1,
91
91
  content: ()=>formatTo(t, !0).$out("ts", {
92
- imports: {
93
- folderUp: 0
94
- },
92
+ imports: {},
95
93
  exports: "named"
96
94
  })
97
95
  }
@@ -10,31 +10,31 @@ var imports = require('../../compiler/imports.cjs.js');
10
10
  var types = require('./types.cjs.js');
11
11
 
12
12
  let c = "count";
13
- function getTFunction(e, r) {
14
- let { params: n, plural: o } = e, { body: l, imports: s } = function(e, r) {
15
- let { values: n, params: o, plural: l, typeValue: s, equalValues: i } = e, { defaultLocale: m, single: u } = r, y = !u && !i, P = [
13
+ function getTFunction(t, n) {
14
+ let { params: r, plural: o } = t, { body: i, imports: l } = function(t, n) {
15
+ let { values: r, params: o, plural: i, typeValue: l, equalValues: p } = t, { defaultLocale: m, single: u } = n, y = !u && !p, h = [
16
16
  d.types
17
17
  ];
18
- return l && P.push(d.tPluralise), o && ("Primitive" === s ? P.push(d.tInterpolateParams) : ("Array" === s || "Object" === s) && P.push(d.tInterpolateParamsDeep)), {
19
- imports: P,
20
- body: (e)=>{
21
- let r = y ? "locale = locale || global." + helpers.GLOBAL_I18N_IDENTIFIER + "; " : "";
22
- r += "return ";
23
- let i = "";
24
- return utils.isPrimitive(n) ? i += f(n, e) : i += function(e, r, n, a) {
18
+ return i && h.push(d.i18nPluralise), o && ("Primitive" === l ? h.push(d.i18nInterpolateParams) : ("Array" === l || "Object" === l) && h.push(d.i18nInterpolateParamsDeep)), {
19
+ imports: h,
20
+ body: (t)=>{
21
+ let n = y ? "locale = locale || global." + helpers.GLOBAL_I18N_IDENTIFIER + "; " : "";
22
+ n += "return ";
23
+ let p = "";
24
+ return utils.isPrimitive(r) ? p += f(r, t) : p += function(t, n, r, a) {
25
25
  let o = "";
26
- if (n) for(let n in r){
27
- let l = r[n];
28
- n === e || utils.areEqual(l, r[e]) || (o += `locale === "${n}" ? ${f(l, a)} : `);
26
+ if (r) for(let r in n){
27
+ let i = n[r];
28
+ r === t || utils.areEqual(i, n[t]) || (o += `locale === "${r}" ? ${f(i, a)} : `);
29
29
  }
30
- return o + f(r[e], a);
31
- }(m, n, y, e), l && (i = `tPluralise(${i}, params.${c})`), o && ("Primitive" === s ? i = `tInterpolateParams(${i}, params)` : ("Array" === s || "Object" === s) && (i = `tInterpolateParamsDeep(${i}, params)`)), r + i;
30
+ return o + f(n[t], a);
31
+ }(m, r, y, t), i && (p = `i18nPluralise(${p}, params.${c})`), o && ("Primitive" === l ? p = `i18nInterpolateParams(${p}, params)` : ("Array" === l || "Object" === l) && (p = `i18nInterpolateParamsDeep(${p}, params)`)), n + p;
32
32
  }
33
33
  };
34
- }(e, r), m = [], u = n ? o ? {
35
- ...n,
34
+ }(t, n), m = [], u = r ? o ? {
35
+ ...r,
36
36
  [c]: "number"
37
- } : n : o ? {
37
+ } : r : o ? {
38
38
  [c]: "number"
39
39
  } : null;
40
40
  return u && m.push({
@@ -49,50 +49,50 @@ function getTFunction(e, r) {
49
49
  description: "Use this to override the current locale",
50
50
  defaults: "current locale"
51
51
  }), {
52
- body: l,
52
+ body: i,
53
53
  args: m,
54
- imports: s
54
+ imports: l
55
55
  };
56
56
  }
57
- function f(t, { format: a }) {
58
- let l = "ts" === a ? " as const" : "";
59
- return utils.isString(t) || utils.isNumber(t) ? `"${t}"${l}` : utils.isBoolean(t) ? `${t}` : utils.isArray(t) ? JSON.stringify(t) + l : `(${JSON.stringify(t)}${l})`;
57
+ function f(e, { format: a }) {
58
+ let i = "ts" === a ? " as const" : "";
59
+ return utils.isString(e) || utils.isNumber(e) ? `"${e}"${i}` : utils.isBoolean(e) ? `${e}` : utils.isArray(e) ? JSON.stringify(e) + i : `(${JSON.stringify(e)}${i})`;
60
60
  }
61
61
  let d = {
62
62
  types: types.getImportTypes(),
63
- tInterpolateParams: new imports.ImportsCompiler({
64
- path: "internal/tInterpolateParams",
63
+ i18nInterpolateParams: new imports.ImportsCompiler({
64
+ path: "@koine/i18n",
65
65
  named: [
66
66
  {
67
- name: "tInterpolateParams"
67
+ name: "i18nInterpolateParams"
68
68
  }
69
69
  ]
70
70
  }),
71
- tInterpolateParamsDeep: new imports.ImportsCompiler({
72
- path: "internal/tInterpolateParamsDeep",
71
+ i18nInterpolateParamsDeep: new imports.ImportsCompiler({
72
+ path: "@koine/i18n",
73
73
  named: [
74
74
  {
75
- name: "tInterpolateParamsDeep"
75
+ name: "i18nInterpolateParamsDeep"
76
76
  }
77
77
  ]
78
78
  }),
79
- tPluralise: new imports.ImportsCompiler({
80
- path: "internal/tPluralise",
79
+ i18nPluralise: new imports.ImportsCompiler({
80
+ path: "@koine/i18n",
81
81
  named: [
82
82
  {
83
- name: "tPluralise"
83
+ name: "i18nPluralise"
84
84
  }
85
85
  ]
86
86
  })
87
- }, y = (t, e)=>{
88
- let r = [], n = new Set();
89
- for(let a in n.add(d.types), t){
90
- let o = t[a], { imports: l, args: p, body: i } = getTFunction(o, e);
91
- l.forEach((t)=>n.add(t)), r.push(new functions.FunctionsCompiler({
92
- imports: l,
87
+ }, y = (e, t)=>{
88
+ let n = [], r = new Set();
89
+ for(let a in r.add(d.types), e){
90
+ let o = e[a], { imports: i, args: s, body: p } = getTFunction(o, t);
91
+ i.forEach((e)=>r.add(e)), n.push(new functions.FunctionsCompiler({
92
+ imports: i,
93
93
  name: o.fnName,
94
- args: p,
95
- body: i,
94
+ args: s,
95
+ body: p,
96
96
  comment: {
97
97
  tags: [
98
98
  {
@@ -104,18 +104,18 @@ let d = {
104
104
  }));
105
105
  }
106
106
  return {
107
- functions: r,
108
- allImports: n
107
+ functions: n,
108
+ allImports: r
109
109
  };
110
110
  };
111
- var d$1 = createAdapter.createGenerator("js", (t)=>{
112
- let { config: e, options: { translations: r, adapter: { modularize: n } }, translations: a } = t, { dir: o } = r.functions, { functions: l, allImports: p } = y(a, e);
113
- return n ? l.reduce((t, e)=>(t[e.name] = {
111
+ var d$1 = createAdapter.createGenerator("js", (e)=>{
112
+ let { config: t, options: { translations: n, adapter: { modularize: r } }, translations: a } = e, { dir: o } = n.functions, { functions: i, allImports: s } = y(a, t);
113
+ return r ? i.reduce((e, t)=>(e[t.name] = {
114
114
  dir: o,
115
- name: e.name,
115
+ name: t.name,
116
116
  ext: "ts",
117
117
  index: !0,
118
- content: ()=>e.$out("ts", {
118
+ content: ()=>t.$out("ts", {
119
119
  imports: {
120
120
  folderUp: 1
121
121
  },
@@ -124,14 +124,12 @@ var d$1 = createAdapter.createGenerator("js", (t)=>{
124
124
  comments: !0,
125
125
  pure: !0
126
126
  })
127
- }, t), {}) : {
127
+ }, e), {}) : {
128
128
  $t: {
129
129
  name: o,
130
130
  ext: "ts",
131
131
  index: !1,
132
- content: ()=>"" + (imports.ImportsCompiler.outMany("ts", p, {
133
- folderUp: 0
134
- }) + functions.FunctionsCompiler.outMany("ts", l, {
132
+ content: ()=>"" + (imports.ImportsCompiler.outMany("ts", s) + functions.FunctionsCompiler.outMany("ts", i, {
135
133
  imports: !1,
136
134
  exports: "named",
137
135
  style: "function",
@@ -6,31 +6,31 @@ import { ImportsCompiler } from '../../compiler/imports.esm.js';
6
6
  import { getImportTypes } from './types.esm.js';
7
7
 
8
8
  let c = "count";
9
- function getTFunction(e, r) {
10
- let { params: n, plural: o } = e, { body: l, imports: s } = function(e, r) {
11
- let { values: n, params: o, plural: l, typeValue: s, equalValues: i } = e, { defaultLocale: m, single: u } = r, y = !u && !i, P = [
9
+ function getTFunction(t, n) {
10
+ let { params: r, plural: o } = t, { body: i, imports: l } = function(t, n) {
11
+ let { values: r, params: o, plural: i, typeValue: l, equalValues: p } = t, { defaultLocale: m, single: u } = n, y = !u && !p, h = [
12
12
  d.types
13
13
  ];
14
- return l && P.push(d.tPluralise), o && ("Primitive" === s ? P.push(d.tInterpolateParams) : ("Array" === s || "Object" === s) && P.push(d.tInterpolateParamsDeep)), {
15
- imports: P,
16
- body: (e)=>{
17
- let r = y ? "locale = locale || global." + GLOBAL_I18N_IDENTIFIER + "; " : "";
18
- r += "return ";
19
- let i = "";
20
- return isPrimitive(n) ? i += f(n, e) : i += function(e, r, n, a) {
14
+ return i && h.push(d.i18nPluralise), o && ("Primitive" === l ? h.push(d.i18nInterpolateParams) : ("Array" === l || "Object" === l) && h.push(d.i18nInterpolateParamsDeep)), {
15
+ imports: h,
16
+ body: (t)=>{
17
+ let n = y ? "locale = locale || global." + GLOBAL_I18N_IDENTIFIER + "; " : "";
18
+ n += "return ";
19
+ let p = "";
20
+ return isPrimitive(r) ? p += f(r, t) : p += function(t, n, r, a) {
21
21
  let o = "";
22
- if (n) for(let n in r){
23
- let l = r[n];
24
- n === e || areEqual(l, r[e]) || (o += `locale === "${n}" ? ${f(l, a)} : `);
22
+ if (r) for(let r in n){
23
+ let i = n[r];
24
+ r === t || areEqual(i, n[t]) || (o += `locale === "${r}" ? ${f(i, a)} : `);
25
25
  }
26
- return o + f(r[e], a);
27
- }(m, n, y, e), l && (i = `tPluralise(${i}, params.${c})`), o && ("Primitive" === s ? i = `tInterpolateParams(${i}, params)` : ("Array" === s || "Object" === s) && (i = `tInterpolateParamsDeep(${i}, params)`)), r + i;
26
+ return o + f(n[t], a);
27
+ }(m, r, y, t), i && (p = `i18nPluralise(${p}, params.${c})`), o && ("Primitive" === l ? p = `i18nInterpolateParams(${p}, params)` : ("Array" === l || "Object" === l) && (p = `i18nInterpolateParamsDeep(${p}, params)`)), n + p;
28
28
  }
29
29
  };
30
- }(e, r), m = [], u = n ? o ? {
31
- ...n,
30
+ }(t, n), m = [], u = r ? o ? {
31
+ ...r,
32
32
  [c]: "number"
33
- } : n : o ? {
33
+ } : r : o ? {
34
34
  [c]: "number"
35
35
  } : null;
36
36
  return u && m.push({
@@ -45,50 +45,50 @@ function getTFunction(e, r) {
45
45
  description: "Use this to override the current locale",
46
46
  defaults: "current locale"
47
47
  }), {
48
- body: l,
48
+ body: i,
49
49
  args: m,
50
- imports: s
50
+ imports: l
51
51
  };
52
52
  }
53
- function f(t, { format: a }) {
54
- let l = "ts" === a ? " as const" : "";
55
- return isString(t) || isNumber(t) ? `"${t}"${l}` : isBoolean(t) ? `${t}` : isArray(t) ? JSON.stringify(t) + l : `(${JSON.stringify(t)}${l})`;
53
+ function f(e, { format: a }) {
54
+ let i = "ts" === a ? " as const" : "";
55
+ return isString(e) || isNumber(e) ? `"${e}"${i}` : isBoolean(e) ? `${e}` : isArray(e) ? JSON.stringify(e) + i : `(${JSON.stringify(e)}${i})`;
56
56
  }
57
57
  let d = {
58
58
  types: getImportTypes(),
59
- tInterpolateParams: new ImportsCompiler({
60
- path: "internal/tInterpolateParams",
59
+ i18nInterpolateParams: new ImportsCompiler({
60
+ path: "@koine/i18n",
61
61
  named: [
62
62
  {
63
- name: "tInterpolateParams"
63
+ name: "i18nInterpolateParams"
64
64
  }
65
65
  ]
66
66
  }),
67
- tInterpolateParamsDeep: new ImportsCompiler({
68
- path: "internal/tInterpolateParamsDeep",
67
+ i18nInterpolateParamsDeep: new ImportsCompiler({
68
+ path: "@koine/i18n",
69
69
  named: [
70
70
  {
71
- name: "tInterpolateParamsDeep"
71
+ name: "i18nInterpolateParamsDeep"
72
72
  }
73
73
  ]
74
74
  }),
75
- tPluralise: new ImportsCompiler({
76
- path: "internal/tPluralise",
75
+ i18nPluralise: new ImportsCompiler({
76
+ path: "@koine/i18n",
77
77
  named: [
78
78
  {
79
- name: "tPluralise"
79
+ name: "i18nPluralise"
80
80
  }
81
81
  ]
82
82
  })
83
- }, y = (t, e)=>{
84
- let r = [], n = new Set();
85
- for(let a in n.add(d.types), t){
86
- let o = t[a], { imports: l, args: p, body: i } = getTFunction(o, e);
87
- l.forEach((t)=>n.add(t)), r.push(new FunctionsCompiler({
88
- imports: l,
83
+ }, y = (e, t)=>{
84
+ let n = [], r = new Set();
85
+ for(let a in r.add(d.types), e){
86
+ let o = e[a], { imports: i, args: s, body: p } = getTFunction(o, t);
87
+ i.forEach((e)=>r.add(e)), n.push(new FunctionsCompiler({
88
+ imports: i,
89
89
  name: o.fnName,
90
- args: p,
91
- body: i,
90
+ args: s,
91
+ body: p,
92
92
  comment: {
93
93
  tags: [
94
94
  {
@@ -100,18 +100,18 @@ let d = {
100
100
  }));
101
101
  }
102
102
  return {
103
- functions: r,
104
- allImports: n
103
+ functions: n,
104
+ allImports: r
105
105
  };
106
106
  };
107
- var d$1 = createGenerator("js", (t)=>{
108
- let { config: e, options: { translations: r, adapter: { modularize: n } }, translations: a } = t, { dir: o } = r.functions, { functions: l, allImports: p } = y(a, e);
109
- return n ? l.reduce((t, e)=>(t[e.name] = {
107
+ var d$1 = createGenerator("js", (e)=>{
108
+ let { config: t, options: { translations: n, adapter: { modularize: r } }, translations: a } = e, { dir: o } = n.functions, { functions: i, allImports: s } = y(a, t);
109
+ return r ? i.reduce((e, t)=>(e[t.name] = {
110
110
  dir: o,
111
- name: e.name,
111
+ name: t.name,
112
112
  ext: "ts",
113
113
  index: !0,
114
- content: ()=>e.$out("ts", {
114
+ content: ()=>t.$out("ts", {
115
115
  imports: {
116
116
  folderUp: 1
117
117
  },
@@ -120,14 +120,12 @@ var d$1 = createGenerator("js", (t)=>{
120
120
  comments: !0,
121
121
  pure: !0
122
122
  })
123
- }, t), {}) : {
123
+ }, e), {}) : {
124
124
  $t: {
125
125
  name: o,
126
126
  ext: "ts",
127
127
  index: !1,
128
- content: ()=>"" + (ImportsCompiler.outMany("ts", p, {
129
- folderUp: 0
130
- }) + FunctionsCompiler.outMany("ts", l, {
128
+ content: ()=>"" + (ImportsCompiler.outMany("ts", s) + FunctionsCompiler.outMany("ts", i, {
131
129
  imports: !1,
132
130
  exports: "named",
133
131
  style: "function",
@@ -52,9 +52,7 @@ var u = createAdapter.createGenerator("js", (a)=>({
52
52
  index: !1,
53
53
  disabled: !0,
54
54
  content: ()=>tInterpolateParamsDeep().$out("ts", {
55
- imports: {
56
- folderUp: 0
57
- },
55
+ imports: {},
58
56
  exports: "named",
59
57
  style: "function"
60
58
  })
@@ -48,9 +48,7 @@ var u = createGenerator("js", (a)=>({
48
48
  index: !1,
49
49
  disabled: !0,
50
50
  content: ()=>tInterpolateParamsDeep().$out("ts", {
51
- imports: {
52
- folderUp: 0
53
- },
51
+ imports: {},
54
52
  exports: "named",
55
53
  style: "function"
56
54
  })
@@ -44,9 +44,7 @@ var T = createAdapter.createGenerator("js", (t)=>({
44
44
  ext: "ts",
45
45
  index: !1,
46
46
  content: ()=>tPluralise().$out("ts", {
47
- imports: {
48
- folderUp: 0
49
- },
47
+ imports: {},
50
48
  exports: "named"
51
49
  })
52
50
  }
@@ -40,9 +40,7 @@ var T = createGenerator("js", (t)=>({
40
40
  ext: "ts",
41
41
  index: !1,
42
42
  content: ()=>tPluralise().$out("ts", {
43
- imports: {
44
- folderUp: 0
45
- },
43
+ imports: {},
46
44
  exports: "named"
47
45
  })
48
46
  }
@@ -103,9 +103,7 @@ var I = createAdapter.createGenerator("js", (t)=>{
103
103
  name: i,
104
104
  ext: "ts",
105
105
  index: !1,
106
- content: ()=>"" + (imports.ImportsCompiler.outMany("ts", d, {
107
- folderUp: 0
108
- }) + functions.FunctionsCompiler.outMany("ts", u, {
106
+ content: ()=>"" + (imports.ImportsCompiler.outMany("ts", d) + functions.FunctionsCompiler.outMany("ts", u, {
109
107
  imports: !1,
110
108
  exports: "named",
111
109
  style: "function",
@@ -99,9 +99,7 @@ var I = createGenerator("js", (t)=>{
99
99
  name: i,
100
100
  ext: "ts",
101
101
  index: !1,
102
- content: ()=>"" + (ImportsCompiler.outMany("ts", d, {
103
- folderUp: 0
104
- }) + FunctionsCompiler.outMany("ts", u, {
102
+ content: ()=>"" + (ImportsCompiler.outMany("ts", d) + FunctionsCompiler.outMany("ts", u, {
105
103
  imports: !1,
106
104
  exports: "named",
107
105
  style: "function",
@@ -125,7 +125,7 @@ type FunctionsCompilerComment = {
125
125
  */
126
126
  tags?: FunctionsCompilerCommentTag[];
127
127
  };
128
- type FunctionsCompilerOutputOptions = {
128
+ export type FunctionsCompilerOutputOptions = {
129
129
  /**
130
130
  * Imports output options, pass `false` to do not output import' statements
131
131
  */
@@ -1,30 +1,31 @@
1
1
  'use strict';
2
2
 
3
3
  var minimatch = require('minimatch');
4
+ var imports = require('./imports.cjs.js');
4
5
 
5
6
  const GLOBAL_I18N_IDENTIFIER = "__i18n_locale";
6
- let getImportDots = (t = 0)=>(t ? Array(t).fill("..").join("/") : ".") + "/";
7
- let getTranslationsDir = (t = 0, e = "translations")=>getImportDots(t) + e;
8
- let compileDataParamsToType = (t)=>"{ " + Object.keys(t).reduce((e, r)=>{
9
- let a = t[r], o = "";
7
+ let getTranslationsDir = (e = 0, r = "translations")=>imports.ImportsCompiler.getDir({
8
+ folderUp: e
9
+ }) + r;
10
+ let compileDataParamsToType = (e)=>"{ " + Object.keys(e).reduce((t, r)=>{
11
+ let a = e[r], i = "";
10
12
  switch(a){
11
13
  case "number":
12
- o = "number";
14
+ i = "number";
13
15
  break;
14
16
  case "string":
15
- o = "string";
17
+ i = "string";
16
18
  break;
17
19
  default:
18
- o = "string | number";
20
+ i = "string | number";
19
21
  }
20
- return e.push(`${r}: ${o};`), e;
22
+ return t.push(`${r}: ${i};`), t;
21
23
  }, []).join(" ") + " }";
22
- let escapeEachChar = (t)=>t.split("").map((t)=>`\\${t}`).join("");
23
- let filterInputTranslationFiles = (e, r = [], a)=>e.filter((e)=>(!a || a && a(e)) && (!r.length || r.every((r)=>!minimatch.minimatch(e.path, r))));
24
+ let escapeEachChar = (e)=>e.split("").map((e)=>`\\${e}`).join("");
25
+ let filterInputTranslationFiles = (t, r = [], a)=>t.filter((t)=>(!a || a && a(t)) && (!r.length || r.every((r)=>!minimatch.minimatch(t.path, r))));
24
26
 
25
27
  exports.GLOBAL_I18N_IDENTIFIER = GLOBAL_I18N_IDENTIFIER;
26
28
  exports.compileDataParamsToType = compileDataParamsToType;
27
29
  exports.escapeEachChar = escapeEachChar;
28
30
  exports.filterInputTranslationFiles = filterInputTranslationFiles;
29
- exports.getImportDots = getImportDots;
30
31
  exports.getTranslationsDir = getTranslationsDir;
@@ -1,6 +1,5 @@
1
1
  import type { I18nCompiler } from "./types";
2
2
  export declare const GLOBAL_I18N_IDENTIFIER = "__i18n_locale";
3
- export declare let getImportDots: (folderUp?: number) => string;
4
3
  /**
5
4
  * TODO: maybe make this folder name or path configurable through options
6
5
  *
@@ -1,23 +1,25 @@
1
1
  import { minimatch } from 'minimatch';
2
+ import { ImportsCompiler } from './imports.esm.js';
2
3
 
3
4
  const GLOBAL_I18N_IDENTIFIER = "__i18n_locale";
4
- let getImportDots = (t = 0)=>(t ? Array(t).fill("..").join("/") : ".") + "/";
5
- let getTranslationsDir = (t = 0, e = "translations")=>getImportDots(t) + e;
6
- let compileDataParamsToType = (t)=>"{ " + Object.keys(t).reduce((e, r)=>{
7
- let a = t[r], o = "";
5
+ let getTranslationsDir = (e = 0, r = "translations")=>ImportsCompiler.getDir({
6
+ folderUp: e
7
+ }) + r;
8
+ let compileDataParamsToType = (e)=>"{ " + Object.keys(e).reduce((t, r)=>{
9
+ let a = e[r], i = "";
8
10
  switch(a){
9
11
  case "number":
10
- o = "number";
12
+ i = "number";
11
13
  break;
12
14
  case "string":
13
- o = "string";
15
+ i = "string";
14
16
  break;
15
17
  default:
16
- o = "string | number";
18
+ i = "string | number";
17
19
  }
18
- return e.push(`${r}: ${o};`), e;
20
+ return t.push(`${r}: ${i};`), t;
19
21
  }, []).join(" ") + " }";
20
- let escapeEachChar = (t)=>t.split("").map((t)=>`\\${t}`).join("");
21
- let filterInputTranslationFiles = (e, r = [], a)=>e.filter((e)=>(!a || a && a(e)) && (!r.length || r.every((r)=>!minimatch(e.path, r))));
22
+ let escapeEachChar = (e)=>e.split("").map((e)=>`\\${e}`).join("");
23
+ let filterInputTranslationFiles = (t, r = [], a)=>t.filter((t)=>(!a || a && a(t)) && (!r.length || r.every((r)=>!minimatch(t.path, r))));
22
24
 
23
- export { GLOBAL_I18N_IDENTIFIER, compileDataParamsToType, escapeEachChar, filterInputTranslationFiles, getImportDots, getTranslationsDir };
25
+ export { GLOBAL_I18N_IDENTIFIER, compileDataParamsToType, escapeEachChar, filterInputTranslationFiles, getTranslationsDir };
@@ -1,28 +1,69 @@
1
1
  'use strict';
2
2
 
3
- var helpers = require('./helpers.cjs.js');
4
-
5
3
  class ImportsCompiler {
6
4
  constructor(t){
7
5
  this.data = t;
8
6
  }
9
- static #t(r, e) {
10
- let { path: s, defaulT: o, named: n = [] } = r, { folderUp: i } = e, a = helpers.getImportDots(i), m = "import ";
11
- return o && (m += o, n.length && (m += ", ")), n.length && (m += "{ " + n.map(({ name: t, type: r })=>r ? "type " + t : t).join(", ") + " }"), m += ' from "' + a + s + '";';
7
+ static #t(t) {
8
+ let r = t.reduce((t, r)=>{
9
+ let { path: e } = r.data;
10
+ return t[e] = t[e] || [], t[e].push(r), t;
11
+ }, {});
12
+ return Object.keys(r).map((t)=>{
13
+ let e = r[t];
14
+ if (e.length > 1) {
15
+ let r = [], i = "";
16
+ return e.forEach((t)=>{
17
+ let { defaulT: e, named: s } = t.data;
18
+ if (s && (r = [
19
+ ...r,
20
+ ...s
21
+ ]), e) {
22
+ if (i) throw Error(`ImportsCompiler: Aggregated imports declare multiple default imports. Default import "${e}" conflicts with "${i}"`);
23
+ i = e;
24
+ }
25
+ }), new ImportsCompiler({
26
+ defaulT: i,
27
+ named: r,
28
+ path: t
29
+ });
30
+ }
31
+ return e[0];
32
+ });
33
+ }
34
+ static getDir(t) {
35
+ let { folderUp: r = 0, external: e } = t;
36
+ return e ? "" : (r ? Array(r).fill("..").join("/") : ".") + "/";
37
+ }
38
+ static #r(t) {
39
+ let { path: r, external: e } = t;
40
+ return "boolean" == typeof e ? e : r.startsWith("@");
41
+ }
42
+ static #e(t, r) {
43
+ let { path: e, defaulT: i, named: s = [] } = t, { folderUp: a } = r || {}, o = this.getDir({
44
+ folderUp: a,
45
+ external: this.#r(t)
46
+ }), n = "import ";
47
+ return i && (n += i, s.length && (n += ", ")), s.length && (n += "{ " + s.map(({ name: t, type: r })=>r ? "type " + t : t).join(", ") + " }"), n += ' from "' + o + e + '";';
12
48
  }
13
- static #r(r, e) {
14
- let { path: s, defaulT: o, named: n = [] } = r, { folderUp: i } = e, a = helpers.getImportDots(i), m = `= require("${a + s}")`, p = "";
15
- return o && (p += `const ${o} ${m};`), n.length && (o && (p += "\n"), p += `const { ${n.filter((t)=>!t.type).map((t)=>t.name).join(", ")} } ${m};`), p;
49
+ static #i(t, r) {
50
+ let { path: e, defaulT: i, named: s = [] } = t, { folderUp: a } = r || {}, o = this.getDir({
51
+ folderUp: a,
52
+ external: this.#r(t)
53
+ }), n = `= require("${o + e}")`, l = "";
54
+ return i && (l += `const ${i} ${n};`), s.length && (i && (l += "\n"), l += `const { ${s.filter((t)=>!t.type).map((t)=>t.name).join(", ")} } ${n};`), l;
16
55
  }
17
56
  $out(t, r) {
18
57
  return ImportsCompiler.out(this.data, t, r);
19
58
  }
20
59
  static out(t, r, e) {
21
- return "ts" === r ? this.#t(t, e) : "cjs" === r ? this.#r(t, e) : "";
60
+ if ("ts" === r) return this.#e(t, e);
61
+ if ("cjs" === r) return this.#i(t, e);
62
+ throw Error(`ImportsCompiler: Unsupported format '${r}'`);
22
63
  }
23
64
  static outMany(t, r, e) {
24
- let s = Array.from(r);
25
- return s.length ? s.map((r)=>this.out(r.data, t, e)).join("\n") + "\n\n" : "";
65
+ let i = this.#t(Array.from(r));
66
+ return i.length ? i.map((r)=>this.out(r.data, t, e)).join("\n") + "\n\n" : "";
26
67
  }
27
68
  }
28
69
 
@@ -3,6 +3,11 @@ export type ImportsCompilerData = {
3
3
  * `myPath` in `import type { MyType } from "../myPath"`;
4
4
  */
5
5
  path: string;
6
+ /**
7
+ * Flags an import from a thirdy part package. This is not needed when the path
8
+ * begins with a `@`, in the other cases we cannot infer this information.
9
+ */
10
+ external?: boolean;
6
11
  /**
7
12
  * `myImport` in `import myImport from "../myPath"`;
8
13
  * or in `import myImport, { myNamed } from "../myPath"`;
@@ -27,15 +32,19 @@ export type ImportsCompilerOutputOptions = {
27
32
  /**
28
33
  * Determines the `../` in `import type { MyType } from "../myPath"`;
29
34
  */
30
- folderUp: number;
35
+ folderUp?: number;
31
36
  };
32
37
  type ImportsCompilerOutputFormat = "ts" | "cjs";
33
38
  export declare class ImportsCompiler {
34
39
  #private;
35
40
  data: ImportsCompilerData;
36
41
  constructor(data: ImportsCompilerData);
37
- $out(format: ImportsCompilerOutputFormat, options: ImportsCompilerOutputOptions): string;
38
- static out(data: ImportsCompilerData, format: ImportsCompilerOutputFormat, options: ImportsCompilerOutputOptions): string;
39
- static outMany(format: ImportsCompilerOutputFormat, instances: ImportsCompiler[] | Set<ImportsCompiler>, options: ImportsCompilerOutputOptions): string;
42
+ static getDir(options: {
43
+ folderUp?: number;
44
+ external?: boolean;
45
+ }): string;
46
+ $out(format: ImportsCompilerOutputFormat, options?: ImportsCompilerOutputOptions): string;
47
+ static out(data: ImportsCompilerData, format: ImportsCompilerOutputFormat, options?: ImportsCompilerOutputOptions): string;
48
+ static outMany(format: ImportsCompilerOutputFormat, instances: ImportsCompiler[] | Set<ImportsCompiler>, options?: ImportsCompilerOutputOptions): string;
40
49
  }
41
50
  export {};
@@ -1,26 +1,67 @@
1
- import { getImportDots } from './helpers.esm.js';
2
-
3
1
  class ImportsCompiler {
4
2
  constructor(t){
5
3
  this.data = t;
6
4
  }
7
- static #t(r, e) {
8
- let { path: s, defaulT: o, named: n = [] } = r, { folderUp: i } = e, a = getImportDots(i), m = "import ";
9
- return o && (m += o, n.length && (m += ", ")), n.length && (m += "{ " + n.map(({ name: t, type: r })=>r ? "type " + t : t).join(", ") + " }"), m += ' from "' + a + s + '";';
5
+ static #t(t) {
6
+ let r = t.reduce((t, r)=>{
7
+ let { path: e } = r.data;
8
+ return t[e] = t[e] || [], t[e].push(r), t;
9
+ }, {});
10
+ return Object.keys(r).map((t)=>{
11
+ let e = r[t];
12
+ if (e.length > 1) {
13
+ let r = [], i = "";
14
+ return e.forEach((t)=>{
15
+ let { defaulT: e, named: s } = t.data;
16
+ if (s && (r = [
17
+ ...r,
18
+ ...s
19
+ ]), e) {
20
+ if (i) throw Error(`ImportsCompiler: Aggregated imports declare multiple default imports. Default import "${e}" conflicts with "${i}"`);
21
+ i = e;
22
+ }
23
+ }), new ImportsCompiler({
24
+ defaulT: i,
25
+ named: r,
26
+ path: t
27
+ });
28
+ }
29
+ return e[0];
30
+ });
31
+ }
32
+ static getDir(t) {
33
+ let { folderUp: r = 0, external: e } = t;
34
+ return e ? "" : (r ? Array(r).fill("..").join("/") : ".") + "/";
35
+ }
36
+ static #r(t) {
37
+ let { path: r, external: e } = t;
38
+ return "boolean" == typeof e ? e : r.startsWith("@");
39
+ }
40
+ static #e(t, r) {
41
+ let { path: e, defaulT: i, named: s = [] } = t, { folderUp: a } = r || {}, o = this.getDir({
42
+ folderUp: a,
43
+ external: this.#r(t)
44
+ }), n = "import ";
45
+ return i && (n += i, s.length && (n += ", ")), s.length && (n += "{ " + s.map(({ name: t, type: r })=>r ? "type " + t : t).join(", ") + " }"), n += ' from "' + o + e + '";';
10
46
  }
11
- static #r(r, e) {
12
- let { path: s, defaulT: o, named: n = [] } = r, { folderUp: i } = e, a = getImportDots(i), m = `= require("${a + s}")`, p = "";
13
- return o && (p += `const ${o} ${m};`), n.length && (o && (p += "\n"), p += `const { ${n.filter((t)=>!t.type).map((t)=>t.name).join(", ")} } ${m};`), p;
47
+ static #i(t, r) {
48
+ let { path: e, defaulT: i, named: s = [] } = t, { folderUp: a } = r || {}, o = this.getDir({
49
+ folderUp: a,
50
+ external: this.#r(t)
51
+ }), n = `= require("${o + e}")`, l = "";
52
+ return i && (l += `const ${i} ${n};`), s.length && (i && (l += "\n"), l += `const { ${s.filter((t)=>!t.type).map((t)=>t.name).join(", ")} } ${n};`), l;
14
53
  }
15
54
  $out(t, r) {
16
55
  return ImportsCompiler.out(this.data, t, r);
17
56
  }
18
57
  static out(t, r, e) {
19
- return "ts" === r ? this.#t(t, e) : "cjs" === r ? this.#r(t, e) : "";
58
+ if ("ts" === r) return this.#e(t, e);
59
+ if ("cjs" === r) return this.#i(t, e);
60
+ throw Error(`ImportsCompiler: Unsupported format '${r}'`);
20
61
  }
21
62
  static outMany(t, r, e) {
22
- let s = Array.from(r);
23
- return s.length ? s.map((r)=>this.out(r.data, t, e)).join("\n") + "\n\n" : "";
63
+ let i = this.#t(Array.from(r));
64
+ return i.length ? i.map((r)=>this.out(r.data, t, e)).join("\n") + "\n\n" : "";
24
65
  }
25
66
  }
26
67
 
package/package.json CHANGED
@@ -5,8 +5,8 @@
5
5
  "node": "^14.18.0 || >=16.0.0"
6
6
  },
7
7
  "dependencies": {
8
- "@koine/node": "2.0.0-beta.166",
9
- "@koine/utils": "2.0.0-beta.166",
8
+ "@koine/node": "2.0.0-beta.167",
9
+ "@koine/utils": "2.0.0-beta.167",
10
10
  "comment-json": "^4.2.4",
11
11
  "consola": "^3.2.3",
12
12
  "glob": "^11.0.0",
@@ -98,5 +98,5 @@
98
98
  "module": "./index.esm.js",
99
99
  "main": "./index.cjs.js",
100
100
  "types": "./index.esm.d.ts",
101
- "version": "2.0.0-beta.166"
101
+ "version": "2.0.0-beta.167"
102
102
  }