pwi-plata-type 0.4.73 → 0.4.74

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.
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PlataRuntimeE = void 0;
4
+ var PlataRuntimeE;
5
+ (function (PlataRuntimeE) {
6
+ PlataRuntimeE.switchUnion = (value, cases) => {
7
+ const f = cases[value];
8
+ if (f === undefined)
9
+ return cases['_default'](value);
10
+ return f(value);
11
+ };
12
+ PlataRuntimeE.switchUnionObject = (v, chave, cases) => {
13
+ if (v === undefined || v === null)
14
+ return cases['_default'](v);
15
+ const f = cases[v[chave]];
16
+ if (f === undefined)
17
+ return cases['_default'](v);
18
+ return f(v);
19
+ };
20
+ })(PlataRuntimeE = exports.PlataRuntimeE || (exports.PlataRuntimeE = {}));
21
+ //# sourceMappingURL=switch.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"switch.js","sourceRoot":"","sources":["../../../src/bin/runtime/switch.ts"],"names":[],"mappings":";;;AAoCA,IAAiB,aAAa,CAwB7B;AAxBD,WAAiB,aAAa;IACb,yBAAW,GAAwB,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;QAC7D,MAAM,CAAC,GAAG,KAAK,CAAC,KAAK,CAAQ,CAAA;QAE7B,IAAI,CAAC,KAAK,SAAS;YACf,OAAO,KAAK,CAAC,UAAU,CAAC,CAAC,KAAY,CAAQ,CAChD;QAED,OAAO,CAAC,CAAC,KAAY,CAAQ,CAAA;IACjC,CAAC,CAAA;IAEY,+BAAiB,GAA8B,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE;QAC5E,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,IAAI;YAC7B,OAAO,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAQ,CACrC;QAED,MAAM,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAA;QAEzB,IAAI,CAAC,KAAK,SAAS;YACf,OAAO,KAAK,CAAC,UAAU,CAAC,CAAC,CAAC,CAAQ,CACrC;QAED,OAAO,CAAC,CAAC,CAAC,CAAQ,CAAA;IACtB,CAAC,CAAA;AACL,CAAC,EAxBgB,aAAa,GAAb,qBAAa,KAAb,qBAAa,QAwB7B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pwi-plata-type",
3
- "version": "0.4.73",
3
+ "version": "0.4.74",
4
4
  "main": "__BUILD__/index",
5
5
  "types": "src/@types/exported.d.ts",
6
6
  "bin": {
@@ -67,6 +67,10 @@ type Dictionary<T> = {
67
67
  [key: string]: T
68
68
  }
69
69
 
70
+ type DictionaryRecord<K extends string | number, T> = {
71
+ [key in K]: T
72
+ }
73
+
70
74
  interface PlataEnvConfig {
71
75
  env: PlataEnv
72
76
  }
@@ -3,4 +3,5 @@
3
3
  /// <reference path="promises.ts" />
4
4
  /// <reference path="require.ts" />
5
5
  /// <reference path="fs.ts" />
6
- /// <reference path="sql.ts" />
6
+ /// <reference path="sql.ts" />
7
+ /// <reference path="switch.ts" />
@@ -0,0 +1,61 @@
1
+ declare global {
2
+ interface PlataRuntime extends PlataRuntimeEType {}
3
+ }
4
+
5
+ type PlataRuntimeEType = typeof PlataRuntimeE
6
+
7
+ type SwitchUnionObjectFunctionCases<V extends DictionaryRecord<string | number, any>, K extends keyof V> = {
8
+ [type in V[K] | '_default']:
9
+ type extends '_default' ? (v: V) => Promise<unknown>
10
+ : V extends { [P in K]: type } ? (v: V) => Promise<unknown>
11
+ : never
12
+ }
13
+
14
+ type SwitchUnionFunctionCases<V extends string | number> = {
15
+ [T in (V | '_default')]: T extends V ?
16
+ (v: T) => Promise<unknown>
17
+ : (v: unknown) => Promise<unknown>
18
+ }
19
+
20
+ type SwitchUnionFunction =
21
+ <V extends string | number, C extends SwitchUnionFunctionCases<V>>
22
+ (
23
+ values: V,
24
+ cases: C,
25
+ ) => PlataPromise<Awaited<ReturnType<C[V | '_default']>>>
26
+ ;
27
+
28
+ type SwitchUnionObjectFunction =
29
+ <V extends DictionaryRecord<string, any>, K extends keyof V, C extends SwitchUnionObjectFunctionCases<V, K> = SwitchUnionObjectFunctionCases<V, K>>
30
+ (
31
+ object: V,
32
+ chave: K,
33
+ cases: C
34
+ ) => PlataPromise<Awaited<ReturnType<C[V[K] | '_default']>>>
35
+ ;
36
+
37
+ export namespace PlataRuntimeE {
38
+ export const switchUnion: SwitchUnionFunction = (value, cases) => {
39
+ const f = cases[value] as any
40
+
41
+ if (f === undefined)
42
+ return cases['_default'](value as any) as any
43
+ ;
44
+
45
+ return f(value as any) as any
46
+ }
47
+
48
+ export const switchUnionObject: SwitchUnionObjectFunction = (v, chave, cases) => {
49
+ if (v === undefined || v === null)
50
+ return cases['_default'](v) as any
51
+ ;
52
+
53
+ const f = cases[v[chave]]
54
+
55
+ if (f === undefined)
56
+ return cases['_default'](v) as any
57
+ ;
58
+
59
+ return f(v) as any
60
+ }
61
+ }
@@ -0,0 +1,77 @@
1
+ import { describe, it, before } from "node:test"
2
+ import assert from "node:assert"
3
+ import { PlataModels } from ".."
4
+ import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
5
+
6
+ describe('Plata Switch', () => {
7
+ const plataPromise = (async () => {
8
+ const g = global as any
9
+
10
+ g.Plata = await loadRuntimeLibs()
11
+ })()
12
+
13
+ type TestUnion = 'Valor1' | 'Valor2'
14
+
15
+ type TestUnionObject = {
16
+ tipo: 'Tipo1'
17
+ valor: number
18
+ } | {
19
+ tipo: 'Tipo2'
20
+ valor: string
21
+ }
22
+
23
+ before(async () => {
24
+ await plataPromise
25
+ })
26
+
27
+ it('switch union (okay)', async () => {
28
+
29
+ const union = Math.ceil(Math.random() * 100) % 2 === 0 ? 'Valor1' : 'Valor2'
30
+
31
+ const result = await Plata.switchUnion(union, {
32
+ _default: async () => false,
33
+ Valor1: async () => true,
34
+ Valor2: async () => true,
35
+ })
36
+
37
+ assert.strictEqual(result, true)
38
+ })
39
+
40
+ it('switch union (nao okay)', async () => {
41
+ const result = await Plata.switchUnion('Valor3' as TestUnion, {
42
+ _default: async () => false,
43
+ Valor1: async () => true,
44
+ Valor2: async () => true,
45
+ })
46
+
47
+ assert.strictEqual(result, false)
48
+ })
49
+
50
+ it('switch union object (okay)', async () => {
51
+ const union: TestUnionObject = Math.ceil(Math.random() * 100) % 2 === 0 ? {
52
+ tipo: 'Tipo1',
53
+ valor: 1,
54
+ } : {
55
+ tipo: 'Tipo2',
56
+ valor: '',
57
+ }
58
+
59
+ const result = await Plata.switchUnionObject(union, 'tipo', {
60
+ _default: async (v) => false,
61
+ Tipo1: async (v) => v.valor === 1,
62
+ Tipo2: async (v) => v.valor === '',
63
+ })
64
+
65
+ assert.strictEqual(result, true)
66
+ })
67
+
68
+ it('switch union object (não okay)', async () => {
69
+ const result = await Plata.switchUnionObject({} as any, 'tipo', {
70
+ _default: async (v) => true,
71
+ Tipo1: async (v) => false,
72
+ Tipo2: async (v) => false,
73
+ })
74
+
75
+ assert.strictEqual(result, true)
76
+ })
77
+ })