Skip to content
This repository has been archived by the owner on Jun 23, 2023. It is now read-only.

Commit

Permalink
fix interpolation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dalisoft committed May 17, 2019
1 parent 426d1e9 commit 7cfb393
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "es6-tween",
"version": "5.5.7",
"version": "5.5.8",
"description": "ES6 implementation of amazing tween.js",
"browser": "bundled/Tween.min.js",
"cdn": "bundled/Tween.min.js",
Expand Down
11 changes: 6 additions & 5 deletions src/Tween.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ class Tween {
) {
continue
}
_valuesStart[property] = start
if (Array.isArray(end)) {
if (!Array.isArray(start)) {
end.unshift(start)
Expand All @@ -437,14 +438,16 @@ class Tween {
} else {
if (end.isString && object[property].isString && !start.isString) {
start.isString = true
} else {
decompose(property, object, _valuesStart, _valuesEnd)
}
}
} else {
decompose(property, object, _valuesStart, _valuesEnd)
}
_valuesStart[property] = start
if (typeof start === 'number' && typeof end === 'string' && end[1] === '=') {
continue
}
decompose(property, object, _valuesStart, _valuesEnd)
}

if (Tween.Renderer && this.node && Tween.Renderer.init) {
Expand Down Expand Up @@ -716,16 +719,14 @@ class Tween {

if (typeof end === 'number') {
object[property] = start + (end - start) * value
} else if (Array.isArray(end) && !end.isString && (!Array.isArray(start) || start.isString)) {
} else if (Array.isArray(end) && !end.isString && !Array.isArray(start)) {
object[property] = _interpolationFunctionCall(end, value, object[property])
} else if (end && end.update) {
end.update(value)
} else if (typeof end === 'function') {
object[property] = end(value)
} else if (typeof end === 'string' && typeof start === 'number') {
object[property] = start + parseFloat(end[0] + end.substr(2)) * value
} else if (start && end && start.splice && end.splice && start.isString && end.isString) {
object[property] = recompose(property, object, _valuesStart, _valuesEnd, value, elapsed)
} else {
recompose(property, object, _valuesStart, _valuesEnd, value, elapsed)
}
Expand Down
31 changes: 27 additions & 4 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ const hex2rgb = (all, hex) => {
}

export function decomposeString (fromValue) {
if (typeof fromValue !== 'string' || (fromValue && fromValue.splice && fromValue.isString)) {
if (fromValue && fromValue.splice && fromValue.isString) {
return fromValue
}
if (typeof fromValue !== 'string') {
return fromValue
}
if (fromValue.charAt(1) === '=') {
return fromValue
}
const value = fromValue
Expand All @@ -71,10 +77,24 @@ export function decomposeString (fromValue) {
}

// Decompose value, now for only `string` that required
export function decompose (prop, obj, from, to, stringBuffer) {
export function decompose (prop, obj, from, to) {
const fromValue = from[prop]
const toValue = to[prop]

if (fromValue === toValue) {
return true
} else if (Array.isArray(fromValue) && Array.isArray(toValue) && fromValue.length === toValue.length) {
for (let i = 0, len = toValue.length; i < len; i++) {
const a = fromValue[i]
const b = toValue[i]

if (a === b || (typeof a === 'number' && typeof b === 'number')) {
continue
} else {
decompose(i, obj[prop], fromValue, toValue)
}
}
}
if (typeof fromValue === 'number' && typeof toValue === 'number') {
//
} else if (fromValue && fromValue.splice && fromValue.isString && toValue && toValue.splice && toValue.isString) {
Expand All @@ -89,6 +109,9 @@ export function decompose (prop, obj, from, to, stringBuffer) {
let fromValue1 = Array.isArray(fromValue) && fromValue[0] === STRING_PROP ? fromValue : decomposeString(fromValue)
let toValue1 = Array.isArray(toValue) && toValue[0] === STRING_PROP ? toValue : decomposeString(toValue)

if (fromValue1 === undefined) {
return
}
let i = 1
while (i < fromValue1.length) {
if (fromValue1[i] === toValue1[i] && typeof fromValue1[i - 1] === 'string') {
Expand Down Expand Up @@ -132,7 +155,7 @@ export const isRGBColor = (v, i, r = RGB) =>
typeof v[i] === 'number' && (v[i - 1] === r || v[i - 3] === r || v[i - 5] === r)
export function recompose (prop, obj, from, to, t, originalT, stringBuffer) {
const fromValue = stringBuffer ? from : from[prop]
const toValue = stringBuffer ? to : to[prop]
let toValue = stringBuffer ? to : to[prop]
if (toValue === undefined) {
return fromValue
}
Expand Down Expand Up @@ -177,7 +200,7 @@ export function recompose (prop, obj, from, to, t, originalT, stringBuffer) {
return STRING_BUFFER
} else if (Array.isArray(fromValue) && fromValue[0] !== STRING_PROP) {
for (let i = 0, len = fromValue.length; i < len; i++) {
if (fromValue[i] === toValue[i]) {
if (fromValue[i] === toValue[i] || typeof obj[prop] === 'string') {
continue
}
recompose(i, obj[prop], fromValue, toValue, t, originalT)
Expand Down
8 changes: 3 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ test('Events', (t) => {

test('Value Interpolation', (t) => {
const m = ['rgb(', 0, ', 204, ', 255, ')']
m.isString = true

let obj = {
a: 0,
Expand All @@ -75,7 +74,6 @@ test('Value Interpolation', (t) => {
})

const m2 = ['rgb(', 255, ', 204, ', 0, ')']
m2.isString = true
new Tween(obj)
.to(
{
Expand All @@ -89,7 +87,7 @@ test('Value Interpolation', (t) => {
j: [1, 2],
k: ['rgb(100, 100, 100)', 'rgb(200, 200, 200)'],
l: '#fc0',
m: 'rgb(255, 204, 0)'
m: m2
},
100
)
Expand All @@ -107,7 +105,7 @@ test('Value Interpolation', (t) => {
t.is(obj.j, 0)
t.is(obj.k, 'rgb(0, 0, 0)')
t.is(obj.l, 'rgb(0, 204, 255)')
t.is(obj.m, 'rgb(0, 204, 255)')
t.deepEqual(obj.m, ['rgb(', 0, ', 204, ', 255, ')'])

update(50)

Expand Down Expand Up @@ -150,7 +148,7 @@ test('Value Interpolation', (t) => {
t.is(obj.j, 2, 'Multi-Interpolation not worked as excepted')
t.is(obj.k, 'rgb(200, 200, 200)', 'Multi-Interpolation not worked as excepted')
t.is(obj.l, 'rgb(255, 204, 0)', 'String interpolation not worked as excepted')
t.is(obj.m, 'rgb(255, 204, 0)', 'Array as string interpolation not worked as excepted')
t.deepEqual(obj.m, m2, 'Array interpolation not worked as excepted')
})

test('Value Array-based Interpolation', (t) => {
Expand Down
16 changes: 13 additions & 3 deletions withPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import puppeteer from 'puppeteer'
const fs = require('fs')
const path = require('path')

// Parse
function describe (jsHandle) {
return jsHandle.executionContext().evaluate((obj) => {
// serialize |obj| however you want
return JSON.stringify(obj)
}, jsHandle)
}

export default (t, run) => {
let _browser
return puppeteer
Expand All @@ -12,11 +20,13 @@ export default (t, run) => {
return browser
.newPage()
.then((page) => {
page.on('console', async (msg) => {
const args = await Promise.all(msg.args().map((arg) => describe(arg)))
console.log('Logs from Headless Chrome', ...args)
})
return page.evaluate(fs.readFileSync(path.join(__dirname, 'bundled/Tween.js'), 'utf8')).then(() => page)
})
.then((page) => {
return run(t, page).then(() => page)
})
.then((page) => run(t, page).then(() => page))
})
.then((page) => {
_browser.close()
Expand Down

0 comments on commit 7cfb393

Please sign in to comment.