Get collection of tokens from a CodeMirror doc - codemirror

I am trying to get a collection of all matched tokens from a doc.
Basically, want to know the total number of occurrences for the term "function" or "var" or any other token I have setup in a document.
Thanks

Probably the runmode addon is the easiest way to do this.

Related

Can regex be used to query a REST API?

There is a free test REST API at https://jsonplaceholder.typicode.com/comments
I've figured out that I can get certain objects by specifying their id like this.
https://jsonplaceholder.typicode.com/comments?id=1
I can also do the same for email. Are more complicated queries possible? What if I wanted to get all objects with ids 1-10, or all objects with a certain word in the body? Could I use something like a logical OR to get all objects with either foo in the name or bar in the body? Can regex be used?
you can use https://jsonplaceholder.typicode.com/comments to get all comments and then filter them as you need. axios only takes a url string. So, yes, you can use regex to build that string.Though you can't get object with id 1-10 or use email instead of id if the API doesn't support it.If the API supports it, you can do it.

Restful URL design full text search on any specified field

My api should support text search on specified fields. So I am thinking what kind of URL style handles it in the best way.
The below pattern, using "q" ,is mentioned in many blogs and documents to be used for full text search but I also need to specify field names:
GET /groups?q=bank+org
So I am thinking to use wildcards like below:
GET /groups?name=*bank*&owner=*org*
I am just wondering if this is aligned with the best practices in the market?
Thanks
Soheil, you are thinking right. "Search" is a "filter parameter" wich always go in the Query String.
When sending parameters that will be used to query a collection of resources you should use... Guess what! Query parameters!
As far as I know, there's no official documentation that states that. It's a common approach and it's widely adopted. The only offical documentation about query string that I'm aware of is the RFC 3986. Quoting:
3.4. Query
The query component contains non-hierarchical data that, along with
data in the path component, serves to identify a
resource within the scope of the URI's scheme and naming authority
(if any). The query component is indicated by the first question
mark ("?") character and terminated by a number sign ("#") character
or by the end of the URI. [...]
For a full text search, you can choose the parameter you find most convenient. Do you think q is a good one? Go for it! But provide a good documentation for it.

Search API Facebook Graph by date range

is there any way to explore the Facebook Graph API by date range? Ex. to find all events on February?
I use following code, but I’m not sure, that’s correct request:
since=2015-01-28T00:00:00%2B0000&until=2015-01-30T00:00:00%2B00000
In this way, I get records for defined date range - ok, but there is missing events – a set is incomplete (despite it doesn’t exceed a limit of API). Why I can’t get all of results for given query?
Maybe do you know another method of filtering results by date?
thanks
There's no way to restrict the search results by since and until as far as I know.
For searching events, you can use the /search endpoint as described at
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#search
but I guess there's no further way to filter the results other than specifying the q parameter.

REST Field filter use

I'm designing a RESTful API and I'm asking myself question about the filter field.
On my gets queries I want the user to be able to select the fields he want to get in the response. I was pretty sure that it would be the field filter jobs to give me the requested field but, after some reshearch, I found that most of the time it's used to add criteria on the fields, as a IF. Is it the user that needs to make show or hide the fields ans the Api return the full ressource everytime ?
I got an other question which is about the URI representation of such filter. Should it be something like /foo?fields=[bar1,bar2] ?
Thanks
It's not common to have a resource where you can specify what fields you want returned, by default all fields will get returned. If your resource has a lot of fields or some fields have really big values, it can be a good idea to have a way to specify which fields you want returned.
In REST there are no strict rules about how you should design your URLs for filters. It is indeed common to use GET parameters because they can be optional and don't have to be in any specific order. Your proposal of /foo?fields=[bar1,bar2] seems fine, however i would personally leave off the brackets.
Google Compute Engine API uses the 'fields' request parameter (see the documentation). The syntax is flexible enough to let user select/restrict even the nested elements. You may find it useful.
Yoga is a framework that allows you to deploy your own REST API's with selectable fields. This can reduce roundtrips to the server, and improve performance.

MongoEngine: How to collect embedded documents from referencing document

I have a class Post which has a list of embedded document called "comments"
Here all i want to do is to retrieve latest comments for all the posts user posted.
How can i achieve that? My current code, i just loop though the 'Post' class for that user and manually collect "comment".
But I also want this to be sorted by recently added, so have sort function to loop over manually collected comments and re-sort.
This seems like very inefficient, so asking for advise. Thanks!
Firstly if you $push onto the list with an update then you will keep the comments in order.
You can use the $slice operator to return the last x comments eg:
Post.objects(id=xxx).fields(slice__comments=-5)
However, the schema may not be efficient especially if you keep growing the number of comments, or comments can be unpublished. In that case you may want to split comments out into their own Document Model and link the comments to the Post by id. This would be two round trips to the database but offers more flexibility - eg. you could filter on date and published.