@akanjs/common 0.0.52 → 0.0.54

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/Logger.js ADDED
@@ -0,0 +1,141 @@
1
+ import dayjs from "dayjs";
2
+ const logLevels = ["trace", "verbose", "debug", "log", "info", "warn", "error"];
3
+ const clc = {
4
+ bold: (text) => `\x1B[1m${text}\x1B[0m`,
5
+ green: (text) => `\x1B[32m${text}\x1B[39m`,
6
+ yellow: (text) => `\x1B[33m${text}\x1B[39m`,
7
+ red: (text) => `\x1B[31m${text}\x1B[39m`,
8
+ magentaBright: (text) => `\x1B[95m${text}\x1B[39m`,
9
+ cyanBright: (text) => `\x1B[96m${text}\x1B[39m`
10
+ };
11
+ const colorizeMap = {
12
+ trace: clc.bold,
13
+ verbose: clc.cyanBright,
14
+ debug: clc.magentaBright,
15
+ log: clc.green,
16
+ info: clc.green,
17
+ warn: clc.yellow,
18
+ error: clc.red
19
+ };
20
+ class Logger {
21
+ static #ignoreCtxSet = /* @__PURE__ */ new Set([
22
+ "InstanceLoader",
23
+ "RoutesResolver",
24
+ "RouterExplorer",
25
+ "NestFactory",
26
+ "WebSocketsController",
27
+ "GraphQLModule",
28
+ "NestApplication"
29
+ ]);
30
+ static level = process.env.NEXT_PUBLIC_LOG_LEVEL ?? "log";
31
+ static #levelIdx = logLevels.findIndex((l) => l === process.env.NEXT_PUBLIC_LOG_LEVEL);
32
+ static #startAt = dayjs();
33
+ static setLevel(level) {
34
+ this.level = level;
35
+ this.#levelIdx = logLevels.findIndex((l) => l === level);
36
+ }
37
+ name;
38
+ constructor(name) {
39
+ this.name = name;
40
+ }
41
+ trace(msg, context = "") {
42
+ if (Logger.#levelIdx <= 0)
43
+ Logger.#printMessages(this.name ?? "App", msg, context, "trace");
44
+ }
45
+ verbose(msg, context = "") {
46
+ if (Logger.#levelIdx <= 1)
47
+ Logger.#printMessages(this.name ?? "App", msg, context, "verbose");
48
+ }
49
+ debug(msg, context = "") {
50
+ if (Logger.#levelIdx <= 2)
51
+ Logger.#printMessages(this.name ?? "App", msg, context, "debug");
52
+ }
53
+ log(msg, context = "") {
54
+ if (Logger.#levelIdx <= 3)
55
+ Logger.#printMessages(this.name ?? "App", msg, context, "log");
56
+ }
57
+ info(msg, context = "") {
58
+ if (Logger.#levelIdx <= 4)
59
+ Logger.#printMessages(this.name ?? "App", msg, context, "info");
60
+ }
61
+ warn(msg, context = "") {
62
+ if (Logger.#levelIdx <= 5)
63
+ Logger.#printMessages(this.name ?? "App", msg, context, "warn");
64
+ }
65
+ error(msg, context = "") {
66
+ if (Logger.#levelIdx <= 6)
67
+ Logger.#printMessages(this.name ?? "App", msg, context, "error");
68
+ }
69
+ raw(msg, method) {
70
+ Logger.rawLog(msg, method);
71
+ }
72
+ rawLog(msg, method) {
73
+ Logger.rawLog(msg, method);
74
+ }
75
+ static trace(msg, context = "") {
76
+ if (Logger.#levelIdx <= 0)
77
+ Logger.#printMessages("App", msg, context, "trace");
78
+ }
79
+ static verbose(msg, context = "") {
80
+ if (Logger.#levelIdx <= 1)
81
+ Logger.#printMessages("App", msg, context, "verbose");
82
+ }
83
+ static debug(msg, context = "") {
84
+ if (Logger.#levelIdx <= 2)
85
+ Logger.#printMessages("App", msg, context, "debug");
86
+ }
87
+ static log(msg, context = "") {
88
+ if (Logger.#levelIdx <= 3)
89
+ Logger.#printMessages("App", msg, context, "log");
90
+ }
91
+ static info(msg, context = "") {
92
+ if (Logger.#levelIdx <= 4)
93
+ Logger.#printMessages("App", msg, context, "info");
94
+ }
95
+ static warn(msg, context = "") {
96
+ if (Logger.#levelIdx <= 5)
97
+ Logger.#printMessages("App", msg, context, "warn");
98
+ }
99
+ static error(msg, context = "") {
100
+ if (Logger.#levelIdx <= 6)
101
+ Logger.#printMessages("App", msg, context, "error");
102
+ }
103
+ static #colorize(msg, logLevel) {
104
+ return colorizeMap[logLevel](msg);
105
+ }
106
+ static #printMessages(name, content, context, logLevel, writeStreamType = logLevel === "error" ? "stderr" : "stdout") {
107
+ if (this.#ignoreCtxSet.has(context))
108
+ return;
109
+ const now = dayjs();
110
+ const processMsg = this.#colorize(
111
+ `[${name ?? "App"}] ${global.process?.pid ?? "window"} -`,
112
+ logLevel
113
+ );
114
+ const timestampMsg = now.format("MM/DD/YYYY, HH:mm:ss A");
115
+ const logLevelMsg = this.#colorize(logLevel.toUpperCase().padStart(7, " "), logLevel);
116
+ const contextMsg = context ? clc.yellow(`[${context}] `) : "";
117
+ const contentMsg = this.#colorize(content, logLevel);
118
+ const timeDiffMsg = clc.yellow(`+${now.diff(Logger.#startAt, "ms")}ms`);
119
+ if (typeof window === "undefined")
120
+ process[writeStreamType].write(
121
+ `${processMsg} ${timestampMsg} ${logLevelMsg} ${contextMsg} ${contentMsg} ${timeDiffMsg}
122
+ `
123
+ );
124
+ else
125
+ console.log(`${processMsg} ${timestampMsg} ${logLevelMsg} ${contextMsg} ${contentMsg} ${timeDiffMsg}
126
+ `);
127
+ }
128
+ static rawLog(msg, method) {
129
+ this.raw(`${msg}
130
+ `, method);
131
+ }
132
+ static raw(msg, method) {
133
+ if (typeof window === "undefined" && method !== "console" && global.process)
134
+ global.process.stdout.write(msg);
135
+ else
136
+ console.log(msg);
137
+ }
138
+ }
139
+ export {
140
+ Logger
141
+ };
package/applyMixins.js ADDED
@@ -0,0 +1,23 @@
1
+ const getAllPropertyDescriptors = (objRef) => {
2
+ const descriptors = {};
3
+ let current = objRef.prototype;
4
+ while (current) {
5
+ Object.getOwnPropertyNames(current).forEach((name) => {
6
+ descriptors[name] ??= Object.getOwnPropertyDescriptor(current, name);
7
+ });
8
+ current = Object.getPrototypeOf(current);
9
+ }
10
+ return descriptors;
11
+ };
12
+ const applyMixins = (derivedCtor, constructors, avoidKeys) => {
13
+ constructors.forEach((baseCtor) => {
14
+ Object.entries(getAllPropertyDescriptors(baseCtor)).forEach(([name, descriptor]) => {
15
+ if (name === "constructor" || avoidKeys?.has(name))
16
+ return;
17
+ Object.defineProperty(derivedCtor.prototype, name, { ...descriptor, configurable: true });
18
+ });
19
+ });
20
+ };
21
+ export {
22
+ applyMixins
23
+ };
@@ -1,3 +1,6 @@
1
- export const capitalize = (str: string) => {
1
+ const capitalize = (str) => {
2
2
  return str.charAt(0).toUpperCase() + str.slice(1);
3
3
  };
4
+ export {
5
+ capitalize
6
+ };
@@ -0,0 +1,30 @@
1
+ import { isDayjs } from "./isDayjs";
2
+ const deepObjectify = (obj, option = {}) => {
3
+ if (isDayjs(obj) || obj?.constructor === Date) {
4
+ if (!option.serializable && !option.convertDate)
5
+ return obj;
6
+ if (option.convertDate === "string")
7
+ return obj.toISOString();
8
+ else if (option.convertDate === "number")
9
+ return isDayjs(obj) ? obj.toDate().getTime() : obj.getTime();
10
+ else
11
+ return isDayjs(obj) ? obj.toDate() : obj;
12
+ } else if (Array.isArray(obj)) {
13
+ return obj.map((o) => deepObjectify(o, option));
14
+ } else if (obj && typeof obj === "object") {
15
+ const val = {};
16
+ Object.keys(obj).forEach((key) => {
17
+ const fieldValue = obj[key];
18
+ if (fieldValue?.__ModelType__ && !option.serializable)
19
+ val[key] = fieldValue;
20
+ else if (typeof obj[key] !== "function")
21
+ val[key] = deepObjectify(fieldValue, option);
22
+ });
23
+ return val;
24
+ } else {
25
+ return obj;
26
+ }
27
+ };
28
+ export {
29
+ deepObjectify
30
+ };
package/index.js ADDED
@@ -0,0 +1,36 @@
1
+ import { splitVersion } from "./splitVersion";
2
+ import { deepObjectify } from "./deepObjectify";
3
+ import { pathGet } from "./pathGet";
4
+ import { isQueryEqual } from "./isQueryEqual";
5
+ import { isValidDate } from "./isValidDate";
6
+ import { objectify } from "./objectify";
7
+ import { randomPick } from "./randomPick";
8
+ import { randomPicks } from "./randomPicks";
9
+ import { pathSet } from "./pathSet";
10
+ import { isDayjs } from "./isDayjs";
11
+ import { mergeVersion } from "./mergeVersion";
12
+ import { pluralize } from "./pluralize";
13
+ import { applyMixins } from "./applyMixins";
14
+ import { capitalize } from "./capitalize";
15
+ import { Logger } from "./Logger";
16
+ import { lowerlize } from "./lowerlize";
17
+ import { sleep } from "./sleep";
18
+ export {
19
+ Logger,
20
+ applyMixins,
21
+ capitalize,
22
+ deepObjectify,
23
+ isDayjs,
24
+ isQueryEqual,
25
+ isValidDate,
26
+ lowerlize,
27
+ mergeVersion,
28
+ objectify,
29
+ pathGet,
30
+ pathSet,
31
+ pluralize,
32
+ randomPick,
33
+ randomPicks,
34
+ sleep,
35
+ splitVersion
36
+ };
@@ -1,3 +1,4 @@
1
1
  import { isDayjs } from "dayjs";
2
-
3
- export { isDayjs };
2
+ export {
3
+ isDayjs
4
+ };
@@ -0,0 +1,30 @@
1
+ import dayjs from "dayjs";
2
+ import { isDayjs } from "./isDayjs";
3
+ const isQueryEqual = (value1, value2) => {
4
+ if (value1 === value2)
5
+ return true;
6
+ if (Array.isArray(value1) && Array.isArray(value2)) {
7
+ if (value1.length !== value2.length)
8
+ return false;
9
+ for (let i = 0; i < value1.length; i++)
10
+ if (!isQueryEqual(value1[i], value2[i]))
11
+ return false;
12
+ return true;
13
+ }
14
+ if ([value1, value2].some((val) => val instanceof Date || isDayjs(val)))
15
+ return dayjs(value1).isSame(dayjs(value2));
16
+ if (typeof value1 === "object" && typeof value2 === "object") {
17
+ if (value1 === null || value2 === null)
18
+ return value1 === value2;
19
+ if (Object.keys(value1).length !== Object.keys(value2).length)
20
+ return false;
21
+ for (const key of Object.keys(value1))
22
+ if (!isQueryEqual(value1[key], value2[key]))
23
+ return false;
24
+ return true;
25
+ }
26
+ return false;
27
+ };
28
+ export {
29
+ isQueryEqual
30
+ };
package/isValidDate.js ADDED
@@ -0,0 +1,16 @@
1
+ import dayjs from "dayjs";
2
+ import customParseFormat from "dayjs/plugin/customParseFormat";
3
+ import { isDayjs } from "./isDayjs";
4
+ dayjs.extend(customParseFormat);
5
+ const isValidDate = (d) => {
6
+ const format = "YYYY-MM-DD";
7
+ if (typeof d === "string") {
8
+ return dayjs(d, format).isValid();
9
+ } else if (isDayjs(d))
10
+ return d.isValid();
11
+ else
12
+ return d instanceof Date && !isNaN(d.getTime());
13
+ };
14
+ export {
15
+ isValidDate
16
+ };
@@ -1,3 +1,6 @@
1
- export const lowerlize = (str: string) => {
1
+ const lowerlize = (str) => {
2
2
  return str.charAt(0).toLowerCase() + str.slice(1);
3
3
  };
4
+ export {
5
+ lowerlize
6
+ };
@@ -0,0 +1,4 @@
1
+ const mergeVersion = (major, minor, patch) => `${major}.${minor}.${patch}`;
2
+ export {
3
+ mergeVersion
4
+ };
package/objectify.js ADDED
@@ -0,0 +1,11 @@
1
+ const objectify = (obj, keys = Object.keys(obj)) => {
2
+ const val = {};
3
+ keys.forEach((key) => {
4
+ if (typeof obj[key] !== "function")
5
+ val[key] = obj[key];
6
+ });
7
+ return val;
8
+ };
9
+ export {
10
+ objectify
11
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@akanjs/common",
3
- "version": "0.0.52",
4
- "type": "commonjs",
3
+ "version": "0.0.54",
4
+ "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -14,8 +14,5 @@
14
14
  "engines": {
15
15
  "node": ">=22"
16
16
  },
17
- "dependencies": {
18
- "dayjs": "^1.11.13",
19
- "pluralize": "^8.0.0"
20
- }
17
+ "dependencies": {}
21
18
  }
package/pathGet.js ADDED
@@ -0,0 +1,7 @@
1
+ const pathGet = (path, obj, separator = ".") => {
2
+ const properties = Array.isArray(path) ? path : path.split(separator);
3
+ return properties.reduce((prev, curr) => prev[curr], obj);
4
+ };
5
+ export {
6
+ pathGet
7
+ };
package/pathSet.js ADDED
@@ -0,0 +1,14 @@
1
+ const pathSet = (obj, path, value) => {
2
+ if (Object(obj) !== obj)
3
+ return obj;
4
+ if (!Array.isArray(path))
5
+ path = path.toString().match(/[^.[\]]+/g) || [];
6
+ path.slice(0, -1).reduce(
7
+ (a, c, i) => Object(a[c]) === a[c] ? a[c] : a[c] = Math.abs(path[i + 1]) >> 0 === +path[i + 1] ? [] : {},
8
+ obj
9
+ )[path[path.length - 1]] = value;
10
+ return obj;
11
+ };
12
+ export {
13
+ pathSet
14
+ };
@@ -1,3 +1,4 @@
1
1
  import pluralize from "pluralize";
2
-
3
- export { pluralize };
2
+ export {
3
+ pluralize
4
+ };
package/randomPick.js ADDED
@@ -0,0 +1,4 @@
1
+ const randomPick = (arr) => arr[Math.floor(Math.random() * arr.length)];
2
+ export {
3
+ randomPick
4
+ };
@@ -1,7 +1,8 @@
1
- export const randomPicks = <T>(arr: T[] | readonly T[], count = 1, allowDuplicate = false): T[] => {
2
- if (!allowDuplicate && arr.length <= count) return arr as T[];
3
- const idxs: number[] = [];
4
- let pickIdx: number;
1
+ const randomPicks = (arr, count = 1, allowDuplicate = false) => {
2
+ if (!allowDuplicate && arr.length <= count)
3
+ return arr;
4
+ const idxs = [];
5
+ let pickIdx;
5
6
  for (let i = 0; i < count; i++) {
6
7
  do {
7
8
  pickIdx = Math.floor(Math.random() * arr.length);
@@ -10,3 +11,6 @@ export const randomPicks = <T>(arr: T[] | readonly T[], count = 1, allowDuplicat
10
11
  }
11
12
  return idxs.map((idx) => arr[idx]);
12
13
  };
14
+ export {
15
+ randomPicks
16
+ };
@@ -1,7 +1,10 @@
1
- export const sleep = async (ms: number) => {
1
+ const sleep = async (ms) => {
2
2
  return new Promise((resolve) => {
3
3
  setTimeout(() => {
4
4
  resolve(true);
5
5
  }, ms);
6
6
  });
7
7
  };
8
+ export {
9
+ sleep
10
+ };
@@ -0,0 +1,7 @@
1
+ const splitVersion = (version) => {
2
+ const [major, minor, patch] = version.split(".");
3
+ return { major, minor, patch };
4
+ };
5
+ export {
6
+ splitVersion
7
+ };
package/types.js ADDED
File without changes
package/Logger.ts DELETED
@@ -1,136 +0,0 @@
1
- import dayjs from "dayjs";
2
-
3
- const logLevels = ["trace", "verbose", "debug", "log", "info", "warn", "error"] as const;
4
- type LogLevel = (typeof logLevels)[number];
5
-
6
- const clc = {
7
- bold: (text: string) => `\x1B[1m${text}\x1B[0m`,
8
- green: (text: string) => `\x1B[32m${text}\x1B[39m`,
9
- yellow: (text: string) => `\x1B[33m${text}\x1B[39m`,
10
- red: (text: string) => `\x1B[31m${text}\x1B[39m`,
11
- magentaBright: (text: string) => `\x1B[95m${text}\x1B[39m`,
12
- cyanBright: (text: string) => `\x1B[96m${text}\x1B[39m`,
13
- };
14
-
15
- const colorizeMap: { [key in LogLevel]: (text: string) => string } = {
16
- trace: clc.bold,
17
- verbose: clc.cyanBright,
18
- debug: clc.magentaBright,
19
- log: clc.green,
20
- info: clc.green,
21
- warn: clc.yellow,
22
- error: clc.red,
23
- };
24
-
25
- export class Logger {
26
- static #ignoreCtxSet = new Set([
27
- "InstanceLoader",
28
- "RoutesResolver",
29
- "RouterExplorer",
30
- "NestFactory",
31
- "WebSocketsController",
32
- "GraphQLModule",
33
- "NestApplication",
34
- ]);
35
- static level: LogLevel = (process.env.NEXT_PUBLIC_LOG_LEVEL as LogLevel | undefined) ?? "log";
36
- static #levelIdx = logLevels.findIndex((l) => l === process.env.NEXT_PUBLIC_LOG_LEVEL);
37
- static #startAt = dayjs();
38
- static setLevel(level: LogLevel) {
39
- this.level = level;
40
- this.#levelIdx = logLevels.findIndex((l) => l === level);
41
- }
42
-
43
- name?: string;
44
- constructor(name?: string) {
45
- this.name = name;
46
- }
47
- trace(msg: string, context = "") {
48
- if (Logger.#levelIdx <= 0) Logger.#printMessages(this.name ?? "App", msg, context, "trace");
49
- }
50
- verbose(msg: string, context = "") {
51
- if (Logger.#levelIdx <= 1) Logger.#printMessages(this.name ?? "App", msg, context, "verbose");
52
- }
53
- debug(msg: string, context = "") {
54
- if (Logger.#levelIdx <= 2) Logger.#printMessages(this.name ?? "App", msg, context, "debug");
55
- }
56
- log(msg: string, context = "") {
57
- if (Logger.#levelIdx <= 3) Logger.#printMessages(this.name ?? "App", msg, context, "log");
58
- }
59
- info(msg: string, context = "") {
60
- if (Logger.#levelIdx <= 4) Logger.#printMessages(this.name ?? "App", msg, context, "info");
61
- }
62
- warn(msg: string, context = "") {
63
- if (Logger.#levelIdx <= 5) Logger.#printMessages(this.name ?? "App", msg, context, "warn");
64
- }
65
- error(msg: string, context = "") {
66
- if (Logger.#levelIdx <= 6) Logger.#printMessages(this.name ?? "App", msg, context, "error");
67
- }
68
- raw(msg: string, method?: "console" | "process") {
69
- Logger.rawLog(msg, method);
70
- }
71
- rawLog(msg: string, method?: "console" | "process") {
72
- Logger.rawLog(msg, method);
73
- }
74
- static trace(msg: string, context = "") {
75
- if (Logger.#levelIdx <= 0) Logger.#printMessages("App", msg, context, "trace");
76
- }
77
- static verbose(msg: string, context = "") {
78
- if (Logger.#levelIdx <= 1) Logger.#printMessages("App", msg, context, "verbose");
79
- }
80
- static debug(msg: string, context = "") {
81
- if (Logger.#levelIdx <= 2) Logger.#printMessages("App", msg, context, "debug");
82
- }
83
- static log(msg: string, context = "") {
84
- if (Logger.#levelIdx <= 3) Logger.#printMessages("App", msg, context, "log");
85
- }
86
- static info(msg: string, context = "") {
87
- if (Logger.#levelIdx <= 4) Logger.#printMessages("App", msg, context, "info");
88
- }
89
- static warn(msg: string, context = "") {
90
- if (Logger.#levelIdx <= 5) Logger.#printMessages("App", msg, context, "warn");
91
- }
92
- static error(msg: string, context = "") {
93
- if (Logger.#levelIdx <= 6) Logger.#printMessages("App", msg, context, "error");
94
- }
95
- static #colorize(msg: string, logLevel: LogLevel) {
96
- return colorizeMap[logLevel](msg);
97
- }
98
- static #printMessages(
99
- name: string | undefined,
100
- content: string,
101
- context: string,
102
- logLevel: LogLevel,
103
- writeStreamType: "stdout" | "stderr" = logLevel === "error" ? "stderr" : "stdout"
104
- ) {
105
- if (this.#ignoreCtxSet.has(context)) return;
106
- const now = dayjs();
107
- const processMsg = this.#colorize(
108
- `[${name ?? "App"}] ${(global.process as unknown as NodeJS.Process | undefined)?.pid ?? "window"} -`,
109
- logLevel
110
- );
111
- const timestampMsg = now.format("MM/DD/YYYY, HH:mm:ss A");
112
- const logLevelMsg = this.#colorize(logLevel.toUpperCase().padStart(7, " "), logLevel);
113
- const contextMsg = context ? clc.yellow(`[${context}] `) : "";
114
- const contentMsg = this.#colorize(content, logLevel);
115
- const timeDiffMsg = clc.yellow(`+${now.diff(Logger.#startAt, "ms")}ms`);
116
- if (typeof window === "undefined")
117
- process[writeStreamType].write(
118
- `${processMsg} ${timestampMsg} ${logLevelMsg} ${contextMsg} ${contentMsg} ${timeDiffMsg}\n`
119
- );
120
- // eslint-disable-next-line no-console
121
- else console.log(`${processMsg} ${timestampMsg} ${logLevelMsg} ${contextMsg} ${contentMsg} ${timeDiffMsg}\n`);
122
- }
123
- static rawLog(msg: string, method?: "console" | "process") {
124
- this.raw(`${msg}\n`, method);
125
- }
126
- static raw(msg: string, method?: "console" | "process") {
127
- if (
128
- typeof window === "undefined" &&
129
- method !== "console" &&
130
- (global.process as unknown as NodeJS.Process | undefined)
131
- )
132
- global.process.stdout.write(msg);
133
- // eslint-disable-next-line no-console
134
- else console.log(msg);
135
- }
136
- }
package/applyMixins.ts DELETED
@@ -1,21 +0,0 @@
1
- type Type<T = any> = new (...args: any[]) => T;
2
- const getAllPropertyDescriptors = (objRef: Type): { [key: string]: PropertyDescriptor } => {
3
- const descriptors: { [key: string]: any } = {};
4
- let current = objRef.prototype as object | null;
5
- while (current) {
6
- Object.getOwnPropertyNames(current).forEach((name) => {
7
- descriptors[name] ??= Object.getOwnPropertyDescriptor(current, name);
8
- });
9
- current = Object.getPrototypeOf(current) as Type | object;
10
- }
11
- return descriptors;
12
- };
13
-
14
- export const applyMixins = (derivedCtor: Type, constructors: (Type | undefined)[], avoidKeys?: Set<string>) => {
15
- constructors.forEach((baseCtor: Type) => {
16
- Object.entries(getAllPropertyDescriptors(baseCtor)).forEach(([name, descriptor]) => {
17
- if (name === "constructor" || avoidKeys?.has(name)) return;
18
- Object.defineProperty(derivedCtor.prototype, name, { ...descriptor, configurable: true });
19
- });
20
- });
21
- };
package/deepObjectify.ts DELETED
@@ -1,26 +0,0 @@
1
- import { isDayjs } from "./isDayjs";
2
-
3
- export const deepObjectify = <T = any>(
4
- obj: T | null | undefined,
5
- option: { serializable?: boolean; convertDate?: "string" | "number" } = {}
6
- ): T => {
7
- if (isDayjs(obj) || obj?.constructor === Date) {
8
- if (!option.serializable && !option.convertDate) return obj as T;
9
- if (option.convertDate === "string") return obj.toISOString() as T;
10
- else if (option.convertDate === "number")
11
- return (isDayjs(obj) ? obj.toDate().getTime() : (obj as Date).getTime()) as T;
12
- else return (isDayjs(obj) ? obj.toDate() : obj) as T;
13
- } else if (Array.isArray(obj)) {
14
- return obj.map((o: object) => deepObjectify(o, option)) as T;
15
- } else if (obj && typeof obj === "object") {
16
- const val = {} as { [key: string]: object };
17
- Object.keys(obj).forEach((key) => {
18
- const fieldValue = obj[key] as { __ModelType__: string } | null | undefined;
19
- if (fieldValue?.__ModelType__ && !option.serializable) val[key] = fieldValue;
20
- else if (typeof obj[key] !== "function") val[key] = deepObjectify(fieldValue, option);
21
- });
22
- return val as T;
23
- } else {
24
- return obj as unknown as T;
25
- }
26
- };
package/index.ts DELETED
@@ -1,18 +0,0 @@
1
- export type * from "./types";
2
- export { splitVersion } from "./splitVersion";
3
- export { deepObjectify } from "./deepObjectify";
4
- export { pathGet } from "./pathGet";
5
- export { isQueryEqual } from "./isQueryEqual";
6
- export { isValidDate } from "./isValidDate";
7
- export { objectify } from "./objectify";
8
- export { randomPick } from "./randomPick";
9
- export { randomPicks } from "./randomPicks";
10
- export { pathSet } from "./pathSet";
11
- export { isDayjs } from "./isDayjs";
12
- export { mergeVersion } from "./mergeVersion";
13
- export { pluralize } from "./pluralize";
14
- export { applyMixins } from "./applyMixins";
15
- export { capitalize } from "./capitalize";
16
- export { Logger } from "./Logger";
17
- export { lowerlize } from "./lowerlize";
18
- export { sleep } from "./sleep";
package/isQueryEqual.ts DELETED
@@ -1,22 +0,0 @@
1
- import dayjs from "dayjs";
2
-
3
- import { isDayjs } from "./isDayjs";
4
-
5
- export const isQueryEqual = (value1: object | null, value2: object | null) => {
6
- if (value1 === value2) return true;
7
- if (Array.isArray(value1) && Array.isArray(value2)) {
8
- if (value1.length !== value2.length) return false;
9
- for (let i = 0; i < value1.length; i++) if (!isQueryEqual(value1[i] as object, value2[i] as object)) return false;
10
- return true;
11
- }
12
- if ([value1, value2].some((val) => val instanceof Date || isDayjs(val)))
13
- return dayjs(value1 as Date).isSame(dayjs(value2 as Date));
14
- if (typeof value1 === "object" && typeof value2 === "object") {
15
- if (value1 === null || value2 === null) return value1 === value2;
16
- if (Object.keys(value1).length !== Object.keys(value2).length) return false;
17
- for (const key of Object.keys(value1))
18
- if (!isQueryEqual(value1[key] as object, value2[key] as object)) return false;
19
- return true;
20
- }
21
- return false;
22
- };
package/isValidDate.ts DELETED
@@ -1,17 +0,0 @@
1
- import dayjs, { Dayjs } from "dayjs";
2
- import customParseFormat from "dayjs/plugin/customParseFormat";
3
-
4
- import { isDayjs } from "./isDayjs";
5
-
6
- dayjs.extend(customParseFormat);
7
-
8
- export const isValidDate = (d: string | Date | Dayjs) => {
9
- const format = "YYYY-MM-DD";
10
- if (typeof d === "string") {
11
- return dayjs(d, format).isValid();
12
- // ! Aggregation에서는 위 코드처럼 해야함. 다른곳에서 필요하면 아래 코드 사용해야 할 이유를 찾아보기
13
- // return dayjs(d, format, true).isValid();
14
- // } else if (isDayjs(d)) return dayjs(d.format(format), format, true).isSame(d);
15
- } else if (isDayjs(d)) return d.isValid();
16
- else return d instanceof Date && !isNaN(d.getTime());
17
- };
package/mergeVersion.ts DELETED
@@ -1,10 +0,0 @@
1
- /**
2
- *
3
- * semantic version 규격에 맞게 조합하는 함수
4
- * https://semver.org/
5
- * @param major 릴리즈(플랫폼(google,apple,etc...)에 올라가 있는) 버전
6
- * @param minor 릴리즈(플랫폼(google,apple,etc...)에 올라가 있는) 버전
7
- * @param patch 코드푸시 (Framework 자체 버전)
8
- * @returns `major.minor.patch` 형식으로 조합된 버전
9
- */
10
- export const mergeVersion = (major: number, minor: number, patch: number) => `${major}.${minor}.${patch}`;
package/objectify.ts DELETED
@@ -1,11 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-return */
2
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
3
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
4
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
5
- export const objectify = (obj: any, keys = Object.keys(obj)) => {
6
- const val: any = {};
7
- keys.forEach((key) => {
8
- if (typeof obj[key] !== "function") val[key] = obj[key];
9
- });
10
- return val;
11
- };
package/pathGet.ts DELETED
@@ -1,4 +0,0 @@
1
- export const pathGet = (path: string | (string | number)[], obj: any, separator = "."): unknown => {
2
- const properties = Array.isArray(path) ? path : path.split(separator);
3
- return properties.reduce((prev: Record<string, any> | any[], curr) => prev[curr] as unknown, obj);
4
- };
package/pathSet.ts DELETED
@@ -1,19 +0,0 @@
1
- /* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
2
- /* eslint-disable @typescript-eslint/restrict-plus-operands */
3
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
4
- /* eslint-disable @typescript-eslint/no-unsafe-return */
5
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
6
- /* eslint-disable @typescript-eslint/no-unsafe-call */
7
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
8
- export const pathSet = (obj: any, path: any, value: any) => {
9
- if (Object(obj) !== obj) return obj;
10
- if (!Array.isArray(path)) path = path.toString().match(/[^.[\]]+/g) || [];
11
- path
12
- .slice(0, -1)
13
- .reduce(
14
- (a: any, c: any, i: any) =>
15
- Object(a[c]) === a[c] ? a[c] : (a[c] = Math.abs(path[i + 1]) >> 0 === +path[i + 1] ? [] : {}),
16
- obj
17
- )[path[path.length - 1]] = value;
18
- return obj;
19
- };
package/randomPick.ts DELETED
@@ -1 +0,0 @@
1
- export const randomPick = <T = any>(arr: T[] | readonly T[]): T => arr[Math.floor(Math.random() * arr.length)];
package/splitVersion.ts DELETED
@@ -1,10 +0,0 @@
1
- /**
2
- *
3
- * semantic version 규격에 맞게 major, minor, patch로 나누는 함수
4
- * https://semver.org/
5
- * @params version `major.minor.patch` 형식으로 조합된 버전
6
- */
7
- export const splitVersion = (version: string) => {
8
- const [major, minor, patch] = version.split(".");
9
- return { major, minor, patch };
10
- };
package/types.ts DELETED
@@ -1,17 +0,0 @@
1
- import type { RequestPolicy } from "@urql/core";
2
-
3
- export interface FetchPolicy<Returns = any> {
4
- cache?: boolean | number | RequestPolicy;
5
- crystalize?: boolean;
6
- url?: string;
7
- onError?: (error: string) => void;
8
- token?: string;
9
- partial?: string[];
10
- transport?: "udp" | "websocket" | "graphql" | "restapi";
11
- }
12
-
13
- export type SnakeCase<S extends string> = S extends `${infer T}_${infer U}` ? `${Lowercase<T>}_${SnakeCase<U>}` : S;
14
- export type SnakeCaseObj<T> = {
15
- [K in keyof T as SnakeCase<K & string>]: T[K] extends object ? SnakeCaseObj<T[K]> : T[K];
16
- };
17
- export type SnakeMsg<Msg> = SnakeCaseObj<Msg>;