Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have created this function to check the pathname of these 3 products, but I think it looks a bit repetitive, so I'm wondering if there's a way to make it more optimised
function checkPathName() {
return (
window.location.pathname !== '/groovybaby-/241315' &&
window.location.pathname !== '/cleopatra/241162' &&
window.location.pathname !== '/cumulus/528678'
)}
checkPathName();
I'm expecting this function to return false for those pathnames
A better option would be to have a Set of these paths and then check if the pathname is in the set:
const PATHS = new Set([
'/groovybaby-/241315',
'/cleopatra/241162',
'/cumulus/528678',
]);
function checkPathName() {
return !PATHS.has(window.location.pathname)
}
You actually don't even need a separate function for this.
you can create array of URLs and then check if it includes your current path.
function checkPathName() {
let urls = ['/groovybaby-/241315', '/cleopatra/241162', '/cumulus/528678' ];
if ( urls.includes(window.location.pathname) )
return false
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
is it possible to read two different values over one parameter in a function? I need to load two values in one parameter into a function.
Thank you for your help
Yes, it's possible. Simplest example:
function test(params) {
return params.first + params.second;
}
console.log(test({
first: 3,
second: 4
}));
If you care about the names, then:
function test(params) {
var {first, second} = params;
return first + second;
}
console.log(test({
first: 3,
second: 4
}));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
what security measures need to be taken when there's file access involved from client.
For instance they could use ../ to get access to root directory while we have path related functions involved.
What's the safest way to write path related code?
1) First check Poison Null bytes
if (filename.indexOf('\0') !== -1) {
return respond('That was evil.');
}
2) Whitelisting
if (!/^[a-z0-9]+$/.test(filename)) {
return respond('illegal character');
}
3) Preventing Directory Traversal
var rootDirectory = '/var/www/';
var path = require('path');
var filename = path.join(rootDirectory, userSuppliedFilename);
if (filename.indexOf(rootDirectory) !== 0) {
return respond('trying to sneak out of the web root?');
}
Reference:
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I do have jQuery and wouldn't mind using it but here's the code I would like to clean up:
if (parent.uglyChild.uglyChild.uglyChild.uglyChild.data == null) {
parent.uglyChild.uglyChild.uglyChild.uglyChild.data = new app.newData();
}
The only way I know how to clean it up is:
var data = parent.uglyChild.uglyChild.uglyChild.uglyChild.data;
if (data == null) { data = new app.newData()}
I have always wondered if there was a way to do something like:
parent.uglyChild.data = parent.uglyChild.data.isNull ? return : new app.newData();
I can't wait to see the shorthand tricks you guys know! :)
You could do something like
var ugly = parent.uglyChild.uglyChild.uglyChild.uglyChild;
ugly.data == null && (ugly.data = value)
you can cache the nearest object to avoid repetition, thanks to JS's leaky assignments:
if ( (x=parent.uglyChild.uglyChild.uglyChild.uglyChild).data == null) {
x.data = value;
}
If you want to use the ternary operator, you could always reassign the value.
parent.uglyChild.data = (parent.uglyChild.data == null) ? null : value;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list of car models that are in the list as Chevrolet,Ford,BMW, so I enacted this code on them.
cars.getMakes() returns a list with the names mentioned above and they are formatted as such.
cars.getAllModels() returns a similar list with just the car model names.
make is a single string that is either a model name or make name. Depends on users input.
I want to test to see if what the user put in actually exists in my predetermined lists.
if it does; true. If not; False.
function makeCheck(make) {
var models = cars.getAllModels();
var makes = cars.getMakes();
if (make == makes[make]) {
return true;
} else {
return false;
}
}
If you want to check that the variable make passed to the function is in the list of valid makes:
function makeCheck(make) {
var makes = cars.getMakes();
return (makes.indexOf(make) != -1);
}