UNPKG

nextpay-querystring

Version:

Thư viện QueryString của NextPay - Chuyển đổi QueryString thành điều kiện select cho MongoDB và MySQL với kiểm soát bảo mật

91 lines 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toDouble = exports.toInt = exports.toString = exports.toDate = exports.toVariable = exports.toBoolean = exports.toOperator = exports.toClause = exports.toBracket = void 0; const exceptions_1 = require("../utils/exceptions"); const tokens_1 = require("../models/tokens"); const bracketRules = [ { lexeme: '(', type: tokens_1.TokenType.LEFT_PAREN }, { lexeme: ')', type: tokens_1.TokenType.RIGHT_PAREN }, { lexeme: '[', type: tokens_1.TokenType.LEFT_BRACKET }, { lexeme: ']', type: tokens_1.TokenType.RIGHT_BRACKET }, ]; const clauseRules = [ { lexeme: 'and', type: tokens_1.TokenType.AND }, { lexeme: 'or', type: tokens_1.TokenType.OR }, { lexeme: 'not', type: tokens_1.TokenType.NOT }, ]; const operatorRules = [ { lexeme: '!=', type: tokens_1.TokenType.BANG_EQUAL }, { lexeme: '==', type: tokens_1.TokenType.DOUBLE_EQUALS }, { lexeme: '===', type: tokens_1.TokenType.TRIPLE_EQUALS }, { lexeme: '<', type: tokens_1.TokenType.LESS }, { lexeme: '<=', type: tokens_1.TokenType.LESS_EQUAL }, { lexeme: '>', type: tokens_1.TokenType.GREATER }, { lexeme: '>=', type: tokens_1.TokenType.GREATER_EQUAL }, { lexeme: 'in', type: tokens_1.TokenType.IN }, { lexeme: 'nin', type: tokens_1.TokenType.NIN }, { lexeme: 'range', type: tokens_1.TokenType.RANGE }, { lexeme: 'like%', type: tokens_1.TokenType.PREFIX_LIKE }, { lexeme: 'like', type: tokens_1.TokenType.MIDDLE_LIKE }, { lexeme: '%like', type: tokens_1.TokenType.SUFFIX_LIKE }, ]; const booleanRules = [ { lexeme: 'true', type: tokens_1.TokenType.BOOLEAN }, { lexeme: 'false', type: tokens_1.TokenType.BOOLEAN }, ]; // > UTILS FUNCS const toTokenUsingRules = (rules) => (lexeme) => { const matchingRule = rules.find(rule => rule.lexeme === lexeme); return matchingRule ? { type: matchingRule.type, lexeme } : (0, exceptions_1.invalidTokenException)(lexeme); }; const parsableToDouble = (lexeme) => !Number.isNaN(Number(lexeme)); const parsableToInt = (lexeme) => { if (!parsableToDouble(lexeme)) return false; return Number.isInteger(Number(lexeme)); }; // < UTILS FUNCS const toBracket = toTokenUsingRules(bracketRules); exports.toBracket = toBracket; const toClause = toTokenUsingRules(clauseRules); exports.toClause = toClause; const toOperator = toTokenUsingRules(operatorRules); exports.toOperator = toOperator; const toBoolean = toTokenUsingRules(booleanRules); exports.toBoolean = toBoolean; const toVariable = (lexeme) => { if (lexeme.length < 2 || lexeme[0] !== '{' || lexeme[lexeme.length - 1] !== '}') { return (0, exceptions_1.invalidTokenException)(lexeme); } return { type: tokens_1.TokenType.VARIABLE, lexeme }; }; exports.toVariable = toVariable; const toDate = (lexeme) => { if (lexeme.length < 2 || lexeme[0] !== '#' || lexeme[lexeme.length - 1] !== '#') { return (0, exceptions_1.invalidTokenException)(lexeme); } return { type: tokens_1.TokenType.DATE, lexeme }; }; exports.toDate = toDate; const toString = (lexeme) => { if (!lexeme.startsWith("'''") || !lexeme.endsWith("'''")) { return (0, exceptions_1.invalidTokenException)(lexeme); } return { type: tokens_1.TokenType.STRING, lexeme }; }; exports.toString = toString; const toInt = (lexeme) => !lexeme.includes('.') && parsableToInt(lexeme) ? { type: tokens_1.TokenType.INT, lexeme } : (0, exceptions_1.invalidTokenException)(lexeme); exports.toInt = toInt; const toDouble = (lexeme) => parsableToDouble(lexeme) ? { type: tokens_1.TokenType.DOUBLE, lexeme } : (0, exceptions_1.invalidTokenException)(lexeme); exports.toDouble = toDouble; //# sourceMappingURL=lexemeToToken.js.map