@willwade/aac-processors 0.1.6 → 0.1.8

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.
Files changed (54) hide show
  1. package/dist/analytics.d.ts +7 -0
  2. package/dist/analytics.js +23 -0
  3. package/dist/browser/index.browser.js +5 -0
  4. package/dist/browser/metrics.js +17 -0
  5. package/dist/browser/processors/gridset/helpers.js +390 -0
  6. package/dist/browser/processors/snap/helpers.js +252 -0
  7. package/dist/browser/utilities/analytics/history.js +116 -0
  8. package/dist/browser/utilities/analytics/metrics/comparison.js +477 -0
  9. package/dist/browser/utilities/analytics/metrics/core.js +775 -0
  10. package/dist/browser/utilities/analytics/metrics/effort.js +221 -0
  11. package/dist/browser/utilities/analytics/metrics/obl-types.js +6 -0
  12. package/dist/browser/utilities/analytics/metrics/obl.js +282 -0
  13. package/dist/browser/utilities/analytics/metrics/sentence.js +121 -0
  14. package/dist/browser/utilities/analytics/metrics/types.js +6 -0
  15. package/dist/browser/utilities/analytics/metrics/vocabulary.js +138 -0
  16. package/dist/browser/utilities/analytics/reference/browser.js +67 -0
  17. package/dist/browser/utilities/analytics/reference/index.js +129 -0
  18. package/dist/browser/utils/dotnetTicks.js +17 -0
  19. package/dist/browser/utils/io.js +16 -2
  20. package/dist/browser/validation/gridsetValidator.js +7 -27
  21. package/dist/browser/validation/obfValidator.js +9 -4
  22. package/dist/browser/validation/snapValidator.js +6 -9
  23. package/dist/browser/validation/touchChatValidator.js +6 -7
  24. package/dist/index.browser.d.ts +1 -0
  25. package/dist/index.browser.js +18 -1
  26. package/dist/index.node.d.ts +2 -2
  27. package/dist/index.node.js +5 -5
  28. package/dist/metrics.d.ts +17 -0
  29. package/dist/metrics.js +44 -0
  30. package/dist/utilities/analytics/metrics/comparison.d.ts +2 -1
  31. package/dist/utilities/analytics/metrics/comparison.js +3 -3
  32. package/dist/utilities/analytics/metrics/vocabulary.d.ts +2 -2
  33. package/dist/utilities/analytics/reference/browser.d.ts +31 -0
  34. package/dist/utilities/analytics/reference/browser.js +73 -0
  35. package/dist/utilities/analytics/reference/index.d.ts +21 -0
  36. package/dist/utilities/analytics/reference/index.js +22 -46
  37. package/dist/utils/io.d.ts +2 -0
  38. package/dist/utils/io.js +18 -2
  39. package/dist/validation/applePanelsValidator.js +11 -28
  40. package/dist/validation/astericsValidator.js +11 -30
  41. package/dist/validation/dotValidator.js +11 -30
  42. package/dist/validation/excelValidator.js +5 -6
  43. package/dist/validation/gridsetValidator.js +29 -26
  44. package/dist/validation/index.d.ts +2 -1
  45. package/dist/validation/index.js +9 -32
  46. package/dist/validation/obfValidator.js +8 -3
  47. package/dist/validation/obfsetValidator.js +11 -30
  48. package/dist/validation/opmlValidator.js +11 -30
  49. package/dist/validation/snapValidator.js +6 -9
  50. package/dist/validation/touchChatValidator.js +6 -7
  51. package/examples/vitedemo/index.html +49 -0
  52. package/examples/vitedemo/src/main.ts +84 -0
  53. package/examples/vitedemo/vite.config.ts +26 -7
  54. package/package.json +9 -1
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
33
33
  return result;
34
34
  };
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.Translation = exports.AstericsGrid = exports.ApplePanels = exports.Opml = exports.Excel = exports.Dot = exports.TouchChat = exports.Obfset = exports.OBF = exports.Snap = exports.Gridset = exports.Validation = exports.Analytics = void 0;
36
+ exports.Translation = exports.AstericsGrid = exports.ApplePanels = exports.Opml = exports.Excel = exports.Dot = exports.TouchChat = exports.Obfset = exports.OBF = exports.Snap = exports.Gridset = exports.Metrics = exports.Validation = exports.Analytics = void 0;
37
37
  exports.getProcessor = getProcessor;
38
38
  exports.getSupportedExtensions = getSupportedExtensions;
39
39
  exports.isExtensionSupported = isExtensionSupported;
@@ -50,12 +50,12 @@ __exportStar(require("./processors"), exports);
50
50
  // ===================================================================
51
51
  // NAMESPACES
52
52
  // ===================================================================
53
- // Analytics namespace
54
- exports.Analytics = __importStar(require("./utilities/analytics"));
55
- // Also export analytics classes directly for convenience
56
- __exportStar(require("./utilities/analytics"), exports);
53
+ // Analytics namespace (usage/history)
54
+ exports.Analytics = __importStar(require("./analytics"));
57
55
  // Validation namespace
58
56
  exports.Validation = __importStar(require("./validation"));
57
+ // Metrics namespace (pageset analytics)
58
+ exports.Metrics = __importStar(require("./metrics"));
59
59
  // Processor namespaces (platform-specific utilities)
60
60
  exports.Gridset = __importStar(require("./gridset"));
61
61
  exports.Snap = __importStar(require("./snap"));
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Metrics Namespace
3
+ *
4
+ * Pageset-focused metrics and analytics (board structure, effort, vocabulary).
5
+ * Use this for analyzing AAC trees, not end-user usage logs.
6
+ */
7
+ export * from './utilities/analytics/metrics/types';
8
+ export * from './utilities/analytics/metrics/effort';
9
+ export * from './utilities/analytics/metrics/obl-types';
10
+ export { OblUtil, OblAnonymizer } from './utilities/analytics/metrics/obl';
11
+ export { MetricsCalculator } from './utilities/analytics/metrics/core';
12
+ export { VocabularyAnalyzer } from './utilities/analytics/metrics/vocabulary';
13
+ export { SentenceAnalyzer } from './utilities/analytics/metrics/sentence';
14
+ export { ComparisonAnalyzer } from './utilities/analytics/metrics/comparison';
15
+ export { ReferenceLoader } from './utilities/analytics/reference';
16
+ export { InMemoryReferenceLoader, createBrowserReferenceLoader, loadReferenceDataFromUrl, type ReferenceData, } from './utilities/analytics/reference/browser';
17
+ export * from './utilities/analytics/utils/idGenerator';
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ /**
3
+ * Metrics Namespace
4
+ *
5
+ * Pageset-focused metrics and analytics (board structure, effort, vocabulary).
6
+ * Use this for analyzing AAC trees, not end-user usage logs.
7
+ */
8
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ var desc = Object.getOwnPropertyDescriptor(m, k);
11
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
+ desc = { enumerable: true, get: function() { return m[k]; } };
13
+ }
14
+ Object.defineProperty(o, k2, desc);
15
+ }) : (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ o[k2] = m[k];
18
+ }));
19
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
20
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
21
+ };
22
+ Object.defineProperty(exports, "__esModule", { value: true });
23
+ exports.loadReferenceDataFromUrl = exports.createBrowserReferenceLoader = exports.InMemoryReferenceLoader = exports.ReferenceLoader = exports.ComparisonAnalyzer = exports.SentenceAnalyzer = exports.VocabularyAnalyzer = exports.MetricsCalculator = exports.OblAnonymizer = exports.OblUtil = void 0;
24
+ __exportStar(require("./utilities/analytics/metrics/types"), exports);
25
+ __exportStar(require("./utilities/analytics/metrics/effort"), exports);
26
+ __exportStar(require("./utilities/analytics/metrics/obl-types"), exports);
27
+ var obl_1 = require("./utilities/analytics/metrics/obl");
28
+ Object.defineProperty(exports, "OblUtil", { enumerable: true, get: function () { return obl_1.OblUtil; } });
29
+ Object.defineProperty(exports, "OblAnonymizer", { enumerable: true, get: function () { return obl_1.OblAnonymizer; } });
30
+ var core_1 = require("./utilities/analytics/metrics/core");
31
+ Object.defineProperty(exports, "MetricsCalculator", { enumerable: true, get: function () { return core_1.MetricsCalculator; } });
32
+ var vocabulary_1 = require("./utilities/analytics/metrics/vocabulary");
33
+ Object.defineProperty(exports, "VocabularyAnalyzer", { enumerable: true, get: function () { return vocabulary_1.VocabularyAnalyzer; } });
34
+ var sentence_1 = require("./utilities/analytics/metrics/sentence");
35
+ Object.defineProperty(exports, "SentenceAnalyzer", { enumerable: true, get: function () { return sentence_1.SentenceAnalyzer; } });
36
+ var comparison_1 = require("./utilities/analytics/metrics/comparison");
37
+ Object.defineProperty(exports, "ComparisonAnalyzer", { enumerable: true, get: function () { return comparison_1.ComparisonAnalyzer; } });
38
+ var reference_1 = require("./utilities/analytics/reference");
39
+ Object.defineProperty(exports, "ReferenceLoader", { enumerable: true, get: function () { return reference_1.ReferenceLoader; } });
40
+ var browser_1 = require("./utilities/analytics/reference/browser");
41
+ Object.defineProperty(exports, "InMemoryReferenceLoader", { enumerable: true, get: function () { return browser_1.InMemoryReferenceLoader; } });
42
+ Object.defineProperty(exports, "createBrowserReferenceLoader", { enumerable: true, get: function () { return browser_1.createBrowserReferenceLoader; } });
43
+ Object.defineProperty(exports, "loadReferenceDataFromUrl", { enumerable: true, get: function () { return browser_1.loadReferenceDataFromUrl; } });
44
+ __exportStar(require("./utilities/analytics/utils/idGenerator"), exports);
@@ -5,12 +5,13 @@
5
5
  * analyze vocabulary differences, and generate CARE component scores.
6
6
  */
7
7
  import { MetricsResult, ComparisonResult } from './types';
8
+ import { type ReferenceDataProvider } from '../reference/index';
8
9
  import { MetricsOptions } from './types';
9
10
  export declare class ComparisonAnalyzer {
10
11
  private vocabAnalyzer;
11
12
  private sentenceAnalyzer;
12
13
  private referenceLoader;
13
- constructor();
14
+ constructor(referenceLoader?: ReferenceDataProvider);
14
15
  private normalize;
15
16
  /**
16
17
  * Compare two board sets
@@ -12,10 +12,10 @@ const vocabulary_1 = require("./vocabulary");
12
12
  const index_1 = require("../reference/index");
13
13
  const effort_1 = require("./effort");
14
14
  class ComparisonAnalyzer {
15
- constructor() {
16
- this.vocabAnalyzer = new vocabulary_1.VocabularyAnalyzer();
15
+ constructor(referenceLoader) {
16
+ this.vocabAnalyzer = new vocabulary_1.VocabularyAnalyzer(referenceLoader);
17
17
  this.sentenceAnalyzer = new sentence_1.SentenceAnalyzer();
18
- this.referenceLoader = new index_1.ReferenceLoader();
18
+ this.referenceLoader = referenceLoader || new index_1.ReferenceLoader();
19
19
  }
20
20
  normalize(word) {
21
21
  return word
@@ -5,7 +5,7 @@
5
5
  * and identifies missing/extra words compared to reference lists.
6
6
  */
7
7
  import { MetricsResult } from './types';
8
- import { ReferenceLoader } from '../reference/index';
8
+ import { type ReferenceDataProvider } from '../reference/index';
9
9
  export interface VocabularyAnalysis {
10
10
  core_coverage: {
11
11
  [listId: string]: {
@@ -33,7 +33,7 @@ export interface VocabularyAnalysis {
33
33
  }
34
34
  export declare class VocabularyAnalyzer {
35
35
  private referenceLoader;
36
- constructor(referenceLoader?: ReferenceLoader);
36
+ constructor(referenceLoader?: ReferenceDataProvider);
37
37
  /**
38
38
  * Analyze vocabulary coverage against core lists
39
39
  */
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Browser-friendly reference data loader using fetch.
3
+ */
4
+ import type { CoreList, CommonWordsData, SynonymsData } from '../metrics/types';
5
+ import type { ReferenceDataProvider } from './index';
6
+ export interface ReferenceData {
7
+ coreLists: CoreList[];
8
+ commonWords: CommonWordsData;
9
+ synonyms: SynonymsData;
10
+ sentences: string[][];
11
+ fringe: string[];
12
+ baseWords: {
13
+ [word: string]: boolean;
14
+ };
15
+ }
16
+ export declare class InMemoryReferenceLoader implements ReferenceDataProvider {
17
+ private data;
18
+ constructor(data: ReferenceData);
19
+ loadCoreLists(): CoreList[];
20
+ loadCommonWords(): CommonWordsData;
21
+ loadSynonyms(): SynonymsData;
22
+ loadSentences(): string[][];
23
+ loadFringe(): string[];
24
+ loadBaseWords(): {
25
+ [word: string]: boolean;
26
+ };
27
+ loadCommonFringe(): string[];
28
+ loadAll(): ReferenceData;
29
+ }
30
+ export declare function loadReferenceDataFromUrl(baseUrl: string, locale?: string): Promise<ReferenceData>;
31
+ export declare function createBrowserReferenceLoader(baseUrl: string, locale?: string): Promise<InMemoryReferenceLoader>;
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ /**
3
+ * Browser-friendly reference data loader using fetch.
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.InMemoryReferenceLoader = void 0;
7
+ exports.loadReferenceDataFromUrl = loadReferenceDataFromUrl;
8
+ exports.createBrowserReferenceLoader = createBrowserReferenceLoader;
9
+ class InMemoryReferenceLoader {
10
+ constructor(data) {
11
+ this.data = data;
12
+ }
13
+ loadCoreLists() {
14
+ return this.data.coreLists;
15
+ }
16
+ loadCommonWords() {
17
+ return this.data.commonWords;
18
+ }
19
+ loadSynonyms() {
20
+ return this.data.synonyms;
21
+ }
22
+ loadSentences() {
23
+ return this.data.sentences;
24
+ }
25
+ loadFringe() {
26
+ return this.data.fringe;
27
+ }
28
+ loadBaseWords() {
29
+ return this.data.baseWords;
30
+ }
31
+ loadCommonFringe() {
32
+ const commonWords = new Set(this.data.commonWords.words.map((w) => w.toLowerCase()));
33
+ const coreWords = new Set();
34
+ this.data.coreLists.forEach((list) => {
35
+ list.words.forEach((word) => coreWords.add(word.toLowerCase()));
36
+ });
37
+ return Array.from(commonWords).filter((word) => !coreWords.has(word));
38
+ }
39
+ loadAll() {
40
+ return this.data;
41
+ }
42
+ }
43
+ exports.InMemoryReferenceLoader = InMemoryReferenceLoader;
44
+ async function loadReferenceDataFromUrl(baseUrl, locale = 'en') {
45
+ const root = baseUrl.replace(/\/$/, '');
46
+ const fetchJson = async (name) => {
47
+ const res = await fetch(`${root}/${name}.${locale}.json`);
48
+ if (!res.ok) {
49
+ throw new Error(`Failed to load ${name}.${locale}.json`);
50
+ }
51
+ return (await res.json());
52
+ };
53
+ const [coreLists, commonWords, synonyms, sentences, fringe, baseWords] = await Promise.all([
54
+ fetchJson('core_lists'),
55
+ fetchJson('common_words'),
56
+ fetchJson('synonyms'),
57
+ fetchJson('sentences'),
58
+ fetchJson('fringe'),
59
+ fetchJson('base_words'),
60
+ ]);
61
+ return {
62
+ coreLists,
63
+ commonWords,
64
+ synonyms,
65
+ sentences,
66
+ fringe,
67
+ baseWords,
68
+ };
69
+ }
70
+ async function createBrowserReferenceLoader(baseUrl, locale = 'en') {
71
+ const data = await loadReferenceDataFromUrl(baseUrl, locale);
72
+ return new InMemoryReferenceLoader(data);
73
+ }
@@ -5,6 +5,27 @@
5
5
  * for AAC metrics analysis.
6
6
  */
7
7
  import { CoreList, CommonWordsData, SynonymsData } from '../metrics/types';
8
+ export interface ReferenceDataProvider {
9
+ loadCoreLists(): CoreList[];
10
+ loadCommonWords(): CommonWordsData;
11
+ loadSynonyms(): SynonymsData;
12
+ loadSentences(): string[][];
13
+ loadFringe(): string[];
14
+ loadBaseWords(): {
15
+ [word: string]: boolean;
16
+ };
17
+ loadCommonFringe(): string[];
18
+ loadAll(): {
19
+ coreLists: CoreList[];
20
+ commonWords: CommonWordsData;
21
+ synonyms: SynonymsData;
22
+ sentences: string[][];
23
+ fringe: string[];
24
+ baseWords: {
25
+ [word: string]: boolean;
26
+ };
27
+ };
28
+ }
8
29
  export declare class ReferenceLoader {
9
30
  private dataDir;
10
31
  private locale;
@@ -5,35 +5,11 @@
5
5
  * Loads reference vocabulary lists, core lists, and sentences
6
6
  * for AAC metrics analysis.
7
7
  */
8
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
- if (k2 === undefined) k2 = k;
10
- var desc = Object.getOwnPropertyDescriptor(m, k);
11
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
- desc = { enumerable: true, get: function() { return m[k]; } };
13
- }
14
- Object.defineProperty(o, k2, desc);
15
- }) : (function(o, m, k, k2) {
16
- if (k2 === undefined) k2 = k;
17
- o[k2] = m[k];
18
- }));
19
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
- Object.defineProperty(o, "default", { enumerable: true, value: v });
21
- }) : function(o, v) {
22
- o["default"] = v;
23
- });
24
- var __importStar = (this && this.__importStar) || function (mod) {
25
- if (mod && mod.__esModule) return mod;
26
- var result = {};
27
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
28
- __setModuleDefault(result, mod);
29
- return result;
30
- };
31
8
  Object.defineProperty(exports, "__esModule", { value: true });
32
9
  exports.ReferenceLoader = void 0;
33
10
  exports.getReferenceDataPath = getReferenceDataPath;
34
11
  exports.hasReferenceData = hasReferenceData;
35
- const fs = __importStar(require("fs"));
36
- const path = __importStar(require("path"));
12
+ const io_1 = require("../../../utils/io");
37
13
  class ReferenceLoader {
38
14
  constructor(dataDir, locale = 'en') {
39
15
  this.locale = locale;
@@ -43,48 +19,48 @@ class ReferenceLoader {
43
19
  else {
44
20
  // Resolve the data directory relative to this file's location
45
21
  // Use __dirname which works correctly after compilation
46
- this.dataDir = path.join(__dirname, 'data');
22
+ this.dataDir = (0, io_1.getPath)().join(__dirname, 'data');
47
23
  }
48
24
  }
49
25
  /**
50
26
  * Load core vocabulary lists
51
27
  */
52
28
  loadCoreLists() {
53
- const filePath = path.join(this.dataDir, `core_lists.${this.locale}.json`);
54
- const content = fs.readFileSync(filePath, 'utf-8');
55
- return JSON.parse(content);
29
+ const filePath = (0, io_1.getPath)().join(this.dataDir, `core_lists.${this.locale}.json`);
30
+ const content = (0, io_1.getFs)().readFileSync(filePath, 'utf-8');
31
+ return JSON.parse(String(content));
56
32
  }
57
33
  /**
58
34
  * Load common words with baseline effort scores
59
35
  */
60
36
  loadCommonWords() {
61
- const filePath = path.join(this.dataDir, `common_words.${this.locale}.json`);
62
- const content = fs.readFileSync(filePath, 'utf-8');
63
- return JSON.parse(content);
37
+ const filePath = (0, io_1.getPath)().join(this.dataDir, `common_words.${this.locale}.json`);
38
+ const content = (0, io_1.getFs)().readFileSync(filePath, 'utf-8');
39
+ return JSON.parse(String(content));
64
40
  }
65
41
  /**
66
42
  * Load synonym mappings
67
43
  */
68
44
  loadSynonyms() {
69
- const filePath = path.join(this.dataDir, `synonyms.${this.locale}.json`);
70
- const content = fs.readFileSync(filePath, 'utf-8');
71
- return JSON.parse(content);
45
+ const filePath = (0, io_1.getPath)().join(this.dataDir, `synonyms.${this.locale}.json`);
46
+ const content = (0, io_1.getFs)().readFileSync(filePath, 'utf-8');
47
+ return JSON.parse(String(content));
72
48
  }
73
49
  /**
74
50
  * Load test sentences
75
51
  */
76
52
  loadSentences() {
77
- const filePath = path.join(this.dataDir, `sentences.${this.locale}.json`);
78
- const content = fs.readFileSync(filePath, 'utf-8');
79
- return JSON.parse(content);
53
+ const filePath = (0, io_1.getPath)().join(this.dataDir, `sentences.${this.locale}.json`);
54
+ const content = (0, io_1.getFs)().readFileSync(filePath, 'utf-8');
55
+ return JSON.parse(String(content));
80
56
  }
81
57
  /**
82
58
  * Load fringe vocabulary
83
59
  */
84
60
  loadFringe() {
85
- const filePath = path.join(this.dataDir, `fringe.${this.locale}.json`);
86
- const content = fs.readFileSync(filePath, 'utf-8');
87
- const data = JSON.parse(content);
61
+ const filePath = (0, io_1.getPath)().join(this.dataDir, `fringe.${this.locale}.json`);
62
+ const content = (0, io_1.getFs)().readFileSync(filePath, 'utf-8');
63
+ const data = JSON.parse(String(content));
88
64
  // Flatten nested category words if needed
89
65
  if (Array.isArray(data) && data.length > 0 && data[0].categories) {
90
66
  const flattened = [];
@@ -101,9 +77,9 @@ class ReferenceLoader {
101
77
  * Load base words hash map
102
78
  */
103
79
  loadBaseWords() {
104
- const filePath = path.join(this.dataDir, `base_words.${this.locale}.json`);
105
- const content = fs.readFileSync(filePath, 'utf-8');
106
- return JSON.parse(content);
80
+ const filePath = (0, io_1.getPath)().join(this.dataDir, `base_words.${this.locale}.json`);
81
+ const content = (0, io_1.getFs)().readFileSync(filePath, 'utf-8');
82
+ return JSON.parse(String(content));
107
83
  }
108
84
  /**
109
85
  * Load common fringe vocabulary
@@ -141,7 +117,7 @@ exports.ReferenceLoader = ReferenceLoader;
141
117
  * Get the default reference data path
142
118
  */
143
119
  function getReferenceDataPath() {
144
- return path.join(__dirname, 'data');
120
+ return String((0, io_1.getPath)().join(__dirname, 'data'));
145
121
  }
146
122
  /**
147
123
  * Check if reference data files exist
@@ -155,5 +131,5 @@ function hasReferenceData() {
155
131
  'synonyms.en.json',
156
132
  'fringe.en.json',
157
133
  ];
158
- return requiredFiles.every((file) => fs.existsSync(path.join(dataPath, file)));
134
+ return requiredFiles.every((file) => (0, io_1.getFs)().existsSync((0, io_1.getPath)().join(dataPath, file)));
159
135
  }
@@ -7,6 +7,8 @@ export declare function getPath(): typeof import('path');
7
7
  export declare function getOs(): typeof import('os');
8
8
  export declare function isNodeRuntime(): boolean;
9
9
  export declare function getBasename(filePath: string): string;
10
+ export declare function toUint8Array(input: Uint8Array | ArrayBuffer | Buffer): Uint8Array;
11
+ export declare function toArrayBuffer(input: Uint8Array | ArrayBuffer | Buffer): ArrayBuffer;
10
12
  export declare function decodeText(input: Uint8Array): string;
11
13
  export declare function encodeBase64(input: Uint8Array): string;
12
14
  export declare function encodeText(text: string): BinaryOutput;
package/dist/utils/io.js CHANGED
@@ -6,6 +6,8 @@ exports.getPath = getPath;
6
6
  exports.getOs = getOs;
7
7
  exports.isNodeRuntime = isNodeRuntime;
8
8
  exports.getBasename = getBasename;
9
+ exports.toUint8Array = toUint8Array;
10
+ exports.toArrayBuffer = toArrayBuffer;
9
11
  exports.decodeText = decodeText;
10
12
  exports.encodeBase64 = encodeBase64;
11
13
  exports.encodeText = encodeText;
@@ -87,8 +89,22 @@ function isNodeRuntime() {
87
89
  return typeof process !== 'undefined' && !!process.versions?.node;
88
90
  }
89
91
  function getBasename(filePath) {
90
- const parts = filePath.split(/[/\\]/);
91
- return parts[parts.length - 1] || filePath;
92
+ const trimmed = filePath.replace(/[/\\]+$/, '') || filePath;
93
+ const parts = trimmed.split(/[/\\]/);
94
+ return parts[parts.length - 1] || trimmed;
95
+ }
96
+ function toUint8Array(input) {
97
+ if (input instanceof Uint8Array) {
98
+ return input;
99
+ }
100
+ return new Uint8Array(input);
101
+ }
102
+ function toArrayBuffer(input) {
103
+ if (input instanceof ArrayBuffer) {
104
+ return input;
105
+ }
106
+ const view = input instanceof Uint8Array ? input : new Uint8Array(input);
107
+ return view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
92
108
  }
93
109
  function decodeText(input) {
94
110
  if (typeof Buffer !== 'undefined' && Buffer.isBuffer(input)) {
@@ -1,45 +1,23 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
4
  };
28
5
  Object.defineProperty(exports, "__esModule", { value: true });
29
6
  exports.ApplePanelsValidator = void 0;
30
7
  /* eslint-disable @typescript-eslint/require-await */
31
- const fs = __importStar(require("fs"));
32
- const path = __importStar(require("path"));
33
8
  const plist_1 = __importDefault(require("plist"));
34
9
  const baseValidator_1 = require("./baseValidator");
10
+ const io_1 = require("../utils/io");
35
11
  /**
36
12
  * Validator for Apple Panels (.plist or .ascconfig directory)
37
13
  */
38
14
  class ApplePanelsValidator extends baseValidator_1.BaseValidator {
39
15
  static async validateFile(filePath) {
40
16
  const validator = new ApplePanelsValidator();
17
+ const fs = (0, io_1.getFs)();
18
+ const path = (0, io_1.getPath)();
41
19
  let content;
42
- const filename = path.basename(filePath);
20
+ const filename = (0, io_1.getBasename)(filePath);
43
21
  let size = 0;
44
22
  const stats = fs.existsSync(filePath) ? fs.statSync(filePath) : null;
45
23
  if (stats?.isDirectory() && filename.toLowerCase().endsWith('.ascconfig')) {
@@ -62,7 +40,12 @@ class ApplePanelsValidator extends baseValidator_1.BaseValidator {
62
40
  return true;
63
41
  }
64
42
  try {
65
- const str = Buffer.isBuffer(content) ? content.toString('utf-8') : String(content);
43
+ if (typeof content !== 'string' &&
44
+ !(content instanceof ArrayBuffer) &&
45
+ !(content instanceof Uint8Array)) {
46
+ return false;
47
+ }
48
+ const str = typeof content === 'string' ? content : (0, io_1.decodeText)((0, io_1.toUint8Array)(content));
66
49
  const parsed = plist_1.default.parse(str);
67
50
  return Boolean(parsed.panels || parsed.Panels);
68
51
  }
@@ -80,7 +63,7 @@ class ApplePanelsValidator extends baseValidator_1.BaseValidator {
80
63
  let parsed = null;
81
64
  await this.add_check('plist_parse', 'valid plist/XML', async () => {
82
65
  try {
83
- const str = Buffer.isBuffer(content) ? content.toString('utf-8') : String(content);
66
+ const str = (0, io_1.decodeText)(content);
84
67
  parsed = plist_1.default.parse(str);
85
68
  }
86
69
  catch (e) {
@@ -1,33 +1,9 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
3
  exports.AstericsGridValidator = void 0;
27
4
  /* eslint-disable @typescript-eslint/require-await */
28
- const fs = __importStar(require("fs"));
29
- const path = __importStar(require("path"));
30
5
  const baseValidator_1 = require("./baseValidator");
6
+ const io_1 = require("../utils/io");
31
7
  /**
32
8
  * Validator for Asterics Grid (.grd) JSON files
33
9
  */
@@ -37,9 +13,9 @@ class AstericsGridValidator extends baseValidator_1.BaseValidator {
37
13
  */
38
14
  static async validateFile(filePath) {
39
15
  const validator = new AstericsGridValidator();
40
- const content = fs.readFileSync(filePath);
41
- const stats = fs.statSync(filePath);
42
- return validator.validate(content, path.basename(filePath), stats.size);
16
+ const content = (0, io_1.readBinaryFromInput)(filePath);
17
+ const stats = (0, io_1.getFs)().statSync(filePath);
18
+ return validator.validate(content, (0, io_1.getBasename)(filePath), stats.size);
43
19
  }
44
20
  /**
45
21
  * Identify whether the content appears to be an Asterics .grd file
@@ -50,7 +26,12 @@ class AstericsGridValidator extends baseValidator_1.BaseValidator {
50
26
  return true;
51
27
  }
52
28
  try {
53
- const str = Buffer.isBuffer(content) ? content.toString('utf-8') : String(content);
29
+ if (typeof content !== 'string' &&
30
+ !(content instanceof ArrayBuffer) &&
31
+ !(content instanceof Uint8Array)) {
32
+ return false;
33
+ }
34
+ const str = typeof content === 'string' ? content : (0, io_1.decodeText)((0, io_1.toUint8Array)(content));
54
35
  const json = JSON.parse(str);
55
36
  return Array.isArray(json?.grids);
56
37
  }
@@ -68,7 +49,7 @@ class AstericsGridValidator extends baseValidator_1.BaseValidator {
68
49
  let json = null;
69
50
  await this.add_check('json_parse', 'valid JSON', async () => {
70
51
  try {
71
- let str = Buffer.isBuffer(content) ? content.toString('utf-8') : String(content);
52
+ let str = (0, io_1.decodeText)(content);
72
53
  if (str.charCodeAt(0) === 0xfeff) {
73
54
  str = str.slice(1);
74
55
  }
@@ -1,49 +1,30 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
3
  exports.DotValidator = void 0;
27
4
  /* eslint-disable @typescript-eslint/require-await */
28
- const fs = __importStar(require("fs"));
29
- const path = __importStar(require("path"));
30
5
  const baseValidator_1 = require("./baseValidator");
6
+ const io_1 = require("../utils/io");
31
7
  /**
32
8
  * Validator for Graphviz DOT files
33
9
  */
34
10
  class DotValidator extends baseValidator_1.BaseValidator {
35
11
  static async validateFile(filePath) {
36
12
  const validator = new DotValidator();
37
- const content = fs.readFileSync(filePath);
38
- const stats = fs.statSync(filePath);
39
- return validator.validate(content, path.basename(filePath), stats.size);
13
+ const content = (0, io_1.readBinaryFromInput)(filePath);
14
+ const stats = (0, io_1.getFs)().statSync(filePath);
15
+ return validator.validate(content, (0, io_1.getBasename)(filePath), stats.size);
40
16
  }
41
17
  static async identifyFormat(content, filename) {
42
18
  const name = filename.toLowerCase();
43
19
  if (name.endsWith('.dot'))
44
20
  return true;
45
21
  try {
46
- const str = Buffer.isBuffer(content) ? content.toString('utf-8') : String(content);
22
+ if (typeof content !== 'string' &&
23
+ !(content instanceof ArrayBuffer) &&
24
+ !(content instanceof Uint8Array)) {
25
+ return false;
26
+ }
27
+ const str = typeof content === 'string' ? content : (0, io_1.decodeText)((0, io_1.toUint8Array)(content));
47
28
  return str.includes('digraph') || str.includes('->');
48
29
  }
49
30
  catch {
@@ -59,7 +40,7 @@ class DotValidator extends baseValidator_1.BaseValidator {
59
40
  });
60
41
  let text = '';
61
42
  await this.add_check('text', 'text content', async () => {
62
- text = Buffer.isBuffer(content) ? content.toString('utf-8') : String(content);
43
+ text = (0, io_1.decodeText)(content);
63
44
  if (!text.trim()) {
64
45
  this.err('DOT file is empty', true);
65
46
  }