From d59e12f25f82bc1c3f9ecc645b596d13e7b28dcb Mon Sep 17 00:00:00 2001 From: kangjuhyup Date: Thu, 26 Dec 2024 14:27:57 +0900 Subject: [PATCH] init : lib/util --- packages/lib/dto/package.json | 2 + .../lib/dto/src/letter/request/add.letter.ts | 2 +- .../lib/dto/src/letter/response/get.page.ts | 2 +- packages/lib/util/package.json | 42 +++++++++++++++ packages/lib/util/src/attachment.ts | 21 ++++++++ packages/lib/util/src/category.ts | 51 +++++++++++++++++++ packages/lib/util/src/index.ts | 4 ++ packages/lib/util/src/random.ts | 14 +++++ packages/lib/util/src/yn.ts | 10 ++++ packages/lib/util/tsconfig.build.json | 4 ++ packages/lib/util/tsconfig.json | 13 +++++ tsconfig.json | 38 +++++++------- yarn.lock | 29 +++++++++++ 13 files changed, 213 insertions(+), 19 deletions(-) create mode 100644 packages/lib/util/package.json create mode 100644 packages/lib/util/src/attachment.ts create mode 100644 packages/lib/util/src/category.ts create mode 100644 packages/lib/util/src/index.ts create mode 100644 packages/lib/util/src/random.ts create mode 100644 packages/lib/util/src/yn.ts create mode 100644 packages/lib/util/tsconfig.build.json create mode 100644 packages/lib/util/tsconfig.json diff --git a/packages/lib/dto/package.json b/packages/lib/dto/package.json index 8446f97..cf82a8e 100644 --- a/packages/lib/dto/package.json +++ b/packages/lib/dto/package.json @@ -17,11 +17,13 @@ "compile": "tsc" }, "peerDependencies": { + "@lib/util": "*", "@nestjs/swagger": "^7.4.2", "class-transformer": "^0.5.1", "class-validator": "^0.14.1" }, "devDependencies": { + "@lib/util": "*", "@nestjs/cli": "^10.0.0", "@nestjs/schematics": "^10.0.0", "@nestjs/swagger": "^8.1.0", diff --git a/packages/lib/dto/src/letter/request/add.letter.ts b/packages/lib/dto/src/letter/request/add.letter.ts index 4040723..5dd5f66 100644 --- a/packages/lib/dto/src/letter/request/add.letter.ts +++ b/packages/lib/dto/src/letter/request/add.letter.ts @@ -1,4 +1,4 @@ -import { LetterCategoryCode } from '@app/util/category'; +import { LetterCategoryCode } from '@lib/util'; import { ApiProperty } from '@nestjs/swagger'; import { IsBoolean, diff --git a/packages/lib/dto/src/letter/response/get.page.ts b/packages/lib/dto/src/letter/response/get.page.ts index 8005287..04651ff 100644 --- a/packages/lib/dto/src/letter/response/get.page.ts +++ b/packages/lib/dto/src/letter/response/get.page.ts @@ -1,4 +1,3 @@ -import { LetterCategoryCode } from '@app/util/category'; import { ApiProperty } from '@nestjs/swagger'; import { Type } from 'class-transformer'; import { @@ -9,6 +8,7 @@ import { IsString, ValidateNested, } from 'class-validator'; +import { LetterCategoryCode } from '@lib/util'; class LetterPageItem { @ApiProperty({ diff --git a/packages/lib/util/package.json b/packages/lib/util/package.json new file mode 100644 index 0000000..d58c3ac --- /dev/null +++ b/packages/lib/util/package.json @@ -0,0 +1,42 @@ +{ + "name": "@lib/util", + "version": "0.0.1", + "description": "", + "author": "", + "private": true, + "license": "UNLICENSED", + "scripts": { + "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", + "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", + "test": "jest", + "test:watch": "jest --watch", + "test:cov": "jest --coverage", + "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", + "build": "yarn clean && yarn compile", + "clean": "rimraf -rf ./dist", + "compile": "tsc" + }, + "devDependencies": { + "@types/jest": "^29.5.2", + "@types/node": "^20.3.1", + "@types/node-imap": "^0", + "@types/supertest": "^6.0.0", + "@typescript-eslint/eslint-plugin": "^6.0.0", + "@typescript-eslint/parser": "^6.0.0", + "eslint": "^8.42.0", + "eslint-config-prettier": "^9.1.0", + "eslint-plugin-prettier": "^5.2.1", + "eslint-plugin-unused-imports": "^4.1.4", + "jest": "^29.5.0", + "prettier": "^3.0.0", + "source-map-support": "^0.5.21", + "supertest": "^6.3.3", + "ts-jest": "^29.1.0", + "ts-loader": "^9.5.1", + "ts-node": "^10.9.1", + "tsconfig-paths": "^4.2.0", + "tsconfig-paths-jest": "^0.0.1", + "typescript": "^5.1.3" + }, + "packageManager": "yarn@4.5.0" +} diff --git a/packages/lib/util/src/attachment.ts b/packages/lib/util/src/attachment.ts new file mode 100644 index 0000000..7639ce5 --- /dev/null +++ b/packages/lib/util/src/attachment.ts @@ -0,0 +1,21 @@ +export const LetterAttachmentCode = { + THUMBNAIL: 'LA001', + LETTER: 'LA002', + BACKGROUND: 'LA003', + COMPONENT: 'LA004', +} as const; + +export type LetterAttachmentCode = + (typeof LetterAttachmentCode)[keyof typeof LetterAttachmentCode]; + +export const pathToLetterAttachmentCode = (path: string) => { + return path.includes('thm/') + ? LetterAttachmentCode.THUMBNAIL + : path.includes('ltr/') + ? LetterAttachmentCode.LETTER + : path.includes('bgr/') + ? LetterAttachmentCode.BACKGROUND + : path.includes('cpn') + ? LetterAttachmentCode.COMPONENT + : undefined; +}; diff --git a/packages/lib/util/src/category.ts b/packages/lib/util/src/category.ts new file mode 100644 index 0000000..180e274 --- /dev/null +++ b/packages/lib/util/src/category.ts @@ -0,0 +1,51 @@ +export const LetterCategory = { + WEDDING: '결혼식', // 결혼식 + BIRTHDAY: '생일', // 생일 + ANNIVERSARY: '기념일', // 기념일 + PARTY: '파티', // 파티 + GRADUATION: '졸업', // 졸업 + CORPORATE: '행사', // 회사 행사 +} as const; + +export type LetterCategory = + (typeof LetterCategory)[keyof typeof LetterCategory]; + +export const LetterCategoryCode = { + WEDDING: 'LT001', // 결혼식 + BIRTHDAY: 'LT002', // 생일 + ANNIVERSARY: 'LT003', // 기념일 + PARTY: 'LT004', // 파티 + GRADUATION: 'LT005', // 졸업 + CORPORATE: 'LT006', // 회사 행사 +} as const; + +export type LetterCategoryCode = + (typeof LetterCategoryCode)[keyof typeof LetterCategoryCode]; + +const categoryToCodeMap: Record = { + [LetterCategory.WEDDING]: LetterCategoryCode.WEDDING, + [LetterCategory.BIRTHDAY]: LetterCategoryCode.BIRTHDAY, + [LetterCategory.ANNIVERSARY]: LetterCategoryCode.ANNIVERSARY, + [LetterCategory.PARTY]: LetterCategoryCode.PARTY, + [LetterCategory.GRADUATION]: LetterCategoryCode.GRADUATION, + [LetterCategory.CORPORATE]: LetterCategoryCode.CORPORATE, +}; + +const codeToCategoryMap: Record = { + [LetterCategoryCode.WEDDING]: LetterCategory.WEDDING, + [LetterCategoryCode.BIRTHDAY]: LetterCategory.BIRTHDAY, + [LetterCategoryCode.ANNIVERSARY]: LetterCategory.ANNIVERSARY, + [LetterCategoryCode.PARTY]: LetterCategory.PARTY, + [LetterCategoryCode.GRADUATION]: LetterCategory.GRADUATION, + [LetterCategoryCode.CORPORATE]: LetterCategory.CORPORATE, +}; + +export function getCategoryFromCode(code: LetterCategoryCode): LetterCategory { + return codeToCategoryMap[code]; +} + +export function getCodeFromCategory( + category: LetterCategory, +): LetterCategoryCode { + return categoryToCodeMap[category]; +} diff --git a/packages/lib/util/src/index.ts b/packages/lib/util/src/index.ts new file mode 100644 index 0000000..ed53ed3 --- /dev/null +++ b/packages/lib/util/src/index.ts @@ -0,0 +1,4 @@ +export * from './attachment'; +export * from './category'; +export * from './random'; +export * from './yn'; \ No newline at end of file diff --git a/packages/lib/util/src/random.ts b/packages/lib/util/src/random.ts new file mode 100644 index 0000000..7ff613a --- /dev/null +++ b/packages/lib/util/src/random.ts @@ -0,0 +1,14 @@ +/** + * 랜덤한 문자열을 반환한다. ( 기본 길이 10 ) + * @param length + * @returns string + */ +export const randomString = (length: number = 10): string => { + const chars = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + let result = ''; + for (let i = 0; i < length; i++) { + result += chars.charAt(Math.floor(Math.random() * chars.length)); + } + return result; +}; diff --git a/packages/lib/util/src/yn.ts b/packages/lib/util/src/yn.ts new file mode 100644 index 0000000..091353c --- /dev/null +++ b/packages/lib/util/src/yn.ts @@ -0,0 +1,10 @@ +export const YN = { + Y: 'Y', + N: 'N', +} as const; + +export type YN = (typeof YN)[keyof typeof YN]; + +export const booleanToYN = (data: boolean): YN => { + return data === true ? YN.Y : YN.N; +}; diff --git a/packages/lib/util/tsconfig.build.json b/packages/lib/util/tsconfig.build.json new file mode 100644 index 0000000..f523add --- /dev/null +++ b/packages/lib/util/tsconfig.build.json @@ -0,0 +1,4 @@ +{ + "extends" : "./tsconfig.json", + "exclude": ["node_modules","test","dist","**/*spec.ts"] +} \ No newline at end of file diff --git a/packages/lib/util/tsconfig.json b/packages/lib/util/tsconfig.json new file mode 100644 index 0000000..a5003b2 --- /dev/null +++ b/packages/lib/util/tsconfig.json @@ -0,0 +1,13 @@ +{ + "include": ["./src"], + "extends" : "../../../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "baseUrl": "./", + "rootDir": ".", + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "composite": true + } +} + \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 8dd9b75..b0ad1df 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,21 +1,25 @@ { "compilerOptions": { - "module": "commonjs", - "declaration": true, - "removeComments": true, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "allowSyntheticDefaultImports": true, - "target": "ES2021", - "sourceMap": true, - "incremental": true, - "skipLibCheck": true, - "strictNullChecks": false, - "noImplicitAny": false, - "strictBindCallApply": false, - "forceConsistentCasingInFileNames": false, - "noFallthroughCasesInSwitch": false, - "baseUrl": "./", - "composite": true + "module": "commonjs", + "declaration": true, + "removeComments": true, + "emitDecoratorMetadata": true, + "experimentalDecorators": true, + "allowSyntheticDefaultImports": true, + "target": "ES2021", + "sourceMap": true, + "incremental": true, + "skipLibCheck": true, + "strictNullChecks": false, + "noImplicitAny": false, + "strictBindCallApply": false, + "forceConsistentCasingInFileNames": false, + "noFallthroughCasesInSwitch": false, + "baseUrl": "./", + "composite": true, + "paths": { + "@lib/util/*": ["packages/lib/util/src/*"], + "@lib/dto/*": ["packages/lib/dto/src/*"] + } } } diff --git a/yarn.lock b/yarn.lock index b54f358..1bef91d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2205,6 +2205,7 @@ __metadata: version: 0.0.0-use.local resolution: "@lib/dto@workspace:packages/lib/dto" dependencies: + "@lib/util": "npm:*" "@nestjs/cli": "npm:^10.0.0" "@nestjs/schematics": "npm:^10.0.0" "@nestjs/swagger": "npm:^8.1.0" @@ -2234,12 +2235,40 @@ __metadata: typescript: "npm:^5.1.3" webpack: "npm:^5.97.1" peerDependencies: + "@lib/util": "*" "@nestjs/swagger": ^7.4.2 class-transformer: ^0.5.1 class-validator: ^0.14.1 languageName: unknown linkType: soft +"@lib/util@npm:*, @lib/util@workspace:packages/lib/util": + version: 0.0.0-use.local + resolution: "@lib/util@workspace:packages/lib/util" + dependencies: + "@types/jest": "npm:^29.5.2" + "@types/node": "npm:^20.3.1" + "@types/node-imap": "npm:^0" + "@types/supertest": "npm:^6.0.0" + "@typescript-eslint/eslint-plugin": "npm:^6.0.0" + "@typescript-eslint/parser": "npm:^6.0.0" + eslint: "npm:^8.42.0" + eslint-config-prettier: "npm:^9.1.0" + eslint-plugin-prettier: "npm:^5.2.1" + eslint-plugin-unused-imports: "npm:^4.1.4" + jest: "npm:^29.5.0" + prettier: "npm:^3.0.0" + source-map-support: "npm:^0.5.21" + supertest: "npm:^6.3.3" + ts-jest: "npm:^29.1.0" + ts-loader: "npm:^9.5.1" + ts-node: "npm:^10.9.1" + tsconfig-paths: "npm:^4.2.0" + tsconfig-paths-jest: "npm:^0.0.1" + typescript: "npm:^5.1.3" + languageName: unknown + linkType: soft + "@ljharb/through@npm:^2.3.12": version: 2.3.13 resolution: "@ljharb/through@npm:2.3.13"