我如何将此信用卡验证器/标识符输出到数组
How would I make this credit card validator /identifier output to an array
我正在做一个信用卡识别和验证的项目,我正在尝试使用节点 fs 写入另一个文件,我的目标是使用一个数组,填充此函数的每一行输出。这是我的完整代码块:
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!')
})
})
我将如何做到这一点?
假设你得到一个 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
等的方式。
现在这是完整的代码块它已经完成并正在运行:
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
然后创建了一个新的 lineReader
函数将处理过的卡片写入 output.txt
我正在做一个信用卡识别和验证的项目,我正在尝试使用节点 fs 写入另一个文件,我的目标是使用一个数组,填充此函数的每一行输出。这是我的完整代码块:
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!')
})
})
我将如何做到这一点?
假设你得到一个 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
等的方式。
现在这是完整的代码块它已经完成并正在运行:
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
然后创建了一个新的 lineReader
函数将处理过的卡片写入 output.txt