Skip to content

Commit 8dbfed9

Browse files
committed
move utils to individual files; export from index
1 parent ab5dec3 commit 8dbfed9

File tree

4 files changed

+52
-50
lines changed

4 files changed

+52
-50
lines changed

src/utils/clamp.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default function clamp(number, lower, upper) {
2+
number = number <= upper ? number : upper;
3+
number = number >= lower ? number : lower;
4+
return number;
5+
}

src/utils/index.js

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
// normalize
2-
export function scaleBetween(unscaledNum, minAllowed, maxAllowed, min, max) {
3-
return (maxAllowed - minAllowed) * (unscaledNum - min) / (max - min) + minAllowed;
4-
}
5-
6-
export function clamp(number, lower, upper) {
7-
number = number <= upper ? number : upper;
8-
number = number >= lower ? number : lower;
9-
return number;
10-
}
1+
export clamp from './clamp';
2+
export getParallaxOffsets from './getParallaxOffsets';
3+
export parseValueAndUnit from './parseValueAndUnit';
4+
export scaleBetween from './scaleBetween';
115

126
export function testForPassiveScroll() {
137
let supportsPassiveOption = false;
@@ -23,43 +17,3 @@ export function testForPassiveScroll() {
2317
return supportsPassiveOption;
2418
}
2519

26-
/**
27-
* Determines the unit of a string and parses the value
28-
*
29-
* @param {string} value
30-
* @return {object} The parsed value and the unit if any
31-
*/
32-
export function parseValueAndUnit(value) {
33-
const isBool = typeof value === 'boolean';
34-
const isObject = typeof value === 'object';
35-
const isString = typeof value === 'string';
36-
const isNumb = typeof value === 'number';
37-
38-
if (isBool || isObject) {
39-
throw new Error('Ivalid value provided. Must provide a value as a string with % or px units.');
40-
}
41-
42-
if (isNumb) {
43-
return {
44-
value,
45-
unit: null,
46-
};
47-
} else if (isString && value.slice(-1) === '%') {
48-
// remove % then parse
49-
value = parseInt(value.slice(0, -1), 10);
50-
51-
return {
52-
value,
53-
unit: '%',
54-
};
55-
} else if (isString && value.slice(-2) === 'px') {
56-
// remove px then parse
57-
value = parseInt(value.slice(0, -2), 10);
58-
59-
return {
60-
value,
61-
unit: 'px',
62-
};
63-
}
64-
65-
}

src/utils/parseValueAndUnit.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Determines the unit of a string and parses the value
3+
*
4+
* @param {string} value
5+
* @return {object} The parsed value and the unit if any
6+
*/
7+
export default function parseValueAndUnit(value) {
8+
const isBool = typeof value === 'boolean';
9+
const isObject = typeof value === 'object';
10+
const isString = typeof value === 'string';
11+
const isNumb = typeof value === 'number';
12+
13+
if (isBool || isObject) {
14+
throw new Error('Ivalid value provided. Must provide a value as a string with % or px units.');
15+
}
16+
17+
if (isNumb) {
18+
return {
19+
value,
20+
unit: '%', // defaults to percent if not unit is passed
21+
};
22+
} else if (isString && value.slice(-1) === '%') {
23+
// remove % then parse
24+
value = parseInt(value.slice(0, -1), 10);
25+
26+
return {
27+
value,
28+
unit: '%',
29+
};
30+
} else if (isString && value.slice(-2) === 'px') {
31+
// remove px then parse
32+
value = parseInt(value.slice(0, -2), 10);
33+
34+
return {
35+
value,
36+
unit: 'px',
37+
};
38+
}
39+
}

src/utils/scaleBetween.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Scale between AKA normalize
2+
export default function scaleBetween(value, newMin, newMax, oldMin, oldMax) {
3+
return (newMax - newMin) * (value - oldMin) / (oldMax - oldMin) + newMin;
4+
}

0 commit comments

Comments
 (0)