resolve-separated.js 942 B

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