util-ex
Version:
Browser-friendly enhanced util fully compatible with standard node.js
27 lines (26 loc) • 878 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
exports.isArrowFunctionStr = isArrowFunctionStr;
/**
* Determines if a string represents a valid JavaScript arrow function.
* @param {string} aFuncString - The string to test.
* @returns {boolean} - True if the string represents a valid arrow function, false otherwise.
* @example
* isArrowFunctionStr('(x, y) => x + y') // true
* isArrowFunctionStr('async x => { return x; }') // true
* isArrowFunctionStr('function(x) { return x; }') // false
*/
function isArrowFunctionStr(aFuncString) {
// Matches:
// (a, b) => ...
// a => ...
// async (a) => ...
// async a => ...
const result = /^[;\s]*(async\s+)?(\([^)]*\)|[a-zA-Z_$][0-9a-zA-Z_$]*)\s*=>\s*[^;\s].*$/s.test(aFuncString);
return result;
}
;
var _default = exports.default = isArrowFunctionStr;