puvox-library 1.0.34 → 1.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1068,6 +1068,86 @@ const puvox_library =
1068
1068
  },
1069
1069
 
1070
1070
 
1071
+ // https://stackoverflow.com/a/7924240/2377343 (has better performance than (ch.match(/\n/g) || []).length or ch.split('\n').length - 1 )
1072
+ occurences_amount(string, subString, allowOverlapping) {
1073
+ string += "";
1074
+ subString += "";
1075
+ if (subString.length <= 0) return (string.length + 1);
1076
+ var n = 0, pos = 0, step = allowOverlapping ? 1 : subString.length;
1077
+ while (true) {
1078
+ pos = string.indexOf(subString, pos);
1079
+ if (pos >= 0) { ++n; pos += step; } else break;
1080
+ }
1081
+ return n;
1082
+ },
1083
+
1084
+ // startReadStream(__dirname + '/aa.csv', ()=>{});
1085
+ async readLineByLine (filePath, callback, linesSize = 10000, delimiterN = true) {
1086
+ // https://stackoverflow.com/questions/6156501/read-a-file-one-line-at-a-time-in-node-js
1087
+ // https://stackoverflow.com/questions/31479379/nodejs-read-very-large-file10gb-process-line-by-line-then-write-to-other-fil
1088
+ // https://stackoverflow.com/questions/7545147/nodejs-synchronization-read-large-file-line-by-line
1089
+ // https://stackoverflow.com/questions/16010915/parsing-huge-logfiles-in-node-js-read-in-line-by-line
1090
+ // calblack format: callback(linesArray, isLastChunk)
1091
+ let resolver = null;
1092
+ let rejector = null;
1093
+ const prom = new Promise((resolve, reject) => {
1094
+ resolver = resolve;
1095
+ rejector = reject;
1096
+ });
1097
+ let buffer = '';
1098
+ const readStream = fs.createReadStream(filePath);
1099
+ let collectedLines = [];
1100
+ readStream.on('data', async function(chunk) {
1101
+ var lines = (buffer + chunk).split(/\n/g);
1102
+ buffer = lines.pop();
1103
+ collectedLines = collectedLines.concat(lines);
1104
+ const length = collectedLines.length;
1105
+ if (length > linesSize) {
1106
+ const remnant = collectedLines.splice(linesSize);
1107
+ await callback(collectedLines, false);
1108
+ collectedLines = remnant;
1109
+ }
1110
+ });
1111
+ readStream.on('end', async function() {
1112
+ collectedLines = collectedLines.concat([buffer]);
1113
+ await callback(collectedLines, true);
1114
+ resolver();
1115
+ });
1116
+ readStream.on('error', function() {
1117
+ rejector();
1118
+ });
1119
+ return prom;
1120
+ },
1121
+ async linesAmountInFile(filePath, delimiterN = true) {
1122
+ const self = this;
1123
+ // calblack format: callback(linesArray, isLastChunk)
1124
+ let resolver = null;
1125
+ let rejector = null;
1126
+ const prom = new Promise((resolve, reject) => {
1127
+ resolver = resolve;
1128
+ rejector = reject;
1129
+ });
1130
+ let buffer = '';
1131
+ const readStream = fs.createReadStream(filePath);
1132
+ let linesAmount = 0;
1133
+ readStream.on('data', async function(chunk) {
1134
+ const ch = chunk + '';
1135
+ const curLenth = self.helpers.occurences_amount(ch, '\n');
1136
+ linesAmount = linesAmount + curLenth;
1137
+ });
1138
+ readStream.on('end', async function() {
1139
+ resolver(linesAmount);
1140
+ });
1141
+ readStream.on('error', function() {
1142
+ rejector();
1143
+ });
1144
+ return prom;
1145
+ },
1146
+
1147
+
1148
+
1149
+
1150
+
1071
1151
  oneSpace(cc){
1072
1152
  return cc.replace(/\s\s+/g, ' ');
1073
1153
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "puvox-library",
3
- "version": "1.0.34",
3
+ "version": "1.0.35",
4
4
  "description": "library-class-javascript",
5
5
  "main": "library_standard.js",
6
6
  "scripts": {