| 1 |
"use strict"; |
| 2 |
|
| 3 |
var from = require("es5-ext/array/from") |
| 4 |
, ensureString = require("type/string/ensure") |
| 5 |
, primitiveSet = require("es5-ext/object/primitive-set") |
| 6 |
, esniff = require("./"); |
| 7 |
|
| 8 |
var allowedSeparators = primitiveSet.apply(null, from(".+-*/,&|;")); |
| 9 |
|
| 10 |
module.exports = function (code, sep/*, limit*/) { |
| 11 |
var expressions, fromIndex, limit = arguments[2] || Infinity; |
| 12 |
code = ensureString(code); |
| 13 |
sep = ensureString(sep); |
| 14 |
if (!allowedSeparators[sep]) throw new Error(sep + " is not supported separator"); |
| 15 |
expressions = []; |
| 16 |
fromIndex = 0; |
| 17 |
esniff(code, function (emitter) { |
| 18 |
emitter.on("trigger:" + sep, function (accessor) { |
| 19 |
if (accessor.scopeDepth !== 0) return; |
| 20 |
var index = accessor.index; |
| 21 |
if (expressions.push(code.slice(fromIndex, index)) === limit) accessor.stop(); |
| 22 |
fromIndex = index + 1; |
| 23 |
}); |
| 24 |
}); |
| 25 |
if (expressions.length < limit) expressions.push(code.slice(fromIndex)); |
| 26 |
return expressions; |
| 27 |
}; |