Skip to content

Commit

Permalink
Update constant
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Jan 17, 2025
1 parent 6cf80b9 commit 5420f22
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 55 deletions.
22 changes: 11 additions & 11 deletions src/js/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@
* constant.js
*/

/* constants */
export const VAL_COMP = 'computedValue';
export const VAL_SPEC = 'specifiedValue';

/* values and units */
const _DIGIT = '(?:0|[1-9]\\d*)';
const _COMP = 'clamp|max|min';
const _COMPARE = 'clamp|max|min';
const _EXPO = 'exp|hypot|log|pow|sqrt';
const _SIGN = 'abs|sign';
const _STEP = 'mod|rem|round';
const _TRIG = 'a?(?:cos|sin|tan)|atan2';
const _EXP = 'exp|hypot|log|pow|sqrt';
const _SIGN = 'abs|sign';
const _MATH = `${_COMP}|${_STEP}|${_TRIG}|${_EXP}|${_SIGN}`;
const _MATH = `${_COMPARE}|${_EXPO}|${_SIGN}|${_STEP}|${_TRIG}`;
const _CALC = `calc|${_MATH}`;
const _VAR = `var|${_CALC}`;
export const ANGLE = 'deg|g?rad|turn';
export const NUM = `[+-]?(?:${_DIGIT}(?:\\.\\d*)?|\\.\\d+)(?:e-?${_DIGIT})?`;
export const NONE = 'none';
export const PCT = `${NUM}%`;
export const SYN_FN_MATH = `^(?:${_MATH})\\($`;
export const SYN_FN_MATH_CALC = `^(?:${_CALC})\\(|(?<=[*\\/\\s\\(])(?:${_CALC})\\(`;
export const SYN_FN_MATH_VAR = `^(?:${_VAR})\\(`;
export const SYN_FN_CALC = `^(?:${_CALC})\\(|(?<=[*\\/\\s\\(])(?:${_CALC})\\(`;
export const SYN_FN_MATH_START = `^(?:${_MATH})\\($`;
export const SYN_FN_VAR = '^var\\(|(?<=[*\\/\\s\\(])var\\(';
export const SYN_FN_VAR_START = `^(?:${_VAR})\\(`;

/* colors */
const _ALPHA = `(?:\\s*\\/\\s*(?:${NUM}|${PCT}|${NONE}))?`;
Expand Down Expand Up @@ -59,3 +55,7 @@ export const SYN_COLOR_TYPE = `${_COLOR_KEY}|hsla?\\(\\s*${SYN_HSL_LV3}\\s*\\)|r
export const SYN_MIX_PART = `(?:${SYN_COLOR_TYPE})(?:\\s+${PCT})?`;
export const SYN_MIX = `color-mix\\(\\s*in\\s+(?:${CS_MIX})\\s*,\\s*${SYN_MIX_PART}\\s*,\\s*${SYN_MIX_PART}\\s*\\)`;
export const SYN_MIX_CAPT = `color-mix\\(\\s*in\\s+(${CS_MIX})\\s*,\\s*(${SYN_MIX_PART})\\s*,\\s*(${SYN_MIX_PART})\\s*\\)`;

/* formats */
export const VAL_COMP = 'computedValue';
export const VAL_SPEC = 'specifiedValue';
11 changes: 3 additions & 8 deletions src/js/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ import { resolve } from './resolve';
import { valueToJsonString } from './util';

/* constants */
import {
SYN_FN_MATH_CALC,
SYN_FN_REL,
SYN_FN_VAR,
VAL_COMP
} from './constant.js';
import { SYN_FN_CALC, SYN_FN_REL, SYN_FN_VAR, VAL_COMP } from './constant.js';

/* regexp */
const REG_FN_MATH_CALC = new RegExp(SYN_FN_MATH_CALC);
const REG_FN_CALC = new RegExp(SYN_FN_CALC);
const REG_FN_REL = new RegExp(SYN_FN_REL);
const REG_FN_VAR = new RegExp(SYN_FN_VAR);

Expand Down Expand Up @@ -88,7 +83,7 @@ export const preProcess = (
}
if (REG_FN_REL.test(value)) {
value = resolveRelativeColor(value, opt) as string;
} else if (REG_FN_MATH_CALC.test(value)) {
} else if (REG_FN_CALC.test(value)) {
const resolvedValue = cssCalc(value, opt) as string | null;
if (resolvedValue) {
value = resolvedValue as string;
Expand Down
24 changes: 12 additions & 12 deletions src/js/css-calc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { roundToPrecision, valueToJsonString } from './util';
import {
FN_VAR,
NUM,
SYN_FN_MATH,
SYN_FN_MATH_CALC,
SYN_FN_MATH_VAR,
SYN_FN_CALC,
SYN_FN_MATH_START,
SYN_FN_VAR,
SYN_FN_VAR_START,
VAL_SPEC
} from './constant.js';
const {
Expand All @@ -31,11 +31,11 @@ const HEX = 16;
const MAX_PCT = 100;

/* regexp */
const REG_FN_MATH_CALC = new RegExp(SYN_FN_MATH_CALC);
const REG_FN_CALC = new RegExp(SYN_FN_CALC);
const REG_FN_MATH_START = new RegExp(SYN_FN_MATH_START);
const REG_FN_VAR = new RegExp(SYN_FN_VAR);
const REG_FN_VAR_START = new RegExp(SYN_FN_VAR_START);
const REG_OPERATOR = /\s[*+/-]\s/;
const REG_START_MATH = new RegExp(SYN_FN_MATH);
const REG_START_MATH_VAR = new RegExp(SYN_FN_MATH_VAR);
const REG_TYPE_DIM = new RegExp(`^(${NUM})([a-z]+)$`);
const REG_TYPE_DIM_PCT = new RegExp(`^(${NUM})([a-z]+|%)$`);
const REG_TYPE_PCT = new RegExp(`^(${NUM})%$`);
Expand Down Expand Up @@ -697,7 +697,7 @@ export const serializeCalc = (
): string | null => {
const { format } = opt;
if (isString(value)) {
if (!REG_START_MATH_VAR.test(value) || format !== VAL_SPEC) {
if (!REG_FN_VAR_START.test(value) || format !== VAL_SPEC) {
return value;
}
value = value.toLowerCase().trim();
Expand Down Expand Up @@ -727,7 +727,7 @@ export const serializeCalc = (
let serializedValue = sortCalcValues(
slicedValues as Array<string>
) as string;
if (REG_START_MATH_VAR.test(serializedValue)) {
if (REG_FN_VAR_START.test(serializedValue)) {
serializedValue = calc(serializedValue, {
toCanonicalUnits: true
}) as string;
Expand Down Expand Up @@ -836,7 +836,7 @@ export const parseTokens = (
case PAREN_OPEN: {
res.push(value);
nest++;
if (REG_START_MATH.test(value)) {
if (REG_FN_MATH_START.test(value)) {
mathFunc.add(nest);
}
break;
Expand Down Expand Up @@ -901,7 +901,7 @@ export const cssCalc = (
} else {
throw new SyntaxError(`Unexpected token ${FN_VAR} found.`);
}
} else if (!REG_FN_MATH_CALC.test(value)) {
} else if (!REG_FN_CALC.test(value)) {
return value;
}
value = value.toLowerCase().trim();
Expand Down Expand Up @@ -933,7 +933,7 @@ export const cssCalc = (
toCanonicalUnits: true
}) as string;
}
if (REG_START_MATH_VAR.test(value)) {
if (REG_FN_VAR_START.test(value)) {
if (REG_TYPE_DIM_PCT.test(resolvedValue)) {
const [, val, unit] = resolvedValue.match(REG_TYPE_DIM_PCT) as [
string,
Expand All @@ -945,7 +945,7 @@ export const cssCalc = (
// wrap with `calc()`
if (
resolvedValue &&
!REG_START_MATH_VAR.test(resolvedValue as string) &&
!REG_FN_VAR_START.test(resolvedValue as string) &&
format === VAL_SPEC
) {
resolvedValue = `calc(${resolvedValue})`;
Expand Down
8 changes: 4 additions & 4 deletions src/js/css-var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { cssCalc } from './css-calc';
import { isColor, valueToJsonString } from './util';

/* constants */
import { FN_VAR, SYN_FN_MATH_CALC, SYN_FN_VAR, VAL_SPEC } from './constant';
import { FN_VAR, SYN_FN_CALC, SYN_FN_VAR, VAL_SPEC } from './constant';
const {
CloseParen: PAREN_CLOSE,
Comment: COMMENT,
Expand All @@ -20,7 +20,7 @@ const {
} = TokenType;

/* regexp */
const REG_FN_MATH_CALC = new RegExp(SYN_FN_MATH_CALC);
const REG_FN_CALC = new RegExp(SYN_FN_CALC);
const REG_FN_VAR = new RegExp(SYN_FN_VAR);

/* cached results */
Expand Down Expand Up @@ -108,7 +108,7 @@ export function resolveCustomProperty(
resolvedValue = item;
}
}
} else if (REG_FN_MATH_CALC.test(item)) {
} else if (REG_FN_CALC.test(item)) {
item = cssCalc(item as string, opt) as string;
if (resolveAsColor) {
if (isColor(item)) {
Expand Down Expand Up @@ -229,7 +229,7 @@ export function cssVar(
const values = parseTokens(tokens, opt);
if (Array.isArray(values)) {
let color = values.join('') as string | null;
if (REG_FN_MATH_CALC.test(color as string)) {
if (REG_FN_CALC.test(color as string)) {
color = cssCalc(color as string, opt) as string | null;
}
if (cacheKey) {
Expand Down
32 changes: 16 additions & 16 deletions src/js/relative-color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { resolve } from './resolve';
import { roundToPrecision, valueToJsonString } from './util';

/* constants */
import { NAMED_COLORS } from './color';
import {
CS_LAB,
CS_LCH,
Expand All @@ -23,12 +24,11 @@ import {
FN_VAR,
NONE,
SYN_COLOR_TYPE,
SYN_FN_MATH,
SYN_FN_MATH_START,
SYN_FN_VAR,
SYN_MIX,
VAL_SPEC
} from './constant';
import { NAMED_COLORS } from './color';
const {
CloseParen: PAREN_CLOSE,
Comment: COMMENT,
Expand All @@ -41,7 +41,7 @@ const {
Percentage: PCT,
Whitespace: W_SPACE
} = TokenType;
const { HasNoneKeywords: NONE_KEY } = SyntaxFlag;
const { HasNoneKeywords: KEY_NONE } = SyntaxFlag;
const OCT = 8;
const DEC = 10;
const HEX = 16;
Expand All @@ -54,11 +54,11 @@ const REG_COLOR_CAPT = new RegExp(
);
const REG_CS_HSL = /(?:hsla?|hwb)$/;
const REG_CS_CIE = new RegExp(`^(?:${CS_LAB}|${CS_LCH})$`);
const REG_FN_MATH_START = new RegExp(SYN_FN_MATH_START);
const REG_FN_REL = new RegExp(FN_REL);
const REG_FN_REL_CAPT = new RegExp(`^${FN_REL_CAPT}`);
const REG_FN_REL_START = new RegExp(`^${FN_REL}`);
const REG_FN_VAR = new RegExp(SYN_FN_VAR);
const REG_REL = new RegExp(FN_REL);
const REG_REL_CAPT = new RegExp(`^${FN_REL_CAPT}`);
const REG_START_MATH = new RegExp(SYN_FN_MATH);
const REG_START_REL = new RegExp(`^${FN_REL}`);

/* cached results */
export const cachedResults = new LRUCache({
Expand Down Expand Up @@ -128,7 +128,7 @@ export function resolveColorChannels(
channel.push(value);
func = true;
nest++;
if (REG_START_MATH.test(value)) {
if (REG_FN_MATH_START.test(value)) {
mathFunc.add(nest);
}
break;
Expand Down Expand Up @@ -246,7 +246,7 @@ export function extractOriginColor(
if (!value) {
return null;
}
if (!REG_START_REL.test(value)) {
if (!REG_FN_REL_START.test(value)) {
return value;
}
} else {
Expand All @@ -267,7 +267,7 @@ export function extractOriginColor(
return null;
}
}
const cs = value.match(REG_REL_CAPT);
const cs = value.match(REG_FN_REL_CAPT);
let colorSpace: string;
if (cs) {
[, colorSpace] = cs as [string, string];
Expand Down Expand Up @@ -320,8 +320,8 @@ export function extractOriginColor(
}
// nested relative color
} else {
const [, restValue] = value.split(REG_START_REL) as [string, string];
if (REG_START_REL.test(restValue)) {
const [, restValue] = value.split(REG_FN_REL_START) as [string, string];
if (REG_FN_REL_START.test(restValue)) {
const tokens = tokenize({ css: restValue });
const originColor = [] as Array<string>;
let nest = 0;
Expand Down Expand Up @@ -428,7 +428,7 @@ export function resolveRelativeColor(
} else {
throw new SyntaxError(`Unexpected token ${FN_VAR} found.`);
}
} else if (!REG_REL.test(value)) {
} else if (!REG_FN_REL.test(value)) {
return value;
}
value = value.toLowerCase().trim();
Expand Down Expand Up @@ -473,7 +473,7 @@ export function resolveRelativeColor(
} = parsedComponents;
let alpha: number | string;
if (Number.isNaN(Number(alphaComponent))) {
if (syntaxFlags instanceof Set && syntaxFlags.has(NONE_KEY)) {
if (syntaxFlags instanceof Set && syntaxFlags.has(KEY_NONE)) {
alpha = NONE;
} else {
alpha = 0;
Expand All @@ -487,7 +487,7 @@ export function resolveRelativeColor(
[v1, v2, v3] = channelsComponent;
let resolvedValue;
if (REG_CS_CIE.test(colorNotation)) {
const hasNone = syntaxFlags instanceof Set && syntaxFlags.has(NONE_KEY);
const hasNone = syntaxFlags instanceof Set && syntaxFlags.has(KEY_NONE);
if (Number.isNaN(v1)) {
if (hasNone) {
v1 = NONE;
Expand Down Expand Up @@ -543,7 +543,7 @@ export function resolveRelativeColor(
}
} else {
const cs = colorNotation === 'rgb' ? 'srgb' : colorNotation;
const hasNone = syntaxFlags instanceof Set && syntaxFlags.has(NONE_KEY);
const hasNone = syntaxFlags instanceof Set && syntaxFlags.has(KEY_NONE);
if (Number.isNaN(v1)) {
if (hasNone) {
v1 = NONE;
Expand Down
6 changes: 3 additions & 3 deletions src/js/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { valueToJsonString } from './util';
import {
FN_COLOR,
FN_MIX,
SYN_FN_MATH_CALC,
SYN_FN_CALC,
SYN_FN_REL,
SYN_FN_VAR,
VAL_COMP,
Expand All @@ -29,7 +29,7 @@ import {
const RGB_TRANSPARENT = 'rgba(0, 0, 0, 0)';

/* regexp */
const REG_FN_MATH_CALC = new RegExp(SYN_FN_MATH_CALC);
const REG_FN_CALC = new RegExp(SYN_FN_CALC);
const REG_FN_REL = new RegExp(SYN_FN_REL);
const REG_FN_VAR = new RegExp(SYN_FN_VAR);

Expand Down Expand Up @@ -171,7 +171,7 @@ export const resolve = (
color = '';
}
}
if (REG_FN_MATH_CALC.test(color)) {
if (REG_FN_CALC.test(color)) {
const resolvedColor = cssCalc(color, opt) as string | null;
if (resolvedColor) {
color = resolvedColor;
Expand Down
2 changes: 1 addition & 1 deletion test/constant.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as constant from '../src/js/constant.js';
describe('constants', () => {
const items = Object.entries(constant);
for (const [key, value] of items) {
it('should get string, number or array', () => {
it('should get string', () => {
assert.strictEqual(/^[A-Z][A-Z_\d]+$/.test(key), true, 'key');
assert.strictEqual(typeof value, 'string', 'value');
});
Expand Down

0 comments on commit 5420f22

Please sign in to comment.