@gjsify/assert 0.0.4 → 0.1.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/package.json CHANGED
@@ -1,43 +1,31 @@
1
1
  {
2
2
  "name": "@gjsify/assert",
3
- "version": "0.0.4",
3
+ "version": "0.1.1",
4
4
  "description": "Node.js assert module for Gjs",
5
5
  "type": "module",
6
- "main": "lib/cjs/index.js",
7
6
  "module": "lib/esm/index.js",
8
7
  "types": "lib/types/index.d.ts",
9
8
  "exports": {
10
9
  ".": {
11
- "import": {
12
- "types": "./lib/types/index.d.ts",
13
- "default": "./lib/esm/index.js"
14
- },
15
- "require": {
16
- "types": "./lib/types/index.d.ts",
17
- "default": "./lib/cjs/index.js"
18
- }
10
+ "types": "./lib/types/index.d.ts",
11
+ "require": "./cjs-compat.cjs",
12
+ "default": "./lib/esm/index.js"
19
13
  },
20
14
  "./strict": {
21
- "import": {
22
- "types": "./lib/types/strict/index.d.ts",
23
- "default": "./lib/esm/strict/index.js"
24
- },
25
- "require": {
26
- "types": "./lib/types/strict/index.d.ts",
27
- "default": "./lib/cjs/strict/index.js"
28
- }
15
+ "types": "./lib/types/strict/index.d.ts",
16
+ "default": "./lib/esm/strict/index.js"
29
17
  }
30
18
  },
31
19
  "scripts": {
32
- "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo || exit 0",
33
- "print:name": "echo '@gjsify/assert'",
34
- "build": "yarn print:name && yarn build:gjsify",
20
+ "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
21
+ "check": "tsc --noEmit",
22
+ "build": "yarn build:gjsify && yarn build:types",
35
23
  "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
36
- "build:types": "tsc --project tsconfig.types.json || exit 0",
24
+ "build:types": "tsc",
37
25
  "build:test": "yarn build:test:gjs && yarn build:test:node",
38
26
  "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
39
27
  "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
40
- "test": "yarn print:name && yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
28
+ "test": "yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
41
29
  "test:gjs": "gjs -m test.gjs.mjs",
42
30
  "test:node": "node test.node.mjs"
43
31
  },
@@ -47,12 +35,9 @@
47
35
  "assert"
48
36
  ],
49
37
  "devDependencies": {
50
- "@gjsify/cli": "^0.0.4",
51
- "@gjsify/unit": "^0.0.4",
52
- "@types/node": "^20.10.5",
53
- "typescript": "^5.3.3"
54
- },
55
- "dependencies": {
56
- "@gjsify/deno_std": "^0.0.4"
38
+ "@gjsify/cli": "^0.1.1",
39
+ "@gjsify/unit": "^0.1.1",
40
+ "@types/node": "^25.5.0",
41
+ "typescript": "^6.0.2"
57
42
  }
58
43
  }
@@ -0,0 +1,89 @@
1
+ // Reference: Node.js lib/internal/assert/assertion_error.js
2
+ // Reimplemented for GJS
3
+
4
+ import { safeInspect } from './inspect-fallback.js';
5
+
6
+ interface AssertionErrorOptions {
7
+ actual?: unknown;
8
+ expected?: unknown;
9
+ message?: string | Error;
10
+ operator?: string;
11
+ stackStartFn?: Function;
12
+ }
13
+
14
+ export class AssertionError extends Error {
15
+ actual: unknown;
16
+ expected: unknown;
17
+ operator: string;
18
+ code: string;
19
+ generatedMessage: boolean;
20
+
21
+ constructor(options: AssertionErrorOptions) {
22
+ const {
23
+ actual,
24
+ expected,
25
+ operator = 'fail',
26
+ stackStartFn,
27
+ } = options;
28
+
29
+ const isGenerated = options.message == null;
30
+ const message = isGenerated
31
+ ? generateMessage(actual, expected, operator)
32
+ : String(options.message);
33
+
34
+ // super() must be called before any `this` access (SpiderMonkey requirement)
35
+ super(message);
36
+
37
+ this.name = 'AssertionError';
38
+ this.code = 'ERR_ASSERTION';
39
+ this.actual = actual;
40
+ this.expected = expected;
41
+ this.operator = operator;
42
+ this.generatedMessage = isGenerated;
43
+
44
+ if (typeof Error.captureStackTrace === 'function') {
45
+ Error.captureStackTrace(this, stackStartFn || this.constructor);
46
+ }
47
+ }
48
+
49
+ toString(): string {
50
+ return `${this.name} [${this.code}]: ${this.message}`;
51
+ }
52
+
53
+ // Support for util.inspect and console.log
54
+ [Symbol.for('nodejs.util.inspect.custom')](_depth: number, _options: unknown): string {
55
+ return this.toString();
56
+ }
57
+ }
58
+
59
+ const kReadableOperator: Record<string, string> = {
60
+ 'deepStrictEqual': 'Expected values to be strictly deep-equal:',
61
+ 'strictEqual': 'Expected values to be strictly equal:',
62
+ 'strictEqualObject': 'Expected "actual" to be reference-equal to "expected":',
63
+ 'deepEqual': 'Expected values to be loosely deep-equal:',
64
+ 'notDeepStrictEqual': 'Expected "actual" not to be strictly deep-equal to:',
65
+ 'notStrictEqual': 'Expected "actual" to be strictly unequal to:',
66
+ 'notStrictEqualObject': 'Expected "actual" not to be reference-equal to "expected":',
67
+ 'notDeepEqual': 'Expected "actual" not to be loosely deep-equal to:',
68
+ 'notIdentical': 'Values have same structure but are not reference-equal:',
69
+ 'notEqual': 'Expected "actual" to be loosely unequal to:',
70
+ 'equal': 'Expected values to be loosely equal:',
71
+ '==': 'Expected values to be loosely equal:',
72
+ '!=': 'Expected "actual" to be loosely unequal to:',
73
+ '===': 'Expected values to be strictly equal:',
74
+ '!==': 'Expected "actual" to be strictly unequal to:',
75
+ 'fail': 'Failed',
76
+ };
77
+
78
+ function generateMessage(actual: unknown, expected: unknown, operator: string): string {
79
+ const header = kReadableOperator[operator] || `Operator: ${operator}`;
80
+
81
+ if (operator === 'fail') {
82
+ return 'Failed';
83
+ }
84
+
85
+ const actualStr = safeInspect(actual);
86
+ const expectedStr = safeInspect(expected);
87
+
88
+ return `${header}\n\n+ actual - expected\n\n+ ${actualStr}\n- ${expectedStr}\n`;
89
+ }