UNPKG

2.88 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", {
3 value: true
4});
5Object.defineProperty(exports, "default", {
6 enumerable: true,
7 get: ()=>parseAnimationValue
8});
9const DIRECTIONS = new Set([
10 "normal",
11 "reverse",
12 "alternate",
13 "alternate-reverse"
14]);
15const PLAY_STATES = new Set([
16 "running",
17 "paused"
18]);
19const FILL_MODES = new Set([
20 "none",
21 "forwards",
22 "backwards",
23 "both"
24]);
25const ITERATION_COUNTS = new Set([
26 "infinite"
27]);
28const TIMINGS = new Set([
29 "linear",
30 "ease",
31 "ease-in",
32 "ease-out",
33 "ease-in-out",
34 "step-start",
35 "step-end"
36]);
37const TIMING_FNS = [
38 "cubic-bezier",
39 "steps"
40];
41const COMMA = /\,(?![^(]*\))/g // Comma separator that is not located between brackets. E.g.: `cubiz-bezier(a, b, c)` these don't count.
42;
43const SPACE = /\ +(?![^(]*\))/g // Similar to the one above, but with spaces instead.
44;
45const TIME = /^(-?[\d.]+m?s)$/;
46const DIGIT = /^(\d+)$/;
47function parseAnimationValue(input) {
48 let animations = input.split(COMMA);
49 return animations.map((animation)=>{
50 let value = animation.trim();
51 let result = {
52 value
53 };
54 let parts = value.split(SPACE);
55 let seen = new Set();
56 for (let part of parts){
57 if (!seen.has("DIRECTIONS") && DIRECTIONS.has(part)) {
58 result.direction = part;
59 seen.add("DIRECTIONS");
60 } else if (!seen.has("PLAY_STATES") && PLAY_STATES.has(part)) {
61 result.playState = part;
62 seen.add("PLAY_STATES");
63 } else if (!seen.has("FILL_MODES") && FILL_MODES.has(part)) {
64 result.fillMode = part;
65 seen.add("FILL_MODES");
66 } else if (!seen.has("ITERATION_COUNTS") && (ITERATION_COUNTS.has(part) || DIGIT.test(part))) {
67 result.iterationCount = part;
68 seen.add("ITERATION_COUNTS");
69 } else if (!seen.has("TIMING_FUNCTION") && TIMINGS.has(part)) {
70 result.timingFunction = part;
71 seen.add("TIMING_FUNCTION");
72 } else if (!seen.has("TIMING_FUNCTION") && TIMING_FNS.some((f)=>part.startsWith(`${f}(`))) {
73 result.timingFunction = part;
74 seen.add("TIMING_FUNCTION");
75 } else if (!seen.has("DURATION") && TIME.test(part)) {
76 result.duration = part;
77 seen.add("DURATION");
78 } else if (!seen.has("DELAY") && TIME.test(part)) {
79 result.delay = part;
80 seen.add("DELAY");
81 } else if (!seen.has("NAME")) {
82 result.name = part;
83 seen.add("NAME");
84 } else {
85 if (!result.unknown) result.unknown = [];
86 result.unknown.push(part);
87 }
88 }
89 return result;
90 });
91}