[TypeScript] コンソール作成

Node.jsでは実行時に渡される引数の情報はprocess.argvにまとめられている
process.argv[0] = nodeコマンドのパス
process.argv[1] = スクリプトファイルのパス
process.argv[2]以降 = コマンド実行時に渡される引数

src/index.ts

console.log("Node path = " + process.argv[0])
console.log("Script file path = " + process.argv[1])

const data: number[] = []
for(var i = 2; i < process.argv.length; i++){
	data.push(Number(process.argv[i]))
}
console.log(data)

for (let item of data){
	const res = primeFactor(item)
	console.log(item + '= ' + res)
}

function primeFactor(a: number): number[] {
	const v: number[ ] = []
	let x = a
	let n = 2
	while(x > n){
	  if(x % n == 0){
	  	x = x / n
	  	v.push(n)
	  } else {
	  	n += n == 2 ? 1 : 2
	  }
	}
	v.push(x)
	return v
}

$ npm run build
$ node dist/main.js 100 1234 9876
Node path = /usr/bin/node
Script file path = /home/vagrant/dev/hands-on/dist/main.js
[ 100, 1234, 9876 ]
100= 2,2,5,5
1234= 2,617
9876= 2,2,3,823

console.log("Node path = " + process.argv[0])
console.log("Script file path = " + process.argv[1])

const data: number[] = []
for(var i = 2; i < process.argv.length; i++){
	data.push(Number(process.argv[i]))
}
console.log('parameters: ' + data)

const f = aggregate()


for (let item of data){
	const res = f(item)
	console.log(res)
}

function aggregate(): (n:number) => [number, number, number, number, number] {
	let total = 0
	let totalp = 0
	let totalt = 0
	return (n:number):[number, number, number, number, number] => {
		total += n
		let tax = Math.floor(n - n / 1.1)
		totalp += n - tax
		totalt += tax
		return [n, tax, total, totalp, totalt]
	}
}

$ node dist/main.js 1200 1870 3058 2765 1875
Node path = /usr/bin/node
Script file path = /home/vagrant/dev/hands-on/dist/main.js
parameters: 1200,1870,3058,2765,1875
[ 1200, 109, 1200, 1091, 109 ]
[ 1870, 170, 3070, 2791, 279 ]
[ 3058, 278, 6128, 5571, 557 ]
[ 2765, 251, 8893, 8085, 808 ]
[ 1875, 170, 10768, 9790, 978 ]

なるほど、ここまでの知識を使って何か作ってみたいですね