@cyberismo/data-handler 0.0.2
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/LICENSE +702 -0
- package/dist/card-metadata-updater.d.ts +33 -0
- package/dist/card-metadata-updater.js +121 -0
- package/dist/card-metadata-updater.js.map +1 -0
- package/dist/command-handler.d.ts +96 -0
- package/dist/command-handler.js +557 -0
- package/dist/command-handler.js.map +1 -0
- package/dist/command-manager.d.ts +43 -0
- package/dist/command-manager.js +73 -0
- package/dist/command-manager.js.map +1 -0
- package/dist/commands/calculate.d.ts +86 -0
- package/dist/commands/calculate.js +444 -0
- package/dist/commands/calculate.js.map +1 -0
- package/dist/commands/create.d.ts +114 -0
- package/dist/commands/create.js +389 -0
- package/dist/commands/create.js.map +1 -0
- package/dist/commands/edit.d.ts +37 -0
- package/dist/commands/edit.js +99 -0
- package/dist/commands/edit.js.map +1 -0
- package/dist/commands/export-site.d.ts +45 -0
- package/dist/commands/export-site.js +301 -0
- package/dist/commands/export-site.js.map +1 -0
- package/dist/commands/export.d.ts +53 -0
- package/dist/commands/export.js +251 -0
- package/dist/commands/export.js.map +1 -0
- package/dist/commands/import.d.ts +53 -0
- package/dist/commands/import.js +133 -0
- package/dist/commands/import.js.map +1 -0
- package/dist/commands/index.d.ts +26 -0
- package/dist/commands/index.js +27 -0
- package/dist/commands/index.js.map +1 -0
- package/dist/commands/move.d.ts +55 -0
- package/dist/commands/move.js +341 -0
- package/dist/commands/move.js.map +1 -0
- package/dist/commands/remove.d.ts +38 -0
- package/dist/commands/remove.js +192 -0
- package/dist/commands/remove.js.map +1 -0
- package/dist/commands/rename.d.ts +46 -0
- package/dist/commands/rename.js +289 -0
- package/dist/commands/rename.js.map +1 -0
- package/dist/commands/show.d.ts +124 -0
- package/dist/commands/show.js +345 -0
- package/dist/commands/show.js.map +1 -0
- package/dist/commands/transition.d.ts +27 -0
- package/dist/commands/transition.js +92 -0
- package/dist/commands/transition.js.map +1 -0
- package/dist/commands/update.d.ts +29 -0
- package/dist/commands/update.js +64 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/commands/validate.d.ts +143 -0
- package/dist/commands/validate.js +689 -0
- package/dist/commands/validate.js.map +1 -0
- package/dist/containers/card-container.d.ts +44 -0
- package/dist/containers/card-container.js +282 -0
- package/dist/containers/card-container.js.map +1 -0
- package/dist/containers/project/project-paths.d.ts +46 -0
- package/dist/containers/project/project-paths.js +105 -0
- package/dist/containers/project/project-paths.js.map +1 -0
- package/dist/containers/project/resource-collector.d.ts +86 -0
- package/dist/containers/project/resource-collector.js +331 -0
- package/dist/containers/project/resource-collector.js.map +1 -0
- package/dist/containers/project.d.ts +351 -0
- package/dist/containers/project.js +896 -0
- package/dist/containers/project.js.map +1 -0
- package/dist/containers/template.d.ts +108 -0
- package/dist/containers/template.js +433 -0
- package/dist/containers/template.js.map +1 -0
- package/dist/exceptions/index.d.ts +19 -0
- package/dist/exceptions/index.js +26 -0
- package/dist/exceptions/index.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/adoc.d.ts +12 -0
- package/dist/interfaces/adoc.js +13 -0
- package/dist/interfaces/adoc.js.map +1 -0
- package/dist/interfaces/macros.d.ts +45 -0
- package/dist/interfaces/macros.js +13 -0
- package/dist/interfaces/macros.js.map +1 -0
- package/dist/interfaces/project-interfaces.d.ts +121 -0
- package/dist/interfaces/project-interfaces.js +21 -0
- package/dist/interfaces/project-interfaces.js.map +1 -0
- package/dist/interfaces/request-status-interfaces.d.ts +28 -0
- package/dist/interfaces/request-status-interfaces.js +20 -0
- package/dist/interfaces/request-status-interfaces.js.map +1 -0
- package/dist/interfaces/resource-interfaces.d.ts +117 -0
- package/dist/interfaces/resource-interfaces.js +20 -0
- package/dist/interfaces/resource-interfaces.js.map +1 -0
- package/dist/macros/base-macro.d.ts +31 -0
- package/dist/macros/base-macro.js +126 -0
- package/dist/macros/base-macro.js.map +1 -0
- package/dist/macros/common.d.ts +17 -0
- package/dist/macros/common.js +23 -0
- package/dist/macros/common.js.map +1 -0
- package/dist/macros/createCards/index.d.ts +36 -0
- package/dist/macros/createCards/index.js +35 -0
- package/dist/macros/createCards/index.js.map +1 -0
- package/dist/macros/createCards/metadata.d.ts +14 -0
- package/dist/macros/createCards/metadata.js +18 -0
- package/dist/macros/createCards/metadata.js.map +1 -0
- package/dist/macros/graph/index.d.ts +29 -0
- package/dist/macros/graph/index.js +91 -0
- package/dist/macros/graph/index.js.map +1 -0
- package/dist/macros/graph/metadata.d.ts +14 -0
- package/dist/macros/graph/metadata.js +18 -0
- package/dist/macros/graph/metadata.js.map +1 -0
- package/dist/macros/index.d.ts +93 -0
- package/dist/macros/index.js +237 -0
- package/dist/macros/index.js.map +1 -0
- package/dist/macros/report/index.d.ts +26 -0
- package/dist/macros/report/index.js +70 -0
- package/dist/macros/report/index.js.map +1 -0
- package/dist/macros/report/metadata.d.ts +14 -0
- package/dist/macros/report/metadata.js +18 -0
- package/dist/macros/report/metadata.js.map +1 -0
- package/dist/macros/scoreCard/index.d.ts +30 -0
- package/dist/macros/scoreCard/index.js +38 -0
- package/dist/macros/scoreCard/index.js.map +1 -0
- package/dist/macros/scoreCard/metadata.d.ts +14 -0
- package/dist/macros/scoreCard/metadata.js +18 -0
- package/dist/macros/scoreCard/metadata.js.map +1 -0
- package/dist/macros/task-queue.d.ts +46 -0
- package/dist/macros/task-queue.js +69 -0
- package/dist/macros/task-queue.js.map +1 -0
- package/dist/module-manager.d.ts +62 -0
- package/dist/module-manager.js +350 -0
- package/dist/module-manager.js.map +1 -0
- package/dist/permissions/action-guard.d.ts +28 -0
- package/dist/permissions/action-guard.js +61 -0
- package/dist/permissions/action-guard.js.map +1 -0
- package/dist/project-settings.d.ts +42 -0
- package/dist/project-settings.js +120 -0
- package/dist/project-settings.js.map +1 -0
- package/dist/resources/array-handler.d.ts +28 -0
- package/dist/resources/array-handler.js +116 -0
- package/dist/resources/array-handler.js.map +1 -0
- package/dist/resources/card-type-resource.d.ts +72 -0
- package/dist/resources/card-type-resource.js +334 -0
- package/dist/resources/card-type-resource.js.map +1 -0
- package/dist/resources/create-defaults.d.ts +81 -0
- package/dist/resources/create-defaults.js +184 -0
- package/dist/resources/create-defaults.js.map +1 -0
- package/dist/resources/field-type-resource.d.ts +88 -0
- package/dist/resources/field-type-resource.js +411 -0
- package/dist/resources/field-type-resource.js.map +1 -0
- package/dist/resources/file-resource.d.ts +50 -0
- package/dist/resources/file-resource.js +301 -0
- package/dist/resources/file-resource.js.map +1 -0
- package/dist/resources/folder-resource.d.ts +66 -0
- package/dist/resources/folder-resource.js +100 -0
- package/dist/resources/folder-resource.js.map +1 -0
- package/dist/resources/graph-model-resource.d.ts +78 -0
- package/dist/resources/graph-model-resource.js +164 -0
- package/dist/resources/graph-model-resource.js.map +1 -0
- package/dist/resources/graph-view-resource.d.ts +78 -0
- package/dist/resources/graph-view-resource.js +163 -0
- package/dist/resources/graph-view-resource.js.map +1 -0
- package/dist/resources/link-type-resource.d.ts +62 -0
- package/dist/resources/link-type-resource.js +150 -0
- package/dist/resources/link-type-resource.js.map +1 -0
- package/dist/resources/report-resource.d.ts +77 -0
- package/dist/resources/report-resource.js +171 -0
- package/dist/resources/report-resource.js.map +1 -0
- package/dist/resources/resource-object.d.ts +108 -0
- package/dist/resources/resource-object.js +147 -0
- package/dist/resources/resource-object.js.map +1 -0
- package/dist/resources/template-resource.d.ts +82 -0
- package/dist/resources/template-resource.js +173 -0
- package/dist/resources/template-resource.js.map +1 -0
- package/dist/resources/workflow-resource.d.ts +67 -0
- package/dist/resources/workflow-resource.js +156 -0
- package/dist/resources/workflow-resource.js.map +1 -0
- package/dist/types/queries.d.ts +142 -0
- package/dist/types/queries.js +16 -0
- package/dist/types/queries.js.map +1 -0
- package/dist/utils/card-utils.d.ts +34 -0
- package/dist/utils/card-utils.js +78 -0
- package/dist/utils/card-utils.js.map +1 -0
- package/dist/utils/clingo-fact-builder.d.ts +58 -0
- package/dist/utils/clingo-fact-builder.js +126 -0
- package/dist/utils/clingo-fact-builder.js.map +1 -0
- package/dist/utils/clingo-facts.d.ts +97 -0
- package/dist/utils/clingo-facts.js +352 -0
- package/dist/utils/clingo-facts.js.map +1 -0
- package/dist/utils/clingo-parser.d.ts +59 -0
- package/dist/utils/clingo-parser.js +403 -0
- package/dist/utils/clingo-parser.js.map +1 -0
- package/dist/utils/clingo-program-builder.d.ts +39 -0
- package/dist/utils/clingo-program-builder.js +57 -0
- package/dist/utils/clingo-program-builder.js.map +1 -0
- package/dist/utils/common-utils.d.ts +24 -0
- package/dist/utils/common-utils.js +47 -0
- package/dist/utils/common-utils.js.map +1 -0
- package/dist/utils/constants.d.ts +18 -0
- package/dist/utils/constants.js +27 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/csv.d.ts +18 -0
- package/dist/utils/csv.js +45 -0
- package/dist/utils/csv.js.map +1 -0
- package/dist/utils/file-utils.d.ts +69 -0
- package/dist/utils/file-utils.js +158 -0
- package/dist/utils/file-utils.js.map +1 -0
- package/dist/utils/json.d.ts +61 -0
- package/dist/utils/json.js +108 -0
- package/dist/utils/json.js.map +1 -0
- package/dist/utils/lexorank.d.ts +59 -0
- package/dist/utils/lexorank.js +159 -0
- package/dist/utils/lexorank.js.map +1 -0
- package/dist/utils/log-utils.d.ts +40 -0
- package/dist/utils/log-utils.js +109 -0
- package/dist/utils/log-utils.js.map +1 -0
- package/dist/utils/random.d.ts +19 -0
- package/dist/utils/random.js +34 -0
- package/dist/utils/random.js.map +1 -0
- package/dist/utils/resource-utils.d.ts +45 -0
- package/dist/utils/resource-utils.js +137 -0
- package/dist/utils/resource-utils.js.map +1 -0
- package/dist/utils/sanitize-svg.d.ts +18 -0
- package/dist/utils/sanitize-svg.js +38 -0
- package/dist/utils/sanitize-svg.js.map +1 -0
- package/dist/utils/user-preferences.d.ts +64 -0
- package/dist/utils/user-preferences.js +106 -0
- package/dist/utils/user-preferences.js.map +1 -0
- package/dist/utils/validate.d.ts +26 -0
- package/dist/utils/validate.js +53 -0
- package/dist/utils/validate.js.map +1 -0
- package/dist/utils/value-utils.d.ts +58 -0
- package/dist/utils/value-utils.js +181 -0
- package/dist/utils/value-utils.js.map +1 -0
- package/package.json +67 -0
- package/src/card-metadata-updater.ts +182 -0
- package/src/command-handler.ts +686 -0
- package/src/command-manager.ts +99 -0
- package/src/commands/calculate.ts +591 -0
- package/src/commands/create.ts +559 -0
- package/src/commands/edit.ts +123 -0
- package/src/commands/export-site.ts +356 -0
- package/src/commands/export.ts +315 -0
- package/src/commands/import.ts +169 -0
- package/src/commands/index.ts +42 -0
- package/src/commands/move.ts +451 -0
- package/src/commands/remove.ts +244 -0
- package/src/commands/rename.ts +378 -0
- package/src/commands/show.ts +442 -0
- package/src/commands/transition.ts +127 -0
- package/src/commands/update.ts +76 -0
- package/src/commands/validate.ts +962 -0
- package/src/containers/card-container.ts +378 -0
- package/src/containers/project/project-paths.ts +127 -0
- package/src/containers/project/resource-collector.ts +379 -0
- package/src/containers/project.ts +1135 -0
- package/src/containers/template.ts +573 -0
- package/src/exceptions/index.ts +29 -0
- package/src/index.ts +33 -0
- package/src/interfaces/adoc.ts +18 -0
- package/src/interfaces/macros.ts +54 -0
- package/src/interfaces/project-interfaces.ts +208 -0
- package/src/interfaces/request-status-interfaces.ts +30 -0
- package/src/interfaces/resource-interfaces.ts +179 -0
- package/src/macros/base-macro.ts +176 -0
- package/src/macros/common.ts +24 -0
- package/src/macros/createCards/index.ts +57 -0
- package/src/macros/createCards/metadata.ts +21 -0
- package/src/macros/graph/index.ts +130 -0
- package/src/macros/graph/metadata.ts +21 -0
- package/src/macros/index.ts +321 -0
- package/src/macros/report/index.ts +88 -0
- package/src/macros/report/metadata.ts +21 -0
- package/src/macros/scoreCard/index.ts +55 -0
- package/src/macros/scoreCard/metadata.ts +21 -0
- package/src/macros/task-queue.ts +79 -0
- package/src/module-manager.ts +443 -0
- package/src/permissions/action-guard.ts +77 -0
- package/src/project-settings.ts +140 -0
- package/src/resources/array-handler.ts +141 -0
- package/src/resources/card-type-resource.ts +455 -0
- package/src/resources/create-defaults.ts +216 -0
- package/src/resources/field-type-resource.ts +533 -0
- package/src/resources/file-resource.ts +433 -0
- package/src/resources/folder-resource.ts +140 -0
- package/src/resources/graph-model-resource.ts +205 -0
- package/src/resources/graph-view-resource.ts +199 -0
- package/src/resources/link-type-resource.ts +191 -0
- package/src/resources/report-resource.ts +224 -0
- package/src/resources/resource-object.ts +246 -0
- package/src/resources/template-resource.ts +210 -0
- package/src/resources/workflow-resource.ts +205 -0
- package/src/types/queries.ts +149 -0
- package/src/utils/card-utils.ts +83 -0
- package/src/utils/clingo-fact-builder.ts +167 -0
- package/src/utils/clingo-facts.ts +550 -0
- package/src/utils/clingo-parser.ts +519 -0
- package/src/utils/clingo-program-builder.ts +71 -0
- package/src/utils/common-utils.ts +54 -0
- package/src/utils/constants.ts +32 -0
- package/src/utils/csv.ts +53 -0
- package/src/utils/file-utils.ts +182 -0
- package/src/utils/json.ts +118 -0
- package/src/utils/lexorank.ts +180 -0
- package/src/utils/log-utils.ts +127 -0
- package/src/utils/random.ts +37 -0
- package/src/utils/resource-utils.ts +180 -0
- package/src/utils/sanitize-svg.ts +46 -0
- package/src/utils/user-preferences.ts +126 -0
- package/src/utils/validate.ts +66 -0
- package/src/utils/value-utils.ts +189 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2024
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
|
|
6
|
+
|
|
7
|
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
|
8
|
+
|
|
9
|
+
You should have received a copy of the GNU Affero General Public
|
|
10
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
11
|
+
*/
|
|
12
|
+
import type { CSVRowRaw } from '../interfaces/project-interfaces.js';
|
|
13
|
+
/**
|
|
14
|
+
* Reads a CSV file and returns its content as an array of objects.
|
|
15
|
+
* @param file Path to the CSV file.
|
|
16
|
+
* @returns Array of objects. Each object represents a row in the CSV file.
|
|
17
|
+
*/
|
|
18
|
+
export declare function readCsvFile(file: string): Promise<CSVRowRaw[]>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2024
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License version 3 as published by the Free Software Foundation.
|
|
6
|
+
|
|
7
|
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
|
|
8
|
+
|
|
9
|
+
You should have received a copy of the GNU Affero General Public
|
|
10
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
11
|
+
*/
|
|
12
|
+
import { readFile } from 'fs/promises';
|
|
13
|
+
import { parse } from 'csv-parse/sync';
|
|
14
|
+
/**
|
|
15
|
+
* Reads a CSV file and returns its content as an array of objects.
|
|
16
|
+
* @param file Path to the CSV file.
|
|
17
|
+
* @returns Array of objects. Each object represents a row in the CSV file.
|
|
18
|
+
*/
|
|
19
|
+
export async function readCsvFile(file) {
|
|
20
|
+
const content = await readFile(file, {
|
|
21
|
+
encoding: 'utf-8',
|
|
22
|
+
});
|
|
23
|
+
const records = parse(content, {
|
|
24
|
+
bom: true,
|
|
25
|
+
});
|
|
26
|
+
if (!Array.isArray(records) || records.length < 2) {
|
|
27
|
+
throw new Error('CSV file must have headers');
|
|
28
|
+
}
|
|
29
|
+
const [headers, ...data] = records;
|
|
30
|
+
if (!Array.isArray(headers) ||
|
|
31
|
+
new Set(headers).size !== headers.length ||
|
|
32
|
+
headers.length === 0) {
|
|
33
|
+
throw new Error('Error parsing header');
|
|
34
|
+
}
|
|
35
|
+
return data.map((row) => {
|
|
36
|
+
if (!Array.isArray(row)) {
|
|
37
|
+
throw new Error('Row is not an array');
|
|
38
|
+
}
|
|
39
|
+
return headers.reduce((acc, header, index) => {
|
|
40
|
+
acc[header] = row[index];
|
|
41
|
+
return acc;
|
|
42
|
+
}, {});
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=csv.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"csv.js","sourceRoot":"","sources":["../../src/utils/csv.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;EAUE;AAEF,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAGvC;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,IAAY;IAC5C,MAAM,OAAO,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE;QACnC,QAAQ,EAAE,OAAO;KAClB,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,EAAE;QAC7B,GAAG,EAAE,IAAI;KACV,CAAC,CAAC;IAEH,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAClD,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAChD,CAAC;IAED,MAAM,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC;IAEnC,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;QACvB,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,KAAK,OAAO,CAAC,MAAM;QACxC,OAAO,CAAC,MAAM,KAAK,CAAC,EACpB,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;IAC1C,CAAC;IAED,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QACtB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACzC,CAAC;QACD,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE;YAC3C,GAAG,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC;YACzB,OAAO,GAAG,CAAC;QACb,CAAC,EAAE,EAAE,CAAC,CAAC;IACT,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2024
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
6
|
+
the terms of the GNU Affero General Public License version 3 as published by
|
|
7
|
+
the Free Software Foundation. This program is distributed in the hope that it
|
|
8
|
+
will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
9
|
+
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
10
|
+
See the GNU Affero General Public License for more details.
|
|
11
|
+
You should have received a copy of the GNU Affero General Public
|
|
12
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
13
|
+
*/
|
|
14
|
+
import { writeFile } from 'node:fs/promises';
|
|
15
|
+
/**
|
|
16
|
+
* Works like the writeFile method, but ensures that the directory exists
|
|
17
|
+
* There is only one difference: This method only supports a string as the filePath
|
|
18
|
+
*/
|
|
19
|
+
export declare function writeFileSafe(filePath: string, data: Parameters<typeof writeFile>[1], options?: Parameters<typeof writeFile>[2]): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Copies directory content (subdirectories and files) to destination.
|
|
22
|
+
* Note that it won't create 'source', but copies all that is inside of 'source'.
|
|
23
|
+
* @param source path to start from
|
|
24
|
+
* @param destination path where to copy to
|
|
25
|
+
*/
|
|
26
|
+
export declare function copyDir(source: string, destination: string): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Delete directory.
|
|
29
|
+
* @param path path to be deleted
|
|
30
|
+
*/
|
|
31
|
+
export declare function deleteDir(path: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Delete file.
|
|
34
|
+
* @param path path to file to be deleted
|
|
35
|
+
* @returns true, if file was deleted; false otherwise.
|
|
36
|
+
*/
|
|
37
|
+
export declare function deleteFile(path: string): Promise<boolean>;
|
|
38
|
+
/**
|
|
39
|
+
* Removes extension from filename.
|
|
40
|
+
* @param filename Filename
|
|
41
|
+
* @returns filename without extension. If there was no extension, returns the original filename.
|
|
42
|
+
*/
|
|
43
|
+
export declare function stripExtension(filename: string): string;
|
|
44
|
+
/**
|
|
45
|
+
* Lists all files from a folder.
|
|
46
|
+
* @param path path to folder
|
|
47
|
+
* @param pathPrefix relative adjustment to 'path', if any; optional; by default empty.
|
|
48
|
+
* @param files currently collected files; optional; by default empty.
|
|
49
|
+
* @returns array of filenames that are in the folder or in one of its subfolders.
|
|
50
|
+
* @note that 'pathPrefix' and 'files' are generally only used in internal recursion.
|
|
51
|
+
* When calling this from code, do not pass the parameters.
|
|
52
|
+
*/
|
|
53
|
+
export declare function getFilesSync(path: string, pathPrefix?: string, files?: string[]): string[];
|
|
54
|
+
/**
|
|
55
|
+
* Checks if file or folder exists.
|
|
56
|
+
* @param path file or folder path
|
|
57
|
+
* @returns true if file exists, otherwise false
|
|
58
|
+
*/
|
|
59
|
+
export declare function pathExists(path: string): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Handles tilde from a path (ie. appends user home to its place).
|
|
62
|
+
* @param filePath Path to handle tilde from.
|
|
63
|
+
* @returns Path with tilde resolved.
|
|
64
|
+
*/
|
|
65
|
+
export declare function resolveTilde(filePath: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Path separator RE.
|
|
68
|
+
*/
|
|
69
|
+
export declare const sepRegex: RegExp;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2024
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
6
|
+
the terms of the GNU Affero General Public License version 3 as published by
|
|
7
|
+
the Free Software Foundation. This program is distributed in the hope that it
|
|
8
|
+
will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
9
|
+
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
10
|
+
See the GNU Affero General Public License for more details.
|
|
11
|
+
You should have received a copy of the GNU Affero General Public
|
|
12
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
13
|
+
*/
|
|
14
|
+
import { copyFile, mkdir, readdir, rm, unlink, writeFile, } from 'node:fs/promises';
|
|
15
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
16
|
+
import { dirname, join, sep } from 'node:path';
|
|
17
|
+
import { homedir } from 'node:os';
|
|
18
|
+
/**
|
|
19
|
+
* Works like the writeFile method, but ensures that the directory exists
|
|
20
|
+
* There is only one difference: This method only supports a string as the filePath
|
|
21
|
+
*/
|
|
22
|
+
export async function writeFileSafe(filePath, data, options) {
|
|
23
|
+
const dir = dirname(filePath);
|
|
24
|
+
await mkdir(dir, {
|
|
25
|
+
recursive: true,
|
|
26
|
+
});
|
|
27
|
+
return writeFile(filePath, data, options);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Copies directory content (subdirectories and files) to destination.
|
|
31
|
+
* Note that it won't create 'source', but copies all that is inside of 'source'.
|
|
32
|
+
* @param source path to start from
|
|
33
|
+
* @param destination path where to copy to
|
|
34
|
+
*/
|
|
35
|
+
export async function copyDir(source, destination) {
|
|
36
|
+
const entries = await readdir(source, { withFileTypes: true });
|
|
37
|
+
for (const entry of entries) {
|
|
38
|
+
const sourcePath = join(source, entry.name);
|
|
39
|
+
const destinationPath = join(destination, entry.name);
|
|
40
|
+
if (entry.isDirectory()) {
|
|
41
|
+
await mkdir(destinationPath, { recursive: true });
|
|
42
|
+
await copyDir(sourcePath, destinationPath);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
await mkdir(destination, { recursive: true });
|
|
46
|
+
await copyFile(sourcePath, destinationPath);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Delete directory.
|
|
52
|
+
* @param path path to be deleted
|
|
53
|
+
*/
|
|
54
|
+
export async function deleteDir(path) {
|
|
55
|
+
await rm(resolveTilde(path), { recursive: true, force: true });
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Delete file.
|
|
59
|
+
* @param path path to file to be deleted
|
|
60
|
+
* @returns true, if file was deleted; false otherwise.
|
|
61
|
+
*/
|
|
62
|
+
export async function deleteFile(path) {
|
|
63
|
+
if (!path) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
await unlink(path);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
console.error(`Cannot delete file '${path}'`);
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Removes extension from filename.
|
|
77
|
+
* @param filename Filename
|
|
78
|
+
* @returns filename without extension. If there was no extension, returns the original filename.
|
|
79
|
+
*/
|
|
80
|
+
export function stripExtension(filename) {
|
|
81
|
+
// First handle special cases. Just return the filename in all of these cases.
|
|
82
|
+
// 1) If the filename ends to "."" or ".." (e.g. ".cards/local/..")
|
|
83
|
+
const parts = filename.split(sep);
|
|
84
|
+
if (parts.at(parts.length - 1) === '..' ||
|
|
85
|
+
parts.at(parts.length - 1) === '.') {
|
|
86
|
+
return filename;
|
|
87
|
+
}
|
|
88
|
+
const dotLocation = filename.lastIndexOf('.');
|
|
89
|
+
const sepLocation = filename.lastIndexOf(sep);
|
|
90
|
+
// 2) If there is a dot in the filename before sep (e.g. ".cards/local")
|
|
91
|
+
if (dotLocation < sepLocation) {
|
|
92
|
+
return filename;
|
|
93
|
+
}
|
|
94
|
+
// 3) If there is a dot in filename but it is not an actual extension (e.g. "test/.filename", or ".filename")
|
|
95
|
+
if (dotLocation === 0 || filename.at(dotLocation - 1) === sep) {
|
|
96
|
+
return filename;
|
|
97
|
+
}
|
|
98
|
+
const noExtension = filename.split('.').slice(0, -1).join('.');
|
|
99
|
+
// if there was no extension at all, return the original file name.
|
|
100
|
+
return noExtension ? noExtension : filename;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Lists all files from a folder.
|
|
104
|
+
* @param path path to folder
|
|
105
|
+
* @param pathPrefix relative adjustment to 'path', if any; optional; by default empty.
|
|
106
|
+
* @param files currently collected files; optional; by default empty.
|
|
107
|
+
* @returns array of filenames that are in the folder or in one of its subfolders.
|
|
108
|
+
* @note that 'pathPrefix' and 'files' are generally only used in internal recursion.
|
|
109
|
+
* When calling this from code, do not pass the parameters.
|
|
110
|
+
*/
|
|
111
|
+
export function getFilesSync(path, pathPrefix = '', files = []) {
|
|
112
|
+
try {
|
|
113
|
+
for (const entry of readdirSync(path, { withFileTypes: true })) {
|
|
114
|
+
const relativePath = pathPrefix
|
|
115
|
+
? join(pathPrefix, entry.name)
|
|
116
|
+
: entry.name;
|
|
117
|
+
if (entry.isFile()) {
|
|
118
|
+
files.push(relativePath);
|
|
119
|
+
}
|
|
120
|
+
else if (entry.isDirectory()) {
|
|
121
|
+
getFilesSync(join(path, entry.name), relativePath, files);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
// do nothing, wrong path, or no permissions to read the files
|
|
127
|
+
}
|
|
128
|
+
return files;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Checks if file or folder exists.
|
|
132
|
+
* @param path file or folder path
|
|
133
|
+
* @returns true if file exists, otherwise false
|
|
134
|
+
*/
|
|
135
|
+
export function pathExists(path) {
|
|
136
|
+
path = resolveTilde(path);
|
|
137
|
+
return existsSync(path);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Handles tilde from a path (ie. appends user home to its place).
|
|
141
|
+
* @param filePath Path to handle tilde from.
|
|
142
|
+
* @returns Path with tilde resolved.
|
|
143
|
+
*/
|
|
144
|
+
export function resolveTilde(filePath) {
|
|
145
|
+
if (!filePath || typeof filePath !== 'string') {
|
|
146
|
+
return '';
|
|
147
|
+
}
|
|
148
|
+
// '~/folder/path' or '~' not '~alias/folder/path'
|
|
149
|
+
if (filePath.startsWith('~/') || filePath === '~') {
|
|
150
|
+
return filePath.replace('~', homedir());
|
|
151
|
+
}
|
|
152
|
+
return filePath;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Path separator RE.
|
|
156
|
+
*/
|
|
157
|
+
export const sepRegex = /[/\\]/;
|
|
158
|
+
//# sourceMappingURL=file-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-utils.js","sourceRoot":"","sources":["../../src/utils/file-utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;EAYE;AAEF,OAAO,EACL,QAAQ,EACR,KAAK,EACL,OAAO,EACP,EAAE,EACF,MAAM,EACN,SAAS,GACV,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAAgB,EAChB,IAAqC,EACrC,OAAyC;IAEzC,MAAM,GAAG,GAAG,OAA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AC,CAAC;IAC1C,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,OAAO,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2024
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
6
|
+
the terms of the GNU Affero General Public License version 3 as published by
|
|
7
|
+
the Free Software Foundation. This program is distributed in the hope that it
|
|
8
|
+
will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
9
|
+
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
10
|
+
See the GNU Affero General Public License for more details.
|
|
11
|
+
You should have received a copy of the GNU Affero General Public
|
|
12
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
13
|
+
*/
|
|
14
|
+
import { type FileHandle } from 'node:fs/promises';
|
|
15
|
+
/**
|
|
16
|
+
* Handles reading of a JSON file.
|
|
17
|
+
* @param file file name (and path) to read.
|
|
18
|
+
* @returns Parsed JSON content.
|
|
19
|
+
* @throws if file is not found, or file is not a JSON file.
|
|
20
|
+
*/
|
|
21
|
+
export declare function readJsonFileSync(file: string): any;
|
|
22
|
+
/**
|
|
23
|
+
* Handles reading of a JSON file.
|
|
24
|
+
* @param file file name (and path) to read.
|
|
25
|
+
* @returns Parsed JSON content.
|
|
26
|
+
* @throws if file is not found, or file is not a JSON file.
|
|
27
|
+
*/
|
|
28
|
+
export declare function readJsonFile(file: string): Promise<any>;
|
|
29
|
+
/**
|
|
30
|
+
* Reads ADOC file.
|
|
31
|
+
* @param file file name (and path) to read.
|
|
32
|
+
* @returns ADOC file content.
|
|
33
|
+
* @throws if file is not found.
|
|
34
|
+
*/
|
|
35
|
+
export declare function readADocFileSync(file: string): string;
|
|
36
|
+
/**
|
|
37
|
+
* Removes leading or trailing whitespace
|
|
38
|
+
* Can be passed to JSON.stringify method
|
|
39
|
+
* @param _ First parameter is key of object, but it is not used
|
|
40
|
+
* @param value Value of the json object
|
|
41
|
+
* @returns JSON object with values trimmed for leading or trailing white space.
|
|
42
|
+
*/
|
|
43
|
+
export declare function trimReplacer(_: string, value: unknown): unknown;
|
|
44
|
+
/**
|
|
45
|
+
* Format an object with JSON.stringify
|
|
46
|
+
*
|
|
47
|
+
* The purpose of this function is to format the JSON output in a centralised function
|
|
48
|
+
* so that the format can be controlled in a single location.
|
|
49
|
+
*
|
|
50
|
+
* @param json JSON object to format.
|
|
51
|
+
* @returns Formatted JSON string
|
|
52
|
+
*/
|
|
53
|
+
export declare function formatJson(json: object): string;
|
|
54
|
+
/**
|
|
55
|
+
* Writes and formats a JSON file.
|
|
56
|
+
* @param filename file name (and path) to write.
|
|
57
|
+
* @param json JSON object to format.
|
|
58
|
+
* @param options Optional, write options
|
|
59
|
+
* @return true if write succeeded, false otherwise.
|
|
60
|
+
*/
|
|
61
|
+
export declare function writeJsonFile(filename: string | FileHandle, json: object, options?: object): Promise<boolean>;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2024
|
|
4
|
+
|
|
5
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
6
|
+
the terms of the GNU Affero General Public License version 3 as published by
|
|
7
|
+
the Free Software Foundation. This program is distributed in the hope that it
|
|
8
|
+
will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
9
|
+
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
10
|
+
See the GNU Affero General Public License for more details.
|
|
11
|
+
You should have received a copy of the GNU Affero General Public
|
|
12
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
13
|
+
*/
|
|
14
|
+
import { readFileSync } from 'node:fs';
|
|
15
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
16
|
+
/**
|
|
17
|
+
* Handles reading of a JSON file.
|
|
18
|
+
* @param file file name (and path) to read.
|
|
19
|
+
* @returns Parsed JSON content.
|
|
20
|
+
* @throws if file is not found, or file is not a JSON file.
|
|
21
|
+
*/
|
|
22
|
+
export function readJsonFileSync(file) {
|
|
23
|
+
try {
|
|
24
|
+
const raw = readFileSync(file, { flag: 'rs', encoding: 'utf-8' });
|
|
25
|
+
const returnValue = JSON.parse(raw);
|
|
26
|
+
return returnValue;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
if (error instanceof Error) {
|
|
30
|
+
throw new Error(`Error while handling JSON file '${file}' : ${error.message}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Handles reading of a JSON file.
|
|
36
|
+
* @param file file name (and path) to read.
|
|
37
|
+
* @returns Parsed JSON content.
|
|
38
|
+
* @throws if file is not found, or file is not a JSON file.
|
|
39
|
+
*/
|
|
40
|
+
export async function readJsonFile(file) {
|
|
41
|
+
try {
|
|
42
|
+
const raw = await readFile(file, { encoding: 'utf-8' });
|
|
43
|
+
return JSON.parse(raw);
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
if (error instanceof Error) {
|
|
47
|
+
throw new Error(`Error while handling JSON file '${file}' : ${error.message}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Reads ADOC file.
|
|
53
|
+
* @param file file name (and path) to read.
|
|
54
|
+
* @returns ADOC file content.
|
|
55
|
+
* @throws if file is not found.
|
|
56
|
+
*/
|
|
57
|
+
export function readADocFileSync(file) {
|
|
58
|
+
try {
|
|
59
|
+
const raw = readFileSync(file, { encoding: 'utf-8' });
|
|
60
|
+
return raw;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
throw new Error(`Adoc file '${file}' not found`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Removes leading or trailing whitespace
|
|
68
|
+
* Can be passed to JSON.stringify method
|
|
69
|
+
* @param _ First parameter is key of object, but it is not used
|
|
70
|
+
* @param value Value of the json object
|
|
71
|
+
* @returns JSON object with values trimmed for leading or trailing white space.
|
|
72
|
+
*/
|
|
73
|
+
export function trimReplacer(_, value) {
|
|
74
|
+
if (typeof value === 'string') {
|
|
75
|
+
return value.trim();
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Format an object with JSON.stringify
|
|
81
|
+
*
|
|
82
|
+
* The purpose of this function is to format the JSON output in a centralised function
|
|
83
|
+
* so that the format can be controlled in a single location.
|
|
84
|
+
*
|
|
85
|
+
* @param json JSON object to format.
|
|
86
|
+
* @returns Formatted JSON string
|
|
87
|
+
*/
|
|
88
|
+
export function formatJson(json) {
|
|
89
|
+
return JSON.stringify(json, trimReplacer, 4);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Writes and formats a JSON file.
|
|
93
|
+
* @param filename file name (and path) to write.
|
|
94
|
+
* @param json JSON object to format.
|
|
95
|
+
* @param options Optional, write options
|
|
96
|
+
* @return true if write succeeded, false otherwise.
|
|
97
|
+
*/
|
|
98
|
+
export async function writeJsonFile(filename, json, options) {
|
|
99
|
+
try {
|
|
100
|
+
await writeFile(filename, formatJson(json), options);
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
// do nothing, file didn't exist, or no permissions to write
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=json.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","sourceRoot":"","sources":["../../src/utils/json.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;EAYE;AAEF,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAmB,QAAQ,EAAE,SAAS,EAAE,MAAM,kB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ED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,CAAS,EAAE,KAAc;IACpD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC,IAAI,EAAE,CAAC;IACtB,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC;AAC/C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,QAA6B,EAC7B,IAAY,EACZ,OAAgB;IAEhB,IAAI,CAAC;QACH,MAAM,SAAS,CAAC,QAAQ,EAAE,UAAU,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC;QACrD,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,4DAA4D;IAC9D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export declare const FIRST_RANK: string;
|
|
2
|
+
export declare const LAST_RANK: string;
|
|
3
|
+
export declare const EMPTY_RANK: string;
|
|
4
|
+
type Rank = string;
|
|
5
|
+
/**
|
|
6
|
+
* Convert a number to base 26
|
|
7
|
+
* @param num number to convert to base 26
|
|
8
|
+
* @returns the string representation of the number in base 26
|
|
9
|
+
*/
|
|
10
|
+
export declare function enbase(num: number): string;
|
|
11
|
+
/**
|
|
12
|
+
* Convert a rank to a number
|
|
13
|
+
* @param str rank to convert to a number
|
|
14
|
+
* @returns the number representation of the rank
|
|
15
|
+
*/
|
|
16
|
+
export declare function debase(str: string): number;
|
|
17
|
+
/**
|
|
18
|
+
* Returns the next available rank after the given rank
|
|
19
|
+
* @param rank rank to get the next rank after
|
|
20
|
+
* @returns the next available rank after the given rank
|
|
21
|
+
*/
|
|
22
|
+
export declare function getRankAfter(rank: Rank): Rank;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the previous available rank before the given rank
|
|
25
|
+
* if the rank is already the first rank, it throws an error
|
|
26
|
+
* @param rank rank to get the previous rank before
|
|
27
|
+
* @returns the previous available rank before the given rank
|
|
28
|
+
*/
|
|
29
|
+
export declare function getRankBefore(rank: Rank): Rank;
|
|
30
|
+
/**
|
|
31
|
+
* Get the rank between two ranks
|
|
32
|
+
* Rank is a string that represents a number in base 26
|
|
33
|
+
* Rank must be a string of lowercase letters
|
|
34
|
+
* @param rank1 first rank
|
|
35
|
+
* @param rank2 second rank
|
|
36
|
+
* @returns the rank between the two ranks
|
|
37
|
+
*/
|
|
38
|
+
export declare function getRankBetween(rank1: string, rank2: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Rebalance the ranks so that the distance between each rank is equal
|
|
41
|
+
* @param rankAmount number of ranks to rebalance
|
|
42
|
+
* @returns rebalanced ranks
|
|
43
|
+
*/
|
|
44
|
+
export declare function rebalanceRanks(rankAmount: number): string[];
|
|
45
|
+
/**
|
|
46
|
+
* Sort items based on lexorank
|
|
47
|
+
* @param items items to sort
|
|
48
|
+
* @param rankGetter should return the lexorank of the item
|
|
49
|
+
* @returns sorted items
|
|
50
|
+
*/
|
|
51
|
+
export declare function sortItems<T>(items: T[], rankGetter: (item: T) => string): T[];
|
|
52
|
+
/**
|
|
53
|
+
* default compare function
|
|
54
|
+
* @param a first string
|
|
55
|
+
* @param b second string
|
|
56
|
+
* @returns -1 if a < b, 0 if a === b, 1 if a > b
|
|
57
|
+
*/
|
|
58
|
+
export declare function compare(a: string, b: string): number;
|
|
59
|
+
export {};
|