Commit 6fa29798 authored by Artem Sakhatskiy's avatar Artem Sakhatskiy
Browse files

fix for module

parent bb984858
Pipeline #3936715 passed with stages
in 3 minutes and 52 seconds
......@@ -2,4 +2,4 @@
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require('./js/expression/expression'));
__export(require('./js'));
"use strict";
var Internals = require("./internals");
exports.apply = function (expression, target) {
exports.validate(expression);
return expression(target);
};
exports.validate = function (expression) {
var sourceCode = expression.toString();
Internals.validate(sourceCode);
};
exports.getProperty = function (expression) {
var sourceCode = expression.toString();
return Internals.getProperty(sourceCode);
};
"use strict";
var ArrowToken = "=>";
var FunctionToken = "function";
var ReturnToken = "return";
exports.validate = function (sourceCode) {
if (isLambda(sourceCode))
exports.validateLambda(sourceCode);
else
exports.validateFunction(sourceCode);
};
exports.getProperty = function (sourceCode) {
var expressionParts;
if (isLambda(sourceCode))
expressionParts = getLambdaExpressionParts(sourceCode);
else
expressionParts = getFunctionExpressionParts(sourceCode);
return getPropertyFromParts(expressionParts);
};
var isLambda = function (sourceCode) {
return sourceCode.indexOf(ArrowToken) >= 0;
};
exports.validateLambda = function (sourceCode) {
var expressionParts = getLambdaExpressionParts(sourceCode);
validatePropertyExpressionBody(expressionParts);
};
var getLambdaExpressionParts = function (sourceCode) {
var parts = sourceCode
.split(ArrowToken)
.map(function (part) { return part.trim(); });
if (parts.length == 1)
throw new Error("Source code doesn't contain arrow (" + ArrowToken + "). Code: \"" + sourceCode + "\".");
if (parts.length >= 3)
throw new Error("Source code contains too much arrows (" + ArrowToken + "). Code: \"" + sourceCode + "\".");
var token = parts[0];
var propertyExpressionBody = parts[1];
return {
token: token,
propertyExpressionBody: propertyExpressionBody
};
};
exports.validateFunction = function (sourceCode) {
var expressionParts = getFunctionExpressionParts(sourceCode);
validatePropertyExpressionBody(expressionParts);
};
var getFunctionExpressionParts = function (sourceCode) {
// "function (x){ return x.test; }"
var code = sourceCode.trim();
if (code.indexOf(FunctionToken) != 0)
throw Error("Expression is not a function. Code: \"" + sourceCode + "\".");
code = code
.substr(FunctionToken.length)
.trim();
// "(x){ return x.test; }"
if (code.indexOf("(") != 0)
throw Error("Expression is not a function. Code: \"" + sourceCode + "\".");
var indexOfTokenEnd = code.indexOf(")");
if (indexOfTokenEnd < 0)
throw Error("Expression is not a function. Code: \"" + sourceCode + "\".");
var token = code.substr(1, indexOfTokenEnd - 1);
if (token.indexOf(",") >= 0)
throw Error("Expression is not a single argument function. Code: \"" + sourceCode + "\".");
var body = code
.substr(indexOfTokenEnd + 1)
.replace("{", "")
.replace("}", "")
.replace(";", "")
.trim();
// "return x.test"
if (body.indexOf(ReturnToken) < 0)
throw Error("Expression is not a single return statement. Code: \"" + sourceCode + "\".");
body = body
.substr(ReturnToken.length)
.trim();
// "x.test"
return {
token: token,
propertyExpressionBody: body
};
};
var validatePropertyExpressionBody = function (expressionParts) {
if (!expressionParts.token)
throw new Error("Expression token is empty.");
if (!expressionParts.propertyExpressionBody)
throw new Error("Expression body is empty.");
var property = getPropertyFromParts(expressionParts);
if (!property)
throw new Error("Expression body has no property.");
};
var getPropertyFromParts = function (expressionParts) {
if (!expressionParts.token)
throw new Error("Expression token is empty.");
if (!expressionParts.propertyExpressionBody)
throw new Error("Expression body is empty.");
var parts = expressionParts.propertyExpressionBody
.split(".")
.map(function (part) { return part.trim(); });
if (parts.length == 1)
throw new Error("Expression body is not a property expression. Body: \"" + expressionParts.propertyExpressionBody + "\".");
if (parts.length >= 3)
throw new Error("Expression body is not a simple property expression. Body: \"" + expressionParts.propertyExpressionBody + "\".");
var expressionToken = parts[0];
var property = parts[1];
if (expressionParts.token != expressionToken)
throw new Error("Expression body is not correlated to token. Body: \"" + expressionParts.propertyExpressionBody + "\", token: \"" + expressionParts.token + "\".");
var restrictedSymbols = [",", "(", ")", "{", "}", "+", "-", "!", "?", "<", ">", "*", "/", "="];
restrictedSymbols.forEach(function (symbol) {
if (property.indexOf(symbol) >= 0)
throw new Error("Invalid property expression. Body: \"" + expressionParts.propertyExpressionBody + "\".");
});
return property;
};
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(__webpack_require__(1));
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
"use strict";
var Internals = __webpack_require__(2);
exports.apply = function (expression, target) {
exports.validate(expression);
return expression(target);
};
exports.validate = function (expression) {
var sourceCode = expression.toString();
Internals.validate(sourceCode);
};
exports.getProperty = function (expression) {
var sourceCode = expression.toString();
return Internals.getProperty(sourceCode);
};
/***/ },
/* 2 */
/***/ function(module, exports) {
"use strict";
var ArrowToken = "=>";
var FunctionToken = "function";
var ReturnToken = "return";
exports.validate = function (sourceCode) {
if (isLambda(sourceCode))
exports.validateLambda(sourceCode);
else
exports.validateFunction(sourceCode);
};
exports.getProperty = function (sourceCode) {
var expressionParts;
if (isLambda(sourceCode))
expressionParts = getLambdaExpressionParts(sourceCode);
else
expressionParts = getFunctionExpressionParts(sourceCode);
return getPropertyFromParts(expressionParts);
};
var isLambda = function (sourceCode) {
return sourceCode.indexOf(ArrowToken) >= 0;
};
exports.validateLambda = function (sourceCode) {
var expressionParts = getLambdaExpressionParts(sourceCode);
validatePropertyExpressionBody(expressionParts);
};
var getLambdaExpressionParts = function (sourceCode) {
var parts = sourceCode
.split(ArrowToken)
.map(function (part) { return part.trim(); });
if (parts.length == 1)
throw new Error("Source code doesn't contain arrow (" + ArrowToken + "). Code: \"" + sourceCode + "\".");
if (parts.length >= 3)
throw new Error("Source code contains too much arrows (" + ArrowToken + "). Code: \"" + sourceCode + "\".");
var token = parts[0];
var propertyExpressionBody = parts[1];
return {
token: token,
propertyExpressionBody: propertyExpressionBody
};
};
exports.validateFunction = function (sourceCode) {
var expressionParts = getFunctionExpressionParts(sourceCode);
validatePropertyExpressionBody(expressionParts);
};
var getFunctionExpressionParts = function (sourceCode) {
var code = sourceCode.trim();
if (code.indexOf(FunctionToken) != 0)
throw Error("Expression is not a function. Code: \"" + sourceCode + "\".");
code = code
.substr(FunctionToken.length)
.trim();
if (code.indexOf("(") != 0)
throw Error("Expression is not a function. Code: \"" + sourceCode + "\".");
var indexOfTokenEnd = code.indexOf(")");
if (indexOfTokenEnd < 0)
throw Error("Expression is not a function. Code: \"" + sourceCode + "\".");
var token = code.substr(1, indexOfTokenEnd - 1);
if (token.indexOf(",") >= 0)
throw Error("Expression is not a single argument function. Code: \"" + sourceCode + "\".");
var body = code
.substr(indexOfTokenEnd + 1)
.replace("{", "")
.replace("}", "")
.replace(";", "")
.trim();
if (body.indexOf(ReturnToken) < 0)
throw Error("Expression is not a single return statement. Code: \"" + sourceCode + "\".");
body = body
.substr(ReturnToken.length)
.trim();
return {
token: token,
propertyExpressionBody: body
};
};
var validatePropertyExpressionBody = function (expressionParts) {
if (!expressionParts.token)
throw new Error("Expression token is empty.");
if (!expressionParts.propertyExpressionBody)
throw new Error("Expression body is empty.");
var property = getPropertyFromParts(expressionParts);
if (!property)
throw new Error("Expression body has no property.");
};
var getPropertyFromParts = function (expressionParts) {
if (!expressionParts.token)
throw new Error("Expression token is empty.");
if (!expressionParts.propertyExpressionBody)
throw new Error("Expression body is empty.");
var parts = expressionParts.propertyExpressionBody
.split(".")
.map(function (part) { return part.trim(); });
if (parts.length == 1)
throw new Error("Expression body is not a property expression. Body: \"" + expressionParts.propertyExpressionBody + "\".");
if (parts.length >= 3)
throw new Error("Expression body is not a simple property expression. Body: \"" + expressionParts.propertyExpressionBody + "\".");
var expressionToken = parts[0];
var property = parts[1];
if (expressionParts.token != expressionToken)
throw new Error("Expression body is not correlated to token. Body: \"" + expressionParts.propertyExpressionBody + "\", token: \"" + expressionParts.token + "\".");
var restrictedSymbols = [",", "(", ")", "{", "}", "+", "-", "!", "?", "<", ">", "*", "/", "="];
restrictedSymbols.forEach(function (symbol) {
if (property.indexOf(symbol) >= 0)
throw new Error("Invalid property expression. Body: \"" + expressionParts.propertyExpressionBody + "\".");
});
return property;
};
/***/ }
/******/ ]);
\ No newline at end of file
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
__export(require('./expression/expression'));
{
"name": "expression-ts",
"version": "1.0.1",
"version": "1.0.2",
"description": "TypeScript expressions interface",
"main": "index.js",
"typings": "index",
......
"use strict";
var ArrowToken = "=>";
var FunctionToken = "function";
var ReturnToken = "return";
exports.validate = function (sourceCode) {
if (isLambda(sourceCode))
exports.validateLambda(sourceCode);
else
exports.validateFunction(sourceCode);
};
exports.getProperty = function (sourceCode) {
var expressionParts;
if (isLambda(sourceCode))
expressionParts = getLambdaExpressionParts(sourceCode);
else
expressionParts = getFunctionExpressionParts(sourceCode);
return getPropertyFromParts(expressionParts);
};
var isLambda = function (sourceCode) {
return sourceCode.indexOf(ArrowToken) >= 0;
};
exports.validateLambda = function (sourceCode) {
var expressionParts = getLambdaExpressionParts(sourceCode);
validatePropertyExpressionBody(expressionParts);
};
var getLambdaExpressionParts = function (sourceCode) {
var parts = sourceCode
.split(ArrowToken)
.map(function (part) { return part.trim(); });
if (parts.length == 1)
throw new Error("Source code doesn't contain arrow (" + ArrowToken + "). Code: \"" + sourceCode + "\".");
if (parts.length >= 3)
throw new Error("Source code contains too much arrows (" + ArrowToken + "). Code: \"" + sourceCode + "\".");
var token = parts[0];
var propertyExpressionBody = parts[1];
return {
token: token,
propertyExpressionBody: propertyExpressionBody
};
};
exports.validateFunction = function (sourceCode) {
var expressionParts = getFunctionExpressionParts(sourceCode);
validatePropertyExpressionBody(expressionParts);
};
var getFunctionExpressionParts = function (sourceCode) {
// "function (x){ return x.test; }"
var code = sourceCode.trim();
if (code.indexOf(FunctionToken) != 0)
throw Error("Expression is not a function. Code: \"" + sourceCode + "\".");
code = code
.substr(FunctionToken.length)
.trim();
// "(x){ return x.test; }"
if (code.indexOf("(") != 0)
throw Error("Expression is not a function. Code: \"" + sourceCode + "\".");
var indexOfTokenEnd = code.indexOf(")");
if (indexOfTokenEnd < 0)
throw Error("Expression is not a function. Code: \"" + sourceCode + "\".");
var token = code.substr(1, indexOfTokenEnd - 1);
if (token.indexOf(",") >= 0)
throw Error("Expression is not a single argument function. Code: \"" + sourceCode + "\".");
var body = code
.substr(indexOfTokenEnd + 1)
.replace("{", "")
.replace("}", "")
.replace(";", "")
.trim();
// "return x.test"
if (body.indexOf(ReturnToken) < 0)
throw Error("Expression is not a single return statement. Code: \"" + sourceCode + "\".");
body = body
.substr(ReturnToken.length)
.trim();
// "x.test"
return {
token: token,
propertyExpressionBody: body
};
};
var validatePropertyExpressionBody = function (expressionParts) {
if (!expressionParts.token)
throw new Error("Expression token is empty.");
if (!expressionParts.propertyExpressionBody)
throw new Error("Expression body is empty.");
var property = getPropertyFromParts(expressionParts);
if (!property)
throw new Error("Expression body has no property.");
};
var getPropertyFromParts = function (expressionParts) {
if (!expressionParts.token)
throw new Error("Expression token is empty.");
if (!expressionParts.propertyExpressionBody)
throw new Error("Expression body is empty.");
var parts = expressionParts.propertyExpressionBody
.split(".")
.map(function (part) { return part.trim(); });
if (parts.length == 1)
throw new Error("Expression body is not a property expression. Body: \"" + expressionParts.propertyExpressionBody + "\".");
if (parts.length >= 3)
throw new Error("Expression body is not a simple property expression. Body: \"" + expressionParts.propertyExpressionBody + "\".");
var expressionToken = parts[0];
var property = parts[1];
if (expressionParts.token != expressionToken)
throw new Error("Expression body is not correlated to token. Body: \"" + expressionParts.propertyExpressionBody + "\", token: \"" + expressionParts.token + "\".");
var restrictedSymbols = [",", "(", ")", "{", "}", "+", "-", "!", "?", "<", ">", "*", "/", "="];
restrictedSymbols.forEach(function (symbol) {
if (property.indexOf(symbol) >= 0)
throw new Error("Invalid property expression. Body: \"" + expressionParts.propertyExpressionBody + "\".");
});
return property;
};
module.exports = {
entry: './ts/index.ts',
entry: './ts/expression/expression.ts',
output: {
filename: './js/index.js'
filename: './js/expression/expression.js'
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment