Skip to content

Commit d68fa50

Browse files
committed
Add custom stringify support
1 parent fc679a7 commit d68fa50

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ If the `quantity` argument cannot be parsed as a number, returns `false`.
207207
the unit. Instead, you should pass it single quantities only. Parse `1px solid black`, then pass it
208208
the stringified `1px` node (a `word` node) to parse the number and unit.
209209

210-
### valueParser.stringify(nodes)
210+
### valueParser.stringify(nodes[, custom])
211211

212212
Stringifies a node or array of nodes.
213213

214+
The `custom` function is called for each `node`; return a string to override the default behaviour.
215+
214216
### valueParser.walk(nodes, callback[, bubble])
215217

216218
Walks each provided node, recursively walking all descendent nodes within functions.

lib/stringify.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
function stringifyNode(node) {
1+
function stringifyNode(node, custom) {
22
var type = node.type;
33
var value = node.value;
44
var buf;
5+
var customResult;
56

6-
if (type === 'word' || type === 'space') {
7+
if (custom && (customResult = custom(node)) !== undefined) {
8+
return customResult;
9+
} else if (type === 'word' || type === 'space') {
710
return value;
811
} else if (type === 'string') {
912
buf = node.quote || '';
@@ -22,17 +25,17 @@ function stringifyNode(node) {
2225
return value;
2326
}
2427

25-
function stringify(nodes) {
28+
function stringify(nodes, custom) {
2629
var result, i;
2730

2831
if (Array.isArray(nodes)) {
2932
result = '';
3033
for (i = nodes.length - 1; ~i; i -= 1) {
31-
result = stringifyNode(nodes[i]) + result;
34+
result = stringifyNode(nodes[i], custom) + result;
3235
}
3336
return result;
3437
}
35-
return stringifyNode(nodes);
38+
return stringifyNode(nodes, custom);
3639
}
3740

3841
module.exports = stringify;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"max-len": 0,
2323
"no-bitwise": 0,
2424
"complexity": 0,
25-
"no-use-before-define": 0
25+
"no-use-before-define": 0,
26+
"consistent-return": 0
2627
}
2728
},
2829
"author": "Bogdan Chadkin <trysound@yandex.ru>",

test/stringify.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,34 @@ var tests = [
3434
];
3535

3636
test('Stringify', function (t) {
37-
t.plan(tests.length + 1);
37+
t.plan(tests.length + 3);
3838

3939
tests.forEach(function (opts) {
4040
t.equal(stringify(parse(opts.fixture)), opts.fixture, opts.message);
4141
});
4242

4343
var tokens = parse(' rgba(12, 54, 65 ) ');
44-
tokens[1].type = 'word';
4544

45+
t.equal(stringify(tokens, function (node) {
46+
if (node.type === 'function') {
47+
return node.value + '[' + [
48+
node.nodes[0].value,
49+
node.nodes[2].value,
50+
node.nodes[4].value
51+
].join(',') + ']';
52+
}
53+
}), ' rgba[12,54,65] ');
54+
55+
t.equal(stringify(tokens[1], function (node) {
56+
if (node.type === 'function') {
57+
return node.value + '[' + [
58+
node.nodes[0].value,
59+
node.nodes[2].value,
60+
node.nodes[4].value
61+
].join(',') + ']';
62+
}
63+
}), 'rgba[12,54,65]');
64+
65+
tokens[1].type = 'word';
4666
t.equal(stringify(tokens), ' rgba ', 'Shouldn\'t process nodes of work type');
4767
});

0 commit comments

Comments
 (0)