-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtxt.js
41 lines (35 loc) · 1.03 KB
/
txt.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
37
38
39
40
41
import readline from 'readline'
import { sort, compareOn } from '../sorting.js'
import { createStore } from '../storage.js'
import { finished } from 'stream'
import fs from 'fs'
import { promisify } from 'util'
const log = msg => console.log(`${new Date()} ${msg}`)
async function writeLines(lines, filename) {
const writeStream = fs.createWriteStream(filename)
for await (const line of lines) {
writeStream.write(line + '\n')
}
writeStream.end()
await promisify(finished)(writeStream)
}
async function * readLines (filename) {
yield * readline.createInterface(fs.createReadStream(filename))
}
async function write (chunk, filename) {
await writeLines(chunk, filename)
log(`created ${filename}`)
return readLines(filename)
}
const sortTxt = async () => {
const input = readLines('./temp/input.txt')
const sorted = sort(input, {
maxSize: 1000000,
maxFiles: 1000,
comparer: compareOn(x => x),
store: createStore(write)})
await writeLines(sorted, './temp/sorted.txt')
}
log('start')
await sortTxt()
log('end')