node.js - 如何使 Node 行读取器有条件地跳过行

标签 node.js typescript

我目前正在开发一个项目,其中使用行读取器将信用卡号输入验证器和标识符。假设我输入了 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/

相关文章:

javascript - jquery 函数范围内类的 TypeScript 调用方法

javascript - 如何在发送 Nest.js 之前格式化响应?

typescript - 是否可以将部分通配符分配给 typescript 中的类型?

node.js - Get对象没有方法 'executeDbCommand'

javascript - Yeoman Web 应用程序生成器 LiveReload 片段不工作

node.js - 从 mongodb 数组中删除一个元素

javascript - 这可以重构为示例中看到的更具可组合性的样式吗?

typescript - 在NestJS中手动注册一个typeorm EventSubscriber,而不在配置文件中指定

node.js - Angular 6 端口 [编号] 已在使用中。使用 '--port' 指定不同的端口

node.js - 在谷歌云上运行 node.js,但使用 docker 运行时出错