summaryrefslogtreecommitdiff
path: root/node_modules/postcss-ordered-values/src/lib/getValue.js
blob: 6d94d4e211e524fc72b9e08aab2c4f1073e3147d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict';
const { stringify } = require('postcss-value-parser');

/**
 * @param {import('postcss-value-parser').Node[][]} values
 * @return {string}
 */
module.exports = function getValue(values) {
  return stringify(flatten(values));
};
/**
 * @param {import('postcss-value-parser').Node[][]} values
 * @return {import('postcss-value-parser').Node[]}
 */
function flatten(values) {
  /** @type {import('postcss-value-parser').Node[]} */
  const nodes = [];
  for (const [index, arg] of values.entries()) {
    arg.forEach((val, idx) => {
      if (
        idx === arg.length - 1 &&
        index === values.length - 1 &&
        val.type === 'space'
      ) {
        return;
      }
      nodes.push(val);
    });

    if (index !== values.length - 1) {
      nodes[nodes.length - 1].type = 'div';
      nodes[nodes.length - 1].value = ',';
    }
  }
  return nodes;
}