Aucune description

splitter.js 781B

1234567891011121314151617181920212223242526272829303132333435
  1. var Splitter = function Splitter (separator) {
  2. this.separator = separator;
  3. };
  4. Splitter.prototype.split = function (value) {
  5. if (value.indexOf(this.separator) === -1)
  6. return [value];
  7. if (value.indexOf('(') === -1)
  8. return value.split(this.separator);
  9. var level = 0;
  10. var cursor = 0;
  11. var lastStart = 0;
  12. var len = value.length;
  13. var tokens = [];
  14. while (cursor++ < len) {
  15. if (value[cursor] == '(') {
  16. level++;
  17. } else if (value[cursor] == ')') {
  18. level--;
  19. } else if (value[cursor] == this.separator && level === 0) {
  20. tokens.push(value.substring(lastStart, cursor));
  21. lastStart = cursor + 1;
  22. }
  23. }
  24. if (lastStart < cursor + 1)
  25. tokens.push(value.substring(lastStart));
  26. return tokens;
  27. };
  28. module.exports = Splitter;