scorm-again 1.7.0 → 2.0.0

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 (85) hide show
  1. package/.babelrc +18 -7
  2. package/.github/dependabot.yml +5 -0
  3. package/.github/workflows/main.yml +79 -0
  4. package/.jsdoc.json +4 -5
  5. package/.mocharc.json +8 -0
  6. package/.run/Mocha Unit Tests.run.xml +5 -2
  7. package/CONTRIBUTING.md +1 -1
  8. package/README.md +14 -1
  9. package/dist/aicc.js +3661 -7170
  10. package/dist/aicc.js.map +1 -1
  11. package/dist/aicc.min.js +2 -40
  12. package/dist/aicc.min.js.map +1 -0
  13. package/dist/scorm-again.js +5671 -10695
  14. package/dist/scorm-again.js.map +1 -1
  15. package/dist/scorm-again.min.js +2 -52
  16. package/dist/scorm-again.min.js.map +1 -0
  17. package/dist/scorm12.js +2871 -5433
  18. package/dist/scorm12.js.map +1 -1
  19. package/dist/scorm12.min.js +2 -34
  20. package/dist/scorm12.min.js.map +1 -0
  21. package/dist/scorm2004.js +3868 -6797
  22. package/dist/scorm2004.js.map +1 -1
  23. package/dist/scorm2004.min.js +2 -40
  24. package/dist/scorm2004.min.js.map +1 -0
  25. package/eslint.config.js +21 -0
  26. package/package.json +73 -35
  27. package/results.json +34254 -0
  28. package/src/{AICC.js → AICC.ts} +27 -21
  29. package/src/BaseAPI.ts +1449 -0
  30. package/src/Scorm12API.ts +360 -0
  31. package/src/{Scorm2004API.js → Scorm2004API.ts} +245 -163
  32. package/src/cmi/aicc_cmi.ts +1248 -0
  33. package/src/cmi/common.ts +411 -0
  34. package/src/cmi/scorm12_cmi.ts +1426 -0
  35. package/src/cmi/scorm2004_cmi.ts +1874 -0
  36. package/src/constants/api_constants.ts +318 -0
  37. package/src/constants/error_codes.ts +88 -0
  38. package/src/constants/language_constants.ts +394 -0
  39. package/src/constants/regex.ts +97 -0
  40. package/src/constants/{response_constants.js → response_constants.ts} +67 -62
  41. package/src/exceptions.ts +133 -0
  42. package/src/exports/aicc.js +1 -1
  43. package/src/exports/scorm-again.js +3 -3
  44. package/src/exports/scorm12.js +1 -1
  45. package/src/exports/scorm2004.js +1 -1
  46. package/src/{utilities.js → utilities.ts} +114 -74
  47. package/tea.yaml +6 -0
  48. package/test/{AICC.spec.js → AICC.spec.ts} +70 -72
  49. package/test/Scorm12API.spec.ts +580 -0
  50. package/test/Scorm2004API.spec.ts +812 -0
  51. package/test/api_helpers.ts +176 -0
  52. package/test/cmi/{aicc_cmi.spec.js → aicc_cmi.spec.ts} +193 -209
  53. package/test/cmi/{scorm12_cmi.spec.js → scorm12_cmi.spec.ts} +251 -269
  54. package/test/cmi/scorm2004_cmi.spec.ts +1031 -0
  55. package/test/cmi_helpers.ts +207 -0
  56. package/test/exceptions.spec.ts +79 -0
  57. package/test/field_values.ts +202 -0
  58. package/test/utilities.spec.ts +322 -0
  59. package/tsconfig.json +18 -0
  60. package/webpack.config.js +65 -0
  61. package/.circleci/config.yml +0 -99
  62. package/.codeclimate.yml +0 -7
  63. package/.eslintrc.js +0 -36
  64. package/src/.flowconfig +0 -11
  65. package/src/BaseAPI.js +0 -1275
  66. package/src/Scorm12API.js +0 -308
  67. package/src/cmi/aicc_cmi.js +0 -1141
  68. package/src/cmi/common.js +0 -328
  69. package/src/cmi/scorm12_cmi.js +0 -1312
  70. package/src/cmi/scorm2004_cmi.js +0 -1692
  71. package/src/constants/api_constants.js +0 -218
  72. package/src/constants/error_codes.js +0 -87
  73. package/src/constants/language_constants.js +0 -76
  74. package/src/constants/regex.js +0 -84
  75. package/src/exceptions.js +0 -104
  76. package/test/Scorm12API.spec.js +0 -528
  77. package/test/Scorm2004API.spec.js +0 -775
  78. package/test/abstract_classes.spec.js +0 -17
  79. package/test/api_helpers.js +0 -128
  80. package/test/cmi/scorm2004_cmi.spec.js +0 -1066
  81. package/test/cmi_helpers.js +0 -161
  82. package/test/exceptions.spec.js +0 -71
  83. package/test/field_values.js +0 -353
  84. package/test/utilities.spec.js +0 -339
  85. package/webpack.js +0 -78
@@ -0,0 +1,411 @@
1
+ import APIConstants from "../constants/api_constants";
2
+ import ErrorCodes from "../constants/error_codes";
3
+ import Regex from "../constants/regex";
4
+
5
+ const scorm12_constants = APIConstants.scorm12;
6
+ const scorm12_regex = Regex.scorm12;
7
+ const scorm12_error_codes = ErrorCodes.scorm12;
8
+
9
+ export class BaseScormValidationError extends Error {
10
+ constructor(errorCode: number) {
11
+ super(errorCode.toString());
12
+ this._errorCode = errorCode;
13
+ this.name = "ScormValidationError";
14
+ }
15
+
16
+ private readonly _errorCode: number;
17
+
18
+ /**
19
+ * Getter for _errorCode
20
+ * @return {number}
21
+ */
22
+ get errorCode(): number {
23
+ return this._errorCode;
24
+ }
25
+
26
+ setMessage(message: string) {
27
+ this.message = message;
28
+ }
29
+ }
30
+
31
+ export class BaseScorm12ValidationError extends BaseScormValidationError {
32
+ constructor(errorCode: number) {
33
+ super(errorCode);
34
+ this.name = "Scorm12ValidationError";
35
+ }
36
+ }
37
+
38
+ export class BaseScorm2004ValidationError extends BaseScormValidationError {
39
+ constructor(errorCode: number) {
40
+ super(errorCode);
41
+ this.name = "Scorm2004ValidationError";
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Check if the value matches the proper format. If not, throw proper error code.
47
+ *
48
+ * @param {string} value
49
+ * @param {string} regexPattern
50
+ * @param {number} errorCode
51
+ * @param {typeof BaseScormValidationError} errorClass
52
+ * @param {boolean} [allowEmptyString]
53
+ * @return {boolean}
54
+ */
55
+ export function checkValidFormat(
56
+ value: string,
57
+ regexPattern: string,
58
+ errorCode: number,
59
+ errorClass: typeof BaseScormValidationError,
60
+ allowEmptyString?: boolean,
61
+ ): boolean {
62
+ const formatRegex = new RegExp(regexPattern);
63
+ const matches = value.match(formatRegex);
64
+ if (allowEmptyString && value === "") {
65
+ return true;
66
+ }
67
+ if (value === undefined || !matches || matches[0] === "") {
68
+ throw new errorClass(errorCode);
69
+ }
70
+ return true;
71
+ }
72
+
73
+ /**
74
+ * Check if the value matches the proper range. If not, throw proper error code.
75
+ *
76
+ * @param {any} value
77
+ * @param {string} rangePattern
78
+ * @param {number} errorCode
79
+ * @param {typeof BaseScormValidationError} errorClass
80
+ * @return {boolean}
81
+ */
82
+ export function checkValidRange(
83
+ value: any,
84
+ rangePattern: string,
85
+ errorCode: number,
86
+ errorClass: typeof BaseScormValidationError,
87
+ ): boolean {
88
+ const ranges = rangePattern.split("#");
89
+ value = value * 1.0;
90
+ if (value >= ranges[0]) {
91
+ if (ranges[1] === "*" || value <= ranges[1]) {
92
+ return true;
93
+ } else {
94
+ throw new errorClass(errorCode);
95
+ }
96
+ } else {
97
+ throw new errorClass(errorCode);
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Base class for API cmi objects
103
+ */
104
+ export abstract class BaseCMI {
105
+ jsonString? = false;
106
+ private _initialized = false;
107
+ private _start_time: number | undefined;
108
+
109
+ /**
110
+ * Getter for _initialized
111
+ * @return {boolean}
112
+ */
113
+ get initialized(): boolean {
114
+ return this._initialized;
115
+ }
116
+
117
+ /**
118
+ * Getter for _start_time
119
+ * @return {number | undefined}
120
+ */
121
+ get start_time(): number | undefined {
122
+ return this._start_time;
123
+ }
124
+
125
+ /**
126
+ * Called when the API has been initialized after the CMI has been created
127
+ */
128
+ initialize(): void {
129
+ this._initialized = true;
130
+ }
131
+
132
+ /**
133
+ * Called when the player should override the 'session_time' provided by
134
+ * the module
135
+ */
136
+ setStartTime(): void {
137
+ this._start_time = new Date().getTime();
138
+ }
139
+ }
140
+
141
+ /**
142
+ * Base class for cmi root objects
143
+ */
144
+ export abstract class BaseRootCMI extends BaseCMI {
145
+ abstract getCurrentTotalTime(): string;
146
+ }
147
+
148
+ /**
149
+ * Base class for cmi *.score objects
150
+ */
151
+ export class CMIScore extends BaseCMI {
152
+ private __children: string;
153
+ private __score_range: string | false;
154
+ private __invalid_error_code: number;
155
+ private __invalid_type_code: number;
156
+ private __invalid_range_code: number;
157
+ private __decimal_regex: string;
158
+ private __error_class: typeof BaseScormValidationError;
159
+ private _raw = "";
160
+ private _min = "";
161
+ private _max: string;
162
+
163
+ /**
164
+ * Constructor for *.score
165
+ * @param {
166
+ * score_children: string,
167
+ * score_range: string,
168
+ * max: string,
169
+ * invalidErrorCode: number,
170
+ * invalidTypeCode: number,
171
+ * invalidRangeCode: number,
172
+ * decimalRegex: string,
173
+ * errorClass: ErrorClass
174
+ * } params
175
+ */
176
+ constructor(params: {
177
+ score_children?: string;
178
+ score_range?: string;
179
+ max?: string;
180
+ invalidErrorCode?: number;
181
+ invalidTypeCode?: number;
182
+ invalidRangeCode?: number;
183
+ decimalRegex?: string;
184
+ errorClass: typeof BaseScormValidationError;
185
+ }) {
186
+ super();
187
+
188
+ this.__children = params.score_children || scorm12_constants.score_children;
189
+ this.__score_range = !params.score_range
190
+ ? false
191
+ : scorm12_regex.score_range;
192
+ this._max = params.max || params.max === "" ? params.max : "100";
193
+ this.__invalid_error_code =
194
+ params.invalidErrorCode || scorm12_error_codes.INVALID_SET_VALUE;
195
+ this.__invalid_type_code =
196
+ params.invalidTypeCode || scorm12_error_codes.TYPE_MISMATCH;
197
+ this.__invalid_range_code =
198
+ params.invalidRangeCode || scorm12_error_codes.VALUE_OUT_OF_RANGE;
199
+ this.__decimal_regex = params.decimalRegex || scorm12_regex.CMIDecimal;
200
+ this.__error_class = params.errorClass;
201
+ }
202
+
203
+ /**
204
+ * Getter for _children
205
+ * @return {string}
206
+ */
207
+ get _children(): string {
208
+ return this.__children;
209
+ }
210
+
211
+ /**
212
+ * Setter for _children. Just throws an error.
213
+ * @param {string} _children
214
+ */
215
+ set _children(_children: string) {
216
+ throw new this.__error_class(this.__invalid_error_code);
217
+ }
218
+
219
+ /**
220
+ * Getter for _raw
221
+ * @return {string}
222
+ */
223
+ get raw(): string {
224
+ return this._raw;
225
+ }
226
+
227
+ /**
228
+ * Setter for _raw
229
+ * @param {string} raw
230
+ */
231
+ set raw(raw: string) {
232
+ if (
233
+ checkValidFormat(
234
+ raw,
235
+ this.__decimal_regex,
236
+ this.__invalid_type_code,
237
+ this.__error_class,
238
+ ) &&
239
+ (!this.__score_range ||
240
+ checkValidRange(
241
+ raw,
242
+ this.__score_range,
243
+ this.__invalid_range_code,
244
+ this.__error_class,
245
+ ))
246
+ ) {
247
+ this._raw = raw;
248
+ }
249
+ }
250
+
251
+ /**
252
+ * Getter for _min
253
+ * @return {string}
254
+ */
255
+ get min(): string {
256
+ return this._min;
257
+ }
258
+
259
+ /**
260
+ * Setter for _min
261
+ * @param {string} min
262
+ */
263
+ set min(min: string) {
264
+ if (
265
+ checkValidFormat(
266
+ min,
267
+ this.__decimal_regex,
268
+ this.__invalid_type_code,
269
+ this.__error_class,
270
+ ) &&
271
+ (!this.__score_range ||
272
+ checkValidRange(
273
+ min,
274
+ this.__score_range,
275
+ this.__invalid_range_code,
276
+ this.__error_class,
277
+ ))
278
+ ) {
279
+ this._min = min;
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Getter for _max
285
+ * @return {string}
286
+ */
287
+ get max(): string {
288
+ return this._max;
289
+ }
290
+
291
+ /**
292
+ * Setter for _max
293
+ * @param {string} max
294
+ */
295
+ set max(max: string) {
296
+ if (
297
+ checkValidFormat(
298
+ max,
299
+ this.__decimal_regex,
300
+ this.__invalid_type_code,
301
+ this.__error_class,
302
+ ) &&
303
+ (!this.__score_range ||
304
+ checkValidRange(
305
+ max,
306
+ this.__score_range,
307
+ this.__invalid_range_code,
308
+ this.__error_class,
309
+ ))
310
+ ) {
311
+ this._max = max;
312
+ }
313
+ }
314
+
315
+ /**
316
+ * toJSON for *.score
317
+ * @return {
318
+ * {
319
+ * min: string,
320
+ * max: string,
321
+ * raw: string
322
+ * }
323
+ * }
324
+ */
325
+ toJSON(): {
326
+ min: string;
327
+ max: string;
328
+ raw: string;
329
+ } {
330
+ this.jsonString = true;
331
+ const result = {
332
+ raw: this.raw,
333
+ min: this.min,
334
+ max: this.max,
335
+ };
336
+ delete this.jsonString;
337
+ return result;
338
+ }
339
+ }
340
+
341
+ /**
342
+ * Base class for cmi *.n objects
343
+ */
344
+ export class CMIArray extends BaseCMI {
345
+ private _errorCode: number;
346
+ private _errorClass: typeof BaseScormValidationError;
347
+ private __children: string;
348
+ childArray: any[];
349
+
350
+ /**
351
+ * Constructor cmi *.n arrays
352
+ * @param {object} params
353
+ */
354
+ constructor(params: {
355
+ children: string;
356
+ errorCode?: number;
357
+ errorClass?: typeof BaseScormValidationError;
358
+ }) {
359
+ super();
360
+ this.__children = params.children;
361
+ this._errorCode = params.errorCode || scorm12_error_codes.GENERAL;
362
+ this._errorClass = params.errorClass || BaseScorm12ValidationError;
363
+ this.childArray = [];
364
+ }
365
+
366
+ /**
367
+ * Getter for _children
368
+ * @return {string}
369
+ */
370
+ get _children(): string {
371
+ return this.__children;
372
+ }
373
+
374
+ /**
375
+ * Setter for _children. Just throws an error.
376
+ * @param {string} _children
377
+ */
378
+ set _children(_children: string) {
379
+ throw new this._errorClass(this._errorCode);
380
+ }
381
+
382
+ /**
383
+ * Getter for _count
384
+ * @return {number}
385
+ */
386
+ get _count(): number {
387
+ return this.childArray.length;
388
+ }
389
+
390
+ /**
391
+ * Setter for _count. Just throws an error.
392
+ * @param {number} _count
393
+ */
394
+ set _count(_count: number) {
395
+ throw new this._errorClass(this._errorCode);
396
+ }
397
+
398
+ /**
399
+ * toJSON for *.n arrays
400
+ * @return {object}
401
+ */
402
+ toJSON(): object {
403
+ this.jsonString = true;
404
+ const result: { [key: string]: any } = {};
405
+ for (let i = 0; i < this.childArray.length; i++) {
406
+ result[i + ""] = this.childArray[i];
407
+ }
408
+ delete this.jsonString;
409
+ return result;
410
+ }
411
+ }