The following code takes the content of a file, replace some characters, and outputs the result:
test.txt:
# Title
## Title 2
Paragraph
index.js:
#!/usr/bin/env node
'use strict'
var fs = require('fs')
, filename = process.argv[2]
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME')
process.exit(1)
}
function massReplace(text, replacementArray) {
let results = text
for (let [regex, replacement] of replacementArray) {
results = results.replace(regex, replacement)
}
return results
}
function transformHeadings(text, orig) {
return massReplace(text,
[/^## (.*)/gm, '<h2>$1</h2>'],
[/^# (.*)/gm, '<h1>$1</h1>'] ]
)
}
fs.readFile(filename, 'utf8', function(err, data) {
if (err) throw err
data = data.split(/\n\n/gm)
var tree = data.slice()
console.log(transformHeadings(data, tree))
})
I get this error:
alex@alex-K43U:~/node/m2n$ babel-node index4.js test.txt
/home/alex/node/m2n/index4.js:41
throw _iteratorError;
^
TypeError: undefined is not a function
at massReplace (/home/alex/node/m2n/index4.js:17:4)
at transformHeadings (/home/alex/node/m2n/index4.js:30:2)
at /home/alex/node/m2n/index4.js:39:3
at fs.js:336:14
at FSReqWrap.oncomplete (fs.js:99:15)
No idea what the problem is, nor what throw _iteratorError
means.
I'm using Babel to parse the ES6 code.
What could be the problem?
Aucun commentaire:
Enregistrer un commentaire