How to store strings with whitespaces to databases and retrieve them for URL? - database

I have Shops stored in a Postgreql database, I retrieve them from the frontend and use them as a link. However, since there is whitespace in them the URL looks like this /New%20Shop. I don't know what the right solution is, I can trim the string on the frontend link part but that can result in me not being able to query search the shop since there is whitespace at the database part. I can also change the database so that it holds the strings with whitespace with an underscore and replace the underscores with whitespace on the frontend part. These are the solutions that came to my mind however I would love to learn the right approach for this. Thank you.

%20 is how a white space is encoded in URL encoding.
There is no single "right way" no handle mapping URLs to objects, but most apps will store a "slug" in the database which is a URL-safe version of the name (or just use a numeric id). You would use the slug to lookup the matching entity. Typically it is all lowercase with spaces replaced by - and other punctuation marks removed. There are plenty of packages and helper functions that can convert special characters, like letters with accent marks, to their plain versions. For example, you can combine deburr and kebabCase from Lodash.
const slugify = (title) => {
return _.kebabCase(_.deburr(title));
}
console.log(slugify("New Shop")); // logs "new-shop"
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.21/lodash.min.js"></script>

Related

extjs 4.2 - entity encoding with backslash e.g. '\1014'

The value "\1014" is coming from my database and I want to display it in an ExtJS Panel.
The problem is, it gets processed as an entity value, and "A4" is displayed instead
I don't want to have to do entity encoding on the back end.
I tried
Ext.util.Format.htmlEncode('\1014')
But this also returns "A4"
What is the correct way to encode such values on the front-end for display?
This has nothing to do with ExtJS. This is a builtin feature of JavaScript and JSON. If you want to send the non-literal \101 as JSON to the frontend, you have to escape the backslash correctly to the specs in the backend:
{"success":true,"data":{"test":"\\101","id":"extModel2-1"}}
If you don't escape the backslash, it will be converted to the appropriate literal immediately when it hits the frontend and is then indistinguishable from the letter A, so this is not revertible on the frontend.
Relevant fiddle
Relevant older answer
You can parse data using JSON.parse(response.reponseText) instead of Ext.decode

Angular ui-router - Curly brackets etc in url

I have been searching for a way to have curly brackets{} and slashes / in the URL. But the URL looks like this:
URL appearance now:
http://localhost/#/?ph=tes:testes%2Ftest%7Blb0%7D
URL wanted appearance:
http://localhost/#/?ph=tes:tests/test{lb0}
How do I get an URL without the %2f etc?
Thanks in advance!
All valid characters that can be used in a URI (a URL is a type of URI) are defined in RFC 3986.
All other characters can be used in a URL provided that they are "URL Encoded" first. This involves changing the invalid character for specific "codes" (usually in the form of the percent symbol (%) followed by a hexadecimal number).
This link, HTML URL Encoding Reference, contains a list of the encodings for invalid characters.
Happy Helping!
These (%2F, %7B) are escape sequences. They represent the special characters that are in your URL, as a URL cannot contain these character directly.
Where do you want exactly to process this URL?
You can use Javascript's unescape(url) to get the escaped URL string back to the original one if you're going to process it in Javascript itself.

Database - Should I define a max length for stored url?

I'm building a small webapplication which should store urls as input of a form. Sometimes urls can have attributes, parameters etc. like https:/www.stackoverflow.com/search?q=This+could+be+a+very+long+string. But some 'experts' could make use out of the missing validator, which checks the length of the string. Otherwise a limitation would may let occur an error if the url is to long.
I'm using for now SQLite3, but the solution should work database-independent.

AngularJs Search functionality is not working when more than one special characters

I have implemented Angular Js search functionality in application. When I have enter more than one special characters for example '!#' it will display all the results.I think the exclamation character is the problem. How can I resolve this? In their demo site is also not working.http://docs.angularjs.org/api/ng.filter:filter
source can find from their site.
See you are working somewhat wrong, because it allows you to have the multiple special characters search, but in your case you are using "!#" it means not include all those result having "#" in it.
So, if you choose to have "#*" it will show you to get all the result having these characters together.

Jsoup - how to deal with URLs that have space

I'm using Jsoup to connect to a URL with a space in it but keep getting a URI syntax error. Any ideas on how deal with this?
Thanks!!
You should percent-encode URLs. Space is represented as %20.
Additionally, some characters/symbols are used as delimiters in URLs (&, ?, =, etc.) and thus they cannot be put in the URL unless they are meant as delimiters. For example, if you wanted to pass the string "1 &1" I would have to encode it as "1%20%261" or else it will not parse properly.
See http://en.wikipedia.org/wiki/Percent-encoding for more details.
URLs cannot have spaces, by RFC1738 (section 2.2): http://www.ietf.org/rfc/rfc1738.txt
For some additional background on how to handle this, see: http://www.w3schools.com/tags/ref_urlencode.asp
In particular, one mechanism for encoding URLs can be found at:
HTTP URL Address Encoding in Java

Resources