Skip to content

Commit

Permalink
test(postcss-minify-font-values): refactor to make tests more readable
Browse files Browse the repository at this point in the history
Input and output are now directly strings.
  • Loading branch information
ludofischer committed Feb 23, 2024
1 parent 89a5bb1 commit 08212aa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 122 deletions.
6 changes: 1 addition & 5 deletions packages/postcss-minify-font-values/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ function transform(prop, value, opts) {

return tree.toString();
} else if (lowerCasedProp === 'font') {
const tree = valueParser(value);

tree.nodes = minifyFont(tree.nodes, opts);

return tree.toString();
return minifyFont(value, opts);
}

return value;
Expand Down
22 changes: 12 additions & 10 deletions packages/postcss-minify-font-values/src/lib/minify-font.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
const { unit } = require('postcss-value-parser');
const valueParser = require('postcss-value-parser');
const keywords = require('./keywords');
const minifyFamily = require('./minify-family');
const minifyWeight = require('./minify-weight');
Expand Down Expand Up @@ -34,19 +34,20 @@ function normalizeNodes(nodes) {
}

/**
* @param {import('postcss-value-parser').Node[]} nodes
* @param {string} value
* @param {import('../index').Options} opts
* @return {import('postcss-value-parser').Node[]}
* @return {string}
*/
module.exports = function (nodes, opts) {
module.exports = function (value, opts) {
const tree = valueParser(value);
const nodes = tree.nodes;
normalizeNodes(nodes);

let node;

let familyStart = NaN;
let hasSize = false;

for (let i = 0, max = nodes.length; i < max; i += 1) {
node = nodes[i];
const node = nodes[i];

if (node.type === 'word') {
if (hasSize) {
Expand All @@ -61,7 +62,7 @@ module.exports = function (nodes, opts) {
value === 'unset'
) {
familyStart = i;
} else if (keywords.style.has(value) || unit(value)) {
} else if (keywords.style.has(value) || valueParser.unit(value)) {
familyStart = i;
} else if (keywords.variant.has(value)) {
familyStart = i;
Expand All @@ -70,7 +71,7 @@ module.exports = function (nodes, opts) {
familyStart = i;
} else if (keywords.stretch.has(value)) {
familyStart = i;
} else if (keywords.size.has(value) || unit(value)) {
} else if (keywords.size.has(value) || valueParser.unit(value)) {
familyStart = i;
hasSize = true;
}
Expand All @@ -90,5 +91,6 @@ module.exports = function (nodes, opts) {

const family = minifyFamily(nodes.slice(familyStart), opts);

return nodes.slice(0, familyStart).concat(family);
tree.nodes = nodes.slice(0, familyStart).concat(family);
return tree.toString();
};
123 changes: 17 additions & 106 deletions packages/postcss-minify-font-values/test/minify-font.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,134 +6,45 @@ const minifyFont = require('../src/lib/minify-font.js');
test('.8em "Times New Roman", Arial, Helvetica, sans-serif', () => {
assert.equal(
minifyFont(
[
{ type: 'word', value: '.8em' },
{ type: 'space', value: ' ' },
{ type: 'string', quote: '"', value: 'Times New Roman' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: 'Arial' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: 'Helvetica' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: 'sans-serif' },
],
'.8em "Times New Roman", Arial, Helvetica, sans-serif',

{ removeQuotes: true }
),
[
{ type: 'word', value: '.8em' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'Times New Roman,Arial,Helvetica,sans-serif' },
]
'.8em Times New Roman,Arial,Helvetica,sans-serif'
);
});

test('.8em"Times New Roman", Arial, Helvetica, sans-serif', () => {
assert.equal(
minifyFont(
[
{ type: 'word', value: '.8em' },
{ type: 'string', quote: '"', value: 'Times New Roman' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: 'Arial' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: 'Helvetica' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: 'sans-serif' },
],
{ removeQuotes: true }
),
[
{ type: 'word', value: '.8em' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'Times New Roman,Arial,Helvetica,sans-serif' },
]
minifyFont('.8em"Times New Roman", Arial, Helvetica, sans-serif', {
removeQuotes: true,
}),
'.8em Times New Roman,Arial,Helvetica,sans-serif'
);
});

test('ultra-condensed small-caps 1.2em "Fira Sans", sans-serif;', () => {
assert.equal(
minifyFont(
[
{ type: 'word', value: 'ultra-condensed' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'small-caps' },
{ type: 'space', value: ' ' },
{ type: 'word', value: '1.2em' },
{ type: 'space', value: ' ' },
{ type: 'string', quote: '"', value: 'Fira Sans' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: 'sans-serif' },
],
{ removeQuotes: true }
),
[
{ type: 'word', value: 'ultra-condensed' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'small-caps' },
{ type: 'space', value: ' ' },
{ type: 'word', value: '1.2em' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'Fira Sans,sans-serif' },
]
minifyFont('ultra-condensed small-caps 1.2em "Fira Sans", sans-serif;', {
removeQuotes: true,
}),
'ultra-condensed small-caps 1.2em Fira Sans,sans-serif;'
);
});

test('ultra-condensed small-caps 1.2em"Fira Sans", sans-serif;', () => {
assert.equal(
minifyFont(
[
{ type: 'word', value: 'ultra-condensed' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'small-caps' },
{ type: 'space', value: ' ' },
{ type: 'word', value: '1.2em' },
{ type: 'string', quote: '"', value: 'Fira Sans' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: 'sans-serif' },
],
{ removeQuotes: true }
),
[
{ type: 'word', value: 'ultra-condensed' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'small-caps' },
{ type: 'space', value: ' ' },
{ type: 'word', value: '1.2em' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'Fira Sans,sans-serif' },
]
minifyFont('ultra-condensed small-caps 1.2em"Fira Sans", sans-serif;', {
removeQuotes: true,
}),
'ultra-condensed small-caps 1.2em Fira Sans,sans-serif;'
);
});

test('tabs and newlines', () => {
assert.equal(
minifyFont(
[
{ type: 'word', value: 'bold' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'italic' },
{ type: 'space', value: ' \t ' },
{ type: 'word', value: '20px' },
{ type: 'space', value: ' \n ' },
{ type: 'word', value: 'Times' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'New' },
{ type: 'space', value: ' \t ' },
{ type: 'word', value: 'Roman' },
{ type: 'div', value: ',', before: '', after: ' ' },
{ type: 'word', value: 'serif' },
],
{}
),
[
{ type: 'word', value: '700' },
{ type: 'space', value: ' ' },
{ type: 'word', value: 'italic' },
{ type: 'space', value: ' \t ' },
{ type: 'word', value: '20px' },
{ type: 'space', value: ' \n ' },
{ type: 'word', value: 'Times New Roman,serif' },
]
minifyFont('bold italic \t 20px \n Times New\tRoman, serif', {}),
'700 italic \t 20px \n Times New Roman,serif'
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
declare function _exports(nodes: import('postcss-value-parser').Node[], opts: import('../index').Options): import('postcss-value-parser').Node[];
declare function _exports(value: string, opts: import('../index').Options): string;
export = _exports;

0 comments on commit 08212aa

Please sign in to comment.