Skip to content

Commit

Permalink
feat: continued implementation, validation,
Browse files Browse the repository at this point in the history
  • Loading branch information
nytamin committed Jan 25, 2019
1 parent ce656a9 commit f95b4ce
Show file tree
Hide file tree
Showing 10 changed files with 1,823 additions and 876 deletions.
1,947 changes: 1,156 additions & 791 deletions src/__tests__/legacy.spec.ts

Large diffs are not rendered by default.

84 changes: 80 additions & 4 deletions src/__tests__/lib.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
cleanInstances,
invertInstances,
operateOnArrays,
applyRepeatingInstances
applyRepeatingInstances,
capInstances,
operateOnArraysMulti
} from '../lib'

describe('lib', () => {
Expand Down Expand Up @@ -36,13 +38,13 @@ describe('lib', () => {
{ time: 1, value: true }
])).toEqual([
{ time: 1, value: true },
{ time: 2, value: true },
{ time: 2, value: false },
{ time: 2, value: true },
{ time: 3, value: true },
{ time: 20, value: true },
{ time: 20, value: false },
{ time: 100, value: true },
{ time: 20, value: true },
{ time: 100, value: false },
{ time: 100, value: true },
{ time: 300, value: true }
])
})
Expand All @@ -67,6 +69,22 @@ describe('lib', () => {
], true)).toEqual([
{ start: 10, end: 70 }
])

expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 50, end: 70 }
], true, true)).toEqual([ // allow zero-width gaps
{ start: 10, end: 50 },
{ start: 50, end: 70 }
])

expect(cleanInstances([
{ start: 10, end: 60 },
{ start: 50, end: 70 }
], true, true)).toEqual([ // allow zero-width gaps
{ start: 10, end: 70 }
])

expect(cleanInstances([
{ start: 10, end: 50 },
{ start: 50, end: null }
Expand Down Expand Up @@ -130,6 +148,7 @@ describe('lib', () => {
])
})
test('invertInstances', () => {

expect(invertInstances([
{ start: 10, end: 50 },
{ start: 100, end: 110 }
Expand All @@ -154,6 +173,16 @@ describe('lib', () => {
{ start: 100, end: 110 }
])).toEqual([
{ start: 0, end: 10, isFirst: true },
{ start: 100, end: 100 },
{ start: 110, end: null }
])

expect(invertInstances([
{ start: 10, end: 50 },
{ start: 50, end: 110 }
])).toEqual([
{ start: 0, end: 10, isFirst: true },
{ start: 50, end: 50 }, // zero-width gap
{ start: 110, end: null }
])
})
Expand Down Expand Up @@ -195,6 +224,33 @@ describe('lib', () => {
{ start: 101, end: 115 }
])

})
test('operateOnArraysMulti', () => {
const plus = (a: number | null, b: number | null): number | null => {
if (a === null || b === null) return null
return a + b
}

expect(operateOnArraysMulti(
[
{ start: 1, end: 3 },
{ start: 5, end: 7 }
],
[
{ start: 10, end: 20 },
{ start: 50, end: 60 },
{ start: 60, end: 70 }
],
plus
)).toEqual([
{ start: 11, end: 13 },
{ start: 15, end: 17 },
{ start: 51, end: 53 },
{ start: 55, end: 57 },
{ start: 61, end: 63 },
{ start: 65, end: 67 }
])

})
test('applyRepeatingInstances', () => {
expect(applyRepeatingInstances(
Expand Down Expand Up @@ -250,4 +306,24 @@ describe('lib', () => {
])

})
test('capInstances', () => {

expect(capInstances([
{ start: 10, end: 20 },
{ start: 30, end: 40 },
{ start: 50, end: 60 },
{ start: 70, end: 80 },
{ start: 90, end: 100 }
], [
{ start: 25, end: 55 },
{ start: 60, end: 65 },
{ start: 75, end: 95 }
])).toEqual([
{ start: 30, end: 40 },
{ start: 50, end: 55 }, // capped
// { start: 60, end: 60 }, // ?
{ start: 75, end: 80 }, // capped
{ start: 90, end: 95 } // capped
])
})
})
1 change: 1 addition & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface ResolvedTimeline {
objects: ResolvedTimelineObjects
/** Map of all classes on timeline, maps className to object ids */
classes: {[className: string]: Array<string>}
layers: {[layer: string]: Array<string>}
statistics: {
/** Number of objects that were unable to resolve */
unresolvedCount: number
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export * from './api/enums'
export { Resolver } from './resolver/resolver'
export {
Resolver
} from './resolver/resolver'
export {
validateTimeline,
validateObject,
validateKeyframe
} from './resolver/validate'

export { Resolver as LegacyResolver } from './resolver/legacy'
86 changes: 77 additions & 9 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ export function sortEvents<T extends InstanceEvent> (events: Array<T>): Array<T>
if (a.time > b.time) return 1
if (a.time < b.time) return -1

if (a.value && !b.value) return -1
if (!a.value && b.value) return 1
if (a.value && !b.value) return 1
if (!a.value && b.value) return -1
return 0
})
}
/**
* Clean up instances, join overlapping etc..
* @param instances
*/
export function cleanInstances (instances: Array<TimelineObjectInstance>, allowMerge: boolean): Array<TimelineObjectInstance> {
export function cleanInstances (
instances: Array<TimelineObjectInstance>,
allowMerge: boolean,
allowZeroGaps: boolean = false
): Array<TimelineObjectInstance> {

if (allowMerge) {
const events: Array<InstanceEvent<{id: string, value: boolean}>> = []
Expand Down Expand Up @@ -83,21 +87,26 @@ export function cleanInstances (instances: Array<TimelineObjectInstance>, allowM
}
const lastInstance = _.last(returnInstances)
if (_.keys(activeInstances).length) {
// Instance is active
if (
!allowZeroGaps &&
lastInstance &&
lastInstance.end === event.time
) {
// resume previous instance:
lastInstance.end = null
} else if (
!lastInstance ||
lastInstance.end !== null
) {
// Start a new instance:
returnInstances.push({
start: event.time,
end: null
})
}
} else {
// No instances are active
if (lastInstance) {
lastInstance.end = event.time
}
Expand Down Expand Up @@ -146,10 +155,12 @@ export function cleanInstances (instances: Array<TimelineObjectInstance>, allowM
}

}
export function invertInstances (instances: Array<TimelineObjectInstance>): Array<TimelineObjectInstance> {
export function invertInstances (
instances: Array<TimelineObjectInstance>
): Array<TimelineObjectInstance> {

if (instances.length) {
instances = cleanInstances(instances, true)
instances = cleanInstances(instances, true, true)
const invertedInstances: Array<TimelineObjectInstance> = []
if (instances[0].start !== 0) {
invertedInstances.push({
Expand All @@ -170,10 +181,7 @@ export function invertInstances (instances: Array<TimelineObjectInstance>): Arra
})
}
})
return _.compact(_.map(invertedInstances, (instance) => {
if (instance.end === instance.start) return null
return instance
}))
return invertedInstances
} else {
return [{
isFirst: true,
Expand All @@ -182,6 +190,12 @@ export function invertInstances (instances: Array<TimelineObjectInstance>): Arra
}]
}
}
/**
* Perform an action on 2 arrays. Behaves somewhat like the ".*"-operator in Matlab
* @param array0
* @param array1
* @param operate
*/
export function operateOnArrays (
array0: Array<TimelineObjectInstance> | number | null,
array1: Array<TimelineObjectInstance> | number | null,
Expand Down Expand Up @@ -239,6 +253,41 @@ export function operateOnArrays (
}
return cleanInstances(result, false)
}
/**
* Like operateOnArrays, but will multiply the number of elements in array0, with the number of elements in array1
* @param array0
* @param array1
* @param operate
*/
export function operateOnArraysMulti (
array0: Array<TimelineObjectInstance> | number | null,
array1: Array<TimelineObjectInstance> | number | null,
operate: (a: number | null, b: number | null) => number | null
) {
if (array0 === null) return null

if (_.isArray(array1)) {
let resultArray: Array<TimelineObjectInstance> = []
_.each(array1, (array1Val) => {
const result = operateOnArrays(array0, array1Val.start, operate)
if (_.isArray(result)) {
resultArray = resultArray.concat(result)
} else if (result !== null) {
resultArray.push({
start: result,
end: (
array1Val.end !== null ?
result + (array1Val.end - array1Val.start) :
null
)
})
}
})
return resultArray
} else {
return operateOnArrays(array0, array1, operate)
}
}
export function applyRepeatingInstances (
instances: number | TimelineObjectInstance[] | null,
repeatTime0: number | null,
Expand Down Expand Up @@ -315,6 +364,21 @@ export function capInstances (instances: TimelineObjectInstance[], parentInstanc
}
}
})
if (!parent) {
_.each(parentInstances, (p) => {
if (
(instance.end || Infinity) > p.start &&
(instance.end || Infinity) <= (p.end || Infinity)
) {
if (
parent === null ||
(p.end || Infinity) < (parent.end || Infinity)
) {
parent = p
}
}
})
}
if (parent) {
const i2 = _.clone(instance)
if (
Expand All @@ -323,6 +387,10 @@ export function capInstances (instances: TimelineObjectInstance[], parentInstanc
) {
i2.end = parent.end
}
if ((i2.start || Infinity) < parent.start) {
i2.start = parent.start
}

returnInstances.push(i2)
}
})
Expand Down
Loading

0 comments on commit f95b4ce

Please sign in to comment.