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