0

In Stylus it's as easy as split('1/2', '/')[0] (which will split a string value at a certain delimiter, in the example it would return 1), in Sass I needed a huge function to do it. I don't see a way to natively do this in LESS or a function for doing this anywhere.

Anyone know of a way to do this?

corysimmons
  • 7,296
  • 4
  • 57
  • 65
  • Where did you find a `split` function in Stylus? – Amadan Apr 06 '15 at 01:13
  • It's actually undocumented but definitely exists. https://github.com/stylus/stylus/blob/c2d07a0f117de6144105de8599f620c34713d251/lib/functions/index.js#L1083 – corysimmons Apr 06 '15 at 01:21
  • I see, thank you. Given that it is undocumented, you might want to describe what it does in your question, since it is not obvious what it does without reading the code. – Amadan Apr 06 '15 at 01:27
  • 1
    It is certainly a better question now. Unfortunately I still don't have an answer for you, except to point you [here](https://github.com/less/less.js/issues/1465), using recursion and [`extract`](http://lesscss.org/functions/#list-functions-extract). It is limited to space- and comma-separated lists though. – Amadan Apr 06 '15 at 01:37
  • Appreciate it. Currently trying to implement my own version of something like that. It's silly more of these functions aren't built into preprocessors. I see now why people talk about PostCSS being the be-all, end-all, for offering this kind of control. – corysimmons Apr 06 '15 at 01:49

1 Answers1

1

It's silly more of these functions aren't built into preprocessors.

You should possible also explain why you need the split function.

Since Less version 2 you can easily add your own functions, see How to exend the Less compiler with a custom function leveraging a plugin

You should be able to add a split function which returns a list. As already explained by @Amadan you can use this list with loops and the built-in list functions.

Community
  • 1
  • 1
Bass Jobsen
  • 48,736
  • 16
  • 143
  • 224
  • I need to split strings so I can grab the denominator from a fraction and use it in my grid's calculations. I've figured out a way to do this by changing the syntax specifically for LESS (extracting something like `1 of 2` works well), but it's not ideal since it doesn't match the other preprocessors (`'1/2'`) but I don't feel like fighting with it anymore so it'll have to do. – corysimmons Apr 07 '15 at 04:45