2017-12-07 13 views
0

저는 신용 카드 식별 및 유효성 검사를위한 프로젝트를 진행하고 있습니다. 노드 파일을 사용하여 다른 파일에 쓰기를 시도하고 있습니다. 배열을 사용하고,이 함수가 출력하는 각 행을 채 웁니다. 전체 코드 블록은 다음과 같습니다.어떻게하면이 신용 카드 유효성 검사기/식별자 출력을 배열로 만들 수 있습니까?

import { createReadStream } from 'fs' 
import { createInterface } from 'readline' 
import { getCompanyName } from './cardType' 
import cardValidator from './cardValidator' 
import * as fs from 'fs' 

const lineReader = createInterface({ 
input: createReadStream('data/input.txt') 
}) 

const output: string[] = [] 

lineReader.on('line', (creditCard: string) => { 
var company = `${getCompanyName(creditCard)}: ${creditCard} (${ 
cardValidator(creditCard) ? 'valid' : 'invalid' 
})` 

//make company output an array called results then use results to 
//wright lines to the output file 

fs.writeFile('./data/output.txt', results[0, 1, 2, 3, etc], err => { 
if (err) throw err && console.log('it broke as heck') 
console.log('The file has been saved!') 
}) 
}) 

어떻게하면됩니까?

답변

0

전체 코드 블록 지금 그것은이다 등을 사용하는 것이 방법과 유사 완료 및 작업 :

import { createReadStream } from 'fs' 
import { createInterface } from 'readline' 
import { getCompanyName } from './cardType' 
import cardValidator from './cardValidator' 
import * as fs from 'fs' 

const lineReader = createInterface({ 
input: createReadStream('data/input.txt') 
}) 

const output: string[] = [] 

lineReader.on('line', (creditCard: string) => { 
output.push(
`${getCompanyName(creditCard)}: ${creditCard} (${ 
    cardValidator(creditCard) ? 'valid' : 'invalid' 
})` 
) 
}) 

lineReader.on('close',() => { 
fs.writeFile('./data/output.txt', output.join('\n'), err => { 
if (err) throw err 
console.log('The file has been saved!') 
}) 
}) 

내가 output.push에 카드를 처리하는 경우에 추가는 새로운 0123를 생성처리 된 카드를 작성하는 기능 output.txt

0

당신이 done 이벤트를 얻을 가정, 간단한 콜백과 같이 작동합니다 :

const read = (cb: (lines: string) => void) => { // Wrap in a new function 
    const lineReader = createInterface({ 
    input: createReadStream('data/input.txt') 
    }) 

    const output: string[] = [] 

    lineReader.on('line', (creditCard: string) => { 
    output.push(creditCard); 
    }); 

    lineReader.on('done', (creditCard: string) => { 
    cb(output) 
    }); 
} 

이것은 당신이 setTimeout 여기

+0

완료 이벤트가 무슨 뜻인지 이해할 수 없기 때문에이 기능이 작동하지 않습니다. 검색 한 결과 아무것도 찾을 수 없습니다. –