UNPKG

905 BJavaScriptView Raw
1// tooling
2import getArrayedString from './get-arrayed-string';
3import getClosestVariable from './get-closest-variable';
4
5// set a variable on a node
6export default function setVariable(node, name, value, opts) {
7 // if the value is not a default with a value already defined
8 if (!isDefault(value) || getClosestVariable(name, node, opts) === undefined) {
9 // get the value without its default suffix as the first item of its arrayed value
10 const undefaultedValue = 'string' === typeof value ? getArrayedString(value.replace(matchDefault, ''), true) : value;
11
12 // ensure the node has a variables object
13 node.variables = node.variables || {};
14
15 // set the variable
16 node.variables[name] = undefaultedValue;
17 }
18}
19
20// return whether the value is a default
21const isDefault = value => matchDefault.test(value);
22
23// match anything ending with space and then !default
24const matchDefault = /\s+!default$/;