String 'Regex' Validation for below scenario - javascript

var variables = ['A','B'];
var allowedMathFunc = ['Sin','Cos','Tan']
var expression= 'Sin(B)+Tan(B)+100+acos(A)+C'
I want to validate expression string match below scenario
expression should use variables array values
expression should not contain other then allowed math functions.
I tried below code
var variables = ['A','B'];
var allowedMathFunc = ['Sin','Cos','Tan']
var expression= 'Sin(B)+Tan(B)+100+acos(A)'
for variable check I tried this
let expressionVariables = value.replace(/Sin|Log|Exp|Tan|Pos|Rnd|[^A-Za-z]/ig,"");
let expressionUnUsedVar= variables.filter(v=> !expressionVariables.includes(v));
I don't know how to write for both scenario for regex it's not a problem for two different regex.

You can use regex to extract all variables and function names, and then check them if they're included in the allowed function and variable lists (case sensitive):
function testExpression(expression, vars, funcs) {
const usedVariables = expression.match(/\b[a-z]+\b(?!\s*\()/gi);
const usedFunctions = expression.match(/\b[a-z]+\b(?=\s*\()/gi);
return usedVariables?.every(v => vars.includes(v)) && usedFunctions?.every(func => funcs.includes(func));
}
var variables = ['A','B'];
var allowedMathFunc = ['Sin','Cos','Tan']
var expression= 'Sin(B)+Tan(B)+100+acos(A)+C'
console.log(testExpression(expression, variables, allowedMathFunc));
console.log(testExpression('Sin(B)+Tan(B)+100+Cos(A)+C', ['A','B','C'], ['Sin','Cos','Tan']));

Related

Get all match values from two comma separated string

I have two comma separated string as follows,
var hiddenString = '14172,10062,14172,14172,100,10,14172,15000,12000';
var strB = '14172,10062,10064,10025,100,14182';
I need to create another string based on the above two,
if hiddenString have unmatching value with strB,then without those unmatched values need to create e new string and also avoid duplicates.
simply says, I need to get all the matching values from both strings.
As the example based on my two string, I'm expecting the following:
varFinalHiddenString = 14172,10062,100;
How can I do this using JavaScript and that should work in safari and IE 11 or its earlier versions. Please help me, I'm new to the JS.
You can first split() strings to generate arrays from them. Then filter() the smaller array by checking the index of the current item with indexOf() in other array:
var hiddenString = '14172,10062,14172,14172,100,10,14172,15000,12000';
var strB = '14172,10062,10064,10025,100,14182';
var temp1 = hiddenString.split(',');
var temp2 = strB.split(',');
var varFinalHiddenString = temp2.filter(function(s){
return temp1.indexOf(s) > -1;
}).join(',');
console.log(varFinalHiddenString);
Make arrays of the strings, then use the "filter" method. Then convert back to string.
var hiddenString = '14172,10062,14172,14172,100,10,14172,15000,12000';
var strB = '14172,10062,10064,10025,100,14182';
var hiddenStringAsArray = hiddenString.split(',');
var strBArray = strB.split(',');
var resultObject = $(strBArray).filter(hiddenStringAsArray);
var resultArray = resultObject.toArray();
var resultString = resultArray.join(',');
console.log(resultString);

Retrieve value of variables from a string of declarations

I have the following string named abc (which consists of javascript variables declarations):
var abc = `
var exp = 'test';
var test = 15;
var test2 = "wow";
`;
I would like to get the value of exp, test, test2 from this string.
One of the methods that might work is:
eval(abc);
However this solution is not suitable in typescript for security purposes, what other methods would you recommend ( feel free to propose some npm libraries) ?
Creating a function and passing the variable.
new Function has a closed scope in comparison to function eval.
var abc = `
var exp = 'test';
var test = 15;
var test2 = "wow";
`;
var fn = (variable) => (new Function('', `${abc} return ${variable};`))();
console.log(fn('exp'));
console.log(fn('test'));
console.log(fn('test2'));
One way to do that would be the following:
First we find the declarations by splitting the string using ; as delimiter
Now after we have found the declarations we use map() to extract the values of each one and return it into a new array.To find the value of a declaration we split it into 2 parts using '='.The first part is the declaration name and the second part is the declaration value
var abc = `var exp = 'test';
var test = 15;
var test2 = "wow";`;
//Trim string
abc = abc.trim();
//Split by ';'
var declarations = abc.split(';');
//The last item is empty so we remove it
declarations.pop();
console.log(declarations)
//Now that we have the declarations we need to get the values
//so we split each declaration using '='
var values = declarations.map(function(dec){
var value = dec.split("=").pop()
return value;
});
//Bonus
//This gets all declarations from a string and returns them as a key value object
function getDeclarations(s) {
var variables = s.split(";");
//Last variable is an empty string so pop it
variables.pop()
var declarations = {};
variables.forEach(function (variable) {
variable = variable.trim()
var name = variable.split("=")[0].split("var")[1];
var value = variable.split("=").pop();
name = name.trim();
value = value.trim();
declarations[name] = value;
});
return declarations;
}
console.log(values)
console.log(getDeclarations(abc))

replace string in json

I am new to javascript and have this code which will replace the string from A to B, but if there is multiple records of As, it will only replace the first A, while the remaining will be remain as A. Note that the stringify is called twice.
"success": function(json) {
var old = JSON.stringify(json).replace('"新交易"', '"待审核"');
var newdata = JSON.parse(old);
var old = JSON.stringify(newdata).replace('"批准"', '"已充值"');
var newdata = JSON.parse(old);
fnCallback(newdata);
}
This has little to do with JSON. As documented:
To perform a global search and replace, include the g switch in the regular expression.
So change this:
replace('"新交易"', '"待审核"')
... into this:
replace(/"新交易"/g, '"待审核"')
To replace every word in your context use Regular Expressions. So check this example to see how it works:
var someText = '"新交易""新交易""新交易""新交易""新交易""新交易""新交易""新交易"';
var someText2 = '"批准""批准""批准""批准""批准""批准""批准""批准""批准""批准"';
var old = someText.replace(/"新交易"/g, '"replaced"');
var stuff = someText2.replace(/"批准"/g, '"已充值"');
https://jsfiddle.net/n1otvpy1/

is there a way to use newlines in xdmp.eval()

Hi I want to write clean code that I can read and have a good overview.
So I wrote this:
var id = '12345';
var coll = ['scc-roles','scc-proj-' + id];
var spm = 'some-role';
var data = {role : spm, roleNames : 'sccss-user', collection : coll}
var spmRoleId = xdmp.eval('declareUpdate();
var sec = require("/MarkLogic/security.xqy");
var roleId = sec.createRole(role, "Generated project member", roleNames, null, collection,null,null);
var uri = "http://marklogic.com/xdmp/roles/" + roleId;
xdmp.documentAddCollections(uri,collection)',data,{"database" : xdmp.securityDatabase()})
But apparently a newline is not allowed in xdmp.eval() ?
[javascript] JS-JAVASCRIPT: + 'var sec = require("/MarkLogic/security.xqy"); -- Error running JavaScript request: SyntaxError: Unexpected token ILLEGAL
I tried using a '+' sign to generate a strng over more then one line, swapping single and double quotes but no luck.
Being able to test this code (copy paste) to the security database makes a lot of sense to me...
If I wrap it all in one unreadable line , it works ok.
hugo
The way to effectively create a new line in a JavaScrit string is to escape the new line char like this
var str = "I'm displayed\
in two line";
In the final file, you will see effectively a new line.
If you want see in the dist output the new line but not in your src string you could just insert the \n equivalent of a return to line.
var str = "I'm displayed\n in two line";
In es6 you will be able to use ` char to achieve the same thing without \
var str = `I'm displayed
in two line`;
Maybe you would like the strange, yet useful array-notation way of doing this:
var multiline1 = [
'the lazy fox',
'jumped over',
'the dead chicken',
].join('\n');
and the result:
the lazy fox
jumped over
the dead chicken
In general, you should avoid string concatenation to build code for eval. Strings make it difficult to spot bugs and are a great vector for injection attacks. Instead, I'd advise you to write a proper function in XQuery or JavaScript and use xdmp.invokeFunction to evaluate it. invokeFunction takes all of the same options as xdmp.eval.
Here's an example that gets roles in the context of a security database. The applyAs function returns a function that wraps the function provided by the caller, evaluating it with the eval options provided.
function applyAs(fct, options) {
return function() {
var params = Array.prototype.slice.call(arguments);
// Curry the function to include the params by closure.
// xdmp.invokeFunction requires that invoked functions have
// an arity of zero.
var f = (function() {
return fct.apply(null, params);
}).bind(this);
// Allow passing in user name, rather than id
if(options.user) { options.userId = xdmp.user(options.user); delete options.user; }
// Allow the functions themselves to declare their transaction mode
if(fct.transactionMode && !(options.transactionMode)) { options.transactionMode = fct.transactionMode; }
return xdmp.invokeFunction(f, options); // xdmp.invokeFunction returns a ValueIterator
}
}
/**
* Gets an Array of id-name Objects. Requires privileged access to security.
*
* #param names An optional Array of role IDs as strings used to filter
* #return An Array of Objects with role ID keys and role name values
*/
function getRoles(names) {
var sec = require('/MarkLogic/security.xqy');
var db = {database: xdmp.securityDatabase()};
var roleIDs = applyAs(sec.getRoleIds, db);
var rolesItr;
if(Array.isArray(names)) {
rolesItr = roleIDs(xdmp.arrayValues(names));
} else {
rolesItr = roleIDs();
}
var roleNames = applyAs(sec.getRoleNames, db)(rolesItr).toArray().map(function(el) { return el.textContent; });
var roles = [];
var i = 0;
for(var role of rolesItr) {
var r = {}
r[role.textContent] = roleNames[i++];
roles.push(r);
}
return roles;
}
getRoles();
Originally from a gist.

Storing functions within arrays

I'm having a issue with something very simple. I am just wondering as to I can store these functions within an array. Check out some of the code below. I am unsure as to if this is correct as to how I am storing these functions. I am unsure as to if these functions should be within a object literal or array.This is not necessarily for a project, just good practice. Thanks!
//declaring a function
function alert_name(){
//declaring variables within a function asking user their name.
var username = prompt("Hey there, what is your name."," ");
//generating user input
var chameleon = "Welcome " + username;
//combinators
//alert("Welcome " + chameleon+ ", This is 'the website");
};
// inserting quotes into a string that is being alerted from the browser.
function otherTHings(){
var single = 'He said \'RUN\' ever so softly.';
//alert(single);
};
//running these functions and actually carry out the operations
//that have actually been declared into code above.
//string operations
function string_opertaions(){
var complete = "Com" + "plete";
//alert(complete);
// using combinators to do the same thing.
var sentance1 = "My name is";
var sentance2 = "someone";
var totalsenatces = sentance1 += sentance2;
//alert(totalsenatces);
};
//Booleans or true false values
function booleanys(){
var lying = false;
var truthful = true;
};
//Arrays very important. very similar to a object literal but different.
//Arrays store information or values or varibales/data.
var rack = [];
rack[0] = alert_name();
rack[1] = otherTHings();
rack[2] = string_opertaions();
rack[3] = booleanys();
//alert_name();
//otherTHings();
//string_opertaions();
//booleanys();
You are invoking the function and storing the result!
var rack = [];
rack[0] = alert_name;
rack[1] = otherTHings;
rack[2] = string_opertaions;
rack[3] = booleanys;

Categories