Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): clean up #16

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 79 additions & 33 deletions libs/plugins-data-parser/README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,105 @@
# Parser

This library exports `parse()` and `getSelectedUuids()` funtions and some models like `ParsedFile`, `ParsedPage` or `UnparsedSelection`.
This library exports `cleanObject()` and `getSelectedUuids()` funtions and the model `Selection`.

The `parse()` function cleans up and transforms a penpot object into a more typescript friendly object. It returns a `ParsedData` object that can be casted as `ParsedFile` or `ParsedPage`. Note that `ParsedData` is the parent interface and includes all `ParsedFile` and `ParsedPage` properties.
The `cleanObject()` function cleans up objects from useless properties and transforms the remaining ones to camelCase. It returns `unknown`.

Most of the properties are optional and may or may not be present in your result, you should access them with care.
The `getSelectedUuids()` functions, given an `Selection` object, returns the selected Uuids as an array of string.

The `getSelectedUuids()` functions, given an `UnparsedSelection` object, returns the selected Uuids as an array of string.
## Helpers

## Use
### File Helper

Import the parse function and the desired models from plugins data parser.
#### File Helper functions

Examples:
- `setData()`

- `parse()`
You can either pass the data in the constructor or use the `setData()` function.

example:

```ts
const fileHelper = new FileHelper();
fileHelper.setData(data);
```

or

```ts
const fileHelper = new FileHelper(data);
```

- `getCleanData()`

Gets the cleaned up data. It deletes useless properties and and transforms the remaining ones to camelCase.

example:

```ts
const clean = fileHelper.getCleanData();
```

### Page Helper

#### Page Helper functions

- `setData()`

You can either pass the data in the constructor or use the `setData()` function.

example:

```ts
import { parse, ParsedFile } from 'plugins-parser';
const pageHelper = new PageHelper();
pageHelper.setData(data);
```

[...]
or

const parsedFile: ParsedFile = parse(file);
```ts
const pageHelper = new PageHelper(data);
```

const color = parsedFile.data.colors?.[0];
- `getCleanData()`

/** color:
* {
* "path": "Background",
* "color": "#1c1b1f",
* "fileId": "f13ed095-e13f-808c-8002-2830d45911f7",
* "name": "On Background",
* "opacity": 1,
* "id": "136eece0-40ab-8002-8002-296771ace070"
* }
*/
Gets the cleaned up data. It deletes useless properties and and transforms the remaining ones to camelCase.

example:

```ts
const clean = pageHelper.getCleanData();
```

- `getSelectedUuids()`
- `getObjectsArray()`

Returns the objects array, which can contain heavily nested arrays with objects data.

example:

```ts
import { getSelectedUuids, UnparsedSelection } from 'plugins-parser';
const objects = pageHelper.getObjectsArray();
```

- `getObjectById(id: string)`

Returns an object by given uuid. The object is cleaned up and formatted as a `PObject`.

```ts
const obj: PObject = pageHelper.getObjectById(
'3aba0744-11fe-4c41-80fb-1b42aa7ef3e5'
);
```

[...]
### Selection Helper

const selection: UnparsedSelection = { [...] };
#### Selection Helper functions

const ids: string[] = getSelectedUuids(selection); =>
- `static getUuids(selection: Selection)`

/** ids:
* [
* "4fa12080-0d58-80a3-8002-3a2344356e7c",
* "d9a61226-8431-8080-8002-5aea35acc724"
* ]
*/
Returns the selected items in an array.

example:

```ts
const ids = SelectionHelper.getUuids(selection);
```
4 changes: 2 additions & 2 deletions libs/plugins-data-parser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { parse, getSelectedUuids } from './lib/utils';
export { getSelectedUuids, cleanObject, getPartialState } from './lib/utils';
export * from './lib/models';
export { getPartialState } from './lib/utils/parse-state';
export * from './lib/helpers';
22 changes: 22 additions & 0 deletions libs/plugins-data-parser/src/lib/helpers/file.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { cleanObject } from '../utils';

/**
* WIP
*/
export class FileHelper {
private data: unknown = null;

public constructor(data?: unknown) {
if (data) {
this.setData(data);
}
}

public setData(data: unknown): void {
this.data = cleanObject(data);
}

public getCleanData(): unknown {
return this.data;
}
}
3 changes: 3 additions & 0 deletions libs/plugins-data-parser/src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './file.helper';
export * from './page.helper';
export * from './selection.helper';
97 changes: 97 additions & 0 deletions libs/plugins-data-parser/src/lib/helpers/page.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { cleanObject, parseObject, isObject } from '../utils';
import { PObject } from '../models';
import { Name, Uuid } from '../utils/models/util.model';

interface PageArr {
data?: {
arr?: unknown[];
};
[key: string]: unknown;
}

interface PageObject {
root?: {
arr?: unknown[];
};
[key: string]: unknown;
}

/**
* WIP
*/
export class PageHelper {
private data: unknown = null;
private objects: unknown[] = [];

public constructor(data?: unknown) {
if (data) {
this.setData(data);
}
}

public setData(data: unknown): void {
this.data = cleanObject(data);
this.objects = this.getPageObjects(this.data as PageArr);
}

public getCleanData(): unknown {
return this.data;
}

public getObjectsArray(): unknown[] {
return this.objects;
}

public getObjectById(id: string): PObject | null {
if (!this.objects) {
return null;
}

const foundObject = this.findObject(this.objects, id);
return parseObject(foundObject) as PObject;
}

private getPageObjects(obj: PageArr): unknown[] {
const dataArr = obj?.data?.arr;

const objectNameIndex = dataArr?.findIndex(
(item) => isObject(item) && (item as Name)?.name === 'objects'
);

if (!objectNameIndex) {
return [];
}

const objects = dataArr?.[objectNameIndex + 1] as PageObject;

return (isObject(objects) && objects?.root?.arr) || [];
}

private findObject(
data: unknown,
id: string
): Record<string, unknown> | null {
if (Array.isArray(data)) {
for (const item of data) {
const foundObject = this.findObject(item, id);
if (foundObject !== null) {
return foundObject;
}
}
} else if (isObject(data)) {
const obj = data as Record<string, unknown>;
if ((obj?.['id'] as Uuid)?.uuid === id || obj?.['id'] === id) {
return obj;
}

for (const key of Object.keys(obj)) {
const foundObject = this.findObject(obj[key], id);
if (foundObject !== null) {
return foundObject;
}
}
}

return null;
}
}
15 changes: 15 additions & 0 deletions libs/plugins-data-parser/src/lib/helpers/selection.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Selection } from '../models';

/**
* WIP
*/
export class SelectionHelper {
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}

static getUuids(selection: Selection): string[] {
const root = selection?.linked_map?.delegate_map?.root?.arr;

return (root?.filter((r) => r?.uuid).map((r) => r.uuid) as string[]) || [];
}
}
Loading