@surfaice/differ 0.0.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/LICENSE +21 -0
- package/README.md +35 -0
- package/dist/differ.d.ts +7 -0
- package/dist/differ.js +93 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/types.d.ts +23 -0
- package/dist/types.js +1 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 surfaiceai
|
|
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,35 @@
|
|
|
1
|
+
# @surfaice/differ
|
|
2
|
+
|
|
3
|
+
Structural diff engine for Surfaice pages — compare two `SurfaicePage` ASTs and detect UI drift.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @surfaice/differ @surfaice/format
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { diff } from '@surfaice/differ'
|
|
15
|
+
import { parse } from '@surfaice/format'
|
|
16
|
+
|
|
17
|
+
const expected = parse(committedMarkdown)
|
|
18
|
+
const actual = parse(liveMarkdown)
|
|
19
|
+
|
|
20
|
+
const result = diff(expected, actual)
|
|
21
|
+
|
|
22
|
+
if (result.status === 'drift') {
|
|
23
|
+
console.log(result.summary) // "2 added, 1 removed, 3 changed"
|
|
24
|
+
result.added.forEach(el => console.log(`+ [${el.id}] ${el.label}`))
|
|
25
|
+
result.removed.forEach(el => console.log(`- [${el.id}] ${el.label}`))
|
|
26
|
+
result.changed.forEach(c => console.log(`~ [${c.id}].${c.field}: "${c.expected}" → "${c.actual}"`))
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## What Gets Diffed
|
|
31
|
+
|
|
32
|
+
- **Added** — elements in actual but not in committed
|
|
33
|
+
- **Removed** — elements in committed but not in actual
|
|
34
|
+
- **Changed** — field-level diffs: `label`, `type`, `action`, `result`, `navigates`, `accepts`, `shows`, `current`, `attributes`, `options`
|
|
35
|
+
- **Nested** — elements inside `reveals` are diffed recursively
|
package/dist/differ.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SurfaicePage } from '@surfaice/format';
|
|
2
|
+
import type { SurfaiceDiff } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Diff two SurfaicePage ASTs.
|
|
5
|
+
* Returns a structured diff with added, removed, and changed elements.
|
|
6
|
+
*/
|
|
7
|
+
export declare function diff(expected: SurfaicePage, actual: SurfaicePage): SurfaiceDiff;
|
package/dist/differ.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
const COMPARED_FIELDS = [
|
|
2
|
+
'type', 'label', 'action', 'result', 'navigates',
|
|
3
|
+
'accepts', 'shows', 'current',
|
|
4
|
+
];
|
|
5
|
+
/**
|
|
6
|
+
* Diff two SurfaicePage ASTs.
|
|
7
|
+
* Returns a structured diff with added, removed, and changed elements.
|
|
8
|
+
*/
|
|
9
|
+
export function diff(expected, actual) {
|
|
10
|
+
const expectedMap = buildElementMap(expected);
|
|
11
|
+
const actualMap = buildElementMap(actual);
|
|
12
|
+
const added = [];
|
|
13
|
+
const removed = [];
|
|
14
|
+
const changed = [];
|
|
15
|
+
// Removed: in expected, not in actual
|
|
16
|
+
for (const [id, { element, section }] of expectedMap) {
|
|
17
|
+
if (!actualMap.has(id)) {
|
|
18
|
+
removed.push({ id, type: element.type, label: element.label, section });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
// Added: in actual, not in expected
|
|
22
|
+
for (const [id, { element, section }] of actualMap) {
|
|
23
|
+
if (!expectedMap.has(id)) {
|
|
24
|
+
added.push({ id, type: element.type, label: element.label, section });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// Changed: in both, fields differ
|
|
28
|
+
for (const [id, { element: exp, section }] of expectedMap) {
|
|
29
|
+
const actEntry = actualMap.get(id);
|
|
30
|
+
if (!actEntry)
|
|
31
|
+
continue;
|
|
32
|
+
const act = actEntry.element;
|
|
33
|
+
// Compare scalar fields
|
|
34
|
+
for (const field of COMPARED_FIELDS) {
|
|
35
|
+
const expVal = String(exp[field] ?? '');
|
|
36
|
+
const actVal = String(act[field] ?? '');
|
|
37
|
+
if (expVal !== actVal && (expVal || actVal)) {
|
|
38
|
+
changed.push({
|
|
39
|
+
id, section, field,
|
|
40
|
+
expected: expVal || '(none)',
|
|
41
|
+
actual: actVal || '(none)',
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Compare attributes (as sorted comma-separated string)
|
|
46
|
+
const expAttrs = [...(exp.attributes ?? [])].sort().join(', ');
|
|
47
|
+
const actAttrs = [...(act.attributes ?? [])].sort().join(', ');
|
|
48
|
+
if (expAttrs !== actAttrs) {
|
|
49
|
+
changed.push({
|
|
50
|
+
id, section, field: 'attributes',
|
|
51
|
+
expected: expAttrs || '(none)',
|
|
52
|
+
actual: actAttrs || '(none)',
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
// Compare options
|
|
56
|
+
const expOpts = [...(exp.options ?? [])].join(', ');
|
|
57
|
+
const actOpts = [...(act.options ?? [])].join(', ');
|
|
58
|
+
if (expOpts !== actOpts && (expOpts || actOpts)) {
|
|
59
|
+
changed.push({
|
|
60
|
+
id, section, field: 'options',
|
|
61
|
+
expected: expOpts || '(none)',
|
|
62
|
+
actual: actOpts || '(none)',
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const status = added.length === 0 && removed.length === 0 && changed.length === 0
|
|
67
|
+
? 'match'
|
|
68
|
+
: 'drift';
|
|
69
|
+
const parts = [];
|
|
70
|
+
if (added.length)
|
|
71
|
+
parts.push(`${added.length} added`);
|
|
72
|
+
if (removed.length)
|
|
73
|
+
parts.push(`${removed.length} removed`);
|
|
74
|
+
if (changed.length)
|
|
75
|
+
parts.push(`${changed.length} changed`);
|
|
76
|
+
const summary = status === 'match' ? 'No drift detected' : parts.join(', ');
|
|
77
|
+
return { route: expected.route, status, added, removed, changed, summary };
|
|
78
|
+
}
|
|
79
|
+
function buildElementMap(page) {
|
|
80
|
+
const map = new Map();
|
|
81
|
+
for (const section of page.sections) {
|
|
82
|
+
collectElements(section.elements, section.name, map);
|
|
83
|
+
}
|
|
84
|
+
return map;
|
|
85
|
+
}
|
|
86
|
+
function collectElements(elements, section, map) {
|
|
87
|
+
for (const el of elements) {
|
|
88
|
+
map.set(el.id, { element: el, section });
|
|
89
|
+
if (el.reveals?.length) {
|
|
90
|
+
collectElements(el.reveals, section, map);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export interface DiffElement {
|
|
2
|
+
id: string;
|
|
3
|
+
type: string;
|
|
4
|
+
label: string;
|
|
5
|
+
section: string;
|
|
6
|
+
}
|
|
7
|
+
export interface DiffChange {
|
|
8
|
+
id: string;
|
|
9
|
+
section: string;
|
|
10
|
+
/** Which field changed: 'label', 'type', 'action', 'attributes', etc. */
|
|
11
|
+
field: string;
|
|
12
|
+
expected: string;
|
|
13
|
+
actual: string;
|
|
14
|
+
}
|
|
15
|
+
export interface SurfaiceDiff {
|
|
16
|
+
route: string;
|
|
17
|
+
status: 'match' | 'drift';
|
|
18
|
+
added: DiffElement[];
|
|
19
|
+
removed: DiffElement[];
|
|
20
|
+
changed: DiffChange[];
|
|
21
|
+
/** Human-readable one-liner summary */
|
|
22
|
+
summary: string;
|
|
23
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@surfaice/differ",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Structural diff engine for Surfaice pages",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/surfaiceai/surfaice",
|
|
18
|
+
"directory": "packages/differ"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@surfaice/format": "0.0.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"vitest": "^2.0.0",
|
|
25
|
+
"typescript": "^5.5.0"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"README.md"
|
|
30
|
+
],
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public",
|
|
33
|
+
"registry": "https://registry.npmjs.org"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"dev": "tsc --watch"
|
|
39
|
+
}
|
|
40
|
+
}
|