Skip to content

Commit

Permalink
Merge pull request #24 from mbti-nf-team/feat/issue-23
Browse files Browse the repository at this point in the history
  • Loading branch information
saseungmin authored Jul 18, 2023
2 parents b02f6bb + 8f076b3 commit 9c02e22
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-maps-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nft-team/core": major
---

feat(@nft-team/core): 공통으로 사용되는 유틸함수를 core 라이브러리로 생성 (checkEmpty, checkNull, checkNumNull, emptyAThenB, generateArrayOfNumber, isEmpty, trueOrFalse)
36 changes: 36 additions & 0 deletions packages/core/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = {
root: true,
env: {
es6: true,
browser: true,
jest: true,
},
ignorePatterns: [
'node_modules/',
'public/',
'dist/',
'coverage/',
],
extends: ['@nft-team/eslint-config'],
overrides: [
{
files: ['*.ts', '*.tsx'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: ['./tsconfig.json'],
},
},
{
files: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
rules: {
'react-hooks/rules-of-hooks': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
},
],
rules: {
'import/export': 'off',
'import/prefer-default-export': 'off',
'import/no-extraneous-dependencies': 'off',
},
};
15 changes: 15 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 🚧 WIP 🚧 @nft-team/core

<a href="https://github.com/mbti-nf-team/frontend-libraries/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc">
<img src="https://img.shields.io/github/issues/mbti-nf-team/frontend-libraries?style=flat-square" />
</a>

<a href="https://github.com/mbti-nf-team/frontend-libraries/blob/main/LICENSE">
<img src="https://img.shields.io/github/license/mbti-nf-team/frontend-libraries?style=flat-square">
</a>

## 🐛 Bug reporting
[Issues](https://github.com/mbti-nf-team/frontend-libraries/issues)

## 🔒 LICENSE
This Project is [MIT licensed](https://github.com/mbti-nf-team/frontend-libraries/blob/main/LICENSE).
11 changes: 11 additions & 0 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
transform: {
'^.+\\.(t|j)sx?$': [
'@swc/jest',
],
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testPathIgnorePatterns: ['node_modules'],
moduleDirectories: ['node_modules', '<rootDir>/'],
testEnvironment: 'jest-environment-jsdom',
};
2 changes: 2 additions & 0 deletions packages/core/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import 'jest-plugin-context/setup';
import 'given2/setup';
48 changes: 48 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@nft-team/core",
"version": "0.0.0",
"description": "nft team 유틸 함수",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"license": "MIT",
"author": {
"email": "[email protected]",
"name": "SEUNGMIN SA (Harang)",
"url": "https://github.com/saseungmin"
},
"repository": {
"type": "git",
"url": "https://github.com/mbti-nf-team/frontend-libraries.git"
},
"scripts": {
"build": "tsup src/index.ts --format cjs --dts",
"dev": "yarn run build -- --watch",
"test:unit": "jest",
"test:watch": "jest --watch",
"test:watchAll": "jest --watchAll",
"test:coverage": "yarn test:unit --coverage",
"lint": "eslint \"**/*.ts*\""
},
"devDependencies": {
"@nft-team/eslint-config": "1.1.1",
"@nft-team/tsconfig": "0.0.1",
"@swc/core": "^1.3.67",
"@swc/jest": "^0.2.26",
"@types/jest": "^29.5.2",
"@types/jest-plugin-context": "^2.9.5",
"eslint": "^8.44.0",
"given2": "^2.1.7",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"jest-plugin-context": "^2.9.0",
"tsup": "^7.1.0",
"typescript": "^4.5.2"
},
"publishConfig": {
"access": "public"
},
"bugs": {
"email": "[email protected]",
"url": "https://github.com/mbti-nf-team/frontend-libraries/issues"
}
}
4 changes: 4 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
checkEmpty, checkNull, checkNumNull, emptyAThenB, generateArrayOfNumber,
isEmpty, trueOrFalse,
} from './utils';
149 changes: 149 additions & 0 deletions packages/core/src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import {
checkEmpty,
checkNull,
checkNumNull,
emptyAThenB,
generateArrayOfNumber,
isEmpty,
trueOrFalse,
} from './utils';

describe('checkNull', () => {
context('value가 null일 경우', () => {
context('replacementValue가 존재하지 않는 경우', () => {
it('빈 문자열을 반환해야만 한다', () => {
const result = checkNull(null);

expect(result).toBe('');
});
});

context('replacementValue가 존재하는 경우', () => {
it('대체할 문자열(두 번째 인자)을 반환해야만 한다', () => {
const result = checkNull(undefined, '-');

expect(result).toBe('-');
});
});
});

context('value가 null이 아닌 경우', () => {
it('입력된 값이 반환되어야만 한다', () => {
const result = checkNull('nana');

expect(result).toBe('nana');
});
});
});

describe('checkNumNull', () => {
context('value가 null인 경우', () => {
it('0을 반환해야 한다', () => {
const result = checkNumNull(0);

expect(result).toBe(0);
});
});

context('value가 null이 아닌 경우', () => {
it('입력된 값이 반환되어야 한다', () => {
const result = checkNumNull(100);

expect(result).toBe(100);
});
});
});

describe('emptyAThenB', () => {
const b = 'B';

context('null일 경우', () => {
it('첫 번째 인자를 반환야만 한다', () => {
const result = emptyAThenB(b, null);

expect(result).toBe(b);
});
});

context('문자열일 경우', () => {
const a = 'test';

it('두 번째 인자를 반환야만 한다', () => {
const result = emptyAThenB(b, a);

expect(result).toBe(a);
});
});
});

describe('trueOrFalse', () => {
context('falsy한 값이면', () => {
it('false를 반환해야만 한다', () => {
const result = trueOrFalse('');

expect(result).toBe(false);
});
});

context('truthy한 값이면', () => {
it('true를 반환해야만 한다', () => {
const result = trueOrFalse('test');

expect(result).toBe(true);
});
});
});

describe('checkEmpty', () => {
context('value가 undefined이거나 빈 배열인 경우', () => {
it('빈 배열을 반환해야만 한다', () => {
const result = checkEmpty();

expect(result).toEqual([]);
});
});

context('value가 undefined이거나 빈 배열이 아닌 경우', () => {
const mockArray = ['test', 'test2'];

it('입력된 값이 반환되어야 한다', () => {
const result = checkEmpty(mockArray);

expect(result).toEqual(mockArray);
});
});
});

describe('isEmpty', () => {
context('falsy한 값인 경우', () => {
it('true를 반환해야만 한다', () => {
const result = isEmpty(undefined);

expect(result).toBeTruthy();
});
});

context('빈 배열이 아닌 경우', () => {
it('false를 반환해야만 한다', () => {
const result = isEmpty([1]);

expect(result).toBeFalsy();
});
});

context('빈 배열인 경우', () => {
it('true를 반환해야만 한다', () => {
const result = isEmpty([]);

expect(result).toBeTruthy();
});
});
});

describe('generateArrayOfNumber', () => {
it('길이가 5인 배열을 반환해야만한다', () => {
const result = generateArrayOfNumber(5);

expect(result.length).toBe(5);
});
});
51 changes: 51 additions & 0 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export const checkNull = (value?: string | null, replacementValue?: string): string => {
if (value) {
return value;
}

if (replacementValue) {
return replacementValue;
}

return '';
};

export const checkNumNull = (value?: number | null): number => {
if (!value) {
return 0;
}

return value;
};

export const emptyAThenB = (b: string, a?: string | null): string => a || b;

export const trueOrFalse = (value?: string | null | number | boolean): boolean => {
if (!value) {
return false;
}

return true;
};

export const checkEmpty = <T>(value?: T[]): T[] => {
if (!value || !value.length) {
return [];
}

return value;
};

export const isEmpty = <T>(value?: T[]) => {
if (!value) {
return true;
}

if (value.length > 0) {
return false;
}

return true;
};

export const generateArrayOfNumber = (length: number) => Array.from({ length }, (_, i) => i);
5 changes: 5 additions & 0 deletions packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "@nft-team/tsconfig/base.json",
"include": ["."],
"exclude": ["dist", "build", "node_modules"]
}
20 changes: 20 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,26 @@ __metadata:
languageName: node
linkType: hard

"@nft-team/core@workspace:packages/core":
version: 0.0.0-use.local
resolution: "@nft-team/core@workspace:packages/core"
dependencies:
"@nft-team/eslint-config": 1.1.1
"@nft-team/tsconfig": 0.0.1
"@swc/core": ^1.3.67
"@swc/jest": ^0.2.26
"@types/jest": ^29.5.2
"@types/jest-plugin-context": ^2.9.5
eslint: ^8.44.0
given2: ^2.1.7
jest: ^29.5.0
jest-environment-jsdom: ^29.5.0
jest-plugin-context: ^2.9.0
tsup: ^7.1.0
typescript: ^4.5.2
languageName: unknown
linkType: soft

"@nft-team/[email protected], @nft-team/eslint-config@workspace:^, @nft-team/eslint-config@workspace:packages/eslint":
version: 0.0.0-use.local
resolution: "@nft-team/eslint-config@workspace:packages/eslint"
Expand Down

0 comments on commit 9c02e22

Please sign in to comment.