I was trying to export https://github.com/LiosK/UUID.js into a module, but I'm having a rough time - version 4 is worthless to me (use case Cassandra) - does anybody know of a binding for these types of uuids? I can't seem to find one on Google...maybe someone has implemented what's out there?
Thanks!
I think you can just use /dist/uuid.core.js with a slight modification as follows:
add the following line and save:
exports.UUID = UUID;
now in another file use:
var UUID = require("path/to/the/uuid.core.js");
console.log(UUID.generate());
hope that helps
J
https://github.com/broofa/node-uuid now supports v1 format IDs. FWIW.
Related
I am receiving the code from the backend in plain text format. Like below...
def countNegatives(matrix): count = 0 for row in matrix: for num in row: if num < 0: count += 1 return count
It's in simple text form, but I want to display this to the user, and it should be well formatted. Like one below...
def countNegatives(matrix):
count = 0
for row in matrix:
for num in row:
if num < 0:
count += 1
return count
I want to format code of Javascript, Python, Java, C, C++, Ruby and CSS
Can someone provide me a way for doing this. Please help if possible...
You can use prettier. There is probably some other code formatting libs out there. Using react, you wanna build yourself a custom hook for that.
Thanks Raukaute, prettier is a good formatting lib. Playground of prettier is built by React + prettier webworker. You can find the source code on Github, it is a good example. In a nutshell, the index.js create a WebWorker from static UMD js file (prettier/standalone.js) and call format API in App->Playground->PrettierFormat component.
You should use the js-beautify to do this.
It's not perfect but works stable
NPM
I am trying to retrieve random 200 objects from the array returned to me by query.find() method. First i tried to implement all random number generation and all . And just now i got introduced to underscore.js method "_.sample" .
But something is going wrong. I dont have much knowledge of underscore.js. So if someone could help that will be great.
When i try to sun _.sample method it give me the error :
TypeError: Object function (e){if(e instanceof T)return e;if(!(this instanceof T))return new T(e);this._wrapped=e} has no method 'sample'
Someone please explain what exactly this error is. I tried searching but didn't get explanatory content. Thank you in advance.
Here's the code :
var queryPhrases = new Parse.Query("Phrases");
queryPhrases.select("phraseId");
queryPhrases.find().then(function(phrases){
var arrayOfUnused = _.sample(phrases,request.params.count);
user.add("usedPhrases",arrayOfUnused);
user.save();
response.success(arrayOfUnused) ;
});
Parse Cloud Code includes an outdated version of Underscore, but frustratingly, I can't find anything stating which version. While Underscore no longer ships with the Parse Javascript SDK as of v1.6.0 (late 2015), previously it had only used UnderscoreJS v1.4.4 (early 2013), so I'd expect Cloud Code to be using something from around then.
Adding the latest Underscore source to your Cloud Code is always an option, and then require it like any other of your own files.
Alternatively, I used the following to show a list of functions available in the included Underscore Cloud Code module.
var _ = require('underscore');
var availableFunctions = _.functions(_);
console.log('Available Underscore Functions: ' + JSON.stringify(availableFunctions));
Thanks a lot guys for your responses. I couldn't find the reason or the solution to run _.sample in my code. So i implemented it the other way. Here's what i did.
var arrayOfUnused = _.first(_.shuffle(phrases),request.params.count);
This works. :-)
I have the following code in javascript:
var c = new addon.Component();
c.ComponentLength = 3
How should i build my addon, so i can do the previous code? I've already followed the tutorials in http://nodejs.org/api/addons.html , but i'm stuck here.
Is this possible? Does anyone have a solution?
Thanks in advance.
The solution is on the v8.h, search for SetAccessor , which "Sets an accessor on the object template.".
An example: http://syskall.com/how-to-write-your-own-native-nodejs-extension/
I'd like to use the JavaScript toLocaleUpperCase() method to make sure that the capitalization works correctly for the Turkish language. I cannot be sure, however, that Turkish will be set as the user's locale.
Is there a way in modern browsers to set the locale in run time, if I know for sure that the string is in Turkish?
(I ran into this problem while thinking about Turkish, but actually it can be any other language.)
There isn't really anything much out there but I came across this JavaScript setlocale function script that you might find useful.
You unfortunately cannot set locale during runtime. All hope is not lost though, there are many good libraries on npm for you to use. Check out https://www.npmjs.com/package/upper-case and https://www.npmjs.com/package/lower-case for example, it will work for many other languages too.
If that's too much, you can roll your own simple library:
var ALL_LETTERS_LOWERCASE = 'abcçdefgğhıijklmnoöprsştuüvyz';
var ALL_LETTERS_UPPERCASE = 'ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ';
function toLowerCaseLetter(letter) {
letter_index = ALL_LETTERS_UPPERCASE.indexOf(letter);
return ALL_LETTERS_LOWERCASE[letter_index];
}
function toLowerCase(my_str) {
var lower_cased = ''
for (letter of my_str) {
lower_cased += toLowerCaseLetter(letter);
}
return lower_cased;
}
console.log(toLowerCase('ÇDEFGĞHIİJKLMNOÖPRSŞTUÜ'))
Very similar for upper case version.
This option may not have existed back in 2013 but may help new visitors on this topic:
According to MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLocaleUpperCase) the function toLocaleUpperCase takes an optional parameter 'locale'.
Setting the right language tag is a topic on its own (https://www.w3.org/International/articles/language-tags/). Simplest example looks like this
'selam dünya'.toLocaleUpperCase('tr'); // SELAM DÜNYA
How do I parse URL parameters in JavaScript? (These are the parameters I would ordinarily call GET parameters or CGI parameters, but in this case the page is basically submitting to itself, not a server, so there is no GET request and definitely no CGI program.)
I've seen a number of routines on the net that I can copy, but I have no idea how robust any of them are. I'm used to other languages like Perl and Java where I can rely on an extremely well-tested and robust library that I know will handle millions of little edge-cases in the standard. I would like the same here, rather than just cutting and pasting an example.
jQuery URL Utils or jQuery URL Parser.
Here's are two simple functions that do the job : http://adamv.com/dev/javascript/querystring
Here is a sample of the API Reference :
var qs = new Querystring();
// Parse a given querystring
var qs2 = new Querystring("name1=value1&name2=value2");
var v1 = qs2.get("name1");
var v3 = qs2.get("name3", "default value");
If it's "submitting to itself," do you need to do GET parameters?
But if you do, most browsers now have the decodeURIComponent function which will handle individual parameters; your job is to split them on & (String#split will do that nicely). If you want a library, jQuery and Prototype are both well-used and tested.
The best way I have found is to simply do it yourself and funnel the params into a global key/value object.
Getting quer params is simple...
just take a couple of .split()'s
var myquery = thewholeurl.split("?")[1]; //will get the whole querystring with the ?
then you can do a
myparams = myquery.split("&")
then you can do
for each param in myparams
{
temp = param.split("=");
mykeys.push(temp[0]);
myvalues.push(temp[1]);
OR
myObject[temp[0]] = temp[1];
}
It's just a matter of style.
This is not perfect code, just psuedo stuff to give you the idea.
I use the parseUri library available here:
http://stevenlevithan.com/demo/parseuri/js/
It allows you to do exactly what you are asking for:
var uri = 'http://google.com/?q=stackoverflow';
var q = uri.queryKey['q'];
// q = 'stackoverflow'
I've been using it for a while so far and haven't had any problems.
I found this useful for simple url parsing, modifying url (like adding new query params): https://github.com/derek-watson/jsUri
I think this library would work quite well, it is independent so you can use it with JQuery or with YAHOO or Dojo, another advantage is that it is pretty well documented.
http://www.openjsan.org/doc/t/th/theory/HTTP/Query/0.03/lib/HTTP/Query.html
You can use HTTP.Query to do all of the work for you in this case. It is only like 1.2 KB compressed so you could even include it in a bigger library if you wanted.
I recommend query-string library
Installing:
npm install query-string
Usage:
import queryString from 'query-string';
console.log(location.search);
//=> '?foo=bar'
const parsed = queryString.parse(location.search);
console.log(parsed);
//=> {foo: 'bar'}
parsed.foo = 'unicorn';
parsed.ilike = 'pizza';
const stringified = queryString.stringify(parsed);
//=> 'foo=unicorn&ilike=pizza'
location.search = stringified;
// note that `location.search` automatically prepends a question mark
console.log(location.search);
//=> '?foo=unicorn&ilike=pizza'
https://www.npmjs.com/package/query-string
Javascript has no built in support for URL parameters.
Anyway, the location.search property returns the portion of current page URL starting from the question mark ('?').
From this, you can write your own parameter parser or you can make use of one of those available in most common Javascript frameworks, such as JQuery and similar.