@powfix/core-js 0.9.22

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 (101) hide show
  1. package/browser.ts +73 -0
  2. package/dist/browser.d.ts +32 -0
  3. package/dist/browser.js +58 -0
  4. package/dist/index.d.ts +2 -0
  5. package/dist/index.js +20 -0
  6. package/dist/src/constants/COORDINATE.d.ts +7 -0
  7. package/dist/src/constants/COORDINATE.js +10 -0
  8. package/dist/src/constants/DISTANCE.d.ts +13 -0
  9. package/dist/src/constants/DISTANCE.js +18 -0
  10. package/dist/src/constants/DURATION.d.ts +16 -0
  11. package/dist/src/constants/DURATION.js +21 -0
  12. package/dist/src/interfaces/Coordinate.d.ts +8 -0
  13. package/dist/src/interfaces/Coordinate.js +2 -0
  14. package/dist/src/interfaces/Point2.d.ts +4 -0
  15. package/dist/src/interfaces/Point2.js +2 -0
  16. package/dist/src/interfaces/Point3.d.ts +4 -0
  17. package/dist/src/interfaces/Point3.js +2 -0
  18. package/dist/src/scripts/base64-polyfill.d.ts +1 -0
  19. package/dist/src/scripts/base64-polyfill.js +14 -0
  20. package/dist/src/services/Session.d.ts +27 -0
  21. package/dist/src/services/Session.js +140 -0
  22. package/dist/src/services/browser.d.ts +2 -0
  23. package/dist/src/services/browser.js +20 -0
  24. package/dist/src/services/index.d.ts +2 -0
  25. package/dist/src/services/index.js +19 -0
  26. package/dist/src/services/redis/RedisClient.d.ts +20 -0
  27. package/dist/src/services/redis/RedisClient.js +70 -0
  28. package/dist/src/services/redis/RedisPublisher.d.ts +13 -0
  29. package/dist/src/services/redis/RedisPublisher.js +61 -0
  30. package/dist/src/services/redis/RedisSubscriber.d.ts +11 -0
  31. package/dist/src/services/redis/RedisSubscriber.js +68 -0
  32. package/dist/src/services/redis/index.d.ts +3 -0
  33. package/dist/src/services/redis/index.js +20 -0
  34. package/dist/src/services/time/TimeService.d.ts +64 -0
  35. package/dist/src/services/time/TimeService.js +235 -0
  36. package/dist/src/services/time/index.d.ts +1 -0
  37. package/dist/src/services/time/index.js +17 -0
  38. package/dist/src/types/IntRage.d.ts +3 -0
  39. package/dist/src/types/IntRage.js +2 -0
  40. package/dist/src/utils/ArrayUtils.d.ts +10 -0
  41. package/dist/src/utils/ArrayUtils.js +20 -0
  42. package/dist/src/utils/BooleanUtils.d.ts +1 -0
  43. package/dist/src/utils/BooleanUtils.js +9 -0
  44. package/dist/src/utils/CoordinateUtils.d.ts +8 -0
  45. package/dist/src/utils/CoordinateUtils.js +42 -0
  46. package/dist/src/utils/DateUtils.d.ts +12 -0
  47. package/dist/src/utils/DateUtils.js +212 -0
  48. package/dist/src/utils/JuminNumberUtils.d.ts +4 -0
  49. package/dist/src/utils/JuminNumberUtils.js +50 -0
  50. package/dist/src/utils/NumberUtils.d.ts +4 -0
  51. package/dist/src/utils/NumberUtils.js +25 -0
  52. package/dist/src/utils/Point3Utils.d.ts +4 -0
  53. package/dist/src/utils/Point3Utils.js +12 -0
  54. package/dist/src/utils/RandomUtils.d.ts +8 -0
  55. package/dist/src/utils/RandomUtils.js +64 -0
  56. package/dist/src/utils/Sequencer.d.ts +39 -0
  57. package/dist/src/utils/Sequencer.js +148 -0
  58. package/dist/src/utils/StringUtils.d.ts +5 -0
  59. package/dist/src/utils/StringUtils.js +37 -0
  60. package/dist/src/utils/UuidUtils.d.ts +14 -0
  61. package/dist/src/utils/UuidUtils.js +49 -0
  62. package/dist/src/utils/Validator.d.ts +48 -0
  63. package/dist/src/utils/Validator.js +118 -0
  64. package/dist/src/utils/global/between.d.ts +1 -0
  65. package/dist/src/utils/global/between.js +7 -0
  66. package/dist/src/utils/global/sleep.d.ts +1 -0
  67. package/dist/src/utils/global/sleep.js +21 -0
  68. package/index.ts +5 -0
  69. package/package.json +42 -0
  70. package/src/constants/COORDINATE.ts +9 -0
  71. package/src/constants/DISTANCE.ts +15 -0
  72. package/src/constants/DURATION.ts +18 -0
  73. package/src/interfaces/Coordinate.ts +9 -0
  74. package/src/interfaces/Point2.ts +4 -0
  75. package/src/interfaces/Point3.ts +5 -0
  76. package/src/scripts/base64-polyfill.ts +13 -0
  77. package/src/services/Session.ts +141 -0
  78. package/src/services/browser.ts +5 -0
  79. package/src/services/index.ts +4 -0
  80. package/src/services/redis/RedisClient.ts +79 -0
  81. package/src/services/redis/RedisPublisher.ts +48 -0
  82. package/src/services/redis/RedisSubscriber.ts +49 -0
  83. package/src/services/redis/index.ts +4 -0
  84. package/src/services/time/TimeService.ts +304 -0
  85. package/src/services/time/index.ts +1 -0
  86. package/src/types/IntRage.ts +5 -0
  87. package/src/utils/ArrayUtils.ts +25 -0
  88. package/src/utils/BooleanUtils.ts +4 -0
  89. package/src/utils/CoordinateUtils.ts +50 -0
  90. package/src/utils/DateUtils.ts +213 -0
  91. package/src/utils/JuminNumberUtils.ts +47 -0
  92. package/src/utils/NumberUtils.ts +23 -0
  93. package/src/utils/Point3Utils.ts +11 -0
  94. package/src/utils/RandomUtils.ts +62 -0
  95. package/src/utils/Sequencer.ts +178 -0
  96. package/src/utils/StringUtils.ts +43 -0
  97. package/src/utils/UuidUtils.ts +46 -0
  98. package/src/utils/Validator.ts +162 -0
  99. package/src/utils/global/between.ts +3 -0
  100. package/src/utils/global/sleep.ts +6 -0
  101. package/tsconfig.json +103 -0
@@ -0,0 +1,47 @@
1
+ import {between} from "./global/between";
2
+
3
+ export class JuminNumberUtils {
4
+ public static calculateX(juminNumber: string): number {
5
+ const replacedJuminNumber = juminNumber.replace(/\D/g, '');
6
+ if (!between(replacedJuminNumber.length, 12, 13)) {
7
+ throw new Error('Jumin number length must be 12 to 13(without dash)');
8
+ }
9
+
10
+ const numbers = replacedJuminNumber.split('').map(e => parseInt(e, 10));
11
+ const sum = 2 * numbers[0] + 3 * numbers[1] + 4 * numbers[2] + 5 * numbers[3]
12
+ + 6 * numbers[4] + 7 * numbers[5] + 8 * numbers[6] + 9 * numbers[7]
13
+ + 2 * numbers[8] + 3 * numbers[9] + 4 * numbers[10] + 5 * numbers[11];
14
+ return (11 - (sum % 11)) % 10;
15
+ }
16
+
17
+ public static isValid(juminNumber: string): boolean {
18
+ const replacedJuminNumber = juminNumber.replace(/\D/g, '');
19
+ if (replacedJuminNumber.length !== 13) {
20
+ return false;
21
+ }
22
+
23
+ // 연도에 해당하는 숫자와 성별에 해당하는 숫자 비교
24
+ const TODAY_YEAR = parseInt(new Date().getFullYear().toString().slice(-2), 10);
25
+ const yearNum = parseInt(replacedJuminNumber.slice(0, 2), 10);
26
+ const sexNum = replacedJuminNumber.slice(6, 7); // 대시 있는경우 7로 변경
27
+ if (sexNum === '1' || sexNum === '2') {
28
+ if (yearNum < TODAY_YEAR) return false;
29
+ } else if (sexNum === '3' || sexNum === '4') {
30
+ if (yearNum > TODAY_YEAR) return false;
31
+ } else return false;
32
+
33
+ // 월과 일에 해당하는 숫자 조건 검사 (정규식으로)
34
+ const regex = /^\d{2}(0[1-9]|1[0-2])(0[1-9]|[1-2][0-9]|3[0-1])\d{7}$/;
35
+ if (!regex.test(replacedJuminNumber)) return false;
36
+
37
+ // Validation X
38
+ const currentX = parseInt(replacedJuminNumber.slice(-1), 10);
39
+ const calculatedX = JuminNumberUtils.calculateX(replacedJuminNumber);
40
+ if (currentX !== calculatedX) {
41
+ console.log('Invalid X', currentX, calculatedX);
42
+ return false;
43
+ }
44
+
45
+ return true;
46
+ }
47
+ }
@@ -0,0 +1,23 @@
1
+ export class NumberUtils {
2
+ public static formatWithThousandsSeparator(value: number, separator: string = ','): string {
3
+ return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, separator);
4
+ }
5
+
6
+ public static formatBigNumber(value: number, precision: number = 2): string {
7
+ const map: {suffix: string, threshold: number}[] = [
8
+ {suffix: 'Qi', threshold: 1e18},
9
+ {suffix: 'Q', threshold: 1e15},
10
+ {suffix: 'T', threshold: 1e12},
11
+ {suffix: 'B', threshold: 1e9},
12
+ {suffix: 'M', threshold: 1e6},
13
+ {suffix: 'K', threshold: 1e3},
14
+ {suffix: '', threshold: 1},
15
+ ];
16
+
17
+ const found = map.find(e => Math.abs(value) >= e.threshold);
18
+ if (found) {
19
+ return Number((value / found.threshold).toFixed(precision)) + found.suffix;
20
+ }
21
+ return value.toString();
22
+ }
23
+ }
@@ -0,0 +1,11 @@
1
+ import {Point3} from "../interfaces/Point3";
2
+
3
+ export class Point3Utils {
4
+ static distance(p1: Point3, p2: Point3): number {
5
+ const dx = p1.x - p2.x;
6
+ const dy = p1.y - p2.y;
7
+ const dz = p1.z - p2.z;
8
+
9
+ return Math.sqrt( dx * dx + dy * dy + dz * dz );
10
+ }
11
+ }
@@ -0,0 +1,62 @@
1
+ export class RandomUtils {
2
+ static randomNumber(min: number, max: number): number {
3
+ min = Math.ceil(min);
4
+ max = Math.floor(max);
5
+ return Math.floor(Math.random() * (max - min + 1) + min);
6
+ }
7
+
8
+ static randomLatinStrings(length: number) {
9
+ const strings = [];
10
+ for (let i = 0; i < length; ++i) {
11
+ const isLowerCase = RandomUtils.randomNumber(0, 1);
12
+ if (isLowerCase) {
13
+ strings.push(String.fromCharCode(RandomUtils.randomNumber(97, 122)));
14
+ } else {
15
+ strings.push(String.fromCharCode(RandomUtils.randomNumber(65, 90)));
16
+ }
17
+ }
18
+ return strings.join('');
19
+ }
20
+
21
+ static randomLatinLowercaseStrings(length: number) {
22
+ const strings = [];
23
+ for (let i = 0; i < length; ++i) {
24
+ strings.push(String.fromCharCode(RandomUtils.randomNumber(97, 122)));
25
+ }
26
+ return strings.join('');
27
+ }
28
+
29
+ static randomLatinUppercaseStrings(length: number) {
30
+ const strings = [];
31
+ for (let i = 0; i < length; ++i) {
32
+ strings.push(String.fromCharCode(RandomUtils.randomNumber(65, 90)));
33
+ }
34
+ return strings.join('');
35
+ }
36
+
37
+ static randomNumberStrings(length: number) {
38
+ const strings = [];
39
+ for (let i = 0; i < length; ++i) {
40
+ strings.push(String.fromCharCode(RandomUtils.randomNumber(48, 57)));
41
+ }
42
+ return strings.join('');
43
+ }
44
+
45
+ static randomStrings(length: number) {
46
+ const strings = [];
47
+ for (let i = 0; i < length; ++i) {
48
+ const isNumber = RandomUtils.randomNumber(0, 1);
49
+ if (isNumber) {
50
+ strings.push(String.fromCharCode(RandomUtils.randomNumber(48, 57)));
51
+ } else {
52
+ const isLowerCase = RandomUtils.randomNumber(0, 1);
53
+ if (isLowerCase) {
54
+ strings.push(String.fromCharCode(RandomUtils.randomNumber(97, 122)));
55
+ } else {
56
+ strings.push(String.fromCharCode(RandomUtils.randomNumber(65, 90)));
57
+ }
58
+ }
59
+ }
60
+ return strings.join('');
61
+ }
62
+ }
@@ -0,0 +1,178 @@
1
+ import moment from "moment/moment";
2
+ import EventEmitter from 'events';
3
+
4
+ export interface Sequence {
5
+ key: string; // 고유키값
6
+ required?: boolean; // 오류를 무시할건지
7
+ minimumExecutionTime?: number; // 최소실행 시간
8
+ task: Function; // Promise 작업
9
+ description?: string; // 설명
10
+ }
11
+
12
+ export enum SequencerStatus {
13
+ IDLE,
14
+ RUNNING,
15
+ ERROR,
16
+ DONE,
17
+ }
18
+
19
+ export interface SequencerOption {
20
+ sequences?: Sequence[];
21
+ minimumExecutionTime?: number;
22
+ }
23
+
24
+ export enum SequencerEvent {
25
+ START = 'START',
26
+ END = 'END',
27
+
28
+ SEQUENCE_START = 'SEQUENCE_START',
29
+ SEQUENCE_END = 'SEQUENCE_END',
30
+ }
31
+
32
+ export class Sequencer {
33
+ protected readonly sequences: Sequence[] = [];
34
+ protected status: SequencerStatus = SequencerStatus.IDLE;
35
+ protected minimumExecutionTime: number = 0;
36
+
37
+ // Reset variables task is done
38
+ currentSequence: Sequence | null = null;
39
+ startTimestamp: number | null = null;
40
+ endTimestamp: number | null = null;
41
+
42
+ // Emitter
43
+ eventEmitter = new EventEmitter();
44
+
45
+ constructor(option?: SequencerOption) {
46
+ if (option?.sequences) {
47
+ this.sequences.push(...option.sequences);
48
+ }
49
+ }
50
+
51
+ get getCurrentTimeStamp(): number {
52
+ return parseInt(moment().format('x'), 10);
53
+ }
54
+
55
+ get executionTime(): number | null {
56
+ if (this.status === SequencerStatus.IDLE) {
57
+ return null;
58
+ }
59
+
60
+ if (!this.startTimestamp) {
61
+ return null;
62
+ }
63
+
64
+ if (this.startTimestamp && this.endTimestamp) {
65
+ return this.endTimestamp - this.startTimestamp;
66
+ }
67
+
68
+ return this.getCurrentTimeStamp - this.startTimestamp;
69
+ }
70
+
71
+ pushSequence = (sequence: Sequence) => {
72
+ this.sequences.push(sequence);
73
+ }
74
+
75
+ start = async () => {
76
+ if (this.status === SequencerStatus.RUNNING) {
77
+ console.warn('Sequencer status is', this.status);
78
+ return;
79
+ }
80
+
81
+ this.status = SequencerStatus.RUNNING;
82
+ this.currentSequence = null;
83
+ this.startTimestamp = this.getCurrentTimeStamp;
84
+ this.endTimestamp = null;
85
+
86
+ console.log(`Sequence started, started at ${this.startTimestamp}, MINIMUM_EXECUTION_TIME is ${this.minimumExecutionTime}`);
87
+
88
+ for (let sequence of this.sequences) {
89
+ console.log('Currently total execution time', this.executionTime);
90
+
91
+ const sequenceStartTimeStamp = this.getCurrentTimeStamp;
92
+
93
+ this.currentSequence = sequence;
94
+
95
+ // Emitter
96
+ this.eventEmitter.emit(SequencerEvent.SEQUENCE_START, sequence);
97
+
98
+ try {
99
+ await new Promise<void>(async (resolve, reject) => {
100
+ try {
101
+ console.log(`Sequence ${sequence.key} start`);
102
+
103
+ await sequence.task?.();
104
+
105
+ const sequenceEndTimeStamp = this.getCurrentTimeStamp;
106
+ const sequenceExecutionTime = sequenceEndTimeStamp - sequenceStartTimeStamp;
107
+
108
+ console.log(`✅ Sequence ${sequence.key} done at`, sequenceEndTimeStamp);
109
+ console.log('Sequence execution time', sequenceExecutionTime, 'ms');
110
+
111
+ if (sequence.minimumExecutionTime) {
112
+ console.log(`Sequence has minimumExecutionTime`, sequence.minimumExecutionTime, 'ms');
113
+
114
+ if (sequenceExecutionTime < sequence.minimumExecutionTime) {
115
+ const delay = sequence.minimumExecutionTime - sequenceExecutionTime;
116
+ console.log(`Sequence will delay`, delay, 'ms');
117
+
118
+ let dotInterpreterBlocked = false;
119
+ const dotInterpreter = setInterval(() => {
120
+ if (dotInterpreterBlocked) {
121
+ console.log('!');
122
+ return;
123
+ }
124
+
125
+ console.log('.');
126
+ }, 100);
127
+
128
+ setTimeout(() => {
129
+ dotInterpreterBlocked = true;
130
+ clearInterval(dotInterpreter);
131
+ console.log('done');
132
+ resolve();
133
+ }, delay);
134
+ return;
135
+ }
136
+
137
+ console.log('Sequence execution time is greater than minimum execution time');
138
+ resolve();
139
+ return;
140
+ }
141
+
142
+ resolve();
143
+ } catch (e) {
144
+ reject(e);
145
+ }
146
+ });
147
+
148
+ console.log('Out of Promise');
149
+
150
+ // Emitter
151
+ this.eventEmitter.emit(SequencerEvent.SEQUENCE_END, sequence);
152
+ } catch (e) {
153
+ if (sequence.required) {
154
+ console.error(`🚫 Sequence ${sequence.key} failed`, e);
155
+
156
+ this.status = SequencerStatus.ERROR;
157
+ this.currentSequence = null;
158
+ this.endTimestamp = this.currentSequence;
159
+
160
+ // IMPORTANT
161
+ return Promise.reject({
162
+ sequence,
163
+ reason: e,
164
+ });
165
+ }
166
+
167
+ console.log(`Sequence ${sequence.key} failed`, e);
168
+
169
+ // Emitter
170
+ this.eventEmitter.emit(SequencerEvent.SEQUENCE_END, sequence);
171
+ }
172
+ }
173
+
174
+ this.status = SequencerStatus.DONE;
175
+ this.currentSequence = null;
176
+ this.endTimestamp = this.currentSequence;
177
+ };
178
+ }
@@ -0,0 +1,43 @@
1
+ export class StringUtils {
2
+ public static numberWithCommas(x: string | any): string {
3
+ return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
4
+ }
5
+
6
+ public static getByte(s: string): number {
7
+ const getByteLength = (decimal: number) => {
8
+ const LINE_FEED = 10;
9
+ return (decimal >> 7) || (LINE_FEED === decimal) ? 2 : 1
10
+ };
11
+
12
+ return s
13
+ .split('')
14
+ .map((s) => s.charCodeAt(0))
15
+ .reduce((prev, unicodeDecimalValue) => prev + getByteLength(unicodeDecimalValue), 0)
16
+ }
17
+
18
+ public static levenshteinDistance = (str1: string = '', str2: string = ''): number => {
19
+ const track = Array(str2.length + 1).fill(null).map(() =>
20
+ Array(str1.length + 1).fill(null));
21
+
22
+ for (let i = 0; i <= str1.length; i += 1) {
23
+ track[0][i] = i;
24
+ }
25
+
26
+ for (let j = 0; j <= str2.length; j += 1) {
27
+ track[j][0] = j;
28
+ }
29
+
30
+ for (let j = 1; j <= str2.length; j += 1) {
31
+ for (let i = 1; i <= str1.length; i += 1) {
32
+ const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
33
+ track[j][i] = Math.min(
34
+ track[j][i - 1] + 1, // deletion
35
+ track[j - 1][i] + 1, // insertion
36
+ track[j - 1][i - 1] + indicator, // substitution
37
+ );
38
+ }
39
+ }
40
+
41
+ return track[str2.length][str1.length];
42
+ };
43
+ }
@@ -0,0 +1,46 @@
1
+ const uuid = require('uuid');
2
+
3
+ function binaryToString(binary: Buffer): string {
4
+ return Buffer.from(binary).toString('hex');
5
+ }
6
+
7
+ export class UuidUtils {
8
+ static v4(): string {
9
+ return uuid.v4();
10
+ }
11
+
12
+ static format(uuid: string) {
13
+ if (uuid.length === 32) {
14
+ // Without dash: ca23c587d7f84c76be59f53bbc9f91f8
15
+ return `${uuid.substring(0, 8)}-${uuid.substring(8, 12)}-${uuid.substring(12, 16)}-${uuid.substring(16, 20)}-${uuid.substring(20, 32)}`.toUpperCase();
16
+ } else if (uuid.length === 36) {
17
+ // With dash: ca23c587-d7f8-4c76-be59-f53bbc9f91f8
18
+ return uuid.toUpperCase();
19
+ } else {
20
+ // Unexpected uuid
21
+ console.warn('Unexpected uuid length', uuid);
22
+ return uuid;
23
+ }
24
+ }
25
+
26
+ /**
27
+ * (UUID: Buffer) to (UUID: string)
28
+ * @param binary UUID
29
+ * @returns {string|null} When binary not exists return null
30
+ */
31
+ static toString(binary?: Buffer): string | null {
32
+ if (!binary) return null;
33
+ return UuidUtils.format(binaryToString(binary));
34
+ }
35
+
36
+ /** (UUID: string) to (UUID: Buffer) */
37
+ static toBuffer(uuid: string): Buffer {
38
+ return Buffer.from(uuid.replace(/-/g, ''), 'hex');
39
+ }
40
+
41
+ static isValidUUID(uuid: string): boolean {
42
+ if (!uuid) return false;
43
+ if (typeof uuid !== 'string') return false;
44
+ return RegExp(/^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/i).test(uuid);
45
+ }
46
+ }
@@ -0,0 +1,162 @@
1
+ export class Validator {
2
+ public static validate(value: string | null | undefined, options: Validator.Options): {result: boolean, validations: Validator.VALIDATION[], passes: Validator.VALIDATION[], errors: Validator.VALIDATION[]} {
3
+ value = value || '';
4
+
5
+ const validations: Validator.VALIDATION[] = [];
6
+ const errors: Validator.VALIDATION[] = [];
7
+
8
+ const {length} = value;
9
+ const spaceCount = (value.match(/\s/g) || []).length;
10
+ const numberCount = (value.match(/\d/g) || []).length;
11
+ const alphabetCount = (value.match(/[A-Za-z]/g) || []).length;
12
+ const alphabetLowerCaseCount = (value.match(/[a-z]/g) || []).length;
13
+ const alphabetUpperCaseCount = (value.match(/[A-Z]/g) || []).length;
14
+ const specialCount = (value.match(/[~`!@#$%^&*()\-+={[}\]|\\:;"'<,>.?/]/g) || []).length;
15
+ const startsWithNumber = /^\d/.test(value);
16
+ const startsWithAlphabet = /^[A-Za-z]/.test(value);
17
+ const startsWithSpecialCharacter = /^[~`!@#$%^&*()\-+={[}\]|\\:;"'<,>.?/]/.test(value);
18
+
19
+ // 최소 길이
20
+ if (options.minLength !== undefined && options.minLength > -1) {
21
+ validations.push(Validator.VALIDATION.MIN_LENGTH);
22
+ if (length < options.minLength) {
23
+ errors.push(Validator.VALIDATION.MIN_LENGTH);
24
+ }
25
+ }
26
+
27
+ // 최대 길이
28
+ if (options.maxLength !== undefined && options.maxLength > -1) {
29
+ validations.push(Validator.VALIDATION.MAX_LENGTH);
30
+ if (length > options.maxLength) {
31
+ errors.push(Validator.VALIDATION.MAX_LENGTH);
32
+ }
33
+ }
34
+
35
+ const validateMin = (option: number | undefined, value: number, validation: Validator.VALIDATION) => {
36
+ if (option !== undefined && option > -1) {
37
+ validations.push(validation);
38
+ if (value < option) {
39
+ errors.push(validation);
40
+ }
41
+ }
42
+ };
43
+
44
+ const validateMax = (option: number | undefined, value: number, validation: Validator.VALIDATION) => {
45
+ if (option !== undefined && option > -1) {
46
+ validations.push(validation);
47
+ if (value > option) {
48
+ errors.push(validation);
49
+ }
50
+ }
51
+ }
52
+
53
+ // 공백 개수
54
+ validateMin(options.spaceMinCount, spaceCount, Validator.VALIDATION.SPACE_MIN_COUNT);
55
+ validateMax(options.spaceMaxCount, spaceCount, Validator.VALIDATION.SPACE_MAX_COUNT);
56
+
57
+ // 숫자 개수
58
+ validateMin(options.numberMinCount, numberCount, Validator.VALIDATION.NUMBER_MIN_COUNT);
59
+ validateMax(options.numberMaxCount, numberCount, Validator.VALIDATION.NUMBER_MAX_COUNT);
60
+
61
+ // 알파벳 개수
62
+ validateMin(options.alphabetMinCount, alphabetCount, Validator.VALIDATION.ALPHABET_MIN_COUNT);
63
+ validateMax(options.alphabetMaxCount, alphabetCount, Validator.VALIDATION.ALPHABET_MAX_COUNT);
64
+ // 알파벳(소문자) 개수
65
+ validateMin(options.alphabetLowerCaseMinCount, alphabetLowerCaseCount, Validator.VALIDATION.ALPHABET_LOWER_CASE_MIN_COUNT);
66
+ validateMax(options.alphabetLowerCaseMaxCount, alphabetLowerCaseCount, Validator.VALIDATION.ALPHABET_LOWER_CASE_MAX_COUNT);
67
+ // 알파벳(대문자) 개수
68
+ validateMin(options.alphabetUpperCaseMinCount, alphabetUpperCaseCount, Validator.VALIDATION.ALPHABET_UPPER_CASE_MIN_COUNT);
69
+ validateMax(options.alphabetUpperCaseMaxCount, alphabetUpperCaseCount, Validator.VALIDATION.ALPHABET_UPPER_CASE_MAX_COUNT);
70
+
71
+ // 특수문자 개수
72
+ validateMin(options.specialCharacterMinCount, specialCount, Validator.VALIDATION.SPECIAL_CHARACTER_MIN_COUNT);
73
+ validateMax(options.specialCharacterMaxCount, specialCount, Validator.VALIDATION.SPECIAL_CHARACTER_MAX_COUNT);
74
+
75
+ // 숫자로 시작
76
+ if (options.startsWithNumber) {
77
+ validations.push(Validator.VALIDATION.STARTS_WITH_NUMBER);
78
+ if (!startsWithNumber) {
79
+ errors.push(Validator.VALIDATION.STARTS_WITH_NUMBER);
80
+ }
81
+ }
82
+
83
+ // 영문으로 시작
84
+ if (options.startsWithAlphabet) {
85
+ validations.push(Validator.VALIDATION.STARTS_WITH_ALPHABET);
86
+ if (!startsWithAlphabet) {
87
+ errors.push(Validator.VALIDATION.STARTS_WITH_ALPHABET);
88
+ }
89
+ }
90
+
91
+ // 특수문자로 시작
92
+ if (options.startsWithSpecialCharacter) {
93
+ validations.push(Validator.VALIDATION.STARTS_WITH_SPECIAL_CHARACTER);
94
+ if (!startsWithSpecialCharacter) {
95
+ errors.push(Validator.VALIDATION.STARTS_WITH_SPECIAL_CHARACTER);
96
+ }
97
+ }
98
+
99
+ return {
100
+ result: errors.length === 0,
101
+ validations,
102
+ passes: validations.filter(e => !errors.includes(e)),
103
+ errors,
104
+ };
105
+ }
106
+ }
107
+
108
+ export namespace Validator {
109
+ export enum VALIDATION {
110
+ MIN_LENGTH = 'MIN_LENGTH',
111
+ MAX_LENGTH = 'MAX_LENGTH',
112
+
113
+ SPACE_MIN_COUNT = 'SPACE_MIN_COUNT',
114
+ SPACE_MAX_COUNT = 'SPACE_MAX_COUNT',
115
+
116
+ NUMBER_MIN_COUNT = 'NUMBER_MIN_COUNT',
117
+ NUMBER_MAX_COUNT = 'NUMBER_MAX_COUNT',
118
+
119
+ ALPHABET_MIN_COUNT = 'ALPHABET_MIN_COUNT',
120
+ ALPHABET_MAX_COUNT = 'ALPHABET_MAX_COUNT',
121
+
122
+ ALPHABET_LOWER_CASE_MIN_COUNT = 'ALPHABET_LOWER_CASE_MIN_COUNT',
123
+ ALPHABET_LOWER_CASE_MAX_COUNT = 'ALPHABET_LOWER_CASE_MAX_COUNT',
124
+
125
+ ALPHABET_UPPER_CASE_MIN_COUNT = 'ALPHABET_UPPER_CASE_MIN_COUNT',
126
+ ALPHABET_UPPER_CASE_MAX_COUNT = 'ALPHABET_UPPER_CASE_MAX_COUNT',
127
+
128
+ SPECIAL_CHARACTER_MIN_COUNT = 'SPECIAL_CHARACTER_MIN_COUNT',
129
+ SPECIAL_CHARACTER_MAX_COUNT = 'SPECIAL_CHARACTER_MAX_COUNT',
130
+
131
+ STARTS_WITH_ALPHABET = 'STARTS_WITH_ALPHABET',
132
+ STARTS_WITH_NUMBER = 'STARTS_WITH_NUMBER',
133
+ STARTS_WITH_SPECIAL_CHARACTER = 'STARTS_WITH_SPECIAL_CHARACTER',
134
+ }
135
+
136
+ export interface Options {
137
+ minLength?: number; // 최소 길이
138
+ maxLength?: number; // 최대 길이
139
+
140
+ spaceMinCount?: number; // 공백 최소 개수
141
+ spaceMaxCount?: number; // 공백 최대 개수
142
+
143
+ numberMinCount?: number; // 숫자 최소 개수
144
+ numberMaxCount?: number; // 숫자 최대 개수
145
+
146
+ alphabetMinCount?: number; // 알파벳 최소 개수
147
+ alphabetMaxCount?: number; // 알파벳 최대 개수
148
+
149
+ alphabetLowerCaseMinCount?: number; // 알파벳(소문자) 최소 개수
150
+ alphabetLowerCaseMaxCount?: number; // 알페벳(소문자) 최대 개수
151
+
152
+ alphabetUpperCaseMinCount?: number; // 알파벳(대문자) 최소 개수
153
+ alphabetUpperCaseMaxCount?: number; // 알파벳(대문자) 최대 개수
154
+
155
+ specialCharacterMinCount?: number; // 특수문자 최소 개수
156
+ specialCharacterMaxCount?: number; // 특수문자 최대 개수
157
+
158
+ startsWithNumber?: boolean; // 숫자로 시작해야하는가
159
+ startsWithAlphabet?: boolean; // 알파벳으로 시작해야하는가
160
+ startsWithSpecialCharacter?: boolean; // 툭수문자로 시작해야하는가
161
+ }
162
+ }
@@ -0,0 +1,3 @@
1
+ export const between = (value: number, from: number, to: number): boolean => {
2
+ return value >= from && value <= to;
3
+ };
@@ -0,0 +1,6 @@
1
+ export const sleep = async (delayInMilliseconds: number, callback?: (...args: any) => void, ...args: any): Promise<any[]> => new Promise((resolve) => {
2
+ setTimeout(() => {
3
+ callback?.(...args);
4
+ resolve(args);
5
+ }, delayInMilliseconds);
6
+ });