-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (30 loc) · 1.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const fs = require('fs');
const { delimiter } = require('path');
const chunk = require('chunk-text');
const Text = require('./text.js');
// const re = /\s?(?:\w+\s?){1,15}/g;
//read the file & put into a input string
let input = fs.readFileSync('input.txt').toString();
let textArr = [];
//split by \n and create array of Text objects
let sentenceArr = input.toString().split('\n');
sentenceArr.map((sentence) => {
//split by 100 characters and create a Text object
let wordList = chunk(sentence, 100);
if(wordList.length > 1) {
let lastElem = wordList.length -1;
wordList.map((fragment, index) => {
delim = (index == lastElem) ? '\n' : ' '
textArr.push(new Text(fragment, delim))
});
} else {
textArr.push(new Text(sentence, '\n'));
}
})
// console.log(textArr);
Promise.all(textArr.map((elem) => elem.transliterate())).then((result) => {
fs.writeFile('output.txt', result.join(''), function (err) {
if (err) throw err;
console.log('saved result into output.txt');
});
})