Skip to content

Commit

Permalink
feat: implement cap-in-parent, refactoring, rename trigger => enable
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Jan 23, 2019
1 parent bcfe44f commit ce656a9
Show file tree
Hide file tree
Showing 6 changed files with 885 additions and 231 deletions.
253 changes: 253 additions & 0 deletions src/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
import {
extendMandadory,
isNumeric,
sortEvents,
cleanInstances,
invertInstances,
operateOnArrays,
applyRepeatingInstances
} from '../lib'

describe('lib', () => {
test('extendMandadory', () => {
expect(extendMandadory<any, any>(
{ a: 1 },
{ b: 2 }
)).toEqual({ a: 1, b: 2 })
})
test('isNumeric', () => {
expect(isNumeric('123')).toEqual(true)
expect(isNumeric('123.234')).toEqual(true)
expect(isNumeric('-23123.234')).toEqual(true)
expect(isNumeric('123a')).toEqual(false)
expect(isNumeric('123,1')).toEqual(false)
expect(isNumeric('asdf')).toEqual(false)
})
test('sortEvents', () => {
expect(sortEvents([
{ time: 300, value: true },
{ time: 2, value: false },
{ time: 100, value: true },
{ time: 3, value: true },
{ time: 20, value: false },
{ time: 2, value: true },
{ time: 100, value: false },
{ time: 20, value: true },
{ time: 1, value: true }
])).toEqual([
{ time: 1, value: true },
{ time: 2, value: true },
{ time: 2, value: false },
{ time: 3, value: true },
{ time: 20, value: true },
{ time: 20, value: false },
{ time: 100, value: true },
{ time: 100, value: false },
{ time: 300, value: true }
])
})
test('cleanInstances', () => {
expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 20, end: 30 }
], true)).toEqual([
{ start: 10, end: 50 }
])

expect(cleanInstances([
{ start: 20, end: 70 },
{ start: 10, end: 50 }
], true)).toEqual([
{ start: 10, end: 70 }
])

expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 50, end: 70 }
], true)).toEqual([
{ start: 10, end: 70 }
])
expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 50, end: null }
], true)).toEqual([
{ start: 10, end: null }
])

expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 20, end: 92 },
{ start: 100, end: 120 },
{ start: 110, end: null }
], true)).toEqual([
{ start: 10, end: 92 },
{ start: 100, end: null }
])

// no merge:

expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 20, end: 30 }
], false)).toEqual([
{ start: 10, end: 50 },
{ start: 20, end: 30 }
])

expect(cleanInstances([
{ start: 20, end: 70 },
{ start: 10, end: 50 }
], false)).toEqual([
{ start: 10, end: 20 },
{ start: 20, end: 70 }
])

expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 50, end: 70 }
], false)).toEqual([
{ start: 10, end: 50 },
{ start: 50, end: 70 }
])
expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 50, end: null }
], false)).toEqual([
{ start: 10, end: 50 },
{ start: 50, end: null }
])

expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 20, end: 92 },
{ start: 100, end: 120 },
{ start: 110, end: null }
], false)).toEqual([
{ start: 10, end: 20 },
{ start: 20, end: 92 },
{ start: 100, end: 110 },
{ start: 110, end: null }
])
})
test('invertInstances', () => {
expect(invertInstances([
{ start: 10, end: 50 },
{ start: 100, end: 110 }
])).toEqual([
{ start: 0, end: 10, isFirst: true },
{ start: 50, end: 100 },
{ start: 110, end: null }
])

expect(invertInstances([
{ start: 30, end: 70 },
{ start: 100, end: null },
{ start: 10, end: 50 }
])).toEqual([
{ start: 0, end: 10, isFirst: true },
{ start: 70, end: 100 }
])

expect(invertInstances([
{ start: 30, end: 100 },
{ start: 10, end: 50 },
{ start: 100, end: 110 }
])).toEqual([
{ start: 0, end: 10, isFirst: true },
{ start: 110, end: null }
])
})
test('operateOnArrays', () => {
const plus = (a: number | null, b: number | null): number | null => {
if (a === null || b === null) return null
return a + b
}

expect(operateOnArrays(
[
{ start: 30, end: 90 },
{ start: 10, end: 50 },
{ start: 100, end: 110 }
],
1,
plus
)).toEqual([
{ start: 11, end: 31 },
{ start: 31, end: 91 },
{ start: 101, end: 111 }
])

expect(operateOnArrays(
[
{ start: 10, end: 30 },
{ start: 50, end: 70 },
{ start: 100, end: 110 }
],
[
{ start: 0, end: 25 },
{ start: 0, end: 30 },
{ start: 1, end: 5 }
],
plus
)).toEqual([
{ start: 10, end: 50 },
{ start: 50, end: 100 },
{ start: 101, end: 115 }
])

})
test('applyRepeatingInstances', () => {
expect(applyRepeatingInstances(
[
{ start: 20, end: 30 },
{ start: 60, end: 90 },
{ start: 100, end: 110 }
],
100,
{
time: 500,
limitTime: 700,
limitCount: 999
}
)).toEqual([
{ start: 420, end: 430 },
{ start: 460, end: 490 },
{ start: 500, end: 510 },

{ start: 520, end: 530 },
{ start: 560, end: 590 },
{ start: 600, end: 610 },

{ start: 620, end: 630 },
{ start: 660, end: 690 }
// { start: 700, end: 710 },
])

expect(applyRepeatingInstances(
[
{ start: 5, end: 30 },
{ start: 20, end: 90 },
{ start: 100, end: 110 }
],
100,
{
time: 500,
limitTime: 700,
limitCount: 999
}
)).toEqual([
{ start: 405, end: 420 },
{ start: 420, end: 490 },
{ start: 500, end: 505 },

{ start: 505, end: 520 },
{ start: 520, end: 590 },
{ start: 600, end: 605 },

{ start: 605, end: 620 },
{ start: 620, end: 690 }
// { start: 700, end: 705 },
])

})
})
25 changes: 22 additions & 3 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,17 @@ export interface ResolvedTimelineObjects {
}
export interface ResolvedTimelineObject extends TimelineObject {
resolved: {
/** Is set to true when object has been resolved */
resolved: boolean
/** Is set to true while object is resolved (to prevent circular references) */
resolving: boolean
/** Instances of the object on the timeline */
instances: Array<TimelineObjectInstance>
/** Increases the more levels inside of a group the objects is */
levelDeep?: number
/** Id of the parent object */
parentId?: string
/** True if object is a keyframe */
isKeyframe?: boolean
}
}
Expand All @@ -69,7 +76,7 @@ export interface InstanceEvent<T = any> {

export interface TimelineObject {
id: ObjectId
trigger: TimelineTrigger
enable: TimelineEnable

layer: string | number
/** Group children */
Expand All @@ -86,7 +93,18 @@ export interface TimelineObject {
export type Content = {
[key: string]: any
}
export interface TimelineTrigger {
export interface TimelineEnable {
/**
* Examples of references:
* #objectId
* #objectId.start
* #objectId.end
* #objectId.duration
* .className
* .className.start + 5
* $layerName
*/

/** (Optional) The start time of the object. (Cannot be combined with .while) */
start?: Expression
/** (Optional) The end time of the object (Cannot be combined with .while or .duration) */
Expand All @@ -100,12 +118,13 @@ export interface TimelineTrigger {
}
export interface TimelineKeyframe {
id: string
trigger: TimelineTrigger
enable: TimelineEnable
duration?: number | string
classes?: Array<string>
content: Content
disabled?: boolean
}

export interface TimelineObjectKeyframe extends TimelineObject, TimelineKeyframe {
}

Expand Down
Loading

0 comments on commit ce656a9

Please sign in to comment.