|
| 1 | +# functions (>= v3.0.0) - _experimental_ |
| 2 | + |
| 3 | +**This is an experimental LibSass feature. Use with caution.** |
| 4 | + |
| 5 | +`functions` is an `Object` that holds a collection of custom functions that may be invoked by the sass files being compiled. They may take zero or more input parameters and must return a value either synchronously (`return ...;`) or asynchronously (`done();`). Those parameters will be instances of one of the constructors contained in the `require('node-sass').types` hash. The return value must be of one of these types as well. See the list of available types below: |
| 6 | + |
| 7 | +## types.Number(value [, unit = ""]) |
| 8 | +* `getValue()`/ `setValue(value)` : gets / sets the numerical portion of the number |
| 9 | +* `getUnit()` / `setUnit(unit)` : gets / sets the unit portion of the number |
| 10 | + |
| 11 | +## types.String(value) |
| 12 | +* `getValue()` / `setValue(value)` : gets / sets the enclosed string |
| 13 | + |
| 14 | +## types.Color(r, g, b [, a = 1.0]) or types.Color(argb) |
| 15 | +* `getR()` / `setR(value)` : red component (integer from `0` to `255`) |
| 16 | +* `getG()` / `setG(value)` : green component (integer from `0` to `255`) |
| 17 | +* `getB()` / `setB(value)` : blue component (integer from `0` to `255`) |
| 18 | +* `getA()` / `setA(value)` : alpha component (number from `0` to `1.0`) |
| 19 | + |
| 20 | +Example: |
| 21 | + |
| 22 | +```javascript |
| 23 | +var Color = require('node-sass').types.Color, |
| 24 | + c1 = new Color(255, 0, 0), |
| 25 | + c2 = new Color(0xff0088cc); |
| 26 | +``` |
| 27 | + |
| 28 | +## types.Boolean(value) |
| 29 | +* `getValue()` : gets the enclosed boolean |
| 30 | +* `types.Boolean.TRUE` : Singleton instance of `types.Boolean` that holds "true" |
| 31 | +* `types.Boolean.FALSE` : Singleton instance of `types.Boolean` that holds "false" |
| 32 | + |
| 33 | +## types.List(length [, commaSeparator = true]) |
| 34 | +* `getValue(index)` / `setValue(index, value)` : `value` must itself be an instance of one of the constructors in `sass.types`. |
| 35 | +* `getSeparator()` / `setSeparator(isComma)` : whether to use commas as a separator |
| 36 | +* `getLength()` |
| 37 | + |
| 38 | +## types.Map(length) |
| 39 | +* `getKey(index)` / `setKey(index, value)` |
| 40 | +* `getValue(index)` / `setValue(index, value)` |
| 41 | +* `getLength()` |
| 42 | + |
| 43 | +## types.Null() |
| 44 | +* `types.Null.NULL` : Singleton instance of `types.Null`. |
| 45 | + |
| 46 | +## Example |
| 47 | + |
| 48 | +```javascript |
| 49 | +sass.renderSync({ |
| 50 | + data: '#{headings(2,5)} { color: #08c; }', |
| 51 | + functions: { |
| 52 | + 'headings($from: 0, $to: 6)': function(from, to) { |
| 53 | + var i, f = from.getValue(), t = to.getValue(), |
| 54 | + list = new sass.types.List(t - f + 1); |
| 55 | + |
| 56 | + for (i = f; i <= t; i++) { |
| 57 | + list.setValue(i - f, new sass.types.String('h' + i)); |
| 58 | + } |
| 59 | + |
| 60 | + return list; |
| 61 | + } |
| 62 | + } |
| 63 | +}); |
| 64 | +``` |
0 commit comments