eyo-kernel
Advanced tools
Comparing version 2.3.1 to 2.4.0
# Changelog | ||
## v2.4.0 | ||
Поддержка словарей сжатых с помощью `gzip` (`*.txt.gz`) #8. | ||
## v2.3.1 | ||
@@ -4,0 +7,0 @@ - Мелкие правки. |
'use strict'; | ||
const fs = require('fs'); | ||
const zlib = require('zlib'); | ||
const path = require('path'); | ||
const pathSafeDict = '../dict/safe.txt'; | ||
const pathNotSafeDict = '../dict/not_safe.txt'; | ||
const pathSafeDict = '../dict/safe.txt.gz'; | ||
const pathNotSafeDict = '../dict/not_safe.txt.gz'; | ||
module.exports = class Dictionary { | ||
@@ -20,11 +22,33 @@ constructor() { | ||
load(filename, callback) { | ||
fs.readFile(filename, 'utf8', (err, data) => { | ||
if (!err) { | ||
this.set(data); | ||
fs.readFile(filename, (err, buffer) => { | ||
if (err) { | ||
callback(err, buffer); | ||
} else { | ||
this._unzip(filename, buffer, (err, data) => { | ||
let dict = data; | ||
if (!err) { | ||
dict = data.toString('utf8'); | ||
this.set(dict); | ||
} | ||
callback(err, dict); | ||
}); | ||
} | ||
callback(err, data); | ||
}); | ||
} | ||
_isGZ(filename) { | ||
return filename.search(/\.gz$/i) > -1; | ||
} | ||
_unzip(filename, buffer, callback) { | ||
if (this._isGZ(filename)) { | ||
zlib.gunzip(buffer, (err, data) => { | ||
callback(err, data); | ||
}); | ||
} else { | ||
callback(null, buffer); | ||
} | ||
} | ||
/** | ||
@@ -36,6 +60,12 @@ * Синхронно загружает словарь. | ||
loadSync(filename) { | ||
const text = fs.readFileSync(filename, 'utf8'); | ||
this.set(text); | ||
let buffer = fs.readFileSync(filename); | ||
buffer = this._unzipSync(filename, buffer); | ||
this.set(buffer.toString('utf8')); | ||
} | ||
_unzipSync(filename, buffer) { | ||
return this._isGZ(filename) ? zlib.gunzipSync(buffer) : buffer; | ||
} | ||
/** | ||
@@ -42,0 +72,0 @@ * Загружает безопасный встроенный словарь. |
{ | ||
"name": "eyo-kernel", | ||
"description": "Restoring the letter «ё» (yo) in russian texts", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"author": { | ||
@@ -36,3 +36,3 @@ "name": "Denis Seleznev", | ||
"chai": "^4.1.2", | ||
"eslint": "^4.19.1", | ||
"eslint": "^5.0.1", | ||
"istanbul": "^0.4.5", | ||
@@ -39,0 +39,0 @@ "mocha": "^5.2.0" |
@@ -32,3 +32,3 @@ Восстановление буквы «ё» в русских текстах | ||
const safeEyo = new Eyo(); | ||
safeEyo.dictionary.loadSafeSync(); // ./dict/safe.txt | ||
safeEyo.dictionary.loadSafeSync(); // ./dict/safe.txt.gz | ||
console.log(safeEyo.restore(text)); | ||
@@ -39,3 +39,3 @@ console.log(safeEyo.lint(text)); | ||
const notSafeEyo = new Eyo(); | ||
notSafeEyo.dictionary.loadNotSafeSync(); // ./dict/not_safe.txt | ||
notSafeEyo.dictionary.loadNotSafeSync(); // ./dict/not_safe.txt.gz | ||
console.log(notSafeEyo.restore(text)); | ||
@@ -46,2 +46,3 @@ console.log(notSafeEyo.lint(text)); | ||
const eyo = new Eyo(); | ||
// Также поддерживаются словари, сжатые с помощью gzip, *.txt.gz | ||
eyo.dictionary.loadSync('./my_eyo_dict.txt'); | ||
@@ -48,0 +49,0 @@ console.log(eyo.restore(text)); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
290
79
115027