Skip to content

Commit

Permalink
Update regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Dec 27, 2024
1 parent 12c7bda commit b67cf05
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
24 changes: 17 additions & 7 deletions lib/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ exports.TYPES = {
KEYWORD: 9,
NULL_OR_EMPTY_STR: 10,
CALC: 11,
VAR: 12,
};

// rough regular expressions
var integerRegEx = /^[-+]?[0-9]+$/;
var numberRegEx = /^[-+]?[0-9]*\.?[0-9]+$/;
var lengthRegEx = /^(0|[-+]?[0-9]*\.?[0-9]+(in|cm|em|mm|pt|pc|px|ex|rem|vh|vw|ch))$/;
var percentRegEx = /^[-+]?[0-9]*\.?[0-9]+%$/;
// regular expressions
var DIGIT = '(?:0|[1-9]\\d*)';
var NUMBER = `[+-]?(?:${DIGIT}(?:\\.\\d*)?|\\.\\d+)(?:e-?${DIGIT})?`;
var integerRegEx = new RegExp(`^[+-]?${DIGIT}$`);
var numberRegEx = new RegExp(`^${NUMBER}$`);
var lengthRegEx = new RegExp(`^${NUMBER}(?:[cm]m|[dls]?v(?:[bhiw]|max|min)|in|p[ctx]|q|r?(?:[cl]h|cap|e[mx]|ic))$`);

Check failure on line 29 in lib/parsers.js

View workflow job for this annotation

GitHub Actions / Lint and tests (18)

Replace ``^${NUMBER}(?:[cm]m|[dls]?v(?:[bhiw]|max|min)|in|p[ctx]|q|r?(?:[cl]h|cap|e[mx]|ic))$`` with `⏎··`^${NUMBER}(?:[cm]m|[dls]?v(?:[bhiw]|max|min)|in|p[ctx]|q|r?(?:[cl]h|cap|e[mx]|ic))$`⏎`

Check failure on line 29 in lib/parsers.js

View workflow job for this annotation

GitHub Actions / Lint and tests (20)

Replace ``^${NUMBER}(?:[cm]m|[dls]?v(?:[bhiw]|max|min)|in|p[ctx]|q|r?(?:[cl]h|cap|e[mx]|ic))$`` with `⏎··`^${NUMBER}(?:[cm]m|[dls]?v(?:[bhiw]|max|min)|in|p[ctx]|q|r?(?:[cl]h|cap|e[mx]|ic))$`⏎`

Check failure on line 29 in lib/parsers.js

View workflow job for this annotation

GitHub Actions / Lint and tests (latest)

Replace ``^${NUMBER}(?:[cm]m|[dls]?v(?:[bhiw]|max|min)|in|p[ctx]|q|r?(?:[cl]h|cap|e[mx]|ic))$`` with `⏎··`^${NUMBER}(?:[cm]m|[dls]?v(?:[bhiw]|max|min)|in|p[ctx]|q|r?(?:[cl]h|cap|e[mx]|ic))$`⏎`
var percentRegEx = new RegExp(`^${NUMBER}%$`);
var angleRegEx = new RegExp(`^${NUMBER}(?:deg|g?rad|turn)$`);
var urlRegEx = /^url\(\s*([^)]*)\s*\)$/;
var stringRegEx = /^("[^"]*"|'[^']*')$/;
var calcRegEx = /^(?:abs|calc|sign)\(/;
var angleRegEx = /^([-+]?[0-9]*\.?[0-9]+)(deg|grad|rad)$/;
var varRegEx = /^var\(|(?<=[*/\s(])var\(/;
var calcRegEx = /^(?:a?(?:cos|sin|tan)|abs|atan2|calc|clamp|exp|hypot|log|max|min|mod|pow|rem|round|sign|sqrt)\(/;

Check failure on line 35 in lib/parsers.js

View workflow job for this annotation

GitHub Actions / Lint and tests (18)

Insert `⏎·`

Check failure on line 35 in lib/parsers.js

View workflow job for this annotation

GitHub Actions / Lint and tests (20)

Insert `⏎·`

Check failure on line 35 in lib/parsers.js

View workflow job for this annotation

GitHub Actions / Lint and tests (latest)

Insert `⏎·`

// This will return one of the above types based on the passed in string
exports.valueType = function valueType(val) {
Expand Down Expand Up @@ -56,6 +60,9 @@ exports.valueType = function valueType(val) {
if (urlRegEx.test(val)) {
return exports.TYPES.URL;
}
if (varRegEx.test(val)) {
return exports.TYPES.VAR;
}
if (calcRegEx.test(val)) {
return exports.TYPES.CALC;
}
Expand Down Expand Up @@ -158,6 +165,9 @@ exports.parsePercent = function parsePercent(val) {
// either a length or a percent
exports.parseMeasurement = function parseMeasurement(val) {
var type = exports.valueType(val);
if (type === exports.TYPES.VAR) {
return val;
}
if (type === exports.TYPES.CALC) {
return cssCalc(val, {
format: 'specifiedValue',
Expand Down
71 changes: 53 additions & 18 deletions test/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,46 +68,46 @@ describe('valueType', () => {
assert.strictEqual(output, parsers.TYPES.LENGTH);
});

it('returns calc from calc(100px * 2)', () => {
let input = 'calc(100px * 2)';
it('returns var from calc(100px * var(--foo))', () => {
let input = 'calc(100px * var(--foo))';
let output = parsers.valueType(input);

assert.strictEqual(output, parsers.TYPES.CALC);
assert.strictEqual(output, parsers.TYPES.VAR);
});

it('returns calc from calc(100px * calc(2 * 1))', () => {
let input = 'calc(100px * calc(2 * 1))';
it('returns var from var(--foo)', () => {
let input = 'var(--foo)';
let output = parsers.valueType(input);

assert.strictEqual(output, parsers.TYPES.CALC);
assert.strictEqual(output, parsers.TYPES.VAR);
});

it('returns calc from calc(100px * var(--foo))', () => {
let input = 'calc(100px * var(--foo))';
it('returns var from var(--foo, var(--bar))', () => {
let input = 'var(--foo, var(--bar))';
let output = parsers.valueType(input);

assert.strictEqual(output, parsers.TYPES.CALC);
assert.strictEqual(output, parsers.TYPES.VAR);
});

it('returns var from var(--foo)', () => {
let input = 'var(--foo)';
it('returns var from var(--foo, calc(var(--bar) * 2))', () => {
let input = 'var(--foo, calc(var(--bar) * 2))';
let output = parsers.valueType(input);

assert.strictEqual(output, parsers.TYPES.KEYWORD);
assert.strictEqual(output, parsers.TYPES.VAR);
});

it('returns var from var(--foo, var(--bar))', () => {
let input = 'var(--foo, var(--bar))';
it('returns calc from calc(100px * 2)', () => {
let input = 'calc(100px * 2)';
let output = parsers.valueType(input);

assert.strictEqual(output, parsers.TYPES.KEYWORD);
assert.strictEqual(output, parsers.TYPES.CALC);
});

it('returns var from var(--foo, calc(var(--bar) * 2))', () => {
let input = 'var(--foo, calc(var(--bar) * 2))';
it('returns calc from calc(100px * calc(2 * 1))', () => {
let input = 'calc(100px * calc(2 * 1))';
let output = parsers.valueType(input);

assert.strictEqual(output, parsers.TYPES.KEYWORD);
assert.strictEqual(output, parsers.TYPES.CALC);
});
});

Expand All @@ -124,6 +124,41 @@ describe('parsePercent', () => {
it.todo('test');
});
describe('parseMeasurement', () => {
it('should return value with em unit', () => {
let input = '1em';
let output = parsers.parseMeasurement(input);

assert.strictEqual(output, '1em');
});

it('should return value with percent', () => {
let input = '100%';
let output = parsers.parseMeasurement(input);

assert.strictEqual(output, '100%');
});

it('should return value as is', () => {
let input = 'var(/* comment */ --foo)';
let output = parsers.parseMeasurement(input);

assert.strictEqual(output, 'var(/* comment */ --foo)');
});

it('should return calculated value', () => {
let input = 'calc(2em / 3)';
let output = parsers.parseMeasurement(input);

assert.strictEqual(output, 'calc(0.666667em)');
});

it('should return calculated value', () => {
let input = 'calc(100% / 3)';
let output = parsers.parseMeasurement(input);

assert.strictEqual(output, 'calc(33.3333%)');
});

it.todo('test');
});
describe('parseUrl', () => {
Expand Down

0 comments on commit b67cf05

Please sign in to comment.