Javascript equivalent of pluck and uniq from Ruby - javascript

Hi I am trying to convert my database from Postgres to Mongo and changing from Ruby to Javascript, I am having trouble converting the following query.
name_array = RoomRaw.pluck(:name).uniq()
Any help is appreciated.
Edit:
Also is it possible to do the sum of a row? ie.
energy_ac = RoomRaw.where(timestamp: (beginning_of_last_full_hour..end_of_last_full_hour), name: name).last.sum(:energy_ac)

It sounds like you'd like to use a few of these functions without having to re-invent the wheel. While you may be able to write these functions yourself using only JavaScript, I recommend you learn how these functions work overall.
If you're looking to be able to use these types of functions without learning the inner workings, I recommend the lodash library. It's got exactly what you need.

Related

Information about Blazor.platform functions

In order to speed up javascript interop calls from a Blazor WASM web app I need to know a little bit more about what conversions are available. So far I have found one example here of the Blazor.platform.toUint8Arrayfunction that appears to convert an unmarshalled array (pointer(?)) and convert it to an Uint8 array. Is there a place where one can find what functions are available?
Perhaps you may find something here...

Is there a way to decode html entities using javascript without there being a document or jQuery

I'm working in a system where there is no document and no jQuery, but
I do have to present html entities in an understandable way. So the trick of putting the string in an element and then taking the .text() won't work.
I need a pure JavaScript solution. The system isn't reachable from the outside, there is no user-input so security is not really an issue.
Thanks for any help, I'm out of ideas (not that I had to many to begin with)...
Perhaps I should clarify, what I am looking for is a function (or pointers to get me pointing in the right direction) which is able to translate a string with substrings that should translate to characters. So it should be able to translate "blah < blahblah" into "blah < blahblah".
There are no additional frameworks I can use other than pure javascript.
UPDATE:
I've got the html4 part working, not extremely difficult, but I have been busy with other things. Here's the fiddle:html4 entities to characters.
You could have done the same with a dictionary with just the characters already in there, but I didn't feel like making such a dictionary. The function is fairly simple but I guess it could do with some refactoring, can't really be bothered at the moment...
This function exists in PHP (htmlspecialchars_decode). As such, you'll find a javascript port from PHPJS. This is based on a very established codebase, and should be better than rolling something on your own.
Edit / Add:
Flub on my part. I didn't read the entities part properly. You want the equiv of html_entity_decode:
http://phpjs.org/functions/html_entity_decode/
Assuming you are using nodejs, cheerio is exactly what you need. I have used it myself a couple of times with great success for off-browser testing of HTML structures returned from servers.
https://github.com/cheeriojs/cheerio
The most awesome part is that it uses jQuery API.

How to substitude the JSON-JavaScript-Object, if it is not available?

I am trying to substitude or replace the JSON-JavaScript-Object in case it is not available - what would be a neat way to achieve this?
Situation:
I have some code which uses JSON.parse and other functions to handle JSONs, but I have to migrate this code to be compatible to the Maple-Browser of a Samsung 2011-Series, which does not have a built-in JSON-Object.
jquery-1.6.2 lib is available; adding another lib is possible.
Solution attempts:
Found this nice blog http://www.greenhughes.com/content/experimental-couchdb-powered-samsung-internettv-app, which pretty much sums this problem up and even proviedes some solution. But this attempt seems to be 2 years old and I am looking for an improved solution.
Goal:
I would like to keep as much of my code as possible as it is and only handle somewhere the sitation, when a build-in JSON-Object is not available and then make it available in a way, that I do not have to change the calls to this Object.
Many thanks to any input or ideas to achieve this goal!

What Javascript library can evaluate MongoDB-like query predicates against an object?

Is there a javascript library that will allow me to express object predicates in a DSL similar to MongoDB's query language? For the sake of clarity in a large program, I'd like to be able to say:
var obj = {
a: 1,
b: 'abcdefg'
}, qry = {
a: { $gt: 0 },
b: /^abc/
};
if(query(qry).matches(obj)) {
// do something appropriate since
}
instead of:
var obj = {
a: 1,
b: 'abcdefg'
};
if(obj.a>0 && qry.b.test(obj.b)) {
// do something appropriate
}
I'm using Node.js, so anything on NPM would be great. It would be an added bonus if the library can select objects out of an array as well as just matching individual objects.
I reviewed these two related questions, but they weren't particularly helpful for my situation:
Implementing goMongoDB-like Query expression object evaluation
Evaluating MongoDB-like JSON Queries in PHP
OK I found the answer: Sift.js
Now for the long answer: This has been asked and answered before. The salient points are:
Use Sift if you really want Mongo syntax
If you want to be more mainstream, use Underscore.js like everyone else. It has heaps of handy functions in addition to the fact that it basically does what sift does with a slightly different syntax.
You may not need any library at all - modern browsers support many useful functions directly on the Array prototype, like filter() for example.
As a final note, mongodb-riff appears to be trying to do something similar but currently the page states clearly that it doesn't work - perhaps it's abandoned. But his readme is at least of value :-), he mentions sift and Query Engine which looks more mature, though too complicated for me!
Personally I'm going to go with Underscore because now that I've looked into it for the first time, I realise that it has heaps of handy stuff I need, plus I really only wanted to do simple functions like what would be _.find() in Underscore. But I guess if you want to do more complicated mongo-like queries, you'll do it in less LOC with Sift.
Check out Mingo
I implemented it after finding no suitable alternative.
It is still actively being developed but is usable. Test coverage is not complete.
Usable from both browser and nodejs
[Edit]
This library is now the most complete implementation of MongoDB's query language for the frontend.
https://github.com/mirek/node-json-criteria library does exactly that - evaluates criteria queries on JSON objects using MongoDB query format.
lodash.js
You want to use lodash.js. Its a drop in replacement for underscore.js.
The performance is twice as fast.
http://lodash.com/
The closest I could find was linq.js, which is LINQ for JavaScript. Hopefully this will be of some help to you.

Collect prices from page using JavaScript

I'm developing a bookmarklet now and faced this task: I need to collect all prices from any page.
The problem is that the price may be in multiple formats ($19.00, 15.45$, etc), not counting different currencies and html markup. The good news is that I'm using jquery.
If anybody has an idea how it can be accomplish, please share :)
Thanks in advance!
If there is no consistent markup you're probably going to have to write some regex's for the known patterns. For example:
To capture a pattern like $19.00 you'll use a regex that looks something like this:
\$[0-9]*.?[0-9]{1,2}
Since you're target data is so unstructured i'm not sure there is a single good answer to this. You'll need to identify the patterns you are looking for and write the regex's to identify them.
Test your regular expressions here: http://regexpal.com/
Best of luck.
-R

Categories