passing a dynamic value in between get api url - javascript

How to pass a dynamic value in between get api url?
I have a ref that is connected to the input and want value of that input to be passed into a string.
This does not work:
const location = useRef("Warsaw");
"http://api.weatherstack.com/current?&query={location.current.value}"

You have incorrect dynamic string syntax. You should use template literals:
const location = useRef("Warsaw");
let url = `http://api.weatherstack.com/current?&query=${location.current.value}`

You can use template literals
const location = useRef("Warsaw");
const uri = `http://api.weatherstack.com/current?&query=${location.current.value}`

Related

How to extract english words from a string of hashed values?

My web application uses google firestore to upload documents. However, firestore hashes the documents name but I want to extract that name and use it. The formatted string looks like this.
const url = "https://storage.googleapis.com/test/agency-register-crrRhaUSWopn4QRc2M54MX.png";
const url = "https://storage.googleapis.com/test/agency-register-documents-crrRhaUSWopn4QRc2M54MX.png";
const url = "https://storage.googleapis.com/test/agencyRegister-crrRhaUSWopn4QRc2M54MX.png";
const url = "https://storage.googleapis.com/test/agency-crrRhaUSWopn4QRc2M54MX.png";
As you can see, after test/ the file name comes first and before the files type there is a hash and finally the file type which is .png here. How can I get the english words(agency-register, agency-register-documents, agencyRegister, et.) or the file name from these strings? I'm trying to create a dynamic structure.
I don't know the required logic, so I didn't do anything.
Sounds like a job for a regular expression.
For example:
let pattern = /https:\/\/storage\.googleapis\.com\/test\/([\w-]*)-\w*.png/;
let url = "https://storage.googleapis.com/test/agency-register-crrRhaUSWopn4QRc2M54MX.png";
let name = url.match(pattern)[1];
To see what it does, https://regex101.com/r/7TcJuI/1
If you don't want to solve it with regular expressions.
You can do it the way suggested in the comment.
const url = "https://storage.googleapis.com/test/agencyRegister-crrRhaUSWopn4QRc2M54MX.png";
const parts = url.split("test/")[1].split("-");
parts.pop();
const result = parts.join("-");
console.log(result);

How to Split Querystring in JS

I am trying to split the value from querystring using window.location.search after that I am getting the below value like
'?path=/dev/dev-alert-banner--alert-banner&args=alertBannerType:dark'
I tried to use like window.location.search.split('/').pop() but i am getting the value like dev-alert-banner--alert-banner&args=alertBannerType:dark
how can i get the value like
dev-alert-banner--alert-banner
using JS
Use the built-in URLSearchParams API to parse the QS for you - then you can do split with pop:
const value = '?path=/dev/dev-alert-banner--alert-banner&args=alertBannerType:dark';
const params = new URLSearchParams(value);
console.log(params.get("path").split("/").pop());

Find variable substring after known characters inside a string

I have the following RRULE in a string format:
DTSTART;TZID=America/Toronto:20160909T040000RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;UNTIL=20161202T040000
I want to parse the properties of the string into their own respective variables to use inside form inputs to update. The same RRULE properties are going to show up in every string so I know for example that DTSTART will always be in the string.
I thought about using the string method search by specifying the property by its name and then adding the number of characters to add to get the position of the entity and want and then use .substring()
So for example, if I was trying to extract UNTIL, I could do:
export const parseUntilFromRRule = (rrule: string):Date => {
const posInRRule = rrule.search("UNTIL=");
const until = rrule.substring(posInRRule + 6);
return new Date(until);
};
However, for properties in the middle of the string, where the value's length may vary, this method would not work because I would not know the value of the second parameter to pass into substring
What generalized technique can I use to extract each RRULE property from the string?
I would use string split twice here:
var input = "DTSTART;TZID=America/Toronto:20160909T040000RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;UNTIL=20161202T040000";
var rrule = input.split("RRULE:")[1].split(";")[0];
console.log(rrule);
You can split by semicolons, then split each result by = if an entry contains a =, and turn the result into an object:
const input = `DTSTART;TZID=America/Toronto:20160909T040000RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;UNTIL=20161202T040000`;
const segments = input.split(';');
const entryKeyValues = Object.fromEntries(
segments.map(
segment => segment.includes('=')
? segment.split('=')
: [segment, '']
)
);
console.log(entryKeyValues);
console.log(entryKeyValues.UNTIL);

How to get URL parameters names only and not values

There are hundreds of example out here showing how to get URL parameters but I couldn't find any to list ONLY parameters (variables) name and not values?
For example, if I have something like:
www.mydomain.com/filtres?makes=honda,mazda,jeep&models=crv,cx5,wamgler&years=2008
I want to get only makes,models,years and if URL change to
www.mydomain.com/filtres?makes=honda,mazda,jeep&models=crv,cx5,wamgler
should be like makes,models
or on
www.mydomain.com/filtres?makes=honda,mazda,jeep&models=crv,cx5,wamgler&years=2008,2010&city=vancouver
the out should be like makes,models,years,city
You can use URLSearchParams
Sample:
const queryString = "?makes=honda,mazda,jeep&models=crv,cx5,wamgler&years=2008"
const urlParams = new URLSearchParams(queryString);
console.log([...new URLSearchParams(urlParams).keys()]);
console.log(
[...new URLSearchParams(window.location.search).keys()].join(',')
);

use express.js to get the query string of a url

I'm looking for a way to get the query string part of a url using node.js or one of it's extension module.
I've tried using url and express but they give me an array of parameters.
I want to original string.
Any ideas ?
example; for:
http://www.mydom.com?a=337&b=33
give me
a=337&b=33
(with or without the ?)
Use url.parse. By default it will return an object whose query property is the query string. (You can pass true as the second argument if you want query to be an object instead, but since you want a string the default is what you want.)
var url = require('url');
var urlToParse = 'http://www.mydom.com/?a=337&b=33';
var urlObj = url.parse(urlToParse);
console.log(urlObj.query);
// => a=337&b=33
How about using the built-in url.parse method?

Categories

Resources