Skip to content

Commit c447ec2

Browse files
committed
Update pkgs, switch to eslint-config-postcss
1 parent 23829f9 commit c447ec2

File tree

6 files changed

+1809
-2222
lines changed

6 files changed

+1809
-2222
lines changed

index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ const helpers = require('./lib/helpers.js')
99
*/
1010
module.exports = postcss.plugin('easing-gradient', (options = {}) => {
1111
return function (css) {
12-
css.walkRules((rule) => {
13-
rule.walkDecls((decl) => {
12+
css.walkRules(rule => {
13+
rule.walkDecls(decl => {
1414
// If declaration value contains a -gradient.
1515
if (decl.value.includes('-gradient')) {
1616
// Parse the declaration and walk through the nodes - https://github.com/TrySound/postcss-value-parser.
1717
const parsedValue = valueParser(decl.value)
18-
parsedValue.walk((node) => {
18+
parsedValue.walk(node => {
1919
// Only modify gradient as the value can contain more e.g. 'linear-gradient(black, pink) center'.
2020
if (node.value === 'linear-gradient' || node.value === 'radial-gradient') {
2121
// Get a sensible array of gradient parameters where e.g. a function is split into multiple array items
@@ -34,9 +34,9 @@ module.exports = postcss.plugin('easing-gradient', (options = {}) => {
3434
node.type = 'word'
3535
// Assume if it has 4 params it's because the first one is the direction
3636
if (gradientParams.length === 4) {
37-
node.value = `${node.value}(${gradientParams[0]}, ${colorStops.join(', ')})`
37+
node.value = `${ node.value }(${ gradientParams[0] }, ${ colorStops.join(', ') })`
3838
} else {
39-
node.value = `${node.value}(${colorStops.join(', ')})`
39+
node.value = `${ node.value }(${ colorStops.join(', ') })`
4040
}
4141
// Update our declaration value
4242
decl.value = parsedValue.toString()

index.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ const postcss = require('postcss')
22
const plugin = require('./')
33

44
function run (input, output, opts) {
5-
return postcss([ plugin(opts) ]).process(input, {from: null})
5+
return postcss([plugin(opts)]).process(input, { from: null })
66
.then(result => {
77
expect(result.css).toEqual(output)
8-
expect(result.warnings().length).toBe(0)
8+
expect(result.warnings()).toHaveLength(0)
99
})
1010
}
1111

lib/colorStops.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@ const helpers = require('./helpers.js')
33

44
/**
55
* Calculate the color stops based on start+stopColor in an array and easingType
6+
* @param {array} colors Two colors in an array
7+
* @param {array} coordinates An array of coordinates (object with x and y keys)
8+
* @param {number} alphaDecimals How many decimals should be on the returned color values
9+
* @param {string} colorMode Color space used for color interpolation http://gka.github.io/chroma.js/#chroma-mix
10+
* @returns {array} An array of colorstops (a string with color and position)
611
*/
712
module.exports = (colors, coordinates, alphaDecimals = 5, colorMode = 'lrgb') => {
8-
let colorStops = []
13+
const colorStops = []
914
colors = helpers.transparentFix(colors)
1015
coordinates.forEach(coordinate => {
1116
const ammount = coordinate.y
1217
const percent = coordinate.x * 100
1318
let color = chroma.mix(colors[0], colors[1], ammount, colorMode).css('hsl')
1419
color = helpers.roundHslAlpha(color, alphaDecimals)
1520
if (Number(coordinate.x) !== 0 && Number(coordinate.x) !== 1) {
16-
colorStops.push(`${color} ${+percent.toFixed(2)}%`)
21+
colorStops.push(`${ color } ${ +percent.toFixed(2) }%`)
1722
} else {
1823
colorStops.push(color)
1924
}

lib/helpers.js

+37-21
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,50 @@ const easeShorthands = ['ease', 'ease-in', 'ease-out', 'ease-in-out']
77
const timingFunctions = [...easeShorthands, cubicFunction, stepsFunction]
88

99
/**
10-
* Get before a parenthesis
10+
* Test if a string has a parenthesis
11+
* @param {String} str A text string
12+
* @returns {Boolean} If a string has a "("
1113
*/
12-
const hasParenthesis = (str) => str.indexOf('(') !== -1
13-
const getBeforeParenthesisMaybe = (str) => hasParenthesis(str) ? str.substring(0, str.indexOf('(')) : str
14+
const hasParenthesis = str => str.indexOf('(') !== -1
1415

1516
/**
16-
* Test if a string matches a css timing function
17-
*/
18-
exports.isTimingFunction = (str) => timingFunctions.includes(getBeforeParenthesisMaybe(str))
17+
* Get substring before first parenthesis in a string
18+
* @param {String} str A text string
19+
* @returns {String} The substring if the provided string has a parenthesis. Otherwise the string.
20+
*/
21+
const getBeforeParenthesisMaybe = str => hasParenthesis(str) ? str.substring(0, str.indexOf('(')) : str
22+
23+
/**
24+
* Test if a string matches a css timing function
25+
* @param {String} str A text string
26+
* @returns {Boolean} If the string is a timing function
27+
*/
28+
exports.isTimingFunction = str => timingFunctions.includes(getBeforeParenthesisMaybe(str))
1929

2030
/**
2131
* Get insides of a parenthesis
22-
* Also used in this file so declared first and exported after
32+
* @param {String} str A text string
33+
* @returns {String} The substring within the parenthesis
34+
* Note: It's also used in this file so declared first and exported after
2335
*/
24-
const getParenthesisInsides = (str) => str.match(/\((.*)\)/).pop()
36+
const getParenthesisInsides = str => str.match(/\((.*)\)/).pop()
2537
exports.getParenthesisInsides = getParenthesisInsides
2638

2739
/**
2840
* If a color is transparent then convert it to a hsl-transparent of the paired color
41+
* @param {Array} colors An array with two colors
42+
* @returns {Object} A color as chroma object
2943
*/
30-
exports.transparentFix = (colors) => colors.map((color, i) => {
31-
return color === 'transparent'
32-
? chroma(colors[Math.abs(i - 1)]).alpha(0).css('hsl')
33-
: color
44+
exports.transparentFix = colors => colors.map((color, i) => {
45+
return color === 'transparent' ? chroma(colors[Math.abs(i - 1)]).alpha(0).css('hsl') : color
3446
})
3547

3648
/**
3749
* Change value to ';' on child objects of type 'div'
50+
* @param {Array.<Object>} obj An array of objects
51+
* @returns {Array.<Object>} An array of objects
3852
*/
39-
exports.divToSemiColon = (obj) => {
53+
exports.divToSemiColon = obj => {
4054
obj.map(childObj => {
4155
if (childObj.type === 'div') {
4256
childObj.value = ';'
@@ -48,24 +62,26 @@ exports.divToSemiColon = (obj) => {
4862

4963
/**
5064
* Round alpha in hsl colors to alphaDecimals
65+
* @param {String} color As an hsl value
66+
* @param {Number} alphaDecimals An integer specifying max number of deicamals
67+
* @returns {String} A alpha value rounded version of the hsl color string
5168
*/
5269
exports.roundHslAlpha = (color, alphaDecimals) => {
5370
const prefix = getBeforeParenthesisMaybe(color)
5471
const values = getParenthesisInsides(color)
5572
.split(',')
56-
.map(string => string.indexOf('%') === -1
57-
? +Number(string).toFixed(alphaDecimals)
58-
: string.trim()
59-
)
60-
color = `${prefix}(${values.join(', ')})`
73+
.map(string => string.indexOf('%') === -1 ? +Number(string).toFixed(alphaDecimals) : string.trim())
74+
color = `${ prefix }(${ values.join(', ') })`
6175
return color
6276
}
6377

6478
/**
65-
* Return an error message
79+
* Wrap a string telling the user we couldn't parse it
80+
* @param {String} input A string
81+
* @returns {String} The full error message wrapped around the string
6682
*/
67-
exports.errorMsg = (input) => {
83+
exports.errorMsg = input => {
6884
return `Couldn't parse:
69-
${input}
85+
${ input }
7086
Check the syntax to see if it's correct/supported.`
7187
}

0 commit comments

Comments
 (0)