-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove ramda from stylesheet package (#1829)
- Loading branch information
Showing
12 changed files
with
80 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@react-pdf/layout': patch | ||
'@react-pdf/stylesheet': major | ||
--- | ||
|
||
refactor: remove ramda from stylesheet package |
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,11 @@ | ||
/** | ||
* Casts value to array | ||
* | ||
* @param {any} value | ||
* @returns {Array} casted value | ||
*/ | ||
const castArray = value => { | ||
return Array.isArray(value) ? value : [value]; | ||
}; | ||
|
||
export default castArray; |
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,22 @@ | ||
/* eslint-disable no-await-in-loop */ | ||
|
||
import reverse from './reverse'; | ||
|
||
/** | ||
* Performs right-to-left function composition | ||
* | ||
* @param {...any} functions | ||
*/ | ||
const compose = (...fns) => (value, ...args) => { | ||
let result = value; | ||
const reversedFns = reverse(fns); | ||
|
||
for (let i = 0; i < reversedFns.length; i += 1) { | ||
const fn = reversedFns[i]; | ||
result = fn(result, ...args); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export default compose; |
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,3 @@ | ||
const reverse = list => Array.prototype.slice.call(list, 0).reverse(); | ||
|
||
export default reverse; |
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
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 |
---|---|---|
@@ -1,46 +1,44 @@ | ||
import * as R from 'ramda'; | ||
import hlsToHex from 'hsl-to-hex'; | ||
import colorString from 'color-string'; | ||
|
||
const isRgb = R.test(/rgb/g); | ||
const isRgba = R.test(/rgba/g); | ||
const isHsl = R.test(/hsl/g); | ||
const isHsla = R.test(/hsla/g); | ||
const isRgb = value => /rgba?/g.test(value); | ||
const isHsl = value => /hsla?/g.test(value); | ||
|
||
/** | ||
* Transform rgb color to hexa | ||
* | ||
* @param {String} styles value | ||
* @returns {Object} transformed value | ||
*/ | ||
const parseRgb = R.compose(colorString.to.hex, colorString.get.rgb); | ||
const parseRgb = value => { | ||
const rgb = colorString.get.rgb(value); | ||
return colorString.to.hex(rgb); | ||
}; | ||
|
||
/** | ||
* Transform Hsl color to hexa | ||
* | ||
* @param {String} styles value | ||
* @returns {Object} transformed value | ||
*/ | ||
const parseHsl = R.compose( | ||
R.toUpper, | ||
R.apply(hlsToHex), | ||
R.map(Math.round), | ||
colorString.get.hsl, | ||
); | ||
const parseHsl = value => { | ||
const hsl = colorString.get.hsl(value).map(Math.round); | ||
const hex = hlsToHex(...hsl); | ||
|
||
return hex.toUpperCase(); | ||
}; | ||
|
||
/** | ||
* Transform given color to hexa | ||
* | ||
* @param {String} styles value | ||
* @returns {Object} transformed value | ||
*/ | ||
const transformColor = value => | ||
R.cond([ | ||
[isRgba, parseRgb], | ||
[isRgb, parseRgb], | ||
[isHsla, parseHsl], | ||
[isHsl, parseHsl], | ||
[R.T, R.always(value)], | ||
])(value); | ||
const transformColor = value => { | ||
if (isRgb(value)) return parseRgb(value); | ||
if (isHsl(value)) return parseHsl(value); | ||
|
||
return value; | ||
}; | ||
|
||
export default transformColor; |
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