I'm try to use toLowerCase() along with .replace() method for a use case but having some difficulty - angularjs

Let's say I have a website url named:
https://clients.website.com/mock-client/matrix
or
https://clients.website.com/mock-client/Matrix
I'm trying to write some AngularJS code that will take the raw url as shown as an example above and remove the /matrix portion only. The thing is...is that it could be /matrix or /Matrix.
My current code looks like this:
var clientNetworkUrl =
https://clients.website.com/mock-client/matrix
or
https://clients.website.com/mock-client/Matrix
vm.click = function() {
var learnMoreUrl = clientNetworkUrl.replace('matrix', '');
$window.open(learnMoreUrl, '_blank');
};
I would like the output of the function to always strip out /matrix whether it's capitalized or not.
I tried lowercasing the whole url and read that might be a bad idea so I want to take a different approach.
Can anyone help me out with this?

Just use a case sensitive regex
var urls =['https://clients.website.com/mock-client/matrix',
'https://clients.website.com/mock-client/Matrix']
urls.forEach(str => console.log(str.replace(/matrix/gi,'')))

Related

TYPO3 10 and Solr : can't modify the Typoscript config through a Viewhelpers

I'm trying to add search options on my search form that would allow the user to ensure that all the words he searched for are in the results, or at least one, or an "exact match".
I've found MinimumMatch and it's perfect for that.
I've made a custom viewhelper that I placed in my Result.html, it takes as parameter the type of search the user wants (atLeastOne, AllWords, etc...) and I've dug a bit in the source code and it seems I can override values of the current Solr Typoscript by passing an array to a mergeSolrConfiguration function.
So I tried something like that as a draft to see how it works :
public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$previousRequest = static::getUsedSearchRequestFromRenderingContext($renderingContext);
$usedQuery = $previousRequest->getRawUserQuery();
$contextConfiguration = $previousRequest->getContextTypoScriptConfiguration();
$override['plugin.']['tx_solr.']['search.']['query.']['minimumMatch'] = '100%';
$contextConfiguration->mergeSolrConfiguration($override, true, true);
return self::getSearchUriBuilder($renderingContext)->getNewSearchUri($previousRequest, $usedQuery);
}
But it simply doesn't work. Solr keeps using the site's Typoscript config instead of my overriden one.
I saw there was a way to also override the Typoscript of the filters, with setSearchQueryFilterConfiguration so I gave it a try with :
public static function renderStatic(array $arguments, Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
{
$previousRequest = static::getUsedSearchRequestFromRenderingContext($renderingContext);
$usedQuery = $previousRequest->getRawUserQuery();
$previousRequest->getContextTypoScriptConfiguration()
->setSearchQueryFilterConfiguration(['sourceId_intS' => 'sourceId_intS:7']);
return self::getSearchUriBuilder($renderingContext)->getNewSearchUri($previousRequest, $usedQuery);
}
But nope, it keeps using the configuration set in the website's template, completely ignoring my overrides.
Am I going the wrong way with this ? Is the thing I'm trying to do not possible technically ?

Reactjs convert input string to upper case

I'm creating an app in Reactjs using react-strap. I would like to convert an input field to upper case.
From googling, it looks like simply appending "toUpperCase()" to the field would work, but this doesn't appear as an option in Visual Studio code.
I had a similar issue with doing a replace all, but finally got that to work using "const" field:
// replace ":" with "-"
const phrase = item.macs;
const replaced = phrase.replace(/:/g, '-')
item.macs = replaced;
However, converting to a const field doesn't work for making the "toUpperCase()" available.
What should I do to turn this into a string so I can call the "toUpperCase()" function?
Edit: change references from "toUpper" to "toUpperCase". The problem is this is not available as a function.
For example of I do
'myString'.toUpperCase();
it works. But it I can't get it to bring that up in Visual Studio Code, and it's ignored if I code it anyway.
I believe you are looking after toUpperCase.
To make a string uppercase in javascript you can call .toUpperCase() method on it. For example
const foo = 'foo'
const fooUpper = foo.toUpperCase()
console.log(fooUpper) // expected result 'FOO'
I got around this problem by forcing the input item to be regarded as a string by prepending it with a '', like so:
item.macs = '' + item.macs;
item.macs = item.macs.replace(/:/g, '-');
item.macs = item.macs.toUpperCase();
After that, all the string functions were available.

Jasmine - How to Write Test for Array with Named properties and Object

I ran into a strange situation today, thanks to Javascript. I have a Object that look something like this.
$scope.main = [{main : 1},service:true];
Now when I try to expect this inside the jasmine test case for equating the Objects :
expect($scope.main).toEqual([{main : 1},service:true]);
This gives me an error :
Unexpected Token.
Strangely, This is a valid object for Javascript. But Jasmine is not able to accept that.
Is there any way to test this?
Thanks in advance!
EDIT : Attaching a structure screenshot.
Update
I see now based on your screenshot that you are creating the main object in multiple steps. I've shortened it to the following:
var main = [{main: 1}];
main.service = true;
In dev-tools, you are seeing main as something that looks like this: [{main: 1}, service: true].
However, don't be mislead. Dev-tools is showing you a structure that is just meant to be informative. You can't actually create that structure in one line of javascript, because it is invalid. You have to create it in multiple steps, like you have.
This is why when you try to create it in your test in one line, you are getting an Unexpected Token. error. In your test, you have to create the expected object in a similar fashion to how you created your main object. For example:
var expected = [{main: 1}];
expected.service = true;
expect(main).toEqual(expected);

Load Delimiter Separated Values in D3

I am trying to load a pipe separated value file in D3 using the generic d3.dsvFormat() function. While I do not get an error, nothing happens which I think is because the callback function is not executed.
I did check the documentation here but did not find it that helpful: https://github.com/d3/d3-dsv/blob/master/README.md#dsvFormat
Is there a problem with my syntax?
var psv = d3.dsvFormat("|");
psv.parse("my_file.txt", function(error, data) {
Do a bunch of stuff with the data....
console.log(data); //nothing happens here
});
psv.parse wants text, you are passing text - just that it doesn't have any pipe in it: "my_file.txt". As this is one line of text and the first line of text translates into column names, you won't get any data with this.
Your problem is that psv.parse won't fetch a file for you like d3.csv or d3.tsv (Rather psv.parse() acts like d3.csvParse() or d3.tsvParse()). Luckily this is not too difficult to fix.
If we look at the API documentation for d3.csv we can recreate the same functionality for any delimited file. With d3.csv,
If a callback is specified, a GET request is sent, making it
equivalent to:
d3.request(url)
.mimeType("text/csv")
.response(function(xhr) { return d3.csvParse(xhr.responseText, row); })
.get(callback);
(from API docs)
We just have to emulate this for a pipe separated file:
var psv = d3.dsvFormat("|");
d3.request("my_file.txt")
.mimeType("text/plain")
.response(function(data) { return psv.parse(data.response) })
.get(function(data) {
console.log(data);
});
It's a little bit more verbose than a plain old d3.csv() or d3.tsv(), but achieves the same result with the callback function located in the get method.
You could also use data.responseText in the response method, I'm not sure what the difference is though, both appear identical in my quick testing.
You may have also noted that I did not specify a row function in the response method, this is an optional function that allows "conversion of row objects to a more-specific representation", eg: returning numbers rather than strings. API documentation).
One last note:
In relation to your original code, psv.parse("text") returns the data, you don't use a callback function for this method. The following snippet demonstrates how you might parse straight pipe delimited text:
var psv = d3.dsvFormat("|");
var data = psv.parse("a|b\n1|2\n4|3")
console.log(data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>

Can I use same param name multiple times in the URL for codeigniter-restserver?

http://example.com/api/transfer/transfers/code/456/code/234
When using $this->get('code') on a url like above I expect the REST library to return an array or a list of the codes.
Instead it returns the last one.
Is there a way to return both values in a list, or is there another recommandation for formating the URL.
Thank you
I know it has been long since you posted the question. However it could help other people looking for the same thing.
Assuming that transfer is your controller and transfers is the function, another way to format your url could be:
http://example.com/api/transfer/transfers?code[]=456&code[]=234
This was you perform $this->get('code') you'll get an array back.
If you are creating the url via code then you may use http_build_query(). It handles the necessary escaping. It means it will replace [ for %5B and ] for %5D, in this case.
The code would be like:
$codes = array(456, 234);
$query = http_build_query(array('code' => $data));
$url = 'http://example.com/api/transfer/transfers?' . $query;

Resources