Sammy.js trailing slashes - sammy.js

I have this route:
this.get('/#week/:week', function() {...})
which works for /#week/4 but not for /#week/4/
Any good ideas of how to leave the trailing slash optional in Sammy.js?
I'm now trying regex close to this:
this.get(/#week\/:week\/?/, function() {...})
but without any success.
Thanks!

I ran into the same issue. Probably too late, but try replacing :week with something like this:
([^\/]*)
and then use
this.params['splat']
See http://sammyjs.org/docs/routes, search for Regexp. So your route would be:
this.get(/#week\/([^\/]*)\/?/, function() {
console.log(this.params['splat']);
})
Not sure exactly how you'd manage if you had multiple id's though...

The best solution would be to add /?, making your
route:this.get('/#week/:week/?', function() {...})

Related

What is the best way to assert that a component contains a string "case insensitive" using Cypress?

I want to assert that a component contains a string without caring about the string case.
For example, I want
cy.get('#label').should('contain.text', 'integrator');
to pass even if the label contains "Integrator."
What is the best way I can make this assertion?
You can also use cy.contains() with a regular expression
cy.contains('#label', /integrator/i) // should is implied in this command
or as an option
cy.contains('#label', 'integrator', {matchCase:false})
With should() you get retry of the expect()
cy.get('#label')
.should($el => {
expect($el.text().toLowerCase()).to.eq('integrator') // exact
// or
expect($el.text().toLowerCase()).to.contain('integrator') // partial
})
What you need is Regular expressions.
You can use the match assertion:
cy.get('#label')
.invoke('text')
.should('match', /integrator/i) //i = case sensitive
You can do like this as well:
cy.get('#label').then(($ele) => {
expect($ele.text().toLowerCase()).to.contain('integrator')
})

Why does Cypress.io use string assertion type selectors, what are alternatives?

I need a e2e test tool, among those I tried I think Cypress.io seems to be the best.
What I don't understand is why they use string selectors in should() clause like this (I need our testers to have easy time as possible, and I don't think error-prone strings are the way to go):
cy.get('.trigger-input-range')
.invoke('val', 25)
.trigger('change')
.get('input[type=range]').siblings('p')
.should('have.text', '25')
I've been able to sucessfuly use something like this:
cy.get('.trigger-input-range')
.invoke('val', 25)
.trigger('change')
.get('input[type=range]').siblings('p')
.should((e) => {
expect(e).to.have.text('25')
});
this at least gives me some safety with IDE offering some options like "have.text", "have.value" (I plan to use Typescript for these tests, so any suggestions that may help are welcome. Does this have some side-effect? If no, I don't understand them using those string selectors.
Is there any other option?
Could I somehow modify Cypress to have something like this?
cy.get('.trigger-input-range')
.invoke('val', 25)
.trigger('change')
.get('input[type=range]').siblings('p')
.shouldHaveText('25')
.shouldHaveValue('25');
.should('have.text', '25');
is analogous to this:
.should(elem => {
expect(elem).to.have.text('25');
});
They are simply two ways of doing the same thing. The shorter one is an inline way to use the Chai assertion from the longer one, but it will produce the same result.
If you want a command like .shouldHaveText('25'), you should be able to get it by creating your own custom command:
Cypress.Commands.add('shouldHaveText', {
prevSubject: true
}, (subject, expectedText) => {
// Wrap the subject and use .should() to take advantage of automatic retries
cy.wrap(subject).should((elem) => {
expect(elem).to.have.text(expectedText);
});
});
Usage:
cy.get('.someClass').shouldHaveText('25');

remove `\"` from string rails 4

I have params like:
params[:id]= "\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\""
And i want to get expected result as below:
"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
How can I do this?
You can use gsub:
"\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\"".gsub("\"", "")
=> "ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
Or, as #Stefan mentioned, delete:
"\"ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6\"".delete("\"")
=> "ebfd11a9-3aa4-415a-ba72-1b6796ea1bf6"
If this is JSON data, which it could very well be in that format:
JSON.load(params[:id])
This handles things where there's somehow escaped strings in there, or the parameters are an array.
Just Use tr!
params[:id].tr!("\"","")
tr! will also change the main string
In case you do not want to change main string just use :
params[:id].tr("\"","")
Thanks Ilya

How to prevent CKEditor replacing spaces with ?

I'm facing an issue with CKEditor 4, I need to have an output without any html entity so I added config.entities = false; in my config, but some appear when
an inline tag is inserted: the space before is replaced with
text is pasted: every space is replaced with even with config.forcePasteAsPlainText = true;
You can check that on any demo by typing
test test
eg.
Do you know how I can prevent this behaviour?
Thanks!
Based on Reinmars accepted answer and the Entities plugin I created a small plugin with an HTML filter which removes redundant entities. The regular expression could be improved to suit other situations, so please edit this answer.
/*
* Remove entities which were inserted ie. when removing a space and
* immediately inputting a space.
*
* NB: We could also set config.basicEntities to false, but this is stongly
* adviced against since this also does not turn ie. < into <.
* #link http://stackoverflow.com/a/16468264/328272
*
* Based on StackOverflow answer.
* #link http://stackoverflow.com/a/14549010/328272
*/
CKEDITOR.plugins.add('removeRedundantNBSP', {
afterInit: function(editor) {
var config = editor.config,
dataProcessor = editor.dataProcessor,
htmlFilter = dataProcessor && dataProcessor.htmlFilter;
if (htmlFilter) {
htmlFilter.addRules({
text: function(text) {
return text.replace(/(\w) /g, '$1 ');
}
}, {
applyToAll: true,
excludeNestedEditable: true
});
}
}
});
These entities:
// Base HTML entities.
var htmlbase = 'nbsp,gt,lt,amp';
Are an exception. To get rid of them you can set basicEntities: false. But as docs mention this is an insecure setting. So if you only want to remove , then I should just use regexp on output data (e.g. by adding listener for #getData) or, if you want to be more precise, add your own rule to htmlFilter just like entities plugin does here.
Remove all but not <tag> </tag> with Javascript Regexp
This is especially helpful with CKEditor as it creates lines like <p> </p>, which you might want to keep.
Background: I first tried to make a one-liner Javascript using lookaround assertions. It seems you can't chain them, at least not yet. My first approach was unsuccesful:
return text.replace(/(?<!\>) (?!<\/)/gi, " ")
// Removes but not <p> </p>
// It works, but does not remove `<p> blah </p>`.
Here is my updated working one-liner code:
return text.replace(/(?<!\>\s.)( (?!<\/)|(?<!\>) <\/p>)/gi, " ")
This works as intended. You can test it here.
However, this is a shady practise as lookarounds are not fully supported by some browsers.
Read more about Assertions.
What I ended up using in my production code:
I ended up doing a bit hacky approach with multiple replace(). This should work on all browsers.
.trim() // Remove whitespaces
.replace(/\u00a0/g, " ") // Remove unicode non-breaking space
.replace(/((<\w+>)\s*( )\s*(<\/\w+>))/gi, "$2<!--BOOM-->$4") // Replace empty nbsp tags with BOOM
.replace(/ /gi, " ") // remove all
.replace(/((<\w+>)\s*(<!--BOOM-->)\s*(<\/\w+>))/gi, "$2 $4") // Replace BOOM back to empty tags
If you have a better suggestion, I would be happy to hear 😊.
I needed to change the regular expression Imeus sent, in my case, I use TYPO3 and needed to edit the backend editor. This one didn't work. Maybe it can help another one that has the same problem :)
return text.replace(/ /g, ' ');

Mutliple URL Segments to Index Function With CodeIgniter

Please excuse me if this is an incredibly stupid question, as I'm new to CodeIgniter.
I have a controller for my verification system called Verify. I'd like to be able to use it something like site.com/verify/123/abcd, but I only want to use the index function, so both URL segments need to go to it.
I'm sure this can be done with URL routing somehow, but I can't figure out how to pass both URL segments into Verify's index function..
Something like this in routes.php should do the job:
$route['verify/(:any)/(:any)'] = "verify/index/$1/$2";
I'm pretty sure you can just pass any controller method in CodeIgniter multiple arguments without modifying routes or .htaccess unless I misunderstood the problem.
function index($arg_one, $arg_two)
{
}
$arg_one representing the 123 and $arg_two representing the abcd in your example URI.
You will either need to edit the routes or write an htaccess rule, however i didn't understand why you want to limit to just the index function.
If you didnt wanna use routes for some reason, then you could add this function to the controller in question.
public function _remap($method_in, $params = array()) {
$method = 'process_'.$method_in;
if (method_exists($this, $method)) {
return call_user_func_array(array($this, $method), $params);
}
array_unshift($params, $method_in);
$this->index($params);
}
Basically it does the same as default behavior in CI, except instead of sending a 404 on 'cant find method', it sends unfound method calls to the index.
You would need to alter your index function to take an array as the first argument.
OR if you know that you only ever want 2 arguments, you could change the last 2 lines to
$this->index($method_in, $params[0]);
Of course both solutions fail in someone uses an argument which is the same as a method in your controller.

Resources