How to fix JSLint insecure ^ error? - jslint

I have following function. What this does is filters character that is allowed in a subdomain. In JSLint I got following error. Is there any way I can do this without JSLint showing error. I know I can ignore error in JSLint settings but Is there any other way I can improve my code to not show JSLint error.
function filterSubDomain(value) {
return value.replace(/[^a-z0-9\-]/ig, '')
.replace(/^[\-]*/, '')
.replace(/[\-]*$/, '')
.toLowerCase();
}

I think this is easy enough -- just a quick head rethread. I'll repeat the comment from above quickly: JSLint wants you to say what you do want rather than what you don't, because saying what you don't want always leaves room for a superset of what you do want to sneak in. That is, JSLint's intention is to force you to code explicitly/precisely.
So instead of replace, you want to use match here. Here's one way, I believe (stealing from MDN's match code a little):
/*jslint sloppy:true, white:true, devel:true */
function filterSubDomain(value) {
var out = value,
re = /[a-z0-9\-]+/gi,
found;
found = value.match(re);
out = found.join("");
// There are better ways to `trim('-')`.
while (0 === out.indexOf("-")) {
out = out.substr(1);
}
while (out.length === out.lastIndexOf("-")+1) {
out = out.slice(0,out.length-1);
}
return out;
}
console.log(filterSubDomain('---For more inform--ation, - see Chapter 3.4.5.1---'));
// Formoreinform--ation-seeChapter3451
There are other ways to trim, but you get the point. No not's in JavaScript regular expressions with JSLint!

Related

MathQuill and set operators

When using MathQuill one needs to type \nsub to get the ⊈ symbol. The latex you get back is \not\subset, which is fine.
But when you try to set back the same \not\subset expression to the MathQuill field, you get a different result : \neg\subset, which translates to a different rendering.
The problem can be reproduced directly on the MathQuill page (http://mathquill.com/) using the browser console :
Any ideas on how to handle or work around this ?
MathQuill will accept \nsub while typing, but also \notsubset, which got me to a simple find-and-replace solution, which does not require any other code change.
Not the best, but it works; the only downside is that the list of text replacements is fixed; the set operators are the one I found; could be more.
// special math pre-processing
var mathFix = {
"\\not\\subset" : "\\notsubset",
"\\not\\supset" : "\\notsupset"
}
var mathTextFixed = '<your latex here>';
Object.keys(mathFix).forEach(function(t1) {
mathTextFixed = mathTextFixed.replace(t1, mathFix[t1]);
});
mathField.latex(mathTextFixed);

roblox LUA Expected 'end' (to close 'than' at line 3), got '='

function teleportTo(placeCFrame)
local plyr = game.Players.LocalPlayer;
if plyr.Character then
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame;
end
end
teleportTo(game:GetService("Workspace").game:GetService("Workspace").Zeppelin.FuelTank1.Tank.CFrame)
my code is here, idk much about coding thanks for helping
and if you told me how to make the player teleport to a moving object it would be so super
The error is telling you what is going on.
When the code was being interpreted line by line, it expected the next symbol to be end, but instead it got =.
That means that something about how you're using the equals sign is incorrect. So when we look at the line :
return plyr.Character.HumanoidRootPart.CFrame = placeCFrame
You cannot assign a value on the same line as a return command. return is used to pipe a value out of a function so it can be used where the function was called.
But I'm pretty sure that isn't what you intended, you just want to set a player's position. So to fix your issue, remove the return.
if plyr.Character then
plyr.Character.HumanoidRootPart.CFrame = placeCFrame
end
The reason you are getting an error is because = is usually defined as setting a value, and no code can be executed after a return or it will error
If you wanted to, you could add return after all the code is done executing

What do I need to add to use monadUserState with alex when parsing?

I am trying to write a program that will understand a language where embedded comments are allowed. Such as:
/* Here's a comment
/* This comment is further embedded */ second comment is closed
Must close first comment */
This should be recognized as a comment (and as such not stop at the first */ it sees unless it has only seen 1 comment opening prior).
This would be an easy issue to fix in C, I could simply have a counter that incremented when it saw comment opens and decrements when it sees a comment close. If the counter is at 0, we're in "code section".
However, without having state in Haskell, it's a little more challenging.
I've read up on monadUserState which supposedly allows to keep track of a state for this exact type of parsing. However, I can't find very much reading material on it aside from the tutorial page on alex.
When I try to compile it gives the error
templates\wrappers.hs:213:16: Not in scope: `alexEOF`
It should be noted that I directly changed from the "basic" wrapper to the "monadUserState" without changing my code (I don't know what to add in order to use it). It says that this must be initialized in the user code:
data AlexState = AlexState {
alex_pos :: !AlexPosn, -- position at current input location
alex_inp :: String, -- the current input
alex_chr :: !Char, -- the character before the input
alex_bytes :: [Byte], -- rest of the bytes for the current char
alex_scd :: !Int, -- the current startcode
alex_ust :: AlexUserState -- AlexUserState will be defined in the user program
}
I'm a bit of a lexxing noob and I'm not at all sure what I should be adding here to make it at least compile... then I can worry about the logic of the thing.
Update: Working example available here: http://lpaste.net/119212
The file "tiger.x" (link) in the alex github repo contains an example of how to track embedded comments using the monadUserState wrapper.
Well, unfortunately that example doesn't compile but the ideas there should work.
Basically, these lines perform embedded comment processing:
<0> "/*" { enterNewComment `andBegin` state_comment }
<state_comment> "/*" { embedComment }
<state_comment> "*/" { unembedComment }
<state_comment> . ;
<state_comment> \n { skip }
As for alexEOF, the idea is to add an EOF token to your token data type:
data Tokens = ... | EOF
and define alexEOF as:
alexEOF = return EOF
See the file tests/tokens_monadUserState_bytestring.x in the alex repo for an example of this.

ActionScript Unexpected Curly Braces/Semicolon?

I'm editing an ActionScript file and I've run into an issue.
When I put the following, everything is fine.
if (x=x) {
//blah
}
If I put this, it says unexpected ; for one line and } for the another:
for (x=x) {
//blah
}
Same with when I put this:
while (x=x) {
//blah
}
Of course I only put those there as examples to test it, because I thought something was wrong with my code. Is ActionScript, in this part of my file, only allowing IF statements or what? I need to do the same long series of steps to two different strings, but I don't want to put the code in there twice. Do I have to make a function?
Read up on the looping syntax here.
The For loop doesn't take a boolean (true/false), it needs a counter, a boolean check for the limit and an increment.
i.e.
for (counter; condition; action){
statements;
}
I've never used action script but I would suggest trying this with
x==x
Since once = is assignment, not a comparison.
if the for loop still does not function try
for(;x==x;){
}
the semicolons tell it that you want to only use the second statement in the for loop declaration, the condition; since for loops use three statements,
for (variable; condition; iterative action)
by placing semicolons before and after x==x you specify only the condition, which seems to be what you're trying to do.
Turns out using any IF or WHILE statements caused the error no matter what was inside.
I was able to accomplish what I wanted by making another function and sending each string though those.
Appreciate the help, voted up on both of y'all.
you have to write it like this:
if(a==x){
// do that
}
for (x=0; x<maxloops; x++){
// do that
}
while(a==x){
}
The = symbol is used to define values to varialbes, while == has to be used when you comparing / checking (i.e. whether this is equal to that). This both applies to IF and WHILE
the FOR LOOP. Let's say that you want to execute the action "do that" 10 times. then you write
for (x=0; x<10; x++){
// do that
}
the first part x=0 is the definition of the counting variable and its initial value
the second part is the condition (run the loop as long as x is less than 10)
the third part is the stepper. (how the counter will raise its value in each loop). x++ is a short way to write x = x +1;

jQuery Mobile Filtered List - only match beginning of string

Im using the jQuery mobile search filter list:
http://jquerymobile.com/test/docs/lists/lists-performance.html
Im having somer performance issues, my list is a little slow to filter on some phones. To try and aid performance I want to change the search so only items starting with the search text are returned.
So 'aris' currently finds the result 'paris' but I want this changed. I can see its possible from the documentation below but I dont know how to implement the code.
http://jquerymobile.com/test/docs/lists/docs-lists.html
$("document").ready( function (){
$(".ui-listview").listview('option', 'filterCallback', yourFilterFunction)
});
This seems to demonstrate how you write and call your own function, but ive no idea how to write it! Thanks
http://blog.safaribooksonline.com/2012/02/14/jquery-mobile-tip-write-your-own-list-view-filter-function/
UPDATE - Ive tried the following in a seperate js file:
$("document").ready( function (){
function beginsWith( text, pattern) {
text= text.toLowerCase();
pattern = pattern.toLowerCase();
return pattern == text.substr( 0, pattern.length );
}
$(".ui-listview").listview('option', 'filterCallback', beginsWith)
});
might look something like this:
function beginsWith( text, pattern) {
text= text.toLowerCase();
pattern = pattern.toLowerCase();
return pattern == text.substr( 0, pattern.length );
}
Basically you compare from 0 to "length" of what you're matching to the source. So if you pass in "test","tester" it will see you're passing in a string of length 4 and then substr "tester" from 0,4, which gives you "test". Then "test" is equal to "test"... so return true. Lowercase them to make it case insensitive.
Another trick to improve filter performance, only filter once they've entered more than 1 character.
edit it appears jQueryMobile's filter function expects that "true" means it was not found... so it needs to be backwards. return pattern != text.substr( 0, pattern.length );
This worked for me. I am using regular expression here so sort of different way to achieve the same thing.
But the reason why my code didn't work initially was that the list item had a lot of spaces at the beginning and at the end (found that it got added on it's own while debugging).
So I do a trim on the text before doing the match. I have a feeling Jonathan Rowny's implementation will also work if we do text.trim() before matching.
$(".ui-listview").listview('option', 'filterCallback', function (text, searchValue) {
var matcher = new RegExp("^" + searchValue, "i");
return !matcher.test(text.trim());
});

Resources