Skip to content

Commit

Permalink
Version: feat(core): improve inof/outof and add path method
Browse files Browse the repository at this point in the history
- deep.ts: Changed inof/outof to return links instead of from/to objects
- deep.ts: Added path() method to get path through contains relationships
- tests/core.ts: Added tests for path() method
- CONTRIBUTING.md: Added commit message template

Breaking Changes:
- inof() now returns links instead of from objects
- outof() now returns links instead of to objects
  • Loading branch information
ivansglazunov committed Nov 26, 2024
1 parent bc7b356 commit 7ae299e
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ yarn build
2. Follow Deep relationship naming conventions:
- Use PascalCase (capital first letter) for Type relationships (e.g., `Type`, `Contains`, `Value`)
- Use camelCase (lowercase first letter) for instance relationships (e.g., `type`, `from`, `to`, `value`)

## Commits and Releases
For publishing, it is necessary to:
1. Increase the version level, usually patch
2. Extract changes description from `git status` and `git diff` for all modified files
3. Format the message according to the following template:
```
Version: type(scope): summary
body
Breaking Changes:
- list of breaking changes (if any)
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "deep",
"appId": "com.deep_foundation.deep.app",
"macCategory": "public.app-category.developer-tools",
"version": "7.0.0-alpha.5",
"version": "7.0.0-alpha.6",
"description": "Universal solution for working with any meaning",
"license": "Unlicense",
"main": "dist/cli.js",
Expand Down
33 changes: 31 additions & 2 deletions src/deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,7 @@ export class Deep {
const result = new Set<Deep>();
for (const link of this.in.call) {
if (link.type === type) {
result.add(link.from);
result.add(link);
}
}
return this.wrap(result);
Expand All @@ -1644,7 +1644,7 @@ export class Deep {
const result = new Set<Deep>();
for (const link of this.out.call) {
if (link.type === type) {
result.add(link.to);
result.add(link);
}
}
return this.wrap(result);
Expand Down Expand Up @@ -1937,6 +1937,35 @@ export class Deep {
}
return current;
}

/**
* Возвращает путь к текущему deep через входящие Contain связи
* @returns массив строк - путь через contains
*/
path(): string[] {
const result: string[] = [];
const exists = new Set<any>();
let current: Deep | undefined = this;

while (current) {
const contains = current.inof(this.deep.Contain);
if (!contains.size) break;

const contain = contains.first;
if (!contain) break;

// Находим ключ в contains, по которому хранится текущий deep
const from = contain.from;
if (!from) break;

if (exists.has(from)) break;
result.unshift(contain?.call);
exists.add(from);
current = from;
}

return result;
}
}

/**
Expand Down
27 changes: 24 additions & 3 deletions src/tests/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -863,21 +863,42 @@ test('collection getters (types, froms, tos, typeds, outs, ins)', () => {
test('go method', () => {
const deep = new Deep();

// Создаем тестовую структуру
const a = deep.new();
deep.contains.a = a;
const b = deep.new();
a.contains.b = b;
const c = deep.new();
b.contains.c = c;

// Проверяем успешный поиск
assert.equal(deep.go('a', 'b', 'c'), c);
assert.equal(deep.go('a', 'b'), b);
assert.equal(deep.go('a'), a);

// Проверяем неуспешный поиск
assert.equal(deep.go('x'), undefined);
assert.equal(deep.go('a', 'x'), undefined);
assert.equal(deep.go('a', 'b', 'x'), undefined);
});

test('path method', () => {
const deep = new Deep();

const a = deep.new();
deep.contains.a = a;
const b = deep.new();
a.contains.b = b;
const c = deep.new();
b.contains.c = c;

assert.deepEqual(c.path(), ['a', 'b', 'c']);
assert.deepEqual(b.path(), ['a', 'b']);
assert.deepEqual(a.path(), ['a']);
assert.deepEqual(deep.path(), ['deep']);

const x = deep.new();
deep.contains.x = x;
const y = deep.new();
x.contains.y = y;
y.contains.c = c;

assert.deepEqual(c.path(), ['a', 'b', 'c']);
});

0 comments on commit 7ae299e

Please sign in to comment.