Sunday, May 22, 2016

'init' of undefined when using gapi with angular

This can be solved by calling gapi.auth2.init in window.gapi_onload and loading google api js through javascript, as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
export class AppComponent implements OnInit {
    ngOnInit() {
        const gappClientId = "xxxxxx.apps.googleusercontent.com";

        window.gapi_onload = function () {
            console.log("gapi onload called");
            var auth2 = gapi.auth2.init({
                client_id: gappClientId,
                scope: 'profile'
            });
        };
        var po = document.createElement('script');
        po.type = 'text/javascript';
        po.async = true;
        po.src = 'https://apis.google.com/js/auth2.js';

        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(po, s);

    }
}

Sunday, May 1, 2016

In place swap in Javascript

You can do a in-place swap using a buffer in JS with the following code. Strings are themselves immutable in JS. Hence, you cannot do a in-place swap with Strings in JS.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function reverseString(str) {
    var temp;
    for (var i = 0; i<str.length/2; i++) {
        temp = str[i];
        str[i] = str[str.length - i - 1];
        str[str.length - i - 1] = temp;
    }
}

var a = new Buffer("how are you");
console.log(a.toString());
reverseString(a);
The above code outputs:

1
2
how are you
uoy era woh

Monday, November 30, 2015

npm install taking a lot of time

Workaround:

Use http to connect to the npm registry as follows:

npm config set registry http://registry.npmjs.org/

Fix the dependencies to a version for dependencies present in package.json

npm install --save helps you to install/reinstall the dependencies and put their versions in package.json.

However, what if the packages are already installed with *s for the version in package.json and you want to be more specific about the versions in the package.json.  In that case, instead of using npm install --save and reinstall the packages, you can use this node script:

var packageJson, sys, exec, async, depJson, fs, deps;

sys = require('sys');
exec = require('child_process').exec;
async = require('async');
fs = require('fs');

packageJson = require(process.env.PWD + "/package.json");

deps = Object.keys(packageJson.dependencies);
deps = deps.concat(Object.keys(packageJson.devDependencies));

async.each(deps,
    function (dep, cb) {
        console.log("Executing : npm ls --json " + dep);
        exec("npm ls --json " + dep, function (error, stdout) {
            var versionJson = JSON.parse(stdout);
            if (packageJson.dependencies[dep]) {
                packageJson.dependencies[dep] = "~" + versionJson.dependencies[dep].version;
            }
            if (packageJson.devDependencies[dep]) {
                packageJson.devDependencies[dep] = "~" + versionJson.dependencies[dep].version;
            }
            console.log(dep + ": " + versionJson.dependencies[dep].version);
            cb();
        });
    },
    function (err) {
        if (err) {
            console.error("Following error occurred; not modifying package.json");
            console.error(err);
            return;
        }
        fs.writeFile(process.env.PWD + "/package.json", JSON.stringify(packageJson, null, 2));
    });

Thursday, October 22, 2015

ul without bullets

Here is how you can get a unordered list without bullets:

ul {
    list-style-type: none;
    padding: 0;
}

Friday, March 28, 2014

Copying the value of input field in one form to another form

This snippet copies the value of input field #source to another form #hiddenForm.
function copyFieldToForm(source, hiddenForm) {
    var clone = source.clone();
    source.after(clone).appendTo(hiddenForm);
}

copyFieldToForm($("#source"), $("#hiddenForm"));