csv-sort 6.0.4 → 6.0.10
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.
- package/CHANGELOG.md +0 -20
- package/LICENSE +1 -1
- package/README.md +4 -1
- package/dist/csv-sort.esm.js +3 -271
- package/dist/csv-sort.umd.js +42 -40
- package/examples/_quickTake.js +1 -0
- package/package.json +23 -80
- package/types/index.d.ts +5 -1
- package/types/util/findType.d.ts +0 -0
- package/examples/api.json +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,26 +3,6 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
-
## 6.0.3 (2021-11-02)
|
|
7
|
-
|
|
8
|
-
### Bug Fixes
|
|
9
|
-
|
|
10
|
-
- bump TS and separate ESLint plugins away from this monorepo ([b1ebce1](https://github.com/codsen/codsen/commit/b1ebce1637d8c41c2d848fc24b0ba4058865bd5d))
|
|
11
|
-
|
|
12
|
-
### Features
|
|
13
|
-
|
|
14
|
-
- migrate to ES Modules ([c579dff](https://github.com/codsen/codsen/commit/c579dff3b23205e383035ca10ddcec671e35d0fe))
|
|
15
|
-
|
|
16
|
-
### BREAKING CHANGES
|
|
17
|
-
|
|
18
|
-
- programs now are in ES Modules and won't work with Common JS require()
|
|
19
|
-
|
|
20
|
-
## 6.0.1 (2021-09-13)
|
|
21
|
-
|
|
22
|
-
### Bug Fixes
|
|
23
|
-
|
|
24
|
-
- bump TS and separate ESLint plugins away from this monorepo ([2e07d42](https://github.com/codsen/codsen/commit/2e07d424222b6ffedf5fb45c83ad453627ec2904))
|
|
25
|
-
|
|
26
6
|
## 6.0.0 (2021-09-09)
|
|
27
7
|
|
|
28
8
|
### Features
|
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2010
|
|
3
|
+
Copyright (c) 2010-2021 Roy Revelt and other contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
6
|
a copy of this software and associated documentation files (the
|
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ If you need a legacy version which works with `require`, use version 5.1.0
|
|
|
38
38
|
|
|
39
39
|
```js
|
|
40
40
|
import { strict as assert } from "assert";
|
|
41
|
+
|
|
41
42
|
import { sort } from "csv-sort";
|
|
42
43
|
|
|
43
44
|
// Sorts double-entry bookkeeping CSV's - bank statements for example
|
|
@@ -69,7 +70,7 @@ assert.deepEqual(
|
|
|
69
70
|
|
|
70
71
|
## Documentation
|
|
71
72
|
|
|
72
|
-
Please [visit codsen.com](https://codsen.com/os/csv-sort/) for a full description of the API
|
|
73
|
+
Please [visit codsen.com](https://codsen.com/os/csv-sort/) for a full description of the API.
|
|
73
74
|
|
|
74
75
|
## Contributing
|
|
75
76
|
|
|
@@ -81,6 +82,8 @@ MIT License
|
|
|
81
82
|
|
|
82
83
|
Copyright (c) 2010-2021 Roy Revelt and other contributors
|
|
83
84
|
|
|
85
|
+
|
|
84
86
|
List of [currency signs](https://github.com/bengourley/currency-symbol-map) - Copyright © 2017 Ben Gourley - see its [BSD-2-Clause disclaimer](https://opensource.org/licenses/BSD-2-Clause)
|
|
85
87
|
|
|
86
88
|
<img src="https://codsen.com/images/png-codsen-ok.png" width="98" alt="ok" align="center"> <img src="https://codsen.com/images/png-codsen-1.png" width="148" alt="codsen" align="center"> <img src="https://codsen.com/images/png-codsen-star-small.png" width="32" alt="star" align="center">
|
|
89
|
+
|
package/dist/csv-sort.esm.js
CHANGED
|
@@ -1,279 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name csv-sort
|
|
3
3
|
* @fileoverview Sorts double-entry bookkeeping CSV coming from internet banking
|
|
4
|
-
* @version 6.0.
|
|
4
|
+
* @version 6.0.10
|
|
5
5
|
* @author Roy Revelt, Codsen Ltd
|
|
6
6
|
* @license MIT
|
|
7
7
|
* {@link https://codsen.com/os/csv-sort/}
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
import currency from 'currency.js';
|
|
13
|
-
|
|
14
|
-
function isNumeric(str) {
|
|
15
|
-
if (!str.trim()) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
return Number(str) === Number(str);
|
|
19
|
-
}
|
|
20
|
-
const currencySigns = ["د.إ", "؋", "L", "֏", "ƒ", "Kz", "$", "ƒ", "₼", "KM", "৳", "лв", ".د.ب", "FBu", "$b", "R$", "฿", "Nu.", "P", "p.", "BZ$", "FC", "CHF", "¥", "₡", "₱", "Kč", "Fdj", "kr", "RD$", "دج", "kr", "Nfk", "Br", "Ξ", "€", "₾", "₵", "GH₵", "D", "FG", "Q", "L", "kn", "G", "Ft", "Rp", "₪", "₹", "ع.د", "﷼", "kr", "J$", "JD", "¥", "KSh", "лв", "៛", "CF", "₩", "₩", "KD", "лв", "₭", "₨", "M", "Ł", "Lt", "Ls", "LD", "MAD", "lei", "Ar", "ден", "K", "₮", "MOP$", "UM", "₨", "Rf", "MK", "RM", "MT", "₦", "C$", "kr", "₨", "﷼", "B/.", "S/.", "K", "₱", "₨", "zł", "Gs", "﷼", "¥", "lei", "Дин.", "₽", "R₣", "﷼", "₨", "ج.س.", "kr", "£", "Le", "S", "Db", "E", "฿", "SM", "T", "د.ت", "T$", "₤", "₺", "TT$", "NT$", "TSh", "₴", "USh", "$U", "лв", "Bs", "₫", "VT", "WS$", "FCFA", "Ƀ", "CFA", "₣", "﷼", "R", "Z$"];
|
|
21
|
-
function findType(something) {
|
|
22
|
-
/* istanbul ignore next */
|
|
23
|
-
if (typeof something !== "string") {
|
|
24
|
-
throw new Error(`csv-sort/util/findType(): input must be string! Currently it's: ${typeof something}`);
|
|
25
|
-
}
|
|
26
|
-
if (isNumeric(something)) {
|
|
27
|
-
return "numeric";
|
|
28
|
-
}
|
|
29
|
-
/* istanbul ignore next */
|
|
30
|
-
if (currencySigns.some(singleSign =>
|
|
31
|
-
isNumeric(something.replace(singleSign, "").replace(/[,.]/g, "")))) {
|
|
32
|
-
return "numeric";
|
|
33
|
-
}
|
|
34
|
-
if (!something.trim()) {
|
|
35
|
-
return "empty";
|
|
36
|
-
}
|
|
37
|
-
return "text";
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function sort(input) {
|
|
41
|
-
let msgContent = null;
|
|
42
|
-
let msgType = null;
|
|
43
|
-
if (typeof input !== "string") {
|
|
44
|
-
throw new TypeError(`csv-sort/csvSort(): [THROW_ID_01] The input is of a wrong type! We accept either string of array of arrays. We got instead: ${typeof input}, equal to:\n${JSON.stringify(input, null, 4)}`);
|
|
45
|
-
} else if (!input.trim()) {
|
|
46
|
-
return {
|
|
47
|
-
res: [[""]],
|
|
48
|
-
msgContent,
|
|
49
|
-
msgType
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
let content = splitEasy(input);
|
|
53
|
-
let schema = [];
|
|
54
|
-
let stateHeaderRowPresent = false;
|
|
55
|
-
let stateDataColumnRowLengthIsConsistent = true;
|
|
56
|
-
const stateColumnsContainingSameValueEverywhere = [];
|
|
57
|
-
let indexAtWhichEmptyCellsStart = null;
|
|
58
|
-
for (let i = content.length - 1; i >= 0; i--) {
|
|
59
|
-
if (!schema.length) {
|
|
60
|
-
/* istanbul ignore next */
|
|
61
|
-
if (content[i].length !== 1 || content[i][0] !== "") {
|
|
62
|
-
for (let y = 0, len = content[i].length; y < len; y++) {
|
|
63
|
-
schema.push(findType(content[i][y].trim()));
|
|
64
|
-
if (indexAtWhichEmptyCellsStart === null && findType(content[i][y].trim()) === "empty") {
|
|
65
|
-
indexAtWhichEmptyCellsStart = y;
|
|
66
|
-
}
|
|
67
|
-
if (indexAtWhichEmptyCellsStart !== null && findType(content[i][y].trim()) !== "empty") {
|
|
68
|
-
indexAtWhichEmptyCellsStart = null;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
} else {
|
|
73
|
-
if (i === 0) {
|
|
74
|
-
stateHeaderRowPresent = content[i].every(el => findType(el) === "text" || findType(el) === "empty");
|
|
75
|
-
}
|
|
76
|
-
/* istanbul ignore else */
|
|
77
|
-
if (!stateHeaderRowPresent && schema.length !== content[i].length) {
|
|
78
|
-
stateDataColumnRowLengthIsConsistent = false;
|
|
79
|
-
}
|
|
80
|
-
let perRowIndexAtWhichEmptyCellsStart = null;
|
|
81
|
-
for (let y = 0, len = content[i].length; y < len; y++) {
|
|
82
|
-
/* istanbul ignore else */
|
|
83
|
-
if (perRowIndexAtWhichEmptyCellsStart === null && findType(content[i][y].trim()) === "empty") {
|
|
84
|
-
perRowIndexAtWhichEmptyCellsStart = y;
|
|
85
|
-
}
|
|
86
|
-
/* istanbul ignore else */
|
|
87
|
-
if (perRowIndexAtWhichEmptyCellsStart !== null && findType(content[i][y].trim()) !== "empty") {
|
|
88
|
-
perRowIndexAtWhichEmptyCellsStart = null;
|
|
89
|
-
}
|
|
90
|
-
/* istanbul ignore else */
|
|
91
|
-
if (findType(content[i][y].trim()) !== schema[y] && !stateHeaderRowPresent) {
|
|
92
|
-
const toAdd = findType(content[i][y].trim());
|
|
93
|
-
/* istanbul ignore else */
|
|
94
|
-
if (Array.isArray(schema[y])) {
|
|
95
|
-
if (!schema[y].includes(toAdd)) {
|
|
96
|
-
schema[y].push(findType(content[i][y].trim()));
|
|
97
|
-
}
|
|
98
|
-
} else if (schema[y] !== toAdd) {
|
|
99
|
-
const temp = schema[y];
|
|
100
|
-
schema[y] = [];
|
|
101
|
-
schema[y].push(temp);
|
|
102
|
-
schema[y].push(toAdd);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/* istanbul ignore next */
|
|
107
|
-
if (indexAtWhichEmptyCellsStart !== null && perRowIndexAtWhichEmptyCellsStart !== null && perRowIndexAtWhichEmptyCellsStart > indexAtWhichEmptyCellsStart && (!stateHeaderRowPresent || stateHeaderRowPresent && i !== 0)) {
|
|
108
|
-
indexAtWhichEmptyCellsStart = perRowIndexAtWhichEmptyCellsStart;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
/* istanbul ignore else */
|
|
113
|
-
if (!indexAtWhichEmptyCellsStart) {
|
|
114
|
-
indexAtWhichEmptyCellsStart = schema.length;
|
|
115
|
-
}
|
|
116
|
-
let nonEmptyColsStartAt = 0;
|
|
117
|
-
for (let i = 0, len = schema.length; i < len; i++) {
|
|
118
|
-
if (schema[i] === "empty") {
|
|
119
|
-
nonEmptyColsStartAt = i;
|
|
120
|
-
} else {
|
|
121
|
-
break;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
/* istanbul ignore else */
|
|
125
|
-
if (nonEmptyColsStartAt !== 0) {
|
|
126
|
-
content = content.map(arr => arr.slice(nonEmptyColsStartAt + 1, indexAtWhichEmptyCellsStart));
|
|
127
|
-
schema = schema.slice(nonEmptyColsStartAt + 1, indexAtWhichEmptyCellsStart);
|
|
128
|
-
}
|
|
129
|
-
const numericSchemaColumns = [];
|
|
130
|
-
let balanceColumnIndex;
|
|
131
|
-
schema.forEach((colType, i) => {
|
|
132
|
-
if (colType === "numeric") {
|
|
133
|
-
numericSchemaColumns.push(i);
|
|
134
|
-
}
|
|
135
|
-
});
|
|
136
|
-
const traverseUpToThisIndexAtTheTop = stateHeaderRowPresent ? 1 : 0;
|
|
137
|
-
if (numericSchemaColumns.length === 1) {
|
|
138
|
-
balanceColumnIndex = numericSchemaColumns[0];
|
|
139
|
-
} else if (numericSchemaColumns.length === 0) {
|
|
140
|
-
throw new Error('csv-sort/csvSort(): [THROW_ID_03] Your CSV file does not contain numeric-only columns and computer was not able to detect the "Balance" column!');
|
|
141
|
-
} else {
|
|
142
|
-
let potentialBalanceColumnIndexesList = Array.from(numericSchemaColumns);
|
|
143
|
-
const deleteFromPotentialBalanceColumnIndexesList = [];
|
|
144
|
-
for (let i = 0, len = potentialBalanceColumnIndexesList.length; i < len; i++) {
|
|
145
|
-
const suspectedBalanceColumnsIndexNumber = potentialBalanceColumnIndexesList[i];
|
|
146
|
-
let previousValue;
|
|
147
|
-
let lookForTwoEqualAndConsecutive = true;
|
|
148
|
-
let firstValue;
|
|
149
|
-
let lookForAllTheSame = true;
|
|
150
|
-
for (let rowNum = traverseUpToThisIndexAtTheTop, len2 = content.length; rowNum < len2; rowNum++) {
|
|
151
|
-
/* istanbul ignore else */
|
|
152
|
-
if (lookForTwoEqualAndConsecutive) {
|
|
153
|
-
if (previousValue == null) {
|
|
154
|
-
previousValue = content[rowNum][suspectedBalanceColumnsIndexNumber];
|
|
155
|
-
} else if (previousValue === content[rowNum][suspectedBalanceColumnsIndexNumber]) {
|
|
156
|
-
deleteFromPotentialBalanceColumnIndexesList.push(suspectedBalanceColumnsIndexNumber);
|
|
157
|
-
lookForTwoEqualAndConsecutive = false;
|
|
158
|
-
} else {
|
|
159
|
-
previousValue = content[rowNum][suspectedBalanceColumnsIndexNumber];
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
/* istanbul ignore else */
|
|
163
|
-
if (lookForAllTheSame) {
|
|
164
|
-
if (firstValue == null) {
|
|
165
|
-
firstValue = content[rowNum][suspectedBalanceColumnsIndexNumber];
|
|
166
|
-
} else if (content[rowNum][suspectedBalanceColumnsIndexNumber] !== firstValue) {
|
|
167
|
-
lookForAllTheSame = false;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (!lookForTwoEqualAndConsecutive) {
|
|
171
|
-
break;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
/* istanbul ignore else */
|
|
175
|
-
if (lookForAllTheSame) {
|
|
176
|
-
stateColumnsContainingSameValueEverywhere.push(suspectedBalanceColumnsIndexNumber);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
potentialBalanceColumnIndexesList = pull(potentialBalanceColumnIndexesList, ...deleteFromPotentialBalanceColumnIndexesList);
|
|
180
|
-
/* istanbul ignore else */
|
|
181
|
-
if (potentialBalanceColumnIndexesList.length === 1) {
|
|
182
|
-
balanceColumnIndex = potentialBalanceColumnIndexesList[0];
|
|
183
|
-
} else if (potentialBalanceColumnIndexesList.length === 0) {
|
|
184
|
-
throw new Error('csv-sort/csvSort(): [THROW_ID_04] The computer can\'t find the "Balance" column! It saw some numeric-only columns, but they all seem to have certain rows with the same values as rows right below/above them!');
|
|
185
|
-
} else ;
|
|
186
|
-
}
|
|
187
|
-
if (!balanceColumnIndex) {
|
|
188
|
-
throw new Error("csv-sort/csvSort(): [THROW_ID_05] Sadly computer couldn't find its way in this CSV and had to stop working on it.");
|
|
189
|
-
}
|
|
190
|
-
const potentialCreditDebitColumns = pull(Array.from(schema.reduce((result, el, index) => {
|
|
191
|
-
if (typeof el === "string" && el === "numeric" || Array.isArray(el) && el.includes("numeric")) {
|
|
192
|
-
result.push(index);
|
|
193
|
-
}
|
|
194
|
-
return result;
|
|
195
|
-
}, [])), balanceColumnIndex, ...stateColumnsContainingSameValueEverywhere);
|
|
196
|
-
const resContent = [];
|
|
197
|
-
resContent.push(content[content.length - 1].slice(0, indexAtWhichEmptyCellsStart));
|
|
198
|
-
const usedUpRows = [];
|
|
199
|
-
const bottom = stateHeaderRowPresent ? 1 : 0;
|
|
200
|
-
for (let y = content.length - 2; y >= bottom; y--) {
|
|
201
|
-
for (let suspectedRowsIndex = content.length - 2; suspectedRowsIndex >= bottom; suspectedRowsIndex--) {
|
|
202
|
-
if (!usedUpRows.includes(suspectedRowsIndex)) {
|
|
203
|
-
let thisRowIsDone = false;
|
|
204
|
-
for (let suspectedColIndex = 0, len = potentialCreditDebitColumns.length; suspectedColIndex < len; suspectedColIndex++) {
|
|
205
|
-
let diffVal = null;
|
|
206
|
-
if (content[suspectedRowsIndex][potentialCreditDebitColumns[suspectedColIndex]] !== "") {
|
|
207
|
-
diffVal = currency(content[suspectedRowsIndex][potentialCreditDebitColumns[suspectedColIndex]]);
|
|
208
|
-
}
|
|
209
|
-
let totalVal = null;
|
|
210
|
-
/* istanbul ignore else */
|
|
211
|
-
if (content[suspectedRowsIndex][balanceColumnIndex] !== "") {
|
|
212
|
-
totalVal = currency(content[suspectedRowsIndex][balanceColumnIndex]);
|
|
213
|
-
}
|
|
214
|
-
let topmostResContentBalance = null;
|
|
215
|
-
/* istanbul ignore else */
|
|
216
|
-
if (resContent[0][balanceColumnIndex] !== "") {
|
|
217
|
-
topmostResContentBalance = currency(resContent[0][balanceColumnIndex]).format();
|
|
218
|
-
}
|
|
219
|
-
let currentRowsDiffVal = null;
|
|
220
|
-
/* istanbul ignore else */
|
|
221
|
-
if (resContent[resContent.length - 1][potentialCreditDebitColumns[suspectedColIndex]] !== "") {
|
|
222
|
-
currentRowsDiffVal = currency(resContent[resContent.length - 1][potentialCreditDebitColumns[suspectedColIndex]]).format();
|
|
223
|
-
}
|
|
224
|
-
let lastResContentRowsBalance = null;
|
|
225
|
-
/* istanbul ignore else */
|
|
226
|
-
if (resContent[resContent.length - 1][balanceColumnIndex] !== "") {
|
|
227
|
-
lastResContentRowsBalance = currency(resContent[resContent.length - 1][balanceColumnIndex]);
|
|
228
|
-
}
|
|
229
|
-
/* istanbul ignore else */
|
|
230
|
-
if (diffVal && totalVal.add(diffVal).format() === topmostResContentBalance) {
|
|
231
|
-
resContent.unshift(content[suspectedRowsIndex].slice(0, indexAtWhichEmptyCellsStart));
|
|
232
|
-
usedUpRows.push(suspectedRowsIndex);
|
|
233
|
-
thisRowIsDone = true;
|
|
234
|
-
break;
|
|
235
|
-
} else if (diffVal && totalVal.subtract(diffVal).format() === topmostResContentBalance) {
|
|
236
|
-
resContent.unshift(content[suspectedRowsIndex].slice(0, indexAtWhichEmptyCellsStart));
|
|
237
|
-
usedUpRows.push(suspectedRowsIndex);
|
|
238
|
-
thisRowIsDone = true;
|
|
239
|
-
break;
|
|
240
|
-
} else if (currentRowsDiffVal && lastResContentRowsBalance.add(currentRowsDiffVal).format() === totalVal.format()) {
|
|
241
|
-
resContent.push(content[suspectedRowsIndex].slice(0, indexAtWhichEmptyCellsStart));
|
|
242
|
-
usedUpRows.push(suspectedRowsIndex);
|
|
243
|
-
thisRowIsDone = true;
|
|
244
|
-
break;
|
|
245
|
-
} else if (currentRowsDiffVal && lastResContentRowsBalance.subtract(currentRowsDiffVal).format() === totalVal.format()) {
|
|
246
|
-
resContent.push(content[suspectedRowsIndex].slice(0, indexAtWhichEmptyCellsStart));
|
|
247
|
-
usedUpRows.push(suspectedRowsIndex);
|
|
248
|
-
thisRowIsDone = true;
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
/* istanbul ignore else */
|
|
253
|
-
if (thisRowIsDone) {
|
|
254
|
-
thisRowIsDone = false;
|
|
255
|
-
break;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
/* istanbul ignore else */
|
|
261
|
-
if (stateHeaderRowPresent) {
|
|
262
|
-
if (stateDataColumnRowLengthIsConsistent && content[0].length > schema.length) {
|
|
263
|
-
content[0].length = schema.length;
|
|
264
|
-
}
|
|
265
|
-
resContent.unshift(content[0].slice(0, indexAtWhichEmptyCellsStart));
|
|
266
|
-
}
|
|
267
|
-
/* istanbul ignore else */
|
|
268
|
-
if (content.length - (stateHeaderRowPresent ? 2 : 1) !== usedUpRows.length) {
|
|
269
|
-
msgContent = "Not all rows were recognised!";
|
|
270
|
-
msgType = "alert";
|
|
271
|
-
}
|
|
272
|
-
return {
|
|
273
|
-
res: resContent,
|
|
274
|
-
msgContent,
|
|
275
|
-
msgType
|
|
276
|
-
};
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
export { sort };
|
|
10
|
+
import B from"lodash.pull";import{splitEasy as O}from"csv-split-easy";import R from"currency.js";function E(i){return typeof i=="number"&&!isNaN(i)?!0:(i=(i||"").toString().trim(),i?!isNaN(+i):!1)}var N=["\u062F.\u0625","\u060B","L","\u058F","\u0192","Kz","$","\u0192","\u20BC","KM","\u09F3","\u043B\u0432",".\u062F.\u0628","FBu","$b","R$","\u0E3F","Nu.","P","p.","BZ$","FC","CHF","\xA5","\u20A1","\u20B1","K\u010D","Fdj","kr","RD$","\u062F\u062C","kr","Nfk","Br","\u039E","\u20AC","\u20BE","\u20B5","GH\u20B5","D","FG","Q","L","kn","G","Ft","Rp","\u20AA","\u20B9","\u0639.\u062F","\uFDFC","kr","J$","JD","\xA5","KSh","\u043B\u0432","\u17DB","CF","\u20A9","\u20A9","KD","\u043B\u0432","\u20AD","\u20A8","M","\u0141","Lt","Ls","LD","MAD","lei","Ar","\u0434\u0435\u043D","K","\u20AE","MOP$","UM","\u20A8","Rf","MK","RM","MT","\u20A6","C$","kr","\u20A8","\uFDFC","B/.","S/.","K","\u20B1","\u20A8","z\u0142","Gs","\uFDFC","\uFFE5","lei","\u0414\u0438\u043D.","\u20BD","R\u20A3","\uFDFC","\u20A8","\u062C.\u0633.","kr","\xA3","Le","S","Db","E","\u0E3F","SM","T","\u062F.\u062A","T$","\u20A4","\u20BA","TT$","NT$","TSh","\u20B4","USh","$U","\u043B\u0432","Bs","\u20AB","VT","WS$","FCFA","\u0243","CFA","\u20A3","\uFDFC","R","Z$"];function f(i){return E(i)||N.some(b=>E(i.replace(b,"").replace(/[,.]/g,"")))?"numeric":i.trim()?"text":"empty"}var x="6.0.10";var K=x;function _(i){let b=null,w=null;if(typeof i!="string")throw new TypeError(`csv-sort/csvSort(): [THROW_ID_01] The input is of a wrong type! We accept either string of array of arrays. We got instead: ${typeof i}, equal to:
|
|
11
|
+
${JSON.stringify(i,null,4)}`);if(!i.trim())return{res:[[""]],msgContent:b,msgType:w};let n=O(i),s=[],g=!1,S=!0,v=[],o=null;for(let t=n.length-1;t>=0;t--)if(s.length){t===0&&(g=n[t].every(l=>f(l)==="text"||f(l)==="empty")),!g&&s.length!==n[t].length&&(S=!1);let e=null;for(let l=0,p=n[t].length;l<p;l++)if(e===null&&f(n[t][l].trim())==="empty"&&(e=l),e!==null&&f(n[t][l].trim())!=="empty"&&(e=null),f(n[t][l].trim())!==s[l]&&!g){let a=f(n[t][l].trim());if(Array.isArray(s[l]))s[l].includes(a)||s[l].push(f(n[t][l].trim()));else if(s[l]!==a){let u=s[l];s[l]=[],s[l].push(u),s[l].push(a)}}o!==null&&e!==null&&e>o&&(!g||g&&t!==0)&&(o=e)}else if(n[t].length!==1||n[t][0]!=="")for(let e=0,l=n[t].length;e<l;e++)s.push(f(n[t][e].trim())),o===null&&f(n[t][e].trim())==="empty"&&(o=e),o!==null&&f(n[t][e].trim())!=="empty"&&(o=null);o||(o=s.length);let C=0;for(let t=0,e=s.length;t<e&&s[t]==="empty";t++)C=t;C!==0&&(n=n.map(t=>t.slice(C+1,o)),s=s.slice(C+1,o));let V=[],m;s.forEach((t,e)=>{t==="numeric"&&V.push(e)});let k=g?1:0;if(V.length===1)m=V[0];else{if(V.length===0)throw new Error('csv-sort/csvSort(): [THROW_ID_03] Your CSV file does not contain numeric-only columns and computer was not able to detect the "Balance" column!');{let t=Array.from(V),e=[];for(let l=0,p=t.length;l<p;l++){let a=t[l],u,d=!0,y,h=!0;for(let c=k,I=n.length;c<I&&(d&&(u==null?u=n[c][a]:u===n[c][a]?(e.push(a),d=!1):u=n[c][a]),h&&(y==null?y=n[c][a]:n[c][a]!==y&&(h=!1)),!!d);c++);h&&v.push(a)}if(t=B(t,...e),t.length===1)m=t[0];else if(t.length===0)throw new Error(`csv-sort/csvSort(): [THROW_ID_04] The computer can't find the "Balance" column! It saw some numeric-only columns, but they all seem to have certain rows with the same values as rows right below/above them!`)}}if(!m)throw new Error("csv-sort/csvSort(): [THROW_ID_05] Sadly computer couldn't find its way in this CSV and had to stop working on it.");let D=B(Array.from(s.reduce((t,e,l)=>((typeof e=="string"&&e==="numeric"||Array.isArray(e)&&e.includes("numeric"))&&t.push(l),t),[])),m,...v),r=[];r.push(n[n.length-1].slice(0,o));let $=[],T=g?1:0;for(let t=n.length-2;t>=T;t--)for(let e=n.length-2;e>=T;e--)if(!$.includes(e)){let l=!1;for(let p=0,a=D.length;p<a;p++){let u=null;n[e][D[p]]!==""&&(u=R(n[e][D[p]]));let d=null;n[e][m]!==""&&(d=R(n[e][m]));let y=null;r[0][m]!==""&&(y=R(r[0][m]).format());let h=null;r[r.length-1][D[p]]!==""&&(h=R(r[r.length-1][D[p]]).format());let c=null;if(r[r.length-1][m]!==""&&(c=R(r[r.length-1][m])),u&&d.add(u).format()===y){r.unshift(n[e].slice(0,o)),$.push(e),l=!0;break}else if(u&&d.subtract(u).format()===y){r.unshift(n[e].slice(0,o)),$.push(e),l=!0;break}else if(h&&c.add(h).format()===d.format()){r.push(n[e].slice(0,o)),$.push(e),l=!0;break}else if(h&&c.subtract(h).format()===d.format()){r.push(n[e].slice(0,o)),$.push(e),l=!0;break}}if(l){l=!1;break}}return g&&(S&&n[0].length>s.length&&(n[0].length=s.length),r.unshift(n[0].slice(0,o))),n.length-(g?2:1)!==$.length&&(b="Not all rows were recognised!",w="alert"),{res:r,msgContent:b,msgType:w}}export{f as findType,E as isNumeric,_ as sort,K as version};
|
package/dist/csv-sort.umd.js
CHANGED
|
@@ -1,75 +1,77 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name csv-sort
|
|
3
3
|
* @fileoverview Sorts double-entry bookkeeping CSV coming from internet banking
|
|
4
|
-
* @version 6.0.
|
|
4
|
+
* @version 6.0.10
|
|
5
5
|
* @author Roy Revelt, Codsen Ltd
|
|
6
6
|
* @license MIT
|
|
7
7
|
* {@link https://codsen.com/os/csv-sort/}
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
var csvSort=(()=>{var Ve=Object.create;var x=Object.defineProperty;var Ce=Object.getOwnPropertyDescriptor;var Fe=Object.getOwnPropertyNames,z=Object.getOwnPropertySymbols,_e=Object.getPrototypeOf,Y=Object.prototype.hasOwnProperty,je=Object.prototype.propertyIsEnumerable;var W=(e,t,s)=>t in e?x(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s,b=(e,t)=>{for(var s in t||(t={}))Y.call(t,s)&&W(e,s,t[s]);if(z)for(var s of z(t))je.call(t,s)&&W(e,s,t[s]);return e};var Z=e=>x(e,"__esModule",{value:!0});var J=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),We=(e,t)=>{for(var s in t)x(e,s,{get:t[s],enumerable:!0})},Q=(e,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of Fe(t))!Y.call(e,a)&&(s||a!=="default")&&x(e,a,{get:()=>t[a],enumerable:!(n=Ce(t,a))||n.enumerable});return e},k=(e,t)=>Q(Z(x(e!=null?Ve(_e(e)):{},"default",!t&&e&&e.__esModule?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e),Je=(e=>(t,s)=>e&&e.get(t)||(s=Q(Z({}),t,1),e&&e.set(t,s),s))(typeof WeakMap!="undefined"?new WeakMap:0);var H=(e,t,s)=>(W(e,typeof t!="symbol"?t+"":t,s),s);var te=J((Jr,re)=>{function ke(e,t,s){switch(s.length){case 0:return e.call(t);case 1:return e.call(t,s[0]);case 2:return e.call(t,s[0],s[1]);case 3:return e.call(t,s[0],s[1],s[2])}return e.apply(t,s)}function He(e,t){for(var s=-1,n=e?e.length:0,a=Array(n);++s<n;)a[s]=t(e[s],s,e);return a}function Me(e,t,s,n){for(var a=e.length,r=s+(n?1:-1);n?r--:++r<a;)if(t(e[r],r,e))return r;return-1}function Be(e,t,s){if(t!==t)return Me(e,Pe,s);for(var n=s-1,a=e.length;++n<a;)if(e[n]===t)return n;return-1}function qe(e,t,s,n){for(var a=s-1,r=e.length;++a<r;)if(n(e[a],t))return a;return-1}function Pe(e){return e!==e}function Ue(e){return function(t){return e(t)}}var Ke=Array.prototype,X=Ke.splice,ee=Math.max;function Le(e,t,s,n){var a=n?qe:Be,r=-1,l=t.length,u=e;for(e===t&&(t=ze(t)),s&&(u=He(e,Ue(s)));++r<l;)for(var i=0,o=t[r],f=s?s(o):o;(i=a(u,f,i,n))>-1;)u!==e&&X.call(u,i,1),X.call(e,i,1);return e}function Ge(e,t){return t=ee(t===void 0?e.length-1:t,0),function(){for(var s=arguments,n=-1,a=ee(s.length-t,0),r=Array(a);++n<a;)r[n]=s[t+n];n=-1;for(var l=Array(t+1);++n<t;)l[n]=s[n];return l[t]=r,ke(e,this,l)}}function ze(e,t){var s=-1,n=e.length;for(t||(t=Array(n));++s<n;)t[s]=e[s];return t}var Ye=Ge(Ze);function Ze(e,t){return e&&e.length&&t&&t.length?Le(e,t):e}re.exports=Ye});var Oe=J((zr,Ne)=>{var nr=1/0,sr="[object Symbol]",or=/^\s+|\s+$/g,q="\\ud800-\\udfff",fe="\\u0300-\\u036f\\ufe20-\\ufe23",ge="\\u20d0-\\u20f0",ce="\\ufe0e\\ufe0f",ir="["+q+"]",P="["+fe+ge+"]",U="\\ud83c[\\udffb-\\udfff]",lr="(?:"+P+"|"+U+")",pe="[^"+q+"]",he="(?:\\ud83c[\\udde6-\\uddff]){2}",me="[\\ud800-\\udbff][\\udc00-\\udfff]",ye="\\u200d",de=lr+"?",be="["+ce+"]?",ar="(?:"+ye+"(?:"+[pe,he,me].join("|")+")"+be+de+")*",ur=be+de+ar,fr="(?:"+[pe+P+"?",P,he,me,ir].join("|")+")",gr=RegExp(U+"(?="+U+")|"+fr+ur,"g"),cr=RegExp("["+ye+q+fe+ge+ce+"]"),pr=typeof global=="object"&&global&&global.Object===Object&&global,hr=typeof self=="object"&&self&&self.Object===Object&&self,mr=pr||hr||Function("return this")();function yr(e){return e.split("")}function dr(e,t,s,n){for(var a=e.length,r=s+(n?1:-1);n?r--:++r<a;)if(t(e[r],r,e))return r;return-1}function Te(e,t,s){if(t!==t)return dr(e,br,s);for(var n=s-1,a=e.length;++n<a;)if(e[n]===t)return n;return-1}function br(e){return e!==e}function Tr(e,t){for(var s=-1,n=e.length;++s<n&&Te(t,e[s],0)>-1;);return s}function $r(e,t){for(var s=e.length;s--&&Te(t,e[s],0)>-1;);return s}function wr(e){return cr.test(e)}function $e(e){return wr(e)?vr(e):yr(e)}function vr(e){return e.match(gr)||[]}var Sr=Object.prototype,Rr=Sr.toString,we=mr.Symbol,ve=we?we.prototype:void 0,Se=ve?ve.toString:void 0;function Nr(e,t,s){var n=-1,a=e.length;t<0&&(t=-t>a?0:a+t),s=s>a?a:s,s<0&&(s+=a),a=t>s?0:s-t>>>0,t>>>=0;for(var r=Array(a);++n<a;)r[n]=e[n+t];return r}function Re(e){if(typeof e=="string")return e;if(Dr(e))return Se?Se.call(e):"";var t=e+"";return t=="0"&&1/e==-nr?"-0":t}function Or(e,t,s){var n=e.length;return s=s===void 0?n:s,!t&&s>=n?e:Nr(e,t,s)}function Er(e){return!!e&&typeof e=="object"}function Dr(e){return typeof e=="symbol"||Er(e)&&Rr.call(e)==sr}function Ir(e){return e==null?"":Re(e)}function Ar(e,t,s){if(e=Ir(e),e&&(s||t===void 0))return e.replace(or,"");if(!e||!(t=Re(t)))return e;var n=$e(e),a=$e(t),r=Tr(n,a),l=$r(n,a)+1;return Or(n,r,l).join("")}Ne.exports=Ar});var Ie=J((K,L)=>{(function(e,t){typeof K=="object"&&typeof L!="undefined"?L.exports=t():typeof define=="function"&&define.amd?define(t):(e=e||self,e.currency=t())})(K,function(){function e(r,l){if(!(this instanceof e))return new e(r,l);l=Object.assign({},s,l);var u=Math.pow(10,l.precision);this.intValue=r=t(r,l),this.value=r/u,l.increment=l.increment||1/u,l.groups=l.useVedic?a:n,this.s=l,this.p=u}function t(r,l){var u=2<arguments.length&&arguments[2]!==void 0?arguments[2]:!0,i=l.decimal,o=l.errorOnInvalid,f=l.fromCents,g=Math.pow(10,l.precision),y=r instanceof e;if(y&&f)return r.intValue;if(typeof r=="number"||y)i=y?r.value:r;else if(typeof r=="string")o=new RegExp("[^-\\d"+i+"]","g"),i=new RegExp("\\"+i,"g"),i=(i=r.replace(/\((.*)\)/,"-$1").replace(o,"").replace(i,"."))||0;else{if(o)throw Error("Invalid Input");i=0}return f||(i=(i*g).toFixed(4)),u?Math.round(i):i}var s={symbol:"$",separator:",",decimal:".",errorOnInvalid:!1,precision:2,pattern:"!#",negativePattern:"-!#",format:function(r,l){var u=l.pattern,i=l.negativePattern,o=l.symbol,f=l.separator,g=l.decimal;l=l.groups;var y=(""+r).replace(/^-/,"").split("."),m=y[0];return y=y[1],(0<=r.value?u:i).replace("!",o).replace("#",m.replace(l,"$1"+f)+(y?g+y:""))},fromCents:!1},n=/(\d)(?=(\d{3})+\b)/g,a=/(\d)(?=(\d\d)+\d\b)/g;return e.prototype={add:function(r){var l=this.s,u=this.p;return e((this.intValue+t(r,l))/(l.fromCents?1:u),l)},subtract:function(r){var l=this.s,u=this.p;return e((this.intValue-t(r,l))/(l.fromCents?1:u),l)},multiply:function(r){var l=this.s;return e(this.intValue*r/(l.fromCents?1:Math.pow(10,l.precision)),l)},divide:function(r){var l=this.s;return e(this.intValue/t(r,l,!1),l)},distribute:function(r){var l=this.intValue,u=this.p,i=this.s,o=[],f=Math[0<=l?"floor":"ceil"](l/r),g=Math.abs(l-f*r);for(u=i.fromCents?1:u;r!==0;r--){var y=e(f/u,i);0<g--&&(y=y[0<=l?"add":"subtract"](1/u)),o.push(y)}return o},dollars:function(){return~~this.value},cents:function(){return~~(this.intValue%this.p)},format:function(r){var l=this.s;return typeof r=="function"?r(this,l):l.format(this,Object.assign({},l,r))},toString:function(){var r=this.s,l=r.increment;return(Math.round(this.intValue/this.p/l)*l).toFixed(r.precision)},toJSON:function(){return this.value}},e})});var jr={};We(jr,{findType:()=>w,isNumeric:()=>j,sort:()=>_r,version:()=>Fr});var G=k(te(),1);var Qe={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function M(e,t){if(!Array.isArray(e)||!e.length)return e;let s=b(b({},Qe),t),n,a;if(s.strictlyTwoElementsInRangeArrays&&!e.every((u,i)=>!Array.isArray(u)||u.length!==2?(n=i,a=u.length,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_03] The first argument should be an array and must consist of arrays which are natural number indexes representing TWO string index ranges. However, ${n}th range (${JSON.stringify(e[n],null,4)}) has not two but ${a} elements!`);if(!e.every((u,i)=>!Array.isArray(u)||!Number.isInteger(u[0])||u[0]<0||!Number.isInteger(u[1])||u[1]<0?(n=i,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_04] The first argument should be an array and must consist of arrays which are natural number indexes representing string index ranges. However, ${n}th range (${JSON.stringify(e[n],null,4)}) does not consist of only natural numbers!`);let r=e.length**2,l=0;return Array.from(e).sort((u,i)=>(s.progressFn&&(l+=1,s.progressFn(Math.floor(l*100/r))),u[0]===i[0]?u[1]<i[1]?-1:u[1]>i[1]?1:0:u[0]<i[0]?-1:1))}var ne={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function se(e,t){function s(o){return o&&typeof o=="object"&&!Array.isArray(o)}if(!Array.isArray(e)||!e.length)return null;let n;if(t)if(s(t)){if(n=b(b({},ne),t),n.progressFn&&s(n.progressFn)&&!Object.keys(n.progressFn).length)n.progressFn=null;else if(n.progressFn&&typeof n.progressFn!="function")throw new Error(`ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: "${typeof n.progressFn}", equal to ${JSON.stringify(n.progressFn,null,4)}`);if(![1,2,"1","2"].includes(n.mergeType))throw new Error(`ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof n.mergeType}", equal to ${JSON.stringify(n.mergeType,null,4)}`);if(typeof n.joinRangesThatTouchEdges!="boolean")throw new Error(`ranges-merge: [THROW_ID_04] opts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "${typeof n.joinRangesThatTouchEdges}", equal to ${JSON.stringify(n.joinRangesThatTouchEdges,null,4)}`)}else throw new Error(`emlint: [THROW_ID_03] the second input argument must be a plain object. It was given as:
|
|
11
|
+
${JSON.stringify(t,null,4)} (type ${typeof t})`);else n=b({},ne);let a=e.filter(o=>Array.isArray(o)).map(o=>[...o]).filter(o=>o[2]!==void 0||o[0]!==o[1]),r,l,u;n.progressFn?r=M(a,{progressFn:o=>{u=Math.floor(o/5),u!==l&&(l=u,n.progressFn(u))}}):r=M(a);let i=r.length-1;for(let o=i;o>0;o--)n.progressFn&&(u=Math.floor((1-o/i)*78)+21,u!==l&&u>l&&(l=u,n.progressFn(u))),(r[o][0]<=r[o-1][0]||!n.joinRangesThatTouchEdges&&r[o][0]<r[o-1][1]||n.joinRangesThatTouchEdges&&r[o][0]<=r[o-1][1])&&(r[o-1][0]=Math.min(r[o][0],r[o-1][0]),r[o-1][1]=Math.max(r[o][1],r[o-1][1]),r[o][2]!==void 0&&(r[o-1][0]>=r[o][0]||r[o-1][1]<=r[o][1])&&r[o-1][2]!==null&&(r[o][2]===null&&r[o-1][2]!==null?r[o-1][2]=null:r[o-1][2]!=null?+n.mergeType==2&&r[o-1][0]===r[o][0]?r[o-1][2]=r[o][2]:r[o-1][2]+=r[o][2]:r[o-1][2]=r[o][2]),r.splice(o,1),o=r.length);return r.length?r:null}var Xe=!0,B="Invariant failed";function oe(e,t){if(!e){if(Xe)throw new Error(B);var s=typeof t=="function"?t():t,n=s?B+": "+s:B;throw new Error(n)}}function ie(e,t,s){let n=0,a=0;if(arguments.length===0)throw new Error("ranges-apply: [THROW_ID_01] inputs missing!");if(typeof e!="string")throw new TypeError(`ranges-apply: [THROW_ID_02] first input argument must be a string! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(t&&!Array.isArray(t))throw new TypeError(`ranges-apply: [THROW_ID_03] second input argument must be an array (or null)! Currently it's: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);if(s&&typeof s!="function")throw new TypeError(`ranges-apply: [THROW_ID_04] the third input argument must be a function (or falsey)! Currently it's: ${typeof s}, equal to: ${JSON.stringify(s,null,4)}`);if(!t||!t.filter(f=>f).length)return e;let r;Array.isArray(t)&&Number.isInteger(t[0])&&Number.isInteger(t[1])?r=[Array.from(t)]:r=Array.from(t);let l=r.length,u=0;r.filter(f=>f).forEach((f,g)=>{if(s&&(n=Math.floor(u/l*10),n!==a&&(a=n,s(n))),!Array.isArray(f))throw new TypeError(`ranges-apply: [THROW_ID_05] ranges array, second input arg., has ${g}th element not an array: ${JSON.stringify(f,null,4)}, which is ${typeof f}`);if(!Number.isInteger(f[0])){if(!Number.isInteger(+f[0])||+f[0]<0)throw new TypeError(`ranges-apply: [THROW_ID_06] ranges array, second input arg. has ${g}th element, array ${JSON.stringify(f,null,0)}. Its first element is not an integer, string index, but ${typeof f[0]}, equal to: ${JSON.stringify(f[0],null,4)}.`);r[g][0]=+r[g][0]}if(!Number.isInteger(f[1])){if(!Number.isInteger(+f[1])||+f[1]<0)throw new TypeError(`ranges-apply: [THROW_ID_07] ranges array, second input arg. has ${g}th element, array ${JSON.stringify(f,null,0)}. Its second element is not an integer, string index, but ${typeof f[1]}, equal to: ${JSON.stringify(f[1],null,4)}.`);r[g][1]=+r[g][1]}u+=1});let i=se(r,{progressFn:f=>{s&&(n=10+Math.floor(f/10),n!==a&&(a=n,s(n)))}});oe(i);let o=i.length;if(o>0){let f=e.slice(i[o-1][1]);e=i.reduce((g,y,m,d)=>{s&&(n=20+Math.floor(m/o*80),n!==a&&(a=n,s(n)));let O=m===0?0:d[m-1][1],F=d[m][0];return`${g}${e.slice(O,F)}${d[m][2]||""}`},""),e+=f}return e}function _(e,t=1){let s="\xA0";function n(r){return Array.from(r).reverse().join("")}function a(r,l,u){let i=u?`
|
|
12
|
+
`:"\r",o=u?"\r":`
|
|
13
|
+
`;if(!r)return r;let f=0,g=0,y="";for(let m=0,d=r.length;m<d;m++)(r[m]===i||r[m]===o&&r[m-1]!==i)&&g++,`\r
|
|
14
|
+
`.includes(r[m])||r[m]===s?(f=0,r[m]===s?y+=r[m]:r[m]===i?g<=l&&(y+=r[m],r[m+1]===o&&(y+=r[m+1],m++)):r[m]===o&&(!r[m-1]||r[m-1]!==i)&&g<=l&&(y+=r[m])):(f++,!r[m+1]&&!g&&(y+=" "));return y}if(typeof e=="string"&&e.length){let r=1;typeof+t=="number"&&Number.isInteger(+t)&&+t>=0&&(r=+t);let l="",u="";if(!e.trim())l=e;else if(!e[0].trim()){for(let i=0,o=e.length;i<o;i++)if(e[i].trim()){l=e.slice(0,i);break}}if(e.trim()&&(e.slice(-1).trim()===""||e.slice(-1)===s)){for(let i=e.length;i--;)if(e[i].trim()){u=e.slice(i+1);break}}return`${a(l,r,!1)}${e.trim()}${n(a(n(u),r,!0))}`}return e}var er={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function le(e,t){if(!Array.isArray(e)||!e.length)return e;let s=b(b({},er),t),n,a;if(s.strictlyTwoElementsInRangeArrays&&!e.every((u,i)=>!Array.isArray(u)||u.length!==2?(n=i,a=u.length,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_03] The first argument should be an array and must consist of arrays which are natural number indexes representing TWO string index ranges. However, ${n}th range (${JSON.stringify(e[n],null,4)}) has not two but ${a} elements!`);if(!e.every((u,i)=>!Array.isArray(u)||!Number.isInteger(u[0])||u[0]<0||!Number.isInteger(u[1])||u[1]<0?(n=i,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_04] The first argument should be an array and must consist of arrays which are natural number indexes representing string index ranges. However, ${n}th range (${JSON.stringify(e[n],null,4)}) does not consist of only natural numbers!`);let r=e.length**2,l=0;return Array.from(e).sort((u,i)=>(s.progressFn&&(l+=1,s.progressFn(Math.floor(l*100/r))),u[0]===i[0]?u[1]<i[1]?-1:u[1]>i[1]?1:0:u[0]<i[0]?-1:1))}var ae={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function rr(e,t){function s(o){return o&&typeof o=="object"&&!Array.isArray(o)}if(!Array.isArray(e)||!e.length)return null;let n;if(t)if(s(t)){if(n=b(b({},ae),t),n.progressFn&&s(n.progressFn)&&!Object.keys(n.progressFn).length)n.progressFn=null;else if(n.progressFn&&typeof n.progressFn!="function")throw new Error(`ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: "${typeof n.progressFn}", equal to ${JSON.stringify(n.progressFn,null,4)}`);if(![1,2,"1","2"].includes(n.mergeType))throw new Error(`ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof n.mergeType}", equal to ${JSON.stringify(n.mergeType,null,4)}`);if(typeof n.joinRangesThatTouchEdges!="boolean")throw new Error(`ranges-merge: [THROW_ID_04] opts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "${typeof n.joinRangesThatTouchEdges}", equal to ${JSON.stringify(n.joinRangesThatTouchEdges,null,4)}`)}else throw new Error(`emlint: [THROW_ID_03] the second input argument must be a plain object. It was given as:
|
|
15
|
+
${JSON.stringify(t,null,4)} (type ${typeof t})`);else n=b({},ae);let a=e.filter(o=>Array.isArray(o)).map(o=>[...o]).filter(o=>o[2]!==void 0||o[0]!==o[1]),r,l,u;n.progressFn?r=le(a,{progressFn:o=>{u=Math.floor(o/5),u!==l&&(l=u,n.progressFn(u))}}):r=le(a);let i=r.length-1;for(let o=i;o>0;o--)n.progressFn&&(u=Math.floor((1-o/i)*78)+21,u!==l&&u>l&&(l=u,n.progressFn(u))),(r[o][0]<=r[o-1][0]||!n.joinRangesThatTouchEdges&&r[o][0]<r[o-1][1]||n.joinRangesThatTouchEdges&&r[o][0]<=r[o-1][1])&&(r[o-1][0]=Math.min(r[o][0],r[o-1][0]),r[o-1][1]=Math.max(r[o][1],r[o-1][1]),r[o][2]!==void 0&&(r[o-1][0]>=r[o][0]||r[o-1][1]<=r[o][1])&&r[o-1][2]!==null&&(r[o][2]===null&&r[o-1][2]!==null?r[o-1][2]=null:r[o-1][2]!=null?+n.mergeType==2&&r[o-1][0]===r[o][0]?r[o-1][2]=r[o][2]:r[o-1][2]+=r[o][2]:r[o-1][2]=r[o][2]),r.splice(o,1),o=r.length);return r.length?r:null}function E(e){return e!=null}function D(e){return Number.isInteger(e)&&e>=0}function V(e){return typeof e=="string"}var tr={limitToBeAddedWhitespace:!1,limitLinebreaksCount:1,mergeType:1},ue=class{constructor(e){H(this,"ranges");H(this,"opts");let t=b(b({},tr),e);if(t.mergeType&&t.mergeType!==1&&t.mergeType!==2)if(V(t.mergeType)&&t.mergeType.trim()==="1")t.mergeType=1;else if(V(t.mergeType)&&t.mergeType.trim()==="2")t.mergeType=2;else throw new Error(`ranges-push: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof t.mergeType}", equal to ${JSON.stringify(t.mergeType,null,4)}`);this.opts=t,this.ranges=[]}add(e,t,s){if(e==null&&t==null)return;if(E(e)&&!E(t)){if(Array.isArray(e)){if(e.length){if(e.some(r=>Array.isArray(r))){e.forEach(r=>{Array.isArray(r)&&this.add(...r)});return}e.length&&D(+e[0])&&D(+e[1])&&this.add(...e)}return}throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_12] the first input argument, "from" is set (${JSON.stringify(e,null,0)}) but second-one, "to" is not (${JSON.stringify(t,null,0)})`)}else if(!E(e)&&E(t))throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_13] the second input argument, "to" is set (${JSON.stringify(t,null,0)}) but first-one, "from" is not (${JSON.stringify(e,null,0)})`);let n=+e,a=+t;if(D(s)&&(s=String(s)),D(n)&&D(a)){if(E(s)&&!V(s)&&!D(s))throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_08] The third argument, the value to add, was given not as string but ${typeof s}, equal to:
|
|
16
|
+
${JSON.stringify(s,null,4)}`);if(E(this.ranges)&&Array.isArray(this.last())&&n===this.last()[1]){if(this.last()[1]=a,this.last()[2],this.last()[2]!==null&&E(s)){let r=this.last()[2]&&this.last()[2].length>0&&(!this.opts||!this.opts.mergeType||this.opts.mergeType===1)?`${this.last()[2]}${s}`:s;this.opts.limitToBeAddedWhitespace&&(r=_(r,this.opts.limitLinebreaksCount)),V(r)&&!r.length||(this.last()[2]=r)}}else{this.ranges||(this.ranges=[]);let r=s!==void 0&&!(V(s)&&!s.length)?[n,a,s&&this.opts.limitToBeAddedWhitespace?_(s,this.opts.limitLinebreaksCount):s]:[n,a];this.ranges.push(r)}}else throw D(n)&&n>=0?new TypeError(`ranges-push/Ranges/add(): [THROW_ID_10] "to" value, the second input argument, must be a natural number or zero! Currently it's of a type "${typeof a}" equal to: ${JSON.stringify(a,null,4)}`):new TypeError(`ranges-push/Ranges/add(): [THROW_ID_09] "from" value, the first input argument, must be a natural number or zero! Currently it's of a type "${typeof n}" equal to: ${JSON.stringify(n,null,4)}`)}push(e,t,s){this.add(e,t,s)}current(){return Array.isArray(this.ranges)&&this.ranges.length?(this.ranges=rr(this.ranges,{mergeType:this.opts.mergeType}),this.ranges&&this.opts.limitToBeAddedWhitespace?this.ranges.map(e=>E(e[2])?[e[0],e[1],_(e[2],this.opts.limitLinebreaksCount)]:e):this.ranges):null}wipe(){this.ranges=[]}replace(e){if(Array.isArray(e)&&e.length)if(Array.isArray(e[0])&&D(e[0][0]))this.ranges=Array.from(e);else throw new Error(`ranges-push/Ranges/replace(): [THROW_ID_11] Single range was given but we expected array of arrays! The first element, ${JSON.stringify(e[0],null,4)} should be an array and its first element should be an integer, a string index.`);else this.ranges=[]}last(){return Array.isArray(this.ranges)&&this.ranges.length?this.ranges[this.ranges.length-1]:null}};var Ee=k(Oe(),1);function C(e,t){let s=!0,n=[".",",","'"," "],a;if(typeof e!="string")throw new TypeError(`string-remove-thousand-separators/remSep(): [THROW_ID_01] Input must be string! Currently it's: ${typeof e}, equal to:
|
|
17
|
+
${JSON.stringify(e,null,4)}`);if(t&&typeof t!="object")throw new TypeError(`string-remove-thousand-separators/remSep(): [THROW_ID_02] Options object must be a plain object! Currently it's: ${typeof t}, equal to:
|
|
18
|
+
${JSON.stringify(t,null,4)}`);let r=b({removeThousandSeparatorsFromNumbers:!0,padSingleDecimalPlaceNumbers:!0,forceUKStyle:!1},t),l=(0,Ee.default)(e.trim(),'"');if(l==="")return l;let u=new ue;for(let i=0,o=l.length;i<o;i++){if(r.removeThousandSeparatorsFromNumbers&&l[i].trim()===""&&u.add(i,i+1),r.removeThousandSeparatorsFromNumbers&&l[i]==="'"&&(u.add(i,i+1),l[i+1]==="'")){s=!1;break}if(n.includes(l[i])){if(l[i+1]!==void 0&&/^\d*$/.test(l[i+1]))if(l[i+2]!==void 0)if(/^\d*$/.test(l[i+2]))if(l[i+3]!==void 0)if(/^\d*$/.test(l[i+3])){if(l[i+4]!==void 0&&/^\d*$/.test(l[i+4])){s=!1;break}else if(r.removeThousandSeparatorsFromNumbers&&u.add(i,i+1),!a)a=l[i];else if(l[i]!==a){s=!1;break}}else{s=!1;break}else r.removeThousandSeparatorsFromNumbers&&r.forceUKStyle&&l[i]===","&&u.add(i,i+1,".");else{s=!1;break}else r.forceUKStyle&&l[i]===","&&u.add(i,i+1,"."),r.padSingleDecimalPlaceNumbers&&u.add(i+2,i+2,"0")}else if(!/^\d*$/.test(l[i])){s=!1;break}}return s&&u.current()?ie(l,u.current()):l}var xr={removeThousandSeparatorsFromNumbers:!0,padSingleDecimalPlaceNumbers:!0,forceUKStyle:!1};function De(e,t){let s=0,n=0,a=[],r=[],l=!1,u=!0;if(t&&typeof t!="object")throw new Error(`csv-split-easy/split(): [THROW_ID_02] Options object must be a plain object! Currently it's of a type ${typeof t} equal to:
|
|
19
|
+
${JSON.stringify(t,null,4)}`);let i=b(b({},xr),t);if(typeof e!="string")throw new TypeError(`csv-split-easy/split(): [THROW_ID_04] input must be string! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(e==="")return[[""]];e=e.trim();for(let o=0,f=e.length;o<f;o++){if(u&&e[o]!=='"'&&e[o]!==","&&e[o].trim()!==""&&(u=!1),e[o]==='"')if(l&&e[o+1]==='"')o+=1;else if(l){l=!1;let g=e.slice(s,o);g.trim()!==""&&(u=!1);let y=/""/.test(g)?g.replace(/""/g,'"'):C(g,{removeThousandSeparatorsFromNumbers:i.removeThousandSeparatorsFromNumbers,padSingleDecimalPlaceNumbers:i.padSingleDecimalPlaceNumbers,forceUKStyle:i.forceUKStyle});a.push(y)}else l=!0,s=o+1;else if(!l&&e[o]===","){if(e[o-1]!=='"'&&!l){let g=e.slice(s,o);g.trim()!==""&&(u=!1),a.push(C(g,{removeThousandSeparatorsFromNumbers:i.removeThousandSeparatorsFromNumbers,padSingleDecimalPlaceNumbers:i.padSingleDecimalPlaceNumbers,forceUKStyle:i.forceUKStyle}))}s=o+1,n&&(n=0)}else if(e[o]===`
|
|
20
|
+
`||e[o]==="\r"){if(!n){if(n=o,!l&&e[o-1]!=='"'){let g=e.slice(s,o);g.trim()!==""&&(u=!1),a.push(C(g,{removeThousandSeparatorsFromNumbers:i.removeThousandSeparatorsFromNumbers,padSingleDecimalPlaceNumbers:i.padSingleDecimalPlaceNumbers,forceUKStyle:i.forceUKStyle}))}u?a.length=0:r.push(a),u=!0,a=[]}s=o+1}else n&&(n=0,s=o);if(o+1===f){if(e[o]!=='"'){let g=e.slice(s,o+1);g.trim()&&(u=!1),a.push(C(g,{removeThousandSeparatorsFromNumbers:i.removeThousandSeparatorsFromNumbers,padSingleDecimalPlaceNumbers:i.padSingleDecimalPlaceNumbers,forceUKStyle:i.forceUKStyle}))}u?a=[]:r.push(a),u=!0}}return r.length===0?[[""]]:r}var A=k(Ie(),1);function j(e){return typeof e=="number"&&!isNaN(e)?!0:(e=(e||"").toString().trim(),e?!isNaN(+e):!1)}var Vr=["\u062F.\u0625","\u060B","L","\u058F","\u0192","Kz","$","\u0192","\u20BC","KM","\u09F3","\u043B\u0432",".\u062F.\u0628","FBu","$b","R$","\u0E3F","Nu.","P","p.","BZ$","FC","CHF","\xA5","\u20A1","\u20B1","K\u010D","Fdj","kr","RD$","\u062F\u062C","kr","Nfk","Br","\u039E","\u20AC","\u20BE","\u20B5","GH\u20B5","D","FG","Q","L","kn","G","Ft","Rp","\u20AA","\u20B9","\u0639.\u062F","\uFDFC","kr","J$","JD","\xA5","KSh","\u043B\u0432","\u17DB","CF","\u20A9","\u20A9","KD","\u043B\u0432","\u20AD","\u20A8","M","\u0141","Lt","Ls","LD","MAD","lei","Ar","\u0434\u0435\u043D","K","\u20AE","MOP$","UM","\u20A8","Rf","MK","RM","MT","\u20A6","C$","kr","\u20A8","\uFDFC","B/.","S/.","K","\u20B1","\u20A8","z\u0142","Gs","\uFDFC","\uFFE5","lei","\u0414\u0438\u043D.","\u20BD","R\u20A3","\uFDFC","\u20A8","\u062C.\u0633.","kr","\xA3","Le","S","Db","E","\u0E3F","SM","T","\u062F.\u062A","T$","\u20A4","\u20BA","TT$","NT$","TSh","\u20B4","USh","$U","\u043B\u0432","Bs","\u20AB","VT","WS$","FCFA","\u0243","CFA","\u20A3","\uFDFC","R","Z$"];function w(e){return j(e)||Vr.some(t=>j(e.replace(t,"").replace(/[,.]/g,"")))?"numeric":e.trim()?"text":"empty"}var Ae="6.0.10";var Fr=Ae;function _r(e){let t=null,s=null;if(typeof e!="string")throw new TypeError(`csv-sort/csvSort(): [THROW_ID_01] The input is of a wrong type! We accept either string of array of arrays. We got instead: ${typeof e}, equal to:
|
|
21
|
+
${JSON.stringify(e,null,4)}`);if(!e.trim())return{res:[[""]],msgContent:t,msgType:s};let n=De(e),a=[],r=!1,l=!0,u=[],i=null;for(let p=n.length-1;p>=0;p--)if(a.length){p===0&&(r=n[p].every(h=>w(h)==="text"||w(h)==="empty")),!r&&a.length!==n[p].length&&(l=!1);let c=null;for(let h=0,S=n[p].length;h<S;h++)if(c===null&&w(n[p][h].trim())==="empty"&&(c=h),c!==null&&w(n[p][h].trim())!=="empty"&&(c=null),w(n[p][h].trim())!==a[h]&&!r){let T=w(n[p][h].trim());if(Array.isArray(a[h]))a[h].includes(T)||a[h].push(w(n[p][h].trim()));else if(a[h]!==T){let $=a[h];a[h]=[],a[h].push($),a[h].push(T)}}i!==null&&c!==null&&c>i&&(!r||r&&p!==0)&&(i=c)}else if(n[p].length!==1||n[p][0]!=="")for(let c=0,h=n[p].length;c<h;c++)a.push(w(n[p][c].trim())),i===null&&w(n[p][c].trim())==="empty"&&(i=c),i!==null&&w(n[p][c].trim())!=="empty"&&(i=null);i||(i=a.length);let o=0;for(let p=0,c=a.length;p<c&&a[p]==="empty";p++)o=p;o!==0&&(n=n.map(p=>p.slice(o+1,i)),a=a.slice(o+1,i));let f=[],g;a.forEach((p,c)=>{p==="numeric"&&f.push(c)});let y=r?1:0;if(f.length===1)g=f[0];else{if(f.length===0)throw new Error('csv-sort/csvSort(): [THROW_ID_03] Your CSV file does not contain numeric-only columns and computer was not able to detect the "Balance" column!');{let p=Array.from(f),c=[];for(let h=0,S=p.length;h<S;h++){let T=p[h],$,R=!0,I,N=!0;for(let v=y,xe=n.length;v<xe&&(R&&($==null?$=n[v][T]:$===n[v][T]?(c.push(T),R=!1):$=n[v][T]),N&&(I==null?I=n[v][T]:n[v][T]!==I&&(N=!1)),!!R);v++);N&&u.push(T)}if(p=(0,G.default)(p,...c),p.length===1)g=p[0];else if(p.length===0)throw new Error(`csv-sort/csvSort(): [THROW_ID_04] The computer can't find the "Balance" column! It saw some numeric-only columns, but they all seem to have certain rows with the same values as rows right below/above them!`)}}if(!g)throw new Error("csv-sort/csvSort(): [THROW_ID_05] Sadly computer couldn't find its way in this CSV and had to stop working on it.");let m=(0,G.default)(Array.from(a.reduce((p,c,h)=>((typeof c=="string"&&c==="numeric"||Array.isArray(c)&&c.includes("numeric"))&&p.push(h),p),[])),g,...u),d=[];d.push(n[n.length-1].slice(0,i));let O=[],F=r?1:0;for(let p=n.length-2;p>=F;p--)for(let c=n.length-2;c>=F;c--)if(!O.includes(c)){let h=!1;for(let S=0,T=m.length;S<T;S++){let $=null;n[c][m[S]]!==""&&($=(0,A.default)(n[c][m[S]]));let R=null;n[c][g]!==""&&(R=(0,A.default)(n[c][g]));let I=null;d[0][g]!==""&&(I=(0,A.default)(d[0][g]).format());let N=null;d[d.length-1][m[S]]!==""&&(N=(0,A.default)(d[d.length-1][m[S]]).format());let v=null;if(d[d.length-1][g]!==""&&(v=(0,A.default)(d[d.length-1][g])),$&&R.add($).format()===I){d.unshift(n[c].slice(0,i)),O.push(c),h=!0;break}else if($&&R.subtract($).format()===I){d.unshift(n[c].slice(0,i)),O.push(c),h=!0;break}else if(N&&v.add(N).format()===R.format()){d.push(n[c].slice(0,i)),O.push(c),h=!0;break}else if(N&&v.subtract(N).format()===R.format()){d.push(n[c].slice(0,i)),O.push(c),h=!0;break}}if(h){h=!1;break}}return r&&(l&&n[0].length>a.length&&(n[0].length=a.length),d.unshift(n[0].slice(0,i))),n.length-(r?2:1)!==O.length&&(t="Not all rows were recognised!",s="alert"),{res:d,msgContent:t,msgType:s}}return Je(jr);})();
|
|
11
22
|
/**
|
|
12
|
-
* @name
|
|
13
|
-
* @fileoverview
|
|
14
|
-
* @version
|
|
15
|
-
* @author Roy Revelt, Codsen Ltd
|
|
16
|
-
* @license MIT
|
|
17
|
-
* {@link https://codsen.com/os/ranges-sort/}
|
|
18
|
-
*/const h={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function c(e,r){if(!Array.isArray(e)||!e.length)return e;const t={...h,...r};let n,s;if(t.strictlyTwoElementsInRangeArrays&&!e.filter((e=>e)).every(((e,r)=>2===e.length||(n=r,s=e.length,!1))))throw new TypeError(`ranges-sort: [THROW_ID_03] The first argument should be an array and must consist of arrays which are natural number indexes representing TWO string index ranges. However, ${n}th range (${JSON.stringify(e[n],null,4)}) has not two but ${s} elements!`);if(!e.filter((e=>e)).every(((e,r)=>!(!Number.isInteger(e[0])||e[0]<0||!Number.isInteger(e[1])||e[1]<0)||(n=r,!1))))throw new TypeError(`ranges-sort: [THROW_ID_04] The first argument should be an array and must consist of arrays which are natural number indexes representing string index ranges. However, ${n}th range (${JSON.stringify(e[n],null,4)}) does not consist of only natural numbers!`);const i=e.filter((e=>e)).length**2;let o=0;return Array.from(e).filter((e=>e)).sort(((e,r)=>(t.progressFn&&(o+=1,t.progressFn(Math.floor(100*o/i))),e[0]===r[0]?e[1]<r[1]?-1:e[1]>r[1]?1:0:e[0]<r[0]?-1:1)))}
|
|
19
|
-
/**
|
|
20
|
-
* @name ranges-merge
|
|
21
|
-
* @fileoverview Merge and sort string index ranges
|
|
22
|
-
* @version 8.0.4
|
|
23
|
+
* @name csv-split-easy
|
|
24
|
+
* @fileoverview Splits the CSV string into array of arrays, each representing a row of columns
|
|
25
|
+
* @version 6.0.10
|
|
23
26
|
* @author Roy Revelt, Codsen Ltd
|
|
24
27
|
* @license MIT
|
|
25
|
-
* {@link https://codsen.com/os/
|
|
26
|
-
*/
|
|
28
|
+
* {@link https://codsen.com/os/csv-split-easy/}
|
|
29
|
+
*/
|
|
27
30
|
/**
|
|
28
31
|
* @name ranges-apply
|
|
29
32
|
* @fileoverview Take an array of string index ranges, delete/replace the string according to them
|
|
30
|
-
* @version 6.0.
|
|
33
|
+
* @version 6.0.10
|
|
31
34
|
* @author Roy Revelt, Codsen Ltd
|
|
32
35
|
* @license MIT
|
|
33
36
|
* {@link https://codsen.com/os/ranges-apply/}
|
|
34
37
|
*/
|
|
35
38
|
/**
|
|
36
|
-
* @name
|
|
37
|
-
* @fileoverview
|
|
38
|
-
* @version
|
|
39
|
+
* @name ranges-merge
|
|
40
|
+
* @fileoverview Merge and sort string index ranges
|
|
41
|
+
* @version 8.0.10
|
|
39
42
|
* @author Roy Revelt, Codsen Ltd
|
|
40
43
|
* @license MIT
|
|
41
|
-
* {@link https://codsen.com/os/
|
|
44
|
+
* {@link https://codsen.com/os/ranges-merge/}
|
|
42
45
|
*/
|
|
43
|
-
function d(e,r=1){function t(e){return Array.from(e).reverse().join("")}function n(e,r,t){const n=t?"\n":"\r",s=t?"\r":"\n";if(!e)return e;let i=0,o="";for(let t=0,a=e.length;t<a;t++)(e[t]===n||e[t]===s&&e[t-1]!==n)&&i++,"\r\n".includes(e[t])||" "===e[t]?" "===e[t]?o+=e[t]:e[t]===n?i<=r&&(o+=e[t],e[t+1]===s&&(o+=e[t+1],t++)):e[t]===s&&(!e[t-1]||e[t-1]!==n)&&i<=r&&(o+=e[t]):e[t+1]||i||(o+=" ");return o}if("string"==typeof e&&e.length){let s=1;"number"==typeof+r&&Number.isInteger(+r)&&+r>=0&&(s=+r);let i="",o="";if(e.trim()){if(!e[0].trim())for(let r=0,t=e.length;r<t;r++)if(e[r].trim()){i=e.slice(0,r);break}}else i=e;if(e.trim()&&(""===e.slice(-1).trim()||" "===e.slice(-1)))for(let r=e.length;r--;)if(e[r].trim()){o=e.slice(r+1);break}return`${n(i,s,!1)}${e.trim()}${t(n(t(o),s,!0))}`}return e}
|
|
44
46
|
/**
|
|
45
47
|
* @name ranges-push
|
|
46
48
|
* @fileoverview Gather string index ranges
|
|
47
|
-
* @version 6.0.
|
|
49
|
+
* @version 6.0.10
|
|
48
50
|
* @author Roy Revelt, Codsen Ltd
|
|
49
51
|
* @license MIT
|
|
50
52
|
* {@link https://codsen.com/os/ranges-push/}
|
|
51
|
-
*/
|
|
53
|
+
*/
|
|
52
54
|
/**
|
|
53
|
-
* @name
|
|
54
|
-
* @fileoverview
|
|
55
|
-
* @version
|
|
55
|
+
* @name ranges-sort
|
|
56
|
+
* @fileoverview Sort string index ranges
|
|
57
|
+
* @version 5.0.10
|
|
56
58
|
* @author Roy Revelt, Codsen Ltd
|
|
57
59
|
* @license MIT
|
|
58
|
-
* {@link https://codsen.com/os/
|
|
59
|
-
*/
|
|
60
|
+
* {@link https://codsen.com/os/ranges-sort/}
|
|
61
|
+
*/
|
|
60
62
|
/**
|
|
61
|
-
* @name
|
|
62
|
-
* @fileoverview
|
|
63
|
-
* @version 6.0.
|
|
63
|
+
* @name string-collapse-leading-whitespace
|
|
64
|
+
* @fileoverview Collapse the leading and trailing whitespace of a string
|
|
65
|
+
* @version 6.0.10
|
|
64
66
|
* @author Roy Revelt, Codsen Ltd
|
|
65
67
|
* @license MIT
|
|
66
|
-
* {@link https://codsen.com/os/
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
68
|
+
* {@link https://codsen.com/os/string-collapse-leading-whitespace/}
|
|
69
|
+
*/
|
|
70
|
+
/**
|
|
71
|
+
* @name string-remove-thousand-separators
|
|
72
|
+
* @fileoverview Detects and removes thousand separators (dot/comma/quote/space) from string-type digits
|
|
73
|
+
* @version 6.0.10
|
|
74
|
+
* @author Roy Revelt, Codsen Ltd
|
|
75
|
+
* @license MIT
|
|
76
|
+
* {@link https://codsen.com/os/string-remove-thousand-separators/}
|
|
74
77
|
*/
|
|
75
|
-
var re={symbol:"$",separator:",",decimal:".",errorOnInvalid:!1,precision:2,pattern:"!#",negativePattern:"-!#",format:function(e,r){var t=r.pattern,n=r.negativePattern,s=r.symbol,i=r.separator,o=r.decimal,a=r.groups,l=(""+e).replace(/^-/,"").split("."),u=l[0],f=l[1];return(e.value>=0?t:n).replace("!",s).replace("#",u.replace(a,"$1"+i)+(f?o+f:""))},fromCents:!1},te=function(e){return Math.round(e)},ne=function(e){return Math.pow(10,e)},se=/(\d)(?=(\d{3})+\b)/g,ie=/(\d)(?=(\d\d)+\d\b)/g;function oe(e,r){var t=this;if(!(t instanceof oe))return new oe(e,r);var n=Object.assign({},re,r),s=ne(n.precision),i=ae(e,n);t.intValue=i,t.value=i/s,n.increment=n.increment||1/s,n.groups=n.useVedic?ie:se,this.s=n,this.p=s}function ae(e,r){var t=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],n=0,s=r.decimal,i=r.errorOnInvalid,o=r.precision,a=r.fromCents,l=ne(o),u="number"==typeof e,f=e instanceof oe;if(f&&a)return e.intValue;if(u||f)n=f?e.value:e;else if("string"==typeof e){var g=new RegExp("[^-\\d"+s+"]","g"),p=new RegExp("\\"+s,"g");n=(n=e.replace(/\((.*)\)/,"-$1").replace(g,"").replace(p,"."))||0}else{if(i)throw Error("Invalid Input");n=0}return a||(n=(n*=l).toFixed(4)),t?te(n):n}function le(e){return!!e.trim()&&Number(e)==Number(e)}oe.prototype={add:function(e){var r=this.intValue,t=this.s,n=this.p;return oe((r+=ae(e,t))/(t.fromCents?1:n),t)},subtract:function(e){var r=this.intValue,t=this.s,n=this.p;return oe((r-=ae(e,t))/(t.fromCents?1:n),t)},multiply:function(e){var r=this.intValue,t=this.s;return oe((r*=e)/(t.fromCents?1:ne(t.precision)),t)},divide:function(e){var r=this.intValue,t=this.s;return oe(r/=ae(e,t,!1),t)},distribute:function(e){for(var r=this.intValue,t=this.p,n=this.s,s=[],i=Math[r>=0?"floor":"ceil"](r/e),o=Math.abs(r-i*e),a=n.fromCents?1:t;0!==e;e--){var l=oe(i/a,n);o-- >0&&(l=l[r>=0?"add":"subtract"](1/a)),s.push(l)}return s},dollars:function(){return~~this.value},cents:function(){return~~(this.intValue%this.p)},format:function(e){var r=this.s;return"function"==typeof e?e(this,r):r.format(this,Object.assign({},r,e))},toString:function(){var e,r,t=this.s;return(e=this.intValue/this.p,r=t.increment,te(e/r)*r).toFixed(t.precision)},toJSON:function(){return this.value}};const ue=["د.إ","؋","L","֏","ƒ","Kz","$","ƒ","₼","KM","৳","лв",".د.ب","FBu","$b","R$","฿","Nu.","P","p.","BZ$","FC","CHF","¥","₡","₱","Kč","Fdj","kr","RD$","دج","kr","Nfk","Br","Ξ","€","₾","₵","GH₵","D","FG","Q","L","kn","G","Ft","Rp","₪","₹","ع.د","﷼","kr","J$","JD","¥","KSh","лв","៛","CF","₩","₩","KD","лв","₭","₨","M","Ł","Lt","Ls","LD","MAD","lei","Ar","ден","K","₮","MOP$","UM","₨","Rf","MK","RM","MT","₦","C$","kr","₨","﷼","B/.","S/.","K","₱","₨","zł","Gs","﷼","¥","lei","Дин.","₽","R₣","﷼","₨","ج.س.","kr","£","Le","S","Db","E","฿","SM","T","د.ت","T$","₤","₺","TT$","NT$","TSh","₴","USh","$U","лв","Bs","₫","VT","WS$","FCFA","Ƀ","CFA","₣","﷼","R","Z$"];function fe(e){if("string"!=typeof e)throw new Error("csv-sort/util/findType(): input must be string! Currently it's: "+typeof e);return le(e)||ue.some((r=>le(e.replace(r,"").replace(/[,.]/g,""))))?"numeric":e.trim()?"text":"empty"}e.sort=function(e){let r=null,t=null;if("string"!=typeof e)throw new TypeError(`csv-sort/csvSort(): [THROW_ID_01] The input is of a wrong type! We accept either string of array of arrays. We got instead: ${typeof e}, equal to:\n${JSON.stringify(e,null,4)}`);if(!e.trim())return{res:[[""]],msgContent:r,msgType:t};let n=function(e,r){let t=0,n=0,s=[];const i=[];let o=!1,a=!0;if(r&&"object"!=typeof r)throw new Error(`csv-split-easy/split(): [THROW_ID_02] Options object must be a plain object! Currently it's of a type ${typeof r} equal to:\n${JSON.stringify(r,null,4)}`);const l={...ee,...r};if("string"!=typeof e)throw new TypeError(`csv-split-easy/split(): [THROW_ID_04] input must be string! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(""===e)return[[""]];for(let r=0,u=(e=e.trim()).length;r<u;r++){if(a&&'"'!==e[r]&&","!==e[r]&&""!==e[r].trim()&&(a=!1),'"'===e[r])if(o&&'"'===e[r+1])r+=1;else if(o){o=!1;const n=e.slice(t,r);""!==n.trim()&&(a=!1);const i=/""/.test(n)?n.replace(/""/g,'"'):X(n,{removeThousandSeparatorsFromNumbers:l.removeThousandSeparatorsFromNumbers,padSingleDecimalPlaceNumbers:l.padSingleDecimalPlaceNumbers,forceUKStyle:l.forceUKStyle});s.push(i)}else o=!0,t=r+1;else if(o||","!==e[r])if("\n"===e[r]||"\r"===e[r]){if(!n){if(n=r,!o&&'"'!==e[r-1]){const n=e.slice(t,r);""!==n.trim()&&(a=!1),s.push(X(n,{removeThousandSeparatorsFromNumbers:l.removeThousandSeparatorsFromNumbers,padSingleDecimalPlaceNumbers:l.padSingleDecimalPlaceNumbers,forceUKStyle:l.forceUKStyle}))}a?s.length=0:i.push(s),a=!0,s=[]}t=r+1}else n&&(n=0,t=r);else{if('"'!==e[r-1]&&!o){const n=e.slice(t,r);""!==n.trim()&&(a=!1),s.push(X(n,{removeThousandSeparatorsFromNumbers:l.removeThousandSeparatorsFromNumbers,padSingleDecimalPlaceNumbers:l.padSingleDecimalPlaceNumbers,forceUKStyle:l.forceUKStyle}))}t=r+1,n&&(n=0)}if(r+1===u){if('"'!==e[r]){const n=e.slice(t,r+1);n.trim()&&(a=!1),s.push(X(n,{removeThousandSeparatorsFromNumbers:l.removeThousandSeparatorsFromNumbers,padSingleDecimalPlaceNumbers:l.padSingleDecimalPlaceNumbers,forceUKStyle:l.forceUKStyle}))}a?s=[]:i.push(s),a=!0}}return 0===i.length?[[""]]:i}(e),s=[],i=!1,o=!0;const a=[];let l=null;for(let e=n.length-1;e>=0;e--)if(s.length){0===e&&(i=n[e].every((e=>"text"===fe(e)||"empty"===fe(e)))),i||s.length===n[e].length||(o=!1);let r=null;for(let t=0,o=n[e].length;t<o;t++)if(null===r&&"empty"===fe(n[e][t].trim())&&(r=t),null!==r&&"empty"!==fe(n[e][t].trim())&&(r=null),fe(n[e][t].trim())!==s[t]&&!i){const r=fe(n[e][t].trim());if(Array.isArray(s[t]))s[t].includes(r)||s[t].push(fe(n[e][t].trim()));else if(s[t]!==r){const e=s[t];s[t]=[],s[t].push(e),s[t].push(r)}}null!==l&&null!==r&&r>l&&(!i||i&&0!==e)&&(l=r)}else if(1!==n[e].length||""!==n[e][0])for(let r=0,t=n[e].length;r<t;r++)s.push(fe(n[e][r].trim())),null===l&&"empty"===fe(n[e][r].trim())&&(l=r),null!==l&&"empty"!==fe(n[e][r].trim())&&(l=null);l||(l=s.length);let u=0;for(let e=0,r=s.length;e<r&&"empty"===s[e];e++)u=e;0!==u&&(n=n.map((e=>e.slice(u+1,l))),s=s.slice(u+1,l));const f=[];let g;s.forEach(((e,r)=>{"numeric"===e&&f.push(r)}));const h=i?1:0;if(1===f.length)g=f[0];else{if(0===f.length)throw new Error('csv-sort/csvSort(): [THROW_ID_03] Your CSV file does not contain numeric-only columns and computer was not able to detect the "Balance" column!');{let e=Array.from(f);const r=[];for(let t=0,s=e.length;t<s;t++){const s=e[t];let i,o,l=!0,u=!0;for(let e=h,t=n.length;e<t&&(l&&(null==i?i=n[e][s]:i===n[e][s]?(r.push(s),l=!1):i=n[e][s]),u&&(null==o?o=n[e][s]:n[e][s]!==o&&(u=!1)),l);e++);u&&a.push(s)}if(e=p(e,...r),1===e.length)g=e[0];else if(0===e.length)throw new Error('csv-sort/csvSort(): [THROW_ID_04] The computer can\'t find the "Balance" column! It saw some numeric-only columns, but they all seem to have certain rows with the same values as rows right below/above them!')}}if(!g)throw new Error("csv-sort/csvSort(): [THROW_ID_05] Sadly computer couldn't find its way in this CSV and had to stop working on it.");const c=p(Array.from(s.reduce(((e,r,t)=>(("string"==typeof r&&"numeric"===r||Array.isArray(r)&&r.includes("numeric"))&&e.push(t),e)),[])),g,...a),y=[];y.push(n[n.length-1].slice(0,l));const m=[],d=i?1:0;for(let e=n.length-2;e>=d;e--)for(let e=n.length-2;e>=d;e--)if(!m.includes(e)){let r=!1;for(let t=0,s=c.length;t<s;t++){let s=null;""!==n[e][c[t]]&&(s=oe(n[e][c[t]]));let i=null;""!==n[e][g]&&(i=oe(n[e][g]));let o=null;""!==y[0][g]&&(o=oe(y[0][g]).format());let a=null;""!==y[y.length-1][c[t]]&&(a=oe(y[y.length-1][c[t]]).format());let u=null;if(""!==y[y.length-1][g]&&(u=oe(y[y.length-1][g])),s&&i.add(s).format()===o){y.unshift(n[e].slice(0,l)),m.push(e),r=!0;break}if(s&&i.subtract(s).format()===o){y.unshift(n[e].slice(0,l)),m.push(e),r=!0;break}if(a&&u.add(a).format()===i.format()){y.push(n[e].slice(0,l)),m.push(e),r=!0;break}if(a&&u.subtract(a).format()===i.format()){y.push(n[e].slice(0,l)),m.push(e),r=!0;break}}if(r){r=!1;break}}return i&&(o&&n[0].length>s.length&&(n[0].length=s.length),y.unshift(n[0].slice(0,l))),n.length-(i?2:1)!==m.length&&(r="Not all rows were recognised!",t="alert"),{res:y,msgContent:r,msgType:t}},Object.defineProperty(e,"__esModule",{value:!0})}));
|
package/examples/_quickTake.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "csv-sort",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.10",
|
|
4
4
|
"description": "Sorts double-entry bookkeeping CSV coming from internet banking",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"accountancy",
|
|
@@ -38,39 +38,28 @@
|
|
|
38
38
|
},
|
|
39
39
|
"types": "types/index.d.ts",
|
|
40
40
|
"scripts": {
|
|
41
|
-
"build": "
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"prettier": "../../node_modules/prettier/bin-prettier.js '*.{js,css,scss,vue,md,ts}' --write --loglevel silent",
|
|
54
|
-
"republish": "npm publish || :",
|
|
55
|
-
"tap": "tap",
|
|
56
|
-
"pretest": "npm run build",
|
|
57
|
-
"test": "npm run lint && npm run unittest && npm run test:examples && npm run clean_cov && npm run format",
|
|
58
|
-
"test:examples": "../../scripts/test-examples.js && npm run lect && npm run prettier",
|
|
59
|
-
"tsc": "tsc",
|
|
60
|
-
"unittest": "tap --no-only --output-file=testStats.md --reporter=terse && tsc -p tsconfig.json --noEmit && npm run clean_cov && npm run perf"
|
|
41
|
+
"build": "node '../../ops/scripts/esbuild.js' && yarn run dts",
|
|
42
|
+
"dev": "DEV=true node '../../ops/scripts/esbuild.js' && yarn run dts",
|
|
43
|
+
"dts": "rollup -c",
|
|
44
|
+
"examples": "node '../../ops/scripts/run-examples.js'",
|
|
45
|
+
"lect": "node '../../ops/lect/lect.js'",
|
|
46
|
+
"letspublish": "yarn publish || :",
|
|
47
|
+
"lint": "eslint . --fix",
|
|
48
|
+
"perf": "node perf/check.js",
|
|
49
|
+
"prepare": "echo 'ready'",
|
|
50
|
+
"pretest": "yarn run lect && yarn run build",
|
|
51
|
+
"test": "c8 yarn run unit && yarn run examples && yarn run lint",
|
|
52
|
+
"unit": "uvu test"
|
|
61
53
|
},
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"--no-warnings",
|
|
70
|
-
"--experimental-loader",
|
|
71
|
-
"@istanbuljs/esm-loader-hook"
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
|
|
56
|
+
},
|
|
57
|
+
"c8": {
|
|
58
|
+
"check-coverage": true,
|
|
59
|
+
"exclude": [
|
|
60
|
+
"**/test/**/*.*"
|
|
72
61
|
],
|
|
73
|
-
"
|
|
62
|
+
"lines": 100
|
|
74
63
|
},
|
|
75
64
|
"lect": {
|
|
76
65
|
"licence": {
|
|
@@ -78,60 +67,14 @@
|
|
|
78
67
|
"",
|
|
79
68
|
"List of [currency signs](https://github.com/bengourley/currency-symbol-map) - Copyright © 2017 Ben Gourley - see its [BSD-2-Clause disclaimer](https://opensource.org/licenses/BSD-2-Clause)"
|
|
80
69
|
]
|
|
81
|
-
},
|
|
82
|
-
"req": "{ sort }",
|
|
83
|
-
"various": {
|
|
84
|
-
"devDependencies": [
|
|
85
|
-
"@types/lodash.pull"
|
|
86
|
-
]
|
|
87
70
|
}
|
|
88
71
|
},
|
|
89
72
|
"dependencies": {
|
|
90
|
-
"
|
|
91
|
-
"csv-split-easy": "^6.0.4",
|
|
73
|
+
"csv-split-easy": "^6.0.10",
|
|
92
74
|
"currency.js": "^2.0.4",
|
|
93
75
|
"lodash.pull": "^4.1.0"
|
|
94
76
|
},
|
|
95
77
|
"devDependencies": {
|
|
96
|
-
"@
|
|
97
|
-
"@babel/core": "^7.16.0",
|
|
98
|
-
"@babel/node": "^7.16.0",
|
|
99
|
-
"@babel/plugin-external-helpers": "^7.16.0",
|
|
100
|
-
"@babel/plugin-proposal-class-properties": "^7.16.0",
|
|
101
|
-
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.0",
|
|
102
|
-
"@babel/plugin-proposal-object-rest-spread": "^7.16.0",
|
|
103
|
-
"@babel/plugin-proposal-optional-chaining": "^7.16.0",
|
|
104
|
-
"@babel/plugin-transform-runtime": "^7.16.0",
|
|
105
|
-
"@babel/preset-env": "^7.16.0",
|
|
106
|
-
"@babel/preset-typescript": "^7.16.0",
|
|
107
|
-
"@babel/register": "^7.16.0",
|
|
108
|
-
"@istanbuljs/esm-loader-hook": "^0.1.2",
|
|
109
|
-
"@rollup/plugin-babel": "^5.3.0",
|
|
110
|
-
"@rollup/plugin-commonjs": "^21.0.1",
|
|
111
|
-
"@rollup/plugin-json": "^4.1.0",
|
|
112
|
-
"@rollup/plugin-node-resolve": "^13.0.6",
|
|
113
|
-
"@rollup/plugin-strip": "^2.1.0",
|
|
114
|
-
"@rollup/plugin-typescript": "^8.3.0",
|
|
115
|
-
"@types/lodash.pull": "^4.1.6",
|
|
116
|
-
"@types/node": "^16.11.6",
|
|
117
|
-
"@types/tap": "^15.0.5",
|
|
118
|
-
"@typescript-eslint/eslint-plugin": "^5.3.0",
|
|
119
|
-
"@typescript-eslint/parser": "^5.3.0",
|
|
120
|
-
"core-js": "^3.19.1",
|
|
121
|
-
"cross-env": "^7.0.3",
|
|
122
|
-
"eslint": "^8.1.0",
|
|
123
|
-
"lect": "^0.18.4",
|
|
124
|
-
"rollup": "^2.59.0",
|
|
125
|
-
"rollup-plugin-ascii": "^0.0.3",
|
|
126
|
-
"rollup-plugin-banner": "^0.2.1",
|
|
127
|
-
"rollup-plugin-cleanup": "^3.2.1",
|
|
128
|
-
"rollup-plugin-dts": "^4.0.0",
|
|
129
|
-
"rollup-plugin-terser": "^7.0.2",
|
|
130
|
-
"tap": "^15.0.10",
|
|
131
|
-
"tslib": "^2.3.1",
|
|
132
|
-
"typescript": "^4.4.4"
|
|
133
|
-
},
|
|
134
|
-
"engines": {
|
|
135
|
-
"node": ">=12"
|
|
78
|
+
"@types/lodash.pull": "^4.1.6"
|
|
136
79
|
}
|
|
137
80
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
declare function isNumeric(str: any): boolean;
|
|
2
|
+
declare function findType(something: string): string;
|
|
3
|
+
|
|
4
|
+
declare const version: string;
|
|
1
5
|
interface Res {
|
|
2
6
|
res: string[][];
|
|
3
7
|
msgContent: null | string;
|
|
@@ -8,4 +12,4 @@ interface Res {
|
|
|
8
12
|
*/
|
|
9
13
|
declare function sort(input: string): Res;
|
|
10
14
|
|
|
11
|
-
export { sort };
|
|
15
|
+
export { findType, isNumeric, sort, version };
|
package/types/util/findType.d.ts
CHANGED
|
File without changes
|
package/examples/api.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"_quickTake.js":{"title":"Quick Take","content":"import { strict as assert } from \"assert\";\nimport { sort } from \"csv-sort\";\n\n// Sorts double-entry bookkeeping CSV's - bank statements for example\n// see https://en.wikipedia.org/wiki/Double-entry_bookkeeping\n\nassert.deepEqual(\n sort(`Acc Number,Description,Debit Amount,Credit Amount,Balance,\n123456,Client #1 payment,,1000,1940\n123456,Bought carpet,30,,950\n123456,Bought table,10,,940\n123456,Bought pens,10,,1000\n123456,Bought chairs,20,,980\n`),\n {\n res: [\n [\"Acc Number\", \"Description\", \"Debit Amount\", \"Credit Amount\", \"Balance\"],\n [\"123456\", \"Client #1 payment\", \"\", \"1000\", \"1940\"],\n [\"123456\", \"Bought table\", \"10\", \"\", \"940\"],\n [\"123456\", \"Bought carpet\", \"30\", \"\", \"950\"],\n [\"123456\", \"Bought chairs\", \"20\", \"\", \"980\"],\n [\"123456\", \"Bought pens\", \"10\", \"\", \"1000\"],\n ],\n msgContent: null,\n msgType: null,\n }\n);\n// you'll have to join elements and lines from the array yourself"}}
|