-
-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move offset to utils, add tests, test getClass (#318)
- Loading branch information
1 parent
8c8ee36
commit b89f8bc
Showing
6 changed files
with
180 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export function addOffset(...offsets) { | ||
const out = { top: 0, left: 0 }; | ||
|
||
offsets.forEach(({ top, left }) => { | ||
if (typeof top === 'string') { | ||
top = parseFloat(top); | ||
} | ||
if (typeof left === 'string') { | ||
left = parseFloat(left); | ||
} | ||
|
||
out.top += top; | ||
out.left += left; | ||
}); | ||
|
||
return out; | ||
} | ||
|
||
export function offsetToPx(offset, size) { | ||
if (typeof offset.left === 'string' && offset.left.indexOf('%') !== -1) { | ||
offset.left = parseFloat(offset.left) / 100 * size.width; | ||
} | ||
if (typeof offset.top === 'string' && offset.top.indexOf('%') !== -1) { | ||
offset.top = parseFloat(offset.top) / 100 * size.height; | ||
} | ||
|
||
return offset; | ||
} | ||
|
||
export function parseTopLeft(value) { | ||
const [top, left] = value.split(' '); | ||
return { top, left }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { addOffset, offsetToPx, parseTopLeft } from '../../../src/js/utils/offset'; | ||
|
||
describe('Utils - offset', () => { | ||
describe('addOffset', () => { | ||
it('offsets added together', () => { | ||
const offset1 = { top: 20, left: 50 }; | ||
const offset2 = { top: 15, left: 10 }; | ||
expect(addOffset(offset1, offset2)).toStrictEqual({ left: 60, top: 35 }); | ||
}); | ||
}); | ||
|
||
describe('offsetToPx', () => { | ||
it('calculates px from %', () => { | ||
const offset = { left: '30%', top: '20%' }; | ||
const size = { height: 1000, width: 1000 }; | ||
expect(offsetToPx(offset, size)).toStrictEqual({ left: 300, top: 200 }); | ||
}); | ||
}); | ||
|
||
describe('parseTopLeft', () => { | ||
it('splits string to get top/left', () => { | ||
expect(parseTopLeft('50 100')).toStrictEqual({ left: '100', top: '50' }); | ||
}); | ||
}); | ||
}); |