Skip to content

Commit d787592

Browse files
authored
Adds range() function for lists (#3334)
1 parent c816d82 commit d787592

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

lib/less/functions/list.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Comment = require('../tree/comment'),
22
Dimension = require('../tree/dimension'),
33
Declaration = require('../tree/declaration'),
4+
Expression = require('../tree/expression'),
45
Ruleset = require('../tree/ruleset'),
56
Selector = require('../tree/selector'),
67
Element = require('../tree/element'),
@@ -27,6 +28,34 @@ functionRegistry.addMultiple({
2728
length: function(values) {
2829
return new Dimension(getItemsFromNode(values).length);
2930
},
31+
/**
32+
* Creates a Less list of incremental values.
33+
* Modeled after Lodash's range function, also exists natively in PHP
34+
*
35+
* @param {Dimension} [start=1]
36+
* @param {Dimension} end - e.g. 10 or 10px - unit is added to output
37+
* @param {Dimension} [step=1]
38+
*/
39+
range: function(start, end, step) {
40+
var from, to, stepValue = 1, list = [];
41+
if (end) {
42+
to = end;
43+
from = start.value;
44+
if (step) {
45+
stepValue = step.value;
46+
}
47+
}
48+
else {
49+
from = 1;
50+
to = start;
51+
}
52+
53+
for (var i = from; i <= to.value; i += stepValue) {
54+
list.push(new Dimension(i, to.unit));
55+
}
56+
57+
return new Expression(list);
58+
},
3059
each: function(list, rs) {
3160
var rules = [], newRules, iterator;
3261

test/css/functions-each.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@
4040
val2: 2;
4141
val3: 4;
4242
}
43+
.column-list {
44+
list: 1 2 3 4;
45+
}
46+
.col-1 {
47+
width: 25%;
48+
}
49+
.col-2 {
50+
width: 25%;
51+
}
52+
.col-3 {
53+
width: 25%;
54+
}
55+
.col-4 {
56+
width: 25%;
57+
}
58+
.row-1 {
59+
width: 10px;
60+
}
61+
.row-2 {
62+
width: 20px;
63+
}
64+
.row-3 {
65+
width: 30px;
66+
}
4367
.box {
4468
-less-log: a;
4569
-less-log: b;

test/less/functions-each.less

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ each(@selectors, {
7676
});
7777
}
7878

79+
@columns: range(4);
80+
.column-list {
81+
list: @columns;
82+
}
83+
84+
each(@columns, .(@val) {
85+
.col-@{val} {
86+
width: (100% / length(@columns));
87+
}
88+
});
89+
90+
each(range(10px, 30px, 10px), .(@val, @index) {
91+
.row-@{index} {
92+
width: @val;
93+
}
94+
});
95+
7996
@list: a b c d;
8097
.box {
8198
each(@list, {

0 commit comments

Comments
 (0)