@kuroson/cse-groups 0.2.1

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 ADDED
@@ -0,0 +1,37 @@
1
+ # @kuroson/cse-groups
2
+
3
+ ## 0.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 61adb1e: Updated dependency `@typescript-eslint/eslint-plugin` to `8.50.1`.
8
+ Updated dependency `@typescript-eslint/parser` to `8.50.1`.
9
+ Updated dependency `typescript-eslint` to `8.50.1`.
10
+ Updated dependency `next` to `16.1.1`.
11
+ Updated dependency `eslint-config-next` to `16.1.1`.
12
+ Updated dependency `@next/third-parties` to `16.1.1`.
13
+ Updated dependency `ldapts` to `8.0.31`.
14
+ Updated dependency `@next/eslint-plugin-next` to `16.1.1`.
15
+ Updated dependency `eslint-plugin-jest` to `29.10.1`.
16
+ Updated dependency `nodemailer` to `7.0.12`.
17
+ - Updated dependencies [61adb1e]
18
+ - @kuroson/common-utils@0.2.1
19
+ - @kuroson/cse-students@0.3.1
20
+
21
+ ## 0.2.0
22
+
23
+ ### Minor Changes
24
+
25
+ - cdb0acc: Initial release
26
+
27
+ ### Patch Changes
28
+
29
+ - Updated dependencies [cdb0acc]
30
+ - @kuroson/common-utils@0.2.0
31
+ - @kuroson/cse-students@0.3.0
32
+
33
+ ## 0.1.0
34
+
35
+ ### Features
36
+
37
+ - Initial public release
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Alvin Cherk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # @kuroson/cse-groups
2
+
3
+ Group allocation parser for UNSW CSE courses.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @kuroson/cse-groups
9
+ # or
10
+ pnpm add @kuroson/cse-groups
11
+ # or
12
+ yarn add @kuroson/cse-groups
13
+ ```
14
+
15
+ ## Requirements
16
+
17
+ - Node.js >= 22
18
+
19
+ ## License
20
+
21
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,110 @@
1
+ 'use strict';
2
+
3
+ var commonUtils = require('@kuroson/common-utils');
4
+ var path = require('path');
5
+ require('winston');
6
+ var fs = require('fs');
7
+
8
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
+
10
+ var path__default = /*#__PURE__*/_interopDefault(path);
11
+
12
+ // src/Groups.ts
13
+ var readFileFrom = (path2, encoding = "utf-8") => {
14
+ if (fs.existsSync(path2)) {
15
+ return fs.readFileSync(path2, encoding);
16
+ }
17
+ throw new Error(`Failed to read file from expected directory: ${path2}`);
18
+ };
19
+
20
+ // src/Groups.ts
21
+ var Groups = class {
22
+ groupFiles;
23
+ groups = /* @__PURE__ */ new Map();
24
+ logger;
25
+ basePath;
26
+ constructor(groupFiles, options) {
27
+ this.groupFiles = [...groupFiles];
28
+ this.logger = options?.logger;
29
+ this.basePath = options.basePath;
30
+ for (const groupFile of this.groupFiles) {
31
+ const readPath = path__default.default.join(this.basePath, groupFile.fileName);
32
+ this.logger?.verbose(`Reading from ${readPath} for '${groupFile.assignment}'`);
33
+ try {
34
+ const input = readFileFrom(readPath, "utf-8");
35
+ const lines = input.split("\n").slice(1).filter((x) => x.length !== 0);
36
+ const groups = {};
37
+ for (const line of lines) {
38
+ const [id, group] = line.split(",");
39
+ groups[id.startsWith("z") ? id : `z${id}`] = group === "null" || group.length === 0 ? null : group;
40
+ }
41
+ this.groups.set(groupFile.assignment, { ...groups });
42
+ this.logger?.verbose(`Read ${Object.keys(groups).length} groups for '${groupFile.assignment}'`);
43
+ } catch (err) {
44
+ this.logger?.error(`Failed to read from ${groupFile.fileName}`);
45
+ this.logger?.error(commonUtils.parseError(err));
46
+ }
47
+ }
48
+ }
49
+ /**
50
+ * Gets a map of zIDs to group names for a given assignment
51
+ * @param assignment given assignment to get groups for
52
+ */
53
+ getGroups = (assignment) => {
54
+ if (this.groups.has(assignment)) {
55
+ return { ...this.groups.get(assignment) };
56
+ }
57
+ return {};
58
+ };
59
+ /**
60
+ * Gets a map of zIDs to group names, and student names for a given assignment.
61
+ * If a student is not found, they are not included in the output.
62
+ * @param assignment given assignment to get groups for
63
+ * @param allStudents array of all students
64
+ */
65
+ getGroupsFullInfo = (assignment, allStudents) => {
66
+ const groups = this.getGroups(assignment);
67
+ const output = {};
68
+ for (const [zID, group] of Object.entries(groups)) {
69
+ const student = allStudents.find((s) => s.zID === zID);
70
+ if (student === void 0) {
71
+ this.logger?.error(`Could not find student with zID: ${zID}`);
72
+ continue;
73
+ }
74
+ output[zID] = { ...student, group };
75
+ }
76
+ return output;
77
+ };
78
+ /**
79
+ * Gets all the groups for a given assignment
80
+ * @param assignment given assignment to get groups for
81
+ */
82
+ getAllGroups = (assignment) => {
83
+ return Array.from(new Set(Object.values(this.groups.get(assignment) ?? {}))).filter((x) => x !== null);
84
+ };
85
+ /**
86
+ * Gets a map of group names to students for a given assignment
87
+ * @param assignment given assignment to get groups for
88
+ * @param allStudents array of all students
89
+ */
90
+ getByGroupNames = (assignment, allStudents) => {
91
+ const groups = this.getGroups(assignment);
92
+ const stuff = {};
93
+ for (const [zID, group] of Object.entries(groups)) {
94
+ const student = allStudents.find((s) => s.zID === zID);
95
+ if (group === null || student === void 0) {
96
+ continue;
97
+ }
98
+ if (stuff[group] === void 0) {
99
+ stuff[group] = [{ ...student }];
100
+ } else {
101
+ stuff[group].push({ ...student });
102
+ }
103
+ }
104
+ return stuff;
105
+ };
106
+ };
107
+
108
+ exports.Groups = Groups;
109
+ //# sourceMappingURL=index.cjs.map
110
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts","../src/Groups.ts"],"names":["path","existsSync","readFileSync","parseError"],"mappings":";;;;;;;;;;;;AAQO,IAAM,YAAA,GAAe,CAACA,KAAAA,EAAc,QAAA,GAA2B,OAAA,KAAoB;AACxF,EAAA,IAAIC,aAAA,CAAWD,KAAI,CAAA,EAAG;AACpB,IAAA,OAAOE,eAAA,CAAaF,OAAM,QAAQ,CAAA;AAAA,EACpC;AACA,EAAA,MAAM,IAAI,KAAA,CAAM,CAAA,6CAAA,EAAgDA,KAAI,CAAA,CAAE,CAAA;AACxE,CAAA;;;ACKO,IAAM,SAAN,MAA+B;AAAA,EACnB,UAAA;AAAA,EACA,MAAA,uBAAa,GAAA,EAAwB;AAAA,EACrC,MAAA;AAAA,EACA,QAAA;AAAA,EAEV,WAAA,CAAY,YAAgC,OAAA,EAAwB;AACzE,IAAA,IAAA,CAAK,UAAA,GAAa,CAAC,GAAG,UAAU,CAAA;AAChC,IAAA,IAAA,CAAK,SAAS,OAAA,EAAS,MAAA;AACvB,IAAA,IAAA,CAAK,WAAW,OAAA,CAAQ,QAAA;AAExB,IAAA,KAAA,MAAW,SAAA,IAAa,KAAK,UAAA,EAAY;AACvC,MAAA,MAAM,WAAWA,qBAAA,CAAK,IAAA,CAAK,IAAA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,CAAA,KAAM,MAAM,IAAI,CAAA;AAAA,EACvG,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOO,eAAA,GAAkB,CAAC,UAAA,EAAe,WAAA,KAAuD;AAC9F,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,SAAA,CAAU,UAAU,CAAA;AACxC,IAAA,MAAM,QAA2B,EAAC;AAClC,IAAA,KAAA,MAAW,CAAC,GAAA,EAAK,KAAK,KAAK,MAAA,CAAO,OAAA,CAAQ,MAAM,CAAA,EAAG;AACjD,MAAA,MAAM,UAAU,WAAA,CAAY,IAAA,CAAK,CAAC,CAAA,KAAM,CAAA,CAAE,QAAQ,GAAG,CAAA;AAErD,MAAA,IAAI,KAAA,KAAU,IAAA,IAAQ,OAAA,KAAY,MAAA,EAAW;AAC3C,QAAA;AAAA,MACF;AACA,MAAA,IAAI,KAAA,CAAM,KAAK,CAAA,KAAM,MAAA,EAAW;AAC9B,QAAA,KAAA,CAAM,KAAK,CAAA,GAAI,CAAC,EAAE,GAAG,SAAS,CAAA;AAAA,MAChC,CAAA,MAAO;AACL,QAAA,KAAA,CAAM,KAAK,CAAA,CAAE,IAAA,CAAK,EAAE,GAAG,SAAS,CAAA;AAAA,MAClC;AAAA,IACF;AACA,IAAA,OAAO,KAAA;AAAA,EACT,CAAA;AACF","file":"index.cjs","sourcesContent":["import { existsSync, readFileSync } from 'fs';\n\n/**\n * Reads input from a file and returns a string\n * @throws { Error } if the file does not exist\n * @param path path of file\n * @param encoding encoding, defaulted to \"utf-8\"\n */\nexport const readFileFrom = (path: string, encoding: BufferEncoding = 'utf-8'): string => {\n if (existsSync(path)) {\n return readFileSync(path, encoding);\n }\n throw new Error(`Failed to read file from expected directory: ${path}`);\n};\n","import { parseError } from '@kuroson/common-utils';\nimport type { StudentEnrolment } from '@kuroson/cse-students';\nimport path from 'path';\nimport { type Logger } from 'winston';\nimport type { GroupByStudentMap, GroupFileInfo, StudentGroupMap, StudentGroupMapFull } from './types';\nimport { readFileFrom } from './utils';\n\ntype GroupsOptions = {\n /**\n * Optional logger for verbose and error logging\n */\n logger?: Logger;\n /**\n * Base path for reading group files.\n */\n basePath: string;\n};\n\nexport class Groups<T extends string> {\n private readonly groupFiles: GroupFileInfo<T>[];\n private readonly groups = new Map<T, StudentGroupMap>();\n private readonly logger: Logger | undefined;\n private readonly basePath: string;\n\n public constructor(groupFiles: GroupFileInfo<T>[], options: GroupsOptions) {\n this.groupFiles = [...groupFiles];\n this.logger = options?.logger;\n this.basePath = options.basePath;\n\n for (const groupFile of this.groupFiles) {\n const readPath = path.join(this.basePath, groupFile.fileName);\n\n this.logger?.verbose(`Reading from ${readPath} for '${groupFile.assignment}'`);\n try {\n const input = readFileFrom(readPath, 'utf-8');\n const lines = input\n .split('\\n')\n .slice(1)\n .filter((x) => x.length !== 0);\n const groups: StudentGroupMap = {};\n for (const line of lines) {\n const [id, group] = line.split(',') as [string, string];\n groups[id.startsWith('z') ? id : `z${id}`] = group === 'null' || group.length === 0 ? null : group;\n }\n this.groups.set(groupFile.assignment, { ...groups });\n this.logger?.verbose(`Read ${Object.keys(groups).length} groups for '${groupFile.assignment}'`);\n } catch (err: unknown) {\n this.logger?.error(`Failed to read from ${groupFile.fileName}`);\n this.logger?.error(parseError(err));\n }\n }\n }\n\n /**\n * Gets a map of zIDs to group names for a given assignment\n * @param assignment given assignment to get groups for\n */\n public getGroups = (assignment: T | (string & {})): StudentGroupMap => {\n if (this.groups.has(assignment as T)) {\n return { ...this.groups.get(assignment as T) };\n }\n return {};\n };\n\n /**\n * Gets a map of zIDs to group names, and student names for a given assignment.\n * If a student is not found, they are not included in the output.\n * @param assignment given assignment to get groups for\n * @param allStudents array of all students\n */\n public getGroupsFullInfo = (assignment: T, allStudents: StudentEnrolment[]): StudentGroupMapFull => {\n const groups = this.getGroups(assignment);\n const output: StudentGroupMapFull = {};\n for (const [zID, group] of Object.entries(groups)) {\n const student = allStudents.find((s) => s.zID === zID);\n if (student === undefined) {\n this.logger?.error(`Could not find student with zID: ${zID}`);\n continue;\n }\n output[zID] = { ...student, group: group };\n }\n return output;\n };\n\n /**\n * Gets all the groups for a given assignment\n * @param assignment given assignment to get groups for\n */\n public getAllGroups = (assignment: T) => {\n return Array.from(new Set(Object.values(this.groups.get(assignment) ?? {}))).filter((x) => x !== null);\n };\n\n /**\n * Gets a map of group names to students for a given assignment\n * @param assignment given assignment to get groups for\n * @param allStudents array of all students\n */\n public getByGroupNames = (assignment: T, allStudents: StudentEnrolment[]): GroupByStudentMap => {\n const groups = this.getGroups(assignment);\n const stuff: GroupByStudentMap = {};\n for (const [zID, group] of Object.entries(groups)) {\n const student = allStudents.find((s) => s.zID === zID);\n\n if (group === null || student === undefined) {\n continue;\n }\n if (stuff[group] === undefined) {\n stuff[group] = [{ ...student }];\n } else {\n stuff[group].push({ ...student });\n }\n }\n return stuff;\n };\n}\n"]}
@@ -0,0 +1,73 @@
1
+ import { StudentEnrolment } from '@kuroson/cse-students';
2
+ import { Logger } from 'winston';
3
+
4
+ /**
5
+ * Information about a group file
6
+ */
7
+ type GroupFileInfo<T extends string> = {
8
+ /**
9
+ * Path relative to the base path (or absolute path)
10
+ */
11
+ fileName: string;
12
+ /**
13
+ * Assignment identifier
14
+ */
15
+ assignment: T;
16
+ };
17
+ type StudentGroupMap = {
18
+ [zID: string]: string | null;
19
+ };
20
+ type GroupByStudentMap = {
21
+ [group: string]: StudentEnrolment[];
22
+ };
23
+ type StudentGroupMapFull = {
24
+ [zID: string]: StudentEnrolment & {
25
+ /**
26
+ * Name of their Group for a specific assignment
27
+ */
28
+ group: string | null;
29
+ };
30
+ };
31
+
32
+ type GroupsOptions = {
33
+ /**
34
+ * Optional logger for verbose and error logging
35
+ */
36
+ logger?: Logger;
37
+ /**
38
+ * Base path for reading group files.
39
+ */
40
+ basePath: string;
41
+ };
42
+ declare class Groups<T extends string> {
43
+ private readonly groupFiles;
44
+ private readonly groups;
45
+ private readonly logger;
46
+ private readonly basePath;
47
+ constructor(groupFiles: GroupFileInfo<T>[], options: GroupsOptions);
48
+ /**
49
+ * Gets a map of zIDs to group names for a given assignment
50
+ * @param assignment given assignment to get groups for
51
+ */
52
+ getGroups: (assignment: T | (string & {})) => StudentGroupMap;
53
+ /**
54
+ * Gets a map of zIDs to group names, and student names for a given assignment.
55
+ * If a student is not found, they are not included in the output.
56
+ * @param assignment given assignment to get groups for
57
+ * @param allStudents array of all students
58
+ */
59
+ getGroupsFullInfo: (assignment: T, allStudents: StudentEnrolment[]) => StudentGroupMapFull;
60
+ /**
61
+ * Gets all the groups for a given assignment
62
+ * @param assignment given assignment to get groups for
63
+ */
64
+ getAllGroups: (assignment: T) => string[];
65
+ /**
66
+ * Gets a map of group names to students for a given assignment
67
+ * @param assignment given assignment to get groups for
68
+ * @param allStudents array of all students
69
+ */
70
+ getByGroupNames: (assignment: T, allStudents: StudentEnrolment[]) => GroupByStudentMap;
71
+ }
72
+
73
+ export { type GroupByStudentMap, type GroupFileInfo, Groups, type StudentGroupMap, type StudentGroupMapFull };
@@ -0,0 +1,73 @@
1
+ import { StudentEnrolment } from '@kuroson/cse-students';
2
+ import { Logger } from 'winston';
3
+
4
+ /**
5
+ * Information about a group file
6
+ */
7
+ type GroupFileInfo<T extends string> = {
8
+ /**
9
+ * Path relative to the base path (or absolute path)
10
+ */
11
+ fileName: string;
12
+ /**
13
+ * Assignment identifier
14
+ */
15
+ assignment: T;
16
+ };
17
+ type StudentGroupMap = {
18
+ [zID: string]: string | null;
19
+ };
20
+ type GroupByStudentMap = {
21
+ [group: string]: StudentEnrolment[];
22
+ };
23
+ type StudentGroupMapFull = {
24
+ [zID: string]: StudentEnrolment & {
25
+ /**
26
+ * Name of their Group for a specific assignment
27
+ */
28
+ group: string | null;
29
+ };
30
+ };
31
+
32
+ type GroupsOptions = {
33
+ /**
34
+ * Optional logger for verbose and error logging
35
+ */
36
+ logger?: Logger;
37
+ /**
38
+ * Base path for reading group files.
39
+ */
40
+ basePath: string;
41
+ };
42
+ declare class Groups<T extends string> {
43
+ private readonly groupFiles;
44
+ private readonly groups;
45
+ private readonly logger;
46
+ private readonly basePath;
47
+ constructor(groupFiles: GroupFileInfo<T>[], options: GroupsOptions);
48
+ /**
49
+ * Gets a map of zIDs to group names for a given assignment
50
+ * @param assignment given assignment to get groups for
51
+ */
52
+ getGroups: (assignment: T | (string & {})) => StudentGroupMap;
53
+ /**
54
+ * Gets a map of zIDs to group names, and student names for a given assignment.
55
+ * If a student is not found, they are not included in the output.
56
+ * @param assignment given assignment to get groups for
57
+ * @param allStudents array of all students
58
+ */
59
+ getGroupsFullInfo: (assignment: T, allStudents: StudentEnrolment[]) => StudentGroupMapFull;
60
+ /**
61
+ * Gets all the groups for a given assignment
62
+ * @param assignment given assignment to get groups for
63
+ */
64
+ getAllGroups: (assignment: T) => string[];
65
+ /**
66
+ * Gets a map of group names to students for a given assignment
67
+ * @param assignment given assignment to get groups for
68
+ * @param allStudents array of all students
69
+ */
70
+ getByGroupNames: (assignment: T, allStudents: StudentEnrolment[]) => GroupByStudentMap;
71
+ }
72
+
73
+ export { type GroupByStudentMap, type GroupFileInfo, Groups, type StudentGroupMap, type StudentGroupMapFull };
package/dist/index.js ADDED
@@ -0,0 +1,104 @@
1
+ import { parseError } from '@kuroson/common-utils';
2
+ import path from 'path';
3
+ import 'winston';
4
+ import { existsSync, readFileSync } from 'fs';
5
+
6
+ // src/Groups.ts
7
+ var readFileFrom = (path2, encoding = "utf-8") => {
8
+ if (existsSync(path2)) {
9
+ return readFileSync(path2, encoding);
10
+ }
11
+ throw new Error(`Failed to read file from expected directory: ${path2}`);
12
+ };
13
+
14
+ // src/Groups.ts
15
+ var Groups = class {
16
+ groupFiles;
17
+ groups = /* @__PURE__ */ new Map();
18
+ logger;
19
+ basePath;
20
+ constructor(groupFiles, options) {
21
+ this.groupFiles = [...groupFiles];
22
+ this.logger = options?.logger;
23
+ this.basePath = options.basePath;
24
+ for (const groupFile of this.groupFiles) {
25
+ const readPath = path.join(this.basePath, groupFile.fileName);
26
+ this.logger?.verbose(`Reading from ${readPath} for '${groupFile.assignment}'`);
27
+ try {
28
+ const input = readFileFrom(readPath, "utf-8");
29
+ const lines = input.split("\n").slice(1).filter((x) => x.length !== 0);
30
+ const groups = {};
31
+ for (const line of lines) {
32
+ const [id, group] = line.split(",");
33
+ groups[id.startsWith("z") ? id : `z${id}`] = group === "null" || group.length === 0 ? null : group;
34
+ }
35
+ this.groups.set(groupFile.assignment, { ...groups });
36
+ this.logger?.verbose(`Read ${Object.keys(groups).length} groups for '${groupFile.assignment}'`);
37
+ } catch (err) {
38
+ this.logger?.error(`Failed to read from ${groupFile.fileName}`);
39
+ this.logger?.error(parseError(err));
40
+ }
41
+ }
42
+ }
43
+ /**
44
+ * Gets a map of zIDs to group names for a given assignment
45
+ * @param assignment given assignment to get groups for
46
+ */
47
+ getGroups = (assignment) => {
48
+ if (this.groups.has(assignment)) {
49
+ return { ...this.groups.get(assignment) };
50
+ }
51
+ return {};
52
+ };
53
+ /**
54
+ * Gets a map of zIDs to group names, and student names for a given assignment.
55
+ * If a student is not found, they are not included in the output.
56
+ * @param assignment given assignment to get groups for
57
+ * @param allStudents array of all students
58
+ */
59
+ getGroupsFullInfo = (assignment, allStudents) => {
60
+ const groups = this.getGroups(assignment);
61
+ const output = {};
62
+ for (const [zID, group] of Object.entries(groups)) {
63
+ const student = allStudents.find((s) => s.zID === zID);
64
+ if (student === void 0) {
65
+ this.logger?.error(`Could not find student with zID: ${zID}`);
66
+ continue;
67
+ }
68
+ output[zID] = { ...student, group };
69
+ }
70
+ return output;
71
+ };
72
+ /**
73
+ * Gets all the groups for a given assignment
74
+ * @param assignment given assignment to get groups for
75
+ */
76
+ getAllGroups = (assignment) => {
77
+ return Array.from(new Set(Object.values(this.groups.get(assignment) ?? {}))).filter((x) => x !== null);
78
+ };
79
+ /**
80
+ * Gets a map of group names to students for a given assignment
81
+ * @param assignment given assignment to get groups for
82
+ * @param allStudents array of all students
83
+ */
84
+ getByGroupNames = (assignment, allStudents) => {
85
+ const groups = this.getGroups(assignment);
86
+ const stuff = {};
87
+ for (const [zID, group] of Object.entries(groups)) {
88
+ const student = allStudents.find((s) => s.zID === zID);
89
+ if (group === null || student === void 0) {
90
+ continue;
91
+ }
92
+ if (stuff[group] === void 0) {
93
+ stuff[group] = [{ ...student }];
94
+ } else {
95
+ stuff[group].push({ ...student });
96
+ }
97
+ }
98
+ return stuff;
99
+ };
100
+ };
101
+
102
+ export { Groups };
103
+ //# sourceMappingURL=index.js.map
104
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts","../src/Groups.ts"],"names":["path"],"mappings":";;;;;;AAQO,IAAM,YAAA,GAAe,CAACA,KAAAA,EAAc,QAAA,GAA2B,OAAA,KAAoB;AACxF,EAAA,IAAI,UAAA,CAAWA,KAAI,CAAA,EAAG;AACpB,IAAA,OAAO,YAAA,CAAaA,OAAM,QAAQ,CAAA;AAAA,EACpC;AACA,EAAA,MAAM,IAAI,KAAA,CAAM,CAAA,6CAAA,EAAgDA,KAAI,CAAA,CAAE,CAAA;AACxE,CAAA;;;ACKO,IAAM,SAAN,MAA+B;AAAA,EACnB,UAAA;AAAA,EACA,MAAA,uBAAa,GAAA,EAAwB;AAAA,EACrC,MAAA;AAAA,EACA,QAAA;AAAA,EAEV,WAAA,CAAY,YAAgC,OAAA,EAAwB;AACzE,IAAA,IAAA,CAAK,UAAA,GAAa,CAAC,GAAG,UAAU,CAAA;AAChC,IAAA,IAAA,CAAK,SAAS,OAAA,EAAS,MAAA;AACvB,IAAA,IAAA,CAAK,WAAW,OAAA,CAAQ,QAAA;AAExB,IAAA,KAAA,MAAW,SAAA,IAAa,KAAK,UAAA,EAAY;AACvC,MAAA,MAAM,WAAW,IAAA,CAAK,IAAA,CAAK,IAAA,CAAK,QAAA,EAAU,UAAU,QAAQ,CAAA;AAE5D,MAAA,IAAA,CAAK,QAAQ,OAAA,CAAQ,CAAA,aAAA,EAAgB,QAAQ,CAAA,MAAA,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AA,KAAuD;AAC9F,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,SAAA,CAAU,UAAU,CAAA;AACxC,IAAA,MAAM,QAA2B,EAAC;AAClC,IAAA,KAAA,MAAW,CAAC,GAAA,EAAK,KAAK,KAAK,MAAA,CAAO,OAAA,CAAQ,MAAM,CAAA,EAAG;AACjD,MAAA,MAAM,UAAU,WAAA,CAAY,IAAA,CAAK,CAAC,CAAA,KAAM,CAAA,CAAE,QAAQ,GAAG,CAAA;AAErD,MAAA,IAAI,KAAA,KAAU,IAAA,IAAQ,OAAA,KAAY,MAAA,EAAW;AAC3C,QAAA;AAAA,MACF;AACA,MAAA,IAAI,KAAA,CAAM,KAAK,CAAA,KAAM,MAAA,EAAW;AAC9B,QAAA,KAAA,CAAM,KAAK,CAAA,GAAI,CAAC,EAAE,GAAG,SAAS,CAAA;AAAA,MAChC,CAAA,MAAO;AACL,QAAA,KAAA,CAAM,KAAK,CAAA,CAAE,IAAA,CAAK,EAAE,GAAG,SAAS,CAAA;AAAA,MAClC;AAAA,IACF;AACA,IAAA,OAAO,KAAA;AAAA,EACT,CAAA;AACF","file":"index.js","sourcesContent":["import { existsSync, readFileSync } from 'fs';\n\n/**\n * Reads input from a file and returns a string\n * @throws { Error } if the file does not exist\n * @param path path of file\n * @param encoding encoding, defaulted to \"utf-8\"\n */\nexport const readFileFrom = (path: string, encoding: BufferEncoding = 'utf-8'): string => {\n if (existsSync(path)) {\n return readFileSync(path, encoding);\n }\n throw new Error(`Failed to read file from expected directory: ${path}`);\n};\n","import { parseError } from '@kuroson/common-utils';\nimport type { StudentEnrolment } from '@kuroson/cse-students';\nimport path from 'path';\nimport { type Logger } from 'winston';\nimport type { GroupByStudentMap, GroupFileInfo, StudentGroupMap, StudentGroupMapFull } from './types';\nimport { readFileFrom } from './utils';\n\ntype GroupsOptions = {\n /**\n * Optional logger for verbose and error logging\n */\n logger?: Logger;\n /**\n * Base path for reading group files.\n */\n basePath: string;\n};\n\nexport class Groups<T extends string> {\n private readonly groupFiles: GroupFileInfo<T>[];\n private readonly groups = new Map<T, StudentGroupMap>();\n private readonly logger: Logger | undefined;\n private readonly basePath: string;\n\n public constructor(groupFiles: GroupFileInfo<T>[], options: GroupsOptions) {\n this.groupFiles = [...groupFiles];\n this.logger = options?.logger;\n this.basePath = options.basePath;\n\n for (const groupFile of this.groupFiles) {\n const readPath = path.join(this.basePath, groupFile.fileName);\n\n this.logger?.verbose(`Reading from ${readPath} for '${groupFile.assignment}'`);\n try {\n const input = readFileFrom(readPath, 'utf-8');\n const lines = input\n .split('\\n')\n .slice(1)\n .filter((x) => x.length !== 0);\n const groups: StudentGroupMap = {};\n for (const line of lines) {\n const [id, group] = line.split(',') as [string, string];\n groups[id.startsWith('z') ? id : `z${id}`] = group === 'null' || group.length === 0 ? null : group;\n }\n this.groups.set(groupFile.assignment, { ...groups });\n this.logger?.verbose(`Read ${Object.keys(groups).length} groups for '${groupFile.assignment}'`);\n } catch (err: unknown) {\n this.logger?.error(`Failed to read from ${groupFile.fileName}`);\n this.logger?.error(parseError(err));\n }\n }\n }\n\n /**\n * Gets a map of zIDs to group names for a given assignment\n * @param assignment given assignment to get groups for\n */\n public getGroups = (assignment: T | (string & {})): StudentGroupMap => {\n if (this.groups.has(assignment as T)) {\n return { ...this.groups.get(assignment as T) };\n }\n return {};\n };\n\n /**\n * Gets a map of zIDs to group names, and student names for a given assignment.\n * If a student is not found, they are not included in the output.\n * @param assignment given assignment to get groups for\n * @param allStudents array of all students\n */\n public getGroupsFullInfo = (assignment: T, allStudents: StudentEnrolment[]): StudentGroupMapFull => {\n const groups = this.getGroups(assignment);\n const output: StudentGroupMapFull = {};\n for (const [zID, group] of Object.entries(groups)) {\n const student = allStudents.find((s) => s.zID === zID);\n if (student === undefined) {\n this.logger?.error(`Could not find student with zID: ${zID}`);\n continue;\n }\n output[zID] = { ...student, group: group };\n }\n return output;\n };\n\n /**\n * Gets all the groups for a given assignment\n * @param assignment given assignment to get groups for\n */\n public getAllGroups = (assignment: T) => {\n return Array.from(new Set(Object.values(this.groups.get(assignment) ?? {}))).filter((x) => x !== null);\n };\n\n /**\n * Gets a map of group names to students for a given assignment\n * @param assignment given assignment to get groups for\n * @param allStudents array of all students\n */\n public getByGroupNames = (assignment: T, allStudents: StudentEnrolment[]): GroupByStudentMap => {\n const groups = this.getGroups(assignment);\n const stuff: GroupByStudentMap = {};\n for (const [zID, group] of Object.entries(groups)) {\n const student = allStudents.find((s) => s.zID === zID);\n\n if (group === null || student === undefined) {\n continue;\n }\n if (stuff[group] === undefined) {\n stuff[group] = [{ ...student }];\n } else {\n stuff[group].push({ ...student });\n }\n }\n return stuff;\n };\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "@kuroson/cse-groups",
3
+ "version": "0.2.1",
4
+ "description": "Group allocation parser for UNSW CSE courses",
5
+ "license": "MIT",
6
+ "author": "Alvin Cherk <a.cherk@unsw.edu.au>",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.ts",
12
+ "default": "./dist/index.js"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.cts",
16
+ "default": "./dist/index.cjs"
17
+ }
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "main": "./dist/index.cjs",
22
+ "module": "./dist/index.js",
23
+ "types": "./dist/index.d.ts",
24
+ "files": [
25
+ "dist",
26
+ "package.json",
27
+ "README.md",
28
+ "LICENSE",
29
+ "CHANGELOG.md"
30
+ ],
31
+ "dependencies": {
32
+ "@kuroson/common-utils": "0.2.1",
33
+ "@kuroson/cse-students": "0.3.1"
34
+ },
35
+ "devDependencies": {
36
+ "@eslint/js": "9.39.2",
37
+ "@types/jest": "30.0.0",
38
+ "@types/node": "24.10.4",
39
+ "@typescript-eslint/eslint-plugin": "8.50.1",
40
+ "depcheck": "1.4.7",
41
+ "eslint": "9.39.2",
42
+ "eslint-config-prettier": "10.1.8",
43
+ "eslint-plugin-jest": "29.10.1",
44
+ "eslint-plugin-prettier": "5.5.4",
45
+ "eslint-plugin-unused-imports": "4.3.0",
46
+ "jest": "30.2.0",
47
+ "prettier": "3.7.4",
48
+ "ts-jest": "29.4.6",
49
+ "tsup": "8.5.1",
50
+ "typescript": "5.9.3",
51
+ "typescript-eslint": "8.50.1",
52
+ "winston": "3.19.0"
53
+ },
54
+ "peerDependencies": {
55
+ "typescript": ">=5.0.0",
56
+ "winston": ">=3.0.0"
57
+ },
58
+ "peerDependenciesMeta": {
59
+ "typescript": {
60
+ "optional": true
61
+ },
62
+ "winston": {
63
+ "optional": true
64
+ }
65
+ },
66
+ "engines": {
67
+ "node": ">=22"
68
+ },
69
+ "publishConfig": {
70
+ "access": "public"
71
+ },
72
+ "depcheck": {
73
+ "ignores": [
74
+ "ts-jest"
75
+ ],
76
+ "skip-missing": true
77
+ },
78
+ "scripts": {
79
+ "build:all": "tsup",
80
+ "depcheck": "depcheck",
81
+ "lint": "eslint './**/*.{ts,js}'",
82
+ "lint:fix": "eslint './**/*.{ts,js}' --fix",
83
+ "test": "jest",
84
+ "test:cov": "jest --coverage",
85
+ "typecheck": "tsc --noEmit"
86
+ }
87
+ }