Javascript string functions

Knowing string functions is always useful and saves time to Google the correct function. Hence I have prepared list of most useful, usually used string functions.

First of all definition of String

In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable.

In JavaScript context it inherits prototype of String object.
A string can be any text inside double or single quotes.

List of almost all kinds of string manipulations.

Creating a String:
var name = new String (“sachin”)
Or simply
var name = “sachin”

Changing a String
Replace
name.replace(“in”,”out”)
replace replaces first occurrence only.

Change case
name.toUpperCase()
name.toLowerCase()

// Custom title case function

Concat
var fullName = name + ” ” + “Gaikwad”
or
var fullName = name.concat(” Gaikwad”)

Stuff String or Char
name.substring(0,3)+”123″+name.substring(3,name.length)
//sac123hin
// No direct method to stuff string/char in string

Stat- counting

chars
name.length

words
name.split(” “).length

Char at position
name.charAt(0) //s

Check Substring
name.includes(“chi”) // true

Get first occurrence index or last occurrence index of other string resp.
name.indexOf(“sa”)
name.lastIndexOf(“hi”)

Extract another string 
name.slice(2) // chin

Trim whitespace
name.trim() // removes whitespace ” sachin ” to “sachin”

 

Export/Import from/to other data type

Array
name.split(” “) // gives array
nameArray.join() // gives string

Number
parseInt(“123”)// string to integer
parseFloat(“12.3”) // string to float
“”+123 // number to string

 

Auto Slug field in Django

While designing database, many times we need slug field which we populate from some other field of the same model.

We can set auto slug field so that we don’t need to code in order to populate slug field while insert or update.

You can use the field AutoSlugField from module django-autoslug.

1. Install module django-autoslug.
pip install django-autoslug

2. Import in models file where your model exists.
from autoslug import AutoSlugField

3. Set field in model
E.g.
title = models.CharField(max_length=100)
slug = AutoSlugField(populate_from=’title’,default=None)

Install Jitsi Meet on ubuntu 14.04

To install jitsi meet we are going do setup for following components

  1. Jitsi Video-Bridge (Software video-bridge)
  2. Jitsi Jicofo (Component mandatory for jitsi conference)
  3. Prosody ( XMPP Server )
  4. Nginx (Web Server)
  5. Jitsi Meet (Web application – to which the end user will interact. )

 

Java dependency

Check if jdk installed on your ubuntu, it must be > 1.7

java -version
javac -version

Both above must be greater than 1.7

  1. To install jdk
sudo add-apt-repository ppa:webupd8team/java -y
sudo apt-get update
sudo apt-get install oracle-java8-installer
sudo apt-get install oracle-java8-set-default

Check again with same commands

java -version
javac -version

If versions are > 1.7 we are ready to go ahead.

If you have java installed already, but it points to some older version, you can select proper version using following

sudo update-alternatives --config java
sudo update-alternatives --config javac

 

References

Difference between JVM, JRE, JDK
Install JDK
Java alternatives in ubuntu
Check which JRE you are using

Install Prosody – XMPP server

apt-get install prosody

Configure it
1. Open file “/etc/prosody/conf.avail/jitsi.example.com.cfg.lua”
2. Add following lines to config file

VirtualHost "jitsi.example.com"
    authentication = "anonymous"
    ssl = {
        key = "/var/lib/prosody/jitsi.example.com.key";
        certificate = "/var/lib/prosody/jitsi.example.com.crt";
    }
    modules_enabled = {
        "bosh";
        "pubsub";
    }

VirtualHost "auth.jitsi.example.com"
    authentication = "internal_plain"

admins = { "focus@auth.jitsi.example.com" }

Component "conference.jitsi.example.com" "muc"
Component "jitsi-videobridge.jitsi.example.com"
    component_secret = "YOURSECRET1"
Component "focus.jitsi.example.com"
    component_secret = "YOURSECRET2"

3. Set above file in use

sudo ln -s /etc/prosody/conf.avail/jitsi.example.com.cfg.lua /etc/prosody/conf.d/jitsi.example.com.cfg.lua

4. Generate certificates

sudo prosodyctl cert generate jitsi.example.com

5. Create conference user

sudo prosodyctl register focus auth.jitsi.example.com YOURSECRET3

6. Restart prosody

sudo prosodyctl restart

References
Prosody XMPP server
Prosody configurations

 

Install Jitsi Video Bridge

Go to following URL

https://download.jitsi.org/jitsi-videobridge/linux/

Check which version is good for your ubuntu 32/64 bit, using following

uname -a

If it says “x86_64” at the end, it is 64 bit.

wget https://download.jitsi.org/jitsi-videobridge/linux/jitsi-videobridge-linux-{arch-buildnum}.zip
unzip jitsi-videobridge-linux-{arch-buildnum}.zip

(If you dont have unzip installed, you can “sudo apt-get install unzip”)

Make .sip-communicator directory in user home directory

  1. cd ~
  2. mkdir .sip-communicator
  3. nano sip-communicator.properties
  4. add lines
    org.jitsi.impl.neomedia.transform.srtp.SRTPCryptoContext.checkReplay=false
    org.jitsi.videobridge.NAT_HARVESTER_LOCAL_ADDRESS=<Local.IP.Address> 
    org.jitsi.videobridge.NAT_HARVESTER_PUBLIC_ADDRESS=<Public.IP.Address>

    to file and save it (Ctrl + o, Enter, Ctrl + x)

Run jitsi video bridge

  1. cd /path/to/extracted-folter-of-jistsi-video-bridge
  2. ./jvb.sh --host=localhost --domain=jitsi.example.com --port=5347 --secret=YOURSECRET1

To auto start jitsi video bridge

/bin/bash /root/jitsi-videobridge-linux-{arch-buildnum}/jvb.sh --host=localhost --domain=jitsi.example.com --port=5347 --secret=YOURSECRET1 </dev/null >> /var/log/jvb.log 2>&1

 

References

Check if your ubuntu is 32bit/64bit
Install and run jitsi video bridge

Install Jicofo

Install ant

sudo apt-get install ant

Clone jicofo repository

git clone https://github.com/jitsi/jicofo.git
cd jicofo/lib

Download maven task pom and jar file from following location

http://search.maven.org/#search|ga|1|a%3A%22maven-ant-tasks%22

POM file

wget https://repo1.maven.org/maven2/org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-2.1.3.pom

Jar file

wget http://search.maven.org/remotecontent?filepath=org/apache/maven/maven-ant-tasks/2.1.3/maven-ant-tasks-2.1.3.jar

Rename jar file

mv remotecontent\?filepath\=org%2Fapache%2Fmaven%2Fmaven-ant-tasks%2F2.1.3%2Fmaven-ant-tasks-2.1.3.jar maven-ant-tasks-2.1.3.jar

Remove ~/.m2/

sudo rm -R ~/.m2/

Change directory to jicofo

cd /path/to/jicofo-cloned-repo/
mvn -U clean package -DskipTests

( If maven not installed,  sudo apt-get install maven)

ant -lib lib dist.{os-name}

For 64 bit ubuntu use lin64 as {os-name}
(Here {os-name} is one of the ‘lin’, ‘lin64’, ‘macosx’, ‘win’, ‘win64’)

cd dist/{os-name}

(for ubuntu cd dist/linux)

unzip jicofo-linux-x64-build.SVN.zip
cd jicofo-linux-x64-build.SVN/

Run Jicofo

./jicofo.sh --domain=jitsi.example.com --secret=YOURSECRET2 --user_domain=auth.jitsi.example.com --user_name=focus --user_password=YOURSECRET3

 

 

References

ant dist class path

Build fail issue for maven dep

 

Nodejs dependency

Install nodejs using nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

exit the terminal and repoen it

Check the latest stable nodejs version

nvm install 4.4.1

(4.4.1 is the latest stable at this time, you may select another)

Use version

nvm use 4.4.1

sudo apt-get install build-essential

References

Install nodejs using nvm

nvm

Nodejs

 

Install Jitsi Meet

 

Clone jitsi-meet repository

git clone https://github.com/jitsi/jitsi-meet.git

cd /jitsi-meet

npm install

make


Configuration
Edit the configuration file /config.js

var config = {
 hosts: {
 domain: 'jitsi.example.com',
 muc: 'conference.jitsi.example.com',
 bridge: 'jitsi-videobridge.jitsi.example.com'
 },
 useNicks: false,
 bosh: '//jitsi.example.com/http-bind', // FIXME: use xep-0156 for that
 desktopSharing: 'false' // Desktop sharing method. Can be set to 'ext', 'webrtc' or false to disable.
 //chromeExtensionId: 'diibjkoicjeejcmhdnailmkgecihlobk', // Id of desktop streamer Chrome extension
 //minChromeExtVersion: '0.1' // Required version of Chrome extension
};

Deploy jitsi-meet using nginx

sudo apt-get install nginx

cd /etc/nginx/sites-available

add new file jitsi.example.com

add following code

server_names_hash_bucket_size 64;

server {
    listen 80;
    server_name jitsi.example.com;
    # set the root
    root /path/to/jitsi-meet;
    index index.html;
    location ~ ^/([a-zA-Z0-9=\?]+)$ {
        rewrite ^/(.*)$ / break;
    }
    location / {
        ssi on;
    }
    # BOSH
    location /http-bind {
        proxy_pass      http://localhost:5280/http-bind;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
    }
}


cd /etc/nginx/sites-enabled
ln -s ../sites-available/jitsi.example.com jitsi.example.com
Restart sudo service nginx restart

Browse to the jitsi.example.com and you should be ready to use jitsi-meet !!!

References
jitsi meet nginx deploy



Create Postgres DB from command line in Ubuntu 14.04

On ubuntu shell,

sudo su postgres
createdb mydb -O dbownername

If it says “dbownername” role does not exist, you can create it with

createuser dbownername

 

If above commands does not work, you may be missing following

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

 

for more information  createdb manual  createuser manual

 

Cancel earlier http requests in angularjs

When we want data from last http request, data from earlier request (if any) need to be canceled.

// Code goes here

var canceller = $q.defer();
if(earlierRequest && earlierRequest.canceller)
{
  // Cancel earlier requests
  earlierRequest.resolve();
}
var earlierRequest = $http({
  url:'get_async_data.php',
  timeout:canceller.promise
}).then(function(response){
  // Data received
});
earlierRequest.canceller = canceller;

 

Copy MongoDB database from remote host to your machine

You can use the mongo console to copy the database from one host (machine) to another host (machine). It is usually helpful when we are copying large database.

Start the mongo console

  1. by typing command “mongo”
  2. run following commands to copy database
    use admin
    db.runCommand({
       copydb: 1,
       fromdb: "test",
       todb: "records",
       fromhost: "example.net"
    })

    For more information Mongo Documentation

Create Application Launcher on Desktop in ubuntu

  1. Create “your-application.desktop” file on desktop.
  2. Write follwing lines in that file
[Desktop Entry]
Version=1.0
Name=BackMeUp
Comment=Back up your data with one click
Exec=/home/alex/Documents/backup.sh
Icon=/home/alex/Pictures/backup.png
Terminal=false
Type=Application
Categories=Utility;Application;

3. Give executable permission to this file

4. Start using it

For More info UnityLaunchersAndDesktopFiles

Install Symfony 1.x on ubuntu 14.04

Follow link for installation, http://symfony.com/legacy/doc/jobeet/1_4/en/01?orm=Propel

In my case there were problems after installations

  1. 500 server error  – Create Virtual Host to fix this error – follow each step in guideline given.
  2. Build schema XSL and PHP accelerator error –
    1. for XSL  try “sudo apt-get install php5-xsl
    2. for PHP accelerator try “sudo apt-get install php-apc