Skip to content

Commit

Permalink
feat: relottie-metadata plugin (#93)
Browse files Browse the repository at this point in the history
* feat: relottie-metadata logic

* chore(size-limit): add relottie-metadata

* test: use test.each

* fix(helpers): isHexValid & add test cases

* test: update snapshots after isHexValid changes
  • Loading branch information
Aidosmf committed Apr 15, 2024
1 parent 81d53d6 commit d49a56e
Show file tree
Hide file tree
Showing 95 changed files with 3,043 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/ten-plants-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lottiefiles/relottie-metadata': major
---

feat: initial major release that captures these Lottie metadata: colors, fileSize, framerate, frames,
geneartor, height, weight, inPoint, outPoint & version
3 changes: 3 additions & 0 deletions .size-limit.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ module.exports = [
{
path: 'packages/relottie-extract-features/dist/index.js',
},
{
path: 'packages/relottie-metadata/dist/index.js',
},
];
16 changes: 16 additions & 0 deletions packages/relottie-metadata/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
MIT License

Copyright (c) 2023 LottieFiles.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
127 changes: 127 additions & 0 deletions packages/relottie-metadata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# relottie-metadata

![npm](https://img.shields.io/npm/v/@lottiefiles/relottie-metadata)
![npm](https://img.shields.io/npm/dt/%40lottiefiles/relottie-metadata)
![npm](https://img.shields.io/npm/l/@lottiefiles/relottie-metadata)

A [relottie] plugin to extract Lottie metadata from the document and store them in vfile.data.

## Use

```sh
yarn add @lottiefiles/relottie-metadata
```

```ts
import relottieParse, { type ParseFileData } from '@lottiefiles/relottie-parse';
import relottieStringify, { type StringifyFileData } from '@lottiefiles/relottie-stringify';
import relottieMetadata, { type MetadataFileData } from '@lottiefiles/relottie-metadata';
import { unified } from 'unified';

type FileData = ParseFileData & StringifyFileData & MetadataFileData

// The Lottie source: __fixtures__/features/auto-orient.json
const lottieJson = '...';

const vfile = unified()
.use(relottieParse)
.use(relottieMetadata)
.use(relottieStringify)
.processSync(lottieJson);

const data = vfile.data as FileData
const output = data['metadata']
```

`output`:

```snap
{
"colors": Set {
"#3250b0",
},
"fileSize": {
"bytes": 2653,
"formated": {
"exponent": 1,
"symbol": "KiB",
"unit": "KiB",
"value": "2.59",
},
},
"framerate": 60,
"frames": 180,
"generator": "Glaxnimate 0.4.6-26-g7b05e75c",
"height": 512,
"inPoint": 0,
"outPoint": 180,
"version": "5.5.7",
"width": 512,
}
...
```

## FileData

```typescript
export interface MetadataFileData extends Data {
metadata: {
/**
* Set of hex colors used in the animation.
*/
colors: Set<string>;
/** *
* Size of the Lottie JSON
*/
fileSize?: FileSizeValue;
/**
* Framerate in frames per second
*/
framerate: number;
/**
* Total number of frames in the animation.
*/
frames: number;
/**
* Generator of the animation.
*/
generator: string;
/**
* Height of the animation.
*/
height: number;
/**
* Which frame the animation starts at (usually 0)
*/
inPoint: number;
/**
* Which frame the animation stops/loops at, which makes this the duration in frames
*/
outPoint: number;
/**
* Version of the Lottie JSON.
*/
version: string;
/**
* Width of the animation.
*/
width: number;
};
}
```

## Start

1. `pnpm install`
2. `pnpm ts-node playground.ts` for testing playground environment (if you are in the root folder you have to enter the package directory `cd packages/relottie-metadata`)
3. for testing `pnpm test`

## Legal

[MIT](LICENSE) © [LottieFiles](https://www.lottiefiles.com)

<!-- Definitions -->

[relottie]: https://github.com/LottieFiles/relottie

[unified]: https://github.com/unifiedjs/unified
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`relottie-metadata fixtures extra Test in 100-frames.json should have correct data 1`] = `
{
"colors": Set {
"#ffffff",
},
"fileSize": {
"bytes": 13021,
"formated": {
"exponent": 1,
"symbol": "KiB",
"unit": "KiB",
"value": "12.72",
},
},
"framerate": 30,
"frames": 100,
"generator": "@lottiefiles/toolkit-js 0.8.1",
"height": 500,
"inPoint": 0,
"outPoint": 100,
"version": "5.5.5",
"width": 500,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`relottie-metadata fixtures extra Test in 1000-frames.json should have correct data 1`] = `
{
"colors": Set {
"#ffffff",
},
"fileSize": {
"bytes": 124621,
"formated": {
"exponent": 1,
"symbol": "KiB",
"unit": "KiB",
"value": "121.7",
},
},
"framerate": 30,
"frames": 1000,
"generator": "@lottiefiles/toolkit-js 0.8.1",
"height": 500,
"inPoint": 0,
"outPoint": 1000,
"version": "",
"width": 500,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`relottie-metadata fixtures extra Test in 50-frames.json should have correct data 1`] = `
{
"colors": Set {
"#ffffff",
},
"fileSize": {
"bytes": 7439,
"formated": {
"exponent": 1,
"symbol": "KiB",
"unit": "KiB",
"value": "7.26",
},
},
"framerate": 30,
"frames": 50,
"generator": "@lottiefiles/toolkit-js 0.8.1",
"height": 500,
"inPoint": 0,
"outPoint": 50,
"version": "",
"width": 500,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`relottie-metadata fixtures extra Test in bicycle.json should have correct data 1`] = `
{
"colors": Set {
"#00c3a4ff",
"#06d7b6ff",
"#000922ff",
"#021a4fff",
"#f3c6b1ff",
"#252d42ff",
"#b3d5ddff",
"#eb9f7bff",
"#02b699ff",
"#8bbfcbff",
"#4a2800ff",
"#008fefff",
"#00090cff",
"#ffffff",
},
"fileSize": {
"bytes": 145803,
"formated": {
"exponent": 1,
"symbol": "KiB",
"unit": "KiB",
"value": "142.39",
},
},
"framerate": 29.9700012207031,
"frames": 150.000006109625,
"generator": "",
"height": 800,
"inPoint": 0,
"outPoint": 150.000006109625,
"version": "5.6.3",
"width": 800,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`relottie-metadata fixtures extra Test in delivered.json should have correct data 1`] = `
{
"colors": Set {
"#009a13ff",
"#1a7fcaff",
"#e6f5ffff",
},
"fileSize": {
"bytes": 92103,
"formated": {
"exponent": 1,
"symbol": "KiB",
"unit": "KiB",
"value": "89.94",
},
},
"framerate": 30,
"frames": 151,
"generator": "LottieFiles AE 3.0.0",
"height": 1528,
"inPoint": 0,
"outPoint": 151,
"version": "4.8.0",
"width": 1440,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`relottie-metadata fixtures extra Test in ducky.json should have correct data 1`] = `
{
"colors": Set {
"#ffffffff",
"#37bfc5ff",
"#edb41eff",
"#4d2f1eff",
"#ffcc33ff",
"#e35e47ff",
},
"fileSize": {
"bytes": 27948,
"formated": {
"exponent": 1,
"symbol": "KiB",
"unit": "KiB",
"value": "27.29",
},
},
"framerate": 60,
"frames": 120,
"generator": "LottieFiles AE ",
"height": 1024,
"inPoint": 0,
"outPoint": 120,
"version": "4.8.0",
"width": 1024,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`relottie-metadata fixtures extra Test in extremely-big.json should have correct data 1`] = `
{
"colors": Set {
"#ff0000ff",
},
"fileSize": {
"bytes": 1817115,
"formated": {
"exponent": 2,
"symbol": "MiB",
"unit": "MiB",
"value": "1.73",
},
},
"framerate": 29.9700012207031,
"frames": 135.000005498663,
"generator": "",
"height": 5103,
"inPoint": 0,
"outPoint": 135.000005498663,
"version": "5.5.2",
"width": 5103,
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`relottie-metadata fixtures extra Test in grocery.json should have correct data 1`] = `
{
"colors": Set {},
"fileSize": {
"bytes": 157798,
"formated": {
"exponent": 1,
"symbol": "KiB",
"unit": "KiB",
"value": "154.1",
},
},
"framerate": 30,
"frames": 181,
"generator": "LottieFiles AE 3.0.0",
"height": 1528,
"inPoint": 0,
"outPoint": 181,
"version": "4.8.0",
"width": 1440,
}
`;

0 comments on commit d49a56e

Please sign in to comment.