function.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. var ensureString = require("type/string/ensure")
  3. , isValue = require("type/value/is")
  4. , esniff = require("./");
  5. module.exports = function (name/*, options*/) {
  6. var options = Object(arguments[1])
  7. , asProperty = options.asProperty
  8. , asPlain = isValue(options.asPlain) ? options.asPlain : true;
  9. var length, names;
  10. name = ensureString(name);
  11. names = name.split(".").map(function (prop) {
  12. prop = prop.trim();
  13. if (!prop) throw new TypeError(name + " is not valid function name");
  14. return prop;
  15. });
  16. length = names.length;
  17. return function (code) {
  18. code = ensureString(code);
  19. return esniff(code, function (emitter) {
  20. emitter.on("trigger:" + names[0][0], function (accessor) {
  21. if (accessor.previousToken === ".") {
  22. if (!asProperty) return;
  23. } else if (!asPlain) {
  24. return;
  25. }
  26. for (var i = 0, propertyName; (propertyName = names[i]); ++i) {
  27. if (!accessor.skipCodePart(propertyName)) return;
  28. accessor.skipWhitespace();
  29. if (i < length - 1) {
  30. if (!accessor.skipCodePart(".")) return;
  31. accessor.skipWhitespace();
  32. }
  33. }
  34. accessor.collectScope();
  35. });
  36. });
  37. };
  38. };