Skip to content

Commit

Permalink
feat(last): remove valueNode, split Primitive node, capitalize Node type
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidosmf committed Dec 17, 2024
1 parent bc650e6 commit 1a5f7c1
Show file tree
Hide file tree
Showing 94 changed files with 62,971 additions and 90,218 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-pianos-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/relottie-parse': major
---

feat: capitalize Node type values after last@v2
5 changes: 5 additions & 0 deletions .changeset/soft-students-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/last': major
---

feat(primitive): rename to PrimitiveNode, remove "valueType" prop & split it to StringNode, NumberNode, BooleanNode & NullNode
8 changes: 8 additions & 0 deletions .changeset/spicy-elephants-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@lottiefiles/last-builder': major
---

feat: required changes to support last@v2
- capitalize types
- remove "valueType", adjust "pt" helper after PrimitiveNode was split into String, Number, Null & Booelan Nodes
- remove the PrimitiveParts type since it is not relevant anymore after the "valueType" prop is removed
5 changes: 5 additions & 0 deletions .changeset/tall-pandas-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lottiefiles/last': major
---

feat: capitalize Node type value
6 changes: 6 additions & 0 deletions .changeset/tiny-poets-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lottiefiles/relottie-parse': major
---

feat: remove valeuType option and don't add it prop to PrimtiveNode after last@v2 changes

22 changes: 11 additions & 11 deletions packages/last-builder/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,59 +20,59 @@ describe('should create a last tree correctly', () => {
]);

const output = {
type: 'root',
type: 'Root',
title: 'animation',
hasExpressions: false,
children: [
{
type: 'attribute',
type: 'Attribute',
key: 'v',
title: 'version',
children: [
{
type: 'primitive',
type: 'String',
value: '4.8.0',
},
],
},
{
type: 'element',
type: 'Element',
key: 'meta',
title: 'metadata',
children: [
{
type: 'object',
type: 'Object',
title: 'custom',
children: [
{
type: 'attribute',
type: 'Attribute',
key: 'a',
title: 'author',
children: [
{
type: 'primitive',
type: 'String',
value: 'LottieFiles',
},
],
},
{
type: 'attribute',
type: 'Attribute',
key: 'd',
title: 'description',
children: [
{
type: 'primitive',
type: 'String',
value: 'A lottie animation.',
},
],
},
{
type: 'attribute',
type: 'Attribute',
key: 'data',
title: 'custom',
children: [
{
type: 'primitive',
type: 'Number',
value: 2,
},
],
Expand Down
64 changes: 37 additions & 27 deletions packages/last-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import type {
Root,
KeyValue,
KeyNode,
Primitive,
PrimitiveValue,
PrimitiveValueType,
PrimitiveNode,
PrimitiveNodeValue,
ObjectNode,
ObjectNodeValue,
ObjectTitle,
Expand All @@ -30,10 +29,6 @@ export interface Parts {
position?: Position;
}

export interface PrimitiveParts<T> extends Parts {
valueType?: T;
}

type Children<T> = T | T[];

function normalizeChildren<T>(children?: Children<T>): T[] {
Expand Down Expand Up @@ -70,22 +65,32 @@ function normalizeMemberChild<T>(child?: MemberChild<T>): [T] | [] {
}
}

function normalizePrimitiveValue<T>(value: T): T | string {
if (value === undefined) {
return 'empty';
} else {
return value;
}
}

/**
* Create a Primitive Node
* @param value - Primitive's value, e.g. 7
* @param value - Primitive's value, e.g. 7
* @param parts - additional data props e.g. Position
* @returns (last) Primitive node
* @returns
* - (last) PrimitiveNode
* - if value is not PrimitiveNodeValue, return a String node with value "UNKNOWN"
*/
export const pt = (value?: PrimitiveValue, parts?: PrimitiveParts<PrimitiveValueType>): Primitive => {
return u('primitive', { value: normalizePrimitiveValue(value) as PrimitiveValue, ...parts });
export const pt = (value?: PrimitiveNodeValue, parts?: Parts): PrimitiveNode => {
if (value === null) {
return u('Null', { value, ...parts });
}

switch (typeof value) {
case 'string':
return u('String', { value, ...parts });

case 'number':
return u('Number', { value, ...parts });

case 'boolean':
return u('Boolean', { value, ...parts });

default:
return u('String', { value: 'UNKNOWN', ...parts });
}
};

/**
Expand All @@ -95,7 +100,7 @@ export const pt = (value?: PrimitiveValue, parts?: PrimitiveParts<PrimitiveValue
* @returns (last) Key node
*/
export const ky = (value: string, parts?: Parts): KeyNode => {
return u('key', { ...parts }, value);
return u('Key', { ...parts }, value);
};

/**
Expand All @@ -107,7 +112,7 @@ export const ky = (value: string, parts?: Parts): KeyNode => {
*/
export const ob = (title: ObjectTitle, kids?: Children<ObjectNodeValue>, parts?: Parts): ObjectNode =>
u(
'object',
'Object',
{
title,
...parts,
Expand All @@ -124,7 +129,7 @@ export const ob = (title: ObjectTitle, kids?: Children<ObjectNodeValue>, parts?:
*/
export const ar = (title: ArrayTitle, kids?: Children<ArrayNodeValue>, parts?: Parts): ArrayNode =>
u(
'array',
'Array',
{
title,
...parts,
Expand All @@ -140,8 +145,13 @@ export const ar = (title: ArrayTitle, kids?: Children<ArrayNodeValue>, parts?: P
* @param parts - additional data props e.g. Position
* @returns (last) Attribute node
*/
export const at = (key: KeyValue, title: AttributeTitle, kid?: MemberChild<Primitive>, parts?: Parts): Attribute => {
return u('attribute', { key, title, ...parts }, normalizeMemberChild(kid));
export const at = (
key: KeyValue,
title: AttributeTitle,
kid?: MemberChild<PrimitiveNode>,
parts?: Parts,
): Attribute => {
return u('Attribute', { key, title, ...parts }, normalizeMemberChild(kid));
};

/**
Expand All @@ -153,7 +163,7 @@ export const at = (key: KeyValue, title: AttributeTitle, kid?: MemberChild<Primi
* @returns (last) Element node
*/
export const el = (key: KeyValue, title: ElementTitle, kid?: MemberChild<ObjectNode>, parts?: Parts): Element => {
return u('element', { key, title, ...parts }, normalizeMemberChild(kid));
return u('Element', { key, title, ...parts }, normalizeMemberChild(kid));
};

/**
Expand All @@ -165,7 +175,7 @@ export const el = (key: KeyValue, title: ElementTitle, kid?: MemberChild<ObjectN
* @returns (last) Collection node
*/
export const cl = (key: KeyValue, title: CollectionTitle, kid?: MemberChild<ArrayNode>, parts?: Parts): Collection => {
return u('collection', { key, title, ...parts }, normalizeMemberChild(kid));
return u('Collection', { key, title, ...parts }, normalizeMemberChild(kid));
};

/**
Expand All @@ -176,7 +186,7 @@ export const cl = (key: KeyValue, title: CollectionTitle, kid?: MemberChild<Arra
*/
export const rt = (kids?: Children<ObjectNodeValue>, parts?: Parts): Root => {
return u(
'root',
'Root',
{
title: TITLES.object.animation,
hasExpressions: false,
Expand Down
1 change: 0 additions & 1 deletion packages/last/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ type PrimitiveValue = string | number | boolean | null;
interface Primitive extends Literal {
type: 'primitive';
value: PrimitiveValue;
valueType?: PrimitiveValueType;
}
```

Expand Down
48 changes: 32 additions & 16 deletions packages/last/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,34 @@ export interface Data extends UnistData {}
// General
// -----------------------------------------------------------------------------

export type PrimitiveValueType = 'string' | 'number' | 'boolean' | 'null';
export interface StringNode extends Literal {
type: 'String';
value: string;
}

export interface NumberNode extends Literal {
type: 'Number';
value: number;
}

export type PrimitiveValue = string | number | boolean | null;
export interface BooleanNode extends Literal {
type: 'Boolean';
value: boolean;
}

export interface Primitive extends Literal {
type: 'primitive';
value: PrimitiveValue;
valueType?: PrimitiveValueType;
export interface NullNode extends Literal {
type: 'Null';
value: null;
}

export type PrimitiveNodeType = StringNode['type'] | NumberNode['type'] | BooleanNode['type'] | NullNode['type'];

export type PrimitiveNodeValue = StringNode['value'] | NumberNode['value'] | BooleanNode['value'] | NullNode['value'];

export type PrimitiveNode = StringNode | NumberNode | BooleanNode | NullNode;

export interface KeyNode extends Literal {
type: 'key';
type: 'Key';
value: string;
}

Expand All @@ -83,33 +99,33 @@ export type ObjectNodeValue = Attribute | Element | Collection;
export interface ObjectNode extends Parent {
children: ObjectNodeValue[];
title: ObjectTitle;
type: 'object';
type: 'Object';
}

export type ArrayNodeValue = Primitive | ObjectNode | ArrayNode;
export type ArrayNodeValue = PrimitiveNode | ObjectNode | ArrayNode;

export interface ArrayNode extends Parent {
children: ArrayNodeValue[];
title: ArrayTitle;
type: 'array';
type: 'Array';
}

export interface Attribute extends Member {
children: [Primitive] | [];
children: [PrimitiveNode] | [];
title: AttributeTitle;
type: 'attribute';
type: 'Attribute';
}

export interface Element extends Member {
children: [ObjectNode] | [];
title: ElementTitle;
type: 'element';
type: 'Element';
}

export interface Collection extends Member {
children: [ArrayNode] | [];
title: CollectionTitle;
type: 'collection';
type: 'Collection';
}

/**
Expand All @@ -121,9 +137,9 @@ export interface Root extends Omit<ObjectNode, 'type'> {
*/
hasExpressions: boolean;
title: typeof TITLES.object.animation;
type: 'root';
type: 'Root';
}

export type NodeValue = Root | Primitive | KeyNode | ArrayNode | ObjectNode | Attribute | Element | Collection;
export type NodeValue = Root | PrimitiveNode | KeyNode | ArrayNode | ObjectNode | Attribute | Element | Collection;

export type NodeValueType = NodeValue['type'];
9 changes: 8 additions & 1 deletion packages/relottie-extract-features/src/extract-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ const extractFeatures: Plugin<[Options?], Root> = (_ops: Options = {}) => {
const usedData: Used = new Map();

visitParents(tree, (node: AncestorChildNode, ancestor: AncestorNode[]) => {
if (node.type === 'root' || node.type === 'primitive') return;
if (
node.type === 'Root' ||
node.type === 'String' ||
node.type === 'Number' ||
node.type === 'Boolean' ||
node.type === 'Null'
)
return;

const feature = node.title;
const parent = ancestor.at(-1);
Expand Down
4 changes: 2 additions & 2 deletions packages/relottie-metadata/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ const metadata: Plugin<[Options?], Root> = (ops: Options = {}) => {

visit(tree, (node, _index, parent) => {
switch (node.type) {
case 'attribute':
case 'Attribute':
processAttributeNode(node, meta, parent as ObjectNode);

break;

case 'collection':
case 'Collection':
processCollectionNode(node, meta, file);

break;
Expand Down
Loading

0 comments on commit 1a5f7c1

Please sign in to comment.