我目前正在开发一个项目,其中使用行读取器将信用卡号输入验证器和标识符。假设我输入了 10 个号码,它们来自四家不同的信用卡公司。我想忽略三个公司,只显示其余公司的数字。
这家公司的规定(条件)是必须有15位数字,并且以37或34开头
4111111111111111
4111111111111
4012888888881881
378282246310005
6011111111111117
5105105105105100
5105105105105106
9111111111111111
371449635398431
378734493671000
这是我当前的临时模块:
export const isAmex = (creditCard: string): boolean =>
creditCard.length === 15 &&
(creditCard.substring(0, 2) === '37' || creditCard.substring(0, 2) === '34')
export const is2Amex = (creditCard: string): boolean =>
creditCard.length === 15 &&
(creditCard.substring(0, 2) === '37' || creditCard.substring(0, 2) === '34')
export const ifIsntAmex = (creditCard: string) => {
if (!is2Amex(creditCard)) {
return ' '
} else {
return creditCard
}
}
export const getAmexName = (creditCard: string) => {
if (!isAmex(creditCard)) {
return ' '
} else {
return 'AMEX'
}
}
这是索引的一部分:
const outputAmex: string[] = []
lineReader.on('line', (creditCard: string) => {
outputAmex.push(
`${getAmexName(creditCard)}: ${ifIsntAmex(creditCard)}
(${cardValidator(creditCard) ? 'valid' : 'invalid'})`
)
})
lineReader.on('close', () => {
fs.writeFile('./data/Amex.txt', outputAmex.join('\n'), err => {
if (err) throw err
console.log('The file has been saved!')
})
})
它返回类似这样的内容:
: (valid)
: (invalid)
: (valid)
AMEX: 378282246310005 (invalid)
: (valid)
: (valid)
: (invalid)
: (invalid)
AMEX: 371449635398431 (invalid)
AMEX: 378734493671000 (invalid)
如何忽略非 Amex 线路?
最佳答案
lineReader.on('line', (creditCard: string) => {
if (getAmexName(creditCard)) {
outputAmex.push(
`${getAmexName(creditCard)}: ${creditCard} (${
cardValidator(creditCard) ? 'valid' : 'invalid'
})`
)
}
})
这最终对我有用。
关于node.js - 如何使 Node 行读取器有条件地跳过行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47705735/