본문 바로가기

JavaScript TypeScript6

[Node] class 혹은 function의 file path를 찾아보자 가장 쉬운 방법은console.log(__filename);console.log(__dirname); __filename과 __dirname을 이용해 찾는 것이다. 하지만 이 방법은 그 class와 function의 내부에 직접적으로 선언해주어야하며, 만약 해당 class나 function을 export해서 외부에서 써야하는 상황이라면 __filename과 __dirname을 이용해서는 알 수가 없다. 외부에서 __filename과 __dirname을 사용하면 외부의 파일 경로가 나오게 된다.따라서 이는 모든 상황에서 적용될 수 없으며, 알고자 하는 class나 function을 반드시 건드려야하는 큰 단점이 있다.(사실상 쓸 수 없음) 실제로 광범위하게 적용할 수 있는 방법에는 3가지 정도가 있다. 1.. 2023. 11. 16.
[TypeScript] Decorator를 이용한 코드 변경 class TestClass { test() { console.log('test!'); } } 위와 같은 클래스가 존재할 때, test() 메소드를 실행시키면 test! 가 터미널에 찍힐 것이다. 이를 test? 로 출력하기 위해 데코레이터로 코드를 바꾸어 실행 결과를 바꿀 수 있다. function Test(target: any, key: string, descriptor: PropertyDescriptor) { const stringified = String(descriptor.value); const matches = stringified.match(/\{([^}]+)\}/); const extracted = matches[1].replace('!', '?'); const execute = new .. 2023. 11. 7.
[RxJS - 4] pipe(), map() function createHttpObservable(url: string) return Observable.create(observer => { fetch(url) .then(response => { return response.json(); }) .then(body => { observer.next(body); // Subscriber에게 데이터 전달 observer.complete(); // 데이터 전달 완료 }) .catch(err => { observer.error(err); }) }); 위와 같은 Observable 객체를 만드는 함수가 있다고 하자. pipe()는 Observable 객체를 새로운 Observable 객체로 만들어준다. 그리고 map()은 우리가 알고 있는 그 map과 동일하.. 2023. 10. 6.
[RxJS - 3] Observable 생성 fetch('/api') /* { "items": [ {"id": "Open"}, {"id": "OpenNew", "label": "Open New"}, {"id": "Find", "label": "Find..."}, {"id": "FindAgain", "label": "Find Again"}, {"id": "Copy"}, {"id": "CopyAgain", "label": "Copy Again"}, {"id": "CopySVG", "label": "Copy SVG"}, {"id": "ViewSVG", "label": "View SVG"}, {"id": "ViewSource", "label": "View Source"}, ] } */ 위와 같은 API가 있다고 가정했을 때, `fetch('/api')`는.. 2023. 9. 26.
[RxJS - 2] Observable의 파라미터 import { interval, take } from 'rxjs'; const interval$ = interval(1000); // 관례적으로 Observable 객체는 $를 접미로 붙인다. interval$.subscribe(val => console.log("Observable", val)); /* Observable 1 Observable 2 Observable 3 Observable 4 ... */ 이전 글의 위와 같은 상황에서, subscribe의 3가지 파라미터를 알아보자. interval$.subscribe(val => console.log("Observable", val), err => console.log(err) () => console.log("finished!") ); 첫 번째는.. 2023. 9. 26.