Skip to content

Commit

Permalink
Added ability to change loading csv files directory
Browse files Browse the repository at this point in the history
  • Loading branch information
alordash committed Jan 29, 2022
1 parent 1086cdf commit 2e4a177
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/date-parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ConvertedWord, Expression, LoadSeparators } = require('./loader');
const { ConvertedWord, Expression, LoadSeparators, SetSeparatorsDirectoryPath, SetExpressionsDirectoryPath } = require('./loader');
const { isDateType, isTimeType, getDateProperty, ValidModes, ParsedTime, ContextsData, extractTime, TimeTypes, DateTypes } = require('./date-cases/date-cases');
const { extractRegexChars } = require('./regex-extractor');
const { proceedNotUsedContexts } = require('./contexts-handling');
Expand Down Expand Up @@ -546,5 +546,7 @@ module.exports = {
TimeList,
extractRegexChars,
parseDate,
wordsParseDate
wordsParseDate,
SetSeparatorsDirectoryPath,
SetExpressionsDirectoryPath
}
54 changes: 38 additions & 16 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
const fs = require('fs');
const separatorsFilename = 'separators.csv';
const monthSizesFilename = 'months_size.csv'

let expressionsDirectory = __dirname + "/expressions";
let separatorsDirectory = __dirname + "/expressions";

class ConvertedWord {
/**@type {String} */
text;
/**@type {Array.<Number>} */
indexes;
/**@type {String} */
text;
/**@type {Array.<Number>} */
indexes;

/**@param {String} text
* @param {Array.<Numbe>} indexes
*/
constructor(text, indexes) {
this.text = `${text}`;
this.indexes = indexes;
}
/**@param {String} text
* @param {Array.<Numbe>} indexes
*/
constructor(text, indexes) {
this.text = `${text}`;
this.indexes = indexes;
}
}

class Expression {
Expand Down Expand Up @@ -92,7 +96,7 @@ class MonthSize {
* @returns {ParsingRules} Parsing Rules
*/
function LoadExpressionsFile(fileName) {
let raw = fs.readFileSync(__dirname + `/expressions/${fileName}`, { encoding: 'utf-8' }).split(/\n/);
let raw = fs.readFileSync(`${expressionsDirectory}/${fileName}`, { encoding: 'utf-8' }).split(/\n/);
let metadata = {};
let params = raw[1].split(';');
let values = raw[2].split(';');
Expand All @@ -110,11 +114,27 @@ function LoadExpressionsFile(fileName) {
}

/**
* Loads all expressions from .csv files in ./lib/expressions/
* Sets path to directory from which all expressions should be extracted
* @param {String} directoryPath
*/
function SetExpressionsDirectoryPath(directoryPath) {
expressionsDirectory = directoryPath;
}

/**
* Sets path to directory from which all separators should be extracted
* @param {String} directoryPath
*/
function SetSeparatorsDirectoryPath(directoryPath) {
separatorsDirectory = directoryPath;
}

/**
* Loads all expressions from .csv files in ./lib/expressions/ by default
* @returns {Array<ParsingRules>} array of expression clusters
*/
function LoadExpressions() {
let filenames = fs.readdirSync(__dirname + '/expressions/');
let filenames = fs.readdirSync(expressionsDirectory);
let parsingRules = [];
for (const filename of filenames) {
if (filename.split('.').pop() == 'csv' && filename != separatorsFilename) {
Expand All @@ -129,7 +149,7 @@ function LoadExpressions() {
* @returns {ParsingRules}
*/
function LoadSeparators() {
let raw = fs.readFileSync(__dirname + `/expressions/${separatorsFilename}`, { encoding: 'utf-8' }).split(/\n/);
let raw = fs.readFileSync(`${separatorsDirectory}\\${separatorsFilename}`, { encoding: 'utf-8' }).split(/\n/);
let metadata = {};
let params = raw[1].split(';');
let values = raw[2].split(';');
Expand Down Expand Up @@ -170,5 +190,7 @@ module.exports = {
LoadExpressionsFile,
LoadExpressions,
LoadSeparators,
LoadMonthSize
LoadMonthSize,
SetExpressionsDirectoryPath,
SetSeparatorsDirectoryPath
}

0 comments on commit 2e4a177

Please sign in to comment.