@akanjs/base 0.9.56 → 0.9.58-canary.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/cjs/src/base.js CHANGED
@@ -18,62 +18,61 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
18
18
  var base_exports = {};
19
19
  __export(base_exports, {
20
20
  DataList: () => DataList,
21
- Enum: () => Enum,
22
21
  enumOf: () => enumOf,
22
+ isEnum: () => isEnum,
23
23
  logo: () => logo
24
24
  });
25
25
  module.exports = __toCommonJS(base_exports);
26
26
  var import_scalar = require("./scalar");
27
- class Enum {
28
- constructor(values) {
29
- this.values = values;
30
- this.valueMap = new Map(values.map((value, idx) => [value, idx]));
31
- const firstValue = values.at(0);
32
- if (firstValue === void 0)
33
- throw new Error("Enum values are empty");
34
- if (typeof firstValue === "string")
35
- this.type = String;
36
- else if (values.every((value) => value % 1 === 0))
37
- this.type = import_scalar.Int;
38
- else
39
- this.type = import_scalar.Float;
40
- }
41
- value;
42
- // for type
43
- valueMap;
44
- type;
45
- has(value) {
46
- return this.valueMap.has(value);
47
- }
48
- indexOf(value) {
49
- const idx = this.valueMap.get(value);
50
- if (idx === void 0)
51
- throw new Error(`Value ${value} is not in enum`);
52
- return idx;
53
- }
54
- find(callback) {
55
- const val = this.values.find(callback);
56
- if (val === void 0)
57
- throw new Error(`Value not found in enum`);
58
- return val;
59
- }
60
- findIndex(callback) {
61
- const idx = this.values.findIndex(callback);
62
- if (idx === -1)
63
- throw new Error(`Value not found in enum`);
64
- return idx;
65
- }
66
- filter(callback) {
67
- return this.values.filter(callback);
68
- }
69
- map(callback) {
70
- return this.values.map(callback);
71
- }
72
- forEach(callback) {
73
- this.values.forEach(callback);
74
- }
27
+ class EnumPrototype {
75
28
  }
76
- const enumOf = (values) => new Enum(values);
29
+ const isEnum = (enumRef) => Object.getPrototypeOf(Object.getPrototypeOf(enumRef) ?? {}) === EnumPrototype;
30
+ const enumOf = (refName, values) => {
31
+ const valueMap = new Map(values.map((value, idx) => [value, idx]));
32
+ const firstValue = values.at(0);
33
+ if (firstValue === void 0)
34
+ throw new Error("Enum values are empty");
35
+ const type = typeof firstValue === "string" ? String : values.every((value) => value % 1 === 0) ? import_scalar.Int : import_scalar.Float;
36
+ class Enum extends EnumPrototype {
37
+ static values = values;
38
+ static valueMap = valueMap;
39
+ static type = type;
40
+ static has(value) {
41
+ return this.valueMap.has(value);
42
+ }
43
+ static indexOf(value) {
44
+ const idx = this.valueMap.get(value);
45
+ if (idx === void 0)
46
+ throw new Error(`Value ${value} is not in enum`);
47
+ return idx;
48
+ }
49
+ static find(callback) {
50
+ const val = this.values.find(callback);
51
+ if (val === void 0)
52
+ throw new Error(`Value not found in enum`);
53
+ return val;
54
+ }
55
+ static findIndex(callback) {
56
+ const idx = this.values.findIndex(callback);
57
+ if (idx === -1)
58
+ throw new Error(`Value not found in enum`);
59
+ return idx;
60
+ }
61
+ static filter(callback) {
62
+ return this.values.filter(callback);
63
+ }
64
+ static map(callback) {
65
+ return this.values.map(callback);
66
+ }
67
+ static forEach(callback) {
68
+ this.values.forEach(callback);
69
+ }
70
+ static refName = refName;
71
+ refName = refName;
72
+ value;
73
+ }
74
+ return Enum;
75
+ };
77
76
  class DataList {
78
77
  // [immerable] = true;
79
78
  #idMap;
package/cjs/src/scalar.js CHANGED
@@ -27,6 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
28
  var scalar_exports = {};
29
29
  __export(scalar_exports, {
30
+ BaseInsight: () => BaseInsight,
30
31
  BaseObject: () => BaseObject,
31
32
  Dayjs: () => import_dayjs.Dayjs,
32
33
  Float: () => Float,
@@ -58,6 +59,9 @@ class BaseObject {
58
59
  updatedAt;
59
60
  removedAt;
60
61
  }
62
+ class BaseInsight {
63
+ count;
64
+ }
61
65
  class Int {
62
66
  __Scalar__;
63
67
  }
package/esm/src/base.js CHANGED
@@ -1,54 +1,53 @@
1
1
  import { Float, Int } from "./scalar";
2
- class Enum {
3
- constructor(values) {
4
- this.values = values;
5
- this.valueMap = new Map(values.map((value, idx) => [value, idx]));
6
- const firstValue = values.at(0);
7
- if (firstValue === void 0)
8
- throw new Error("Enum values are empty");
9
- if (typeof firstValue === "string")
10
- this.type = String;
11
- else if (values.every((value) => value % 1 === 0))
12
- this.type = Int;
13
- else
14
- this.type = Float;
15
- }
16
- value;
17
- // for type
18
- valueMap;
19
- type;
20
- has(value) {
21
- return this.valueMap.has(value);
22
- }
23
- indexOf(value) {
24
- const idx = this.valueMap.get(value);
25
- if (idx === void 0)
26
- throw new Error(`Value ${value} is not in enum`);
27
- return idx;
28
- }
29
- find(callback) {
30
- const val = this.values.find(callback);
31
- if (val === void 0)
32
- throw new Error(`Value not found in enum`);
33
- return val;
34
- }
35
- findIndex(callback) {
36
- const idx = this.values.findIndex(callback);
37
- if (idx === -1)
38
- throw new Error(`Value not found in enum`);
39
- return idx;
40
- }
41
- filter(callback) {
42
- return this.values.filter(callback);
43
- }
44
- map(callback) {
45
- return this.values.map(callback);
46
- }
47
- forEach(callback) {
48
- this.values.forEach(callback);
49
- }
2
+ class EnumPrototype {
50
3
  }
51
- const enumOf = (values) => new Enum(values);
4
+ const isEnum = (enumRef) => Object.getPrototypeOf(Object.getPrototypeOf(enumRef) ?? {}) === EnumPrototype;
5
+ const enumOf = (refName, values) => {
6
+ const valueMap = new Map(values.map((value, idx) => [value, idx]));
7
+ const firstValue = values.at(0);
8
+ if (firstValue === void 0)
9
+ throw new Error("Enum values are empty");
10
+ const type = typeof firstValue === "string" ? String : values.every((value) => value % 1 === 0) ? Int : Float;
11
+ class Enum extends EnumPrototype {
12
+ static values = values;
13
+ static valueMap = valueMap;
14
+ static type = type;
15
+ static has(value) {
16
+ return this.valueMap.has(value);
17
+ }
18
+ static indexOf(value) {
19
+ const idx = this.valueMap.get(value);
20
+ if (idx === void 0)
21
+ throw new Error(`Value ${value} is not in enum`);
22
+ return idx;
23
+ }
24
+ static find(callback) {
25
+ const val = this.values.find(callback);
26
+ if (val === void 0)
27
+ throw new Error(`Value not found in enum`);
28
+ return val;
29
+ }
30
+ static findIndex(callback) {
31
+ const idx = this.values.findIndex(callback);
32
+ if (idx === -1)
33
+ throw new Error(`Value not found in enum`);
34
+ return idx;
35
+ }
36
+ static filter(callback) {
37
+ return this.values.filter(callback);
38
+ }
39
+ static map(callback) {
40
+ return this.values.map(callback);
41
+ }
42
+ static forEach(callback) {
43
+ this.values.forEach(callback);
44
+ }
45
+ static refName = refName;
46
+ refName = refName;
47
+ value;
48
+ }
49
+ return Enum;
50
+ };
52
51
  class DataList {
53
52
  // [immerable] = true;
54
53
  #idMap;
@@ -162,7 +161,7 @@ const logo = `
162
161
  `;
163
162
  export {
164
163
  DataList,
165
- Enum,
166
164
  enumOf,
165
+ isEnum,
167
166
  logo
168
167
  };
package/esm/src/scalar.js CHANGED
@@ -6,6 +6,9 @@ class BaseObject {
6
6
  updatedAt;
7
7
  removedAt;
8
8
  }
9
+ class BaseInsight {
10
+ count;
11
+ }
9
12
  class Int {
10
13
  __Scalar__;
11
14
  }
@@ -93,6 +96,7 @@ const isGqlClass = (modelRef) => !scalarSet.has(modelRef);
93
96
  const isGqlScalar = (modelRef) => scalarSet.has(modelRef);
94
97
  const isGqlMap = (modelRef) => modelRef === Map;
95
98
  export {
99
+ BaseInsight,
96
100
  BaseObject,
97
101
  Dayjs,
98
102
  Float,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@akanjs/base",
3
- "version": "0.9.56",
3
+ "version": "0.9.58-canary.0",
4
4
  "sourceType": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/src/base.d.ts CHANGED
@@ -1,21 +1,23 @@
1
+ import { Type } from ".";
1
2
  import { Float, Int } from "./scalar";
2
- export declare class Enum<T = string | number> {
3
- readonly values: readonly T[];
4
- readonly value: T;
5
- readonly valueMap: Map<T, number>;
6
- readonly type: StringConstructor | typeof Int | typeof Float;
7
- constructor(values: readonly T[]);
8
- has(value: T): boolean;
9
- indexOf(value: T): number;
10
- find(callback: (value: T, index: number, array: T[]) => boolean): T;
11
- findIndex(callback: (value: T, index: number, array: T[]) => boolean): number;
12
- filter(callback: (value: T, index: number, array: T[]) => boolean): T[];
13
- map<R>(callback: (value: T, index: number, array: T[]) => R): R[];
14
- forEach(callback: (value: T, index: number, array: T[]) => void): void;
15
- }
16
- export declare const enumOf: <V>(values: readonly V[]) => Enum<V>;
17
- export type enumOf<E> = E extends Enum<infer T> ? T : never;
18
- export type EnumType<E> = E extends Enum<infer T> ? T : never;
3
+ export type EnumInstance<RefName extends string = string, T = string | number> = Type<{
4
+ refName: RefName;
5
+ value: T;
6
+ }, {
7
+ refName: RefName;
8
+ type: StringConstructor | typeof Int | typeof Float;
9
+ values: readonly T[];
10
+ valueMap: Map<T, number>;
11
+ has: (value: T) => boolean;
12
+ indexOf: (value: T) => number;
13
+ find: (callback: (value: T, index: number, array: T[]) => boolean) => T;
14
+ findIndex: (callback: (value: T, index: number, array: T[]) => boolean) => number;
15
+ filter: (callback: (value: T, index: number, array: T[]) => boolean) => T[];
16
+ map: <R>(callback: (value: T, index: number, array: T[]) => R) => R[];
17
+ forEach: (callback: (value: T, index: number, array: T[]) => void) => void;
18
+ }>;
19
+ export declare const isEnum: (enumRef: Type) => boolean;
20
+ export declare const enumOf: <RefName extends string, T = string | number>(refName: RefName, values: readonly T[]) => EnumInstance<RefName, T>;
19
21
  export declare class DataList<Light extends {
20
22
  id: string;
21
23
  }> {
package/src/scalar.d.ts CHANGED
@@ -10,6 +10,9 @@ export declare class BaseObject {
10
10
  updatedAt: Dayjs;
11
11
  removedAt: Dayjs | null;
12
12
  }
13
+ export declare class BaseInsight {
14
+ count: number;
15
+ }
13
16
  export declare class Int {
14
17
  __Scalar__: "int";
15
18
  }
package/src/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Dayjs } from "dayjs";
2
2
  export type { SshOptions } from "tunnel-ssh";
3
- export type Type<T = any> = new (...args: any[]) => T;
3
+ export type Type<T = any, Statics = unknown> = (new (...args: any[]) => T) & Statics;
4
4
  export type BufferLike = string | Buffer | DataView | number | ArrayBufferView | Uint8Array | ArrayBuffer | SharedArrayBuffer | readonly any[] | readonly number[];
5
5
  export type GetObject<T> = {
6
6
  [K in keyof T as K extends "prototype" ? never : K]: T[K];
@@ -52,6 +52,11 @@ export type PromiseOrObject<T> = T | Promise<T>;
52
52
  type MergeObjectValues<T> = T extends Record<string, infer V> ? (V extends Record<string, any> ? V : never) : never;
53
53
  type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
54
54
  export type MergedValues<T> = UnionToIntersection<MergeObjectValues<T>>;
55
+ export type Assign<A, B> = Omit<A, keyof B> & B;
56
+ export type ObjectAssign<Objects extends any[]> = Objects extends [
57
+ ...infer Rest extends Record<string, any>[],
58
+ infer Last extends Record<string, any>
59
+ ] ? Rest extends [] ? Last : Assign<ObjectAssign<Rest>, Last> : unknown;
55
60
  export type MergeAll<T extends Record<string, any>[]> = T extends [
56
61
  infer First extends Record<string, any>,
57
62
  ...infer Rest extends Record<string, any>[]
@@ -87,3 +92,9 @@ export type NestedKeysWithAllowed<T, Allowed = Primitive> = T extends Primitive
87
92
  } ? K | `${K}.${NestedKeysWithAllowed<T[K], Allowed>}` : never;
88
93
  }[keyof T & string];
89
94
  export type SingleValue<Value> = Value extends (infer V)[] ? SingleValue<V> : Value;
95
+ export type StrArrToObject<Arr, Value> = Arr extends [infer First, ...infer Rest] ? {
96
+ [K in First & string]: Value;
97
+ } & StrArrToObject<Rest, Value> : unknown;
98
+ export type GetValueOfKey<Model, Key extends string, Fallback = never> = Model extends {
99
+ [key in Key]: any;
100
+ } ? Model[Key] : Fallback;