Javascript Object to Array and visa versa

const obj = {"1":"5","2":7,"3":9,"4":155,"6":"189","7":587,"8":"455","9":"89","10":"80","12":"887.6","13":89,"14":9}
const objToArr = Object.keys(obj).map(function(key) {
  return [key, Number(obj[key]) ];
});
console.log(objToArr)
/*
[ [ '1', 5 ],
  [ '2', 7 ],
  [ '3', 9 ],
  [ '4', 155 ],
  [ '6', 189 ],
  [ '7', 587 ],
  [ '8', 455 ],
  [ '9', 89 ],
  [ '10', 80 ],
  [ '12', 887.6 ],
  [ '13', 89 ],
  [ '14', 9 ] ]
*/


let resultObj = objToArr.reduce(function(result, item, index, array) {
   Object.assign(result, { [ item [ 0 ] ]:item[1]} )
  return result;
}, {}) //watch out the empty {}, which is passed as "result"
console.log(resultObj)
/*
{ '1': 5,
  '2': 7,
  '3': 9,
  '4': 155,
  '6': 189,
  '7': 587,
  '8': 455,
  '9': 89,
  '10': 80,
  '12': 887.6,
  '13': 89,
  '14': 9 }
*/

resultObj2 = {...objToArr};
console.log(resultObj2)
/*
{ '0': [ '1', 5 ],
  '1': [ '2', 7 ],
  '2': [ '3', 9 ],
  '3': [ '4', 155 ],
  '4': [ '6', 189 ],
  '5': [ '7', 587 ],
  '6': [ '8', 455 ],
  '7': [ '9', 89 ],
  '8': [ '10', 80 ],
  '9': [ '12', 887.6 ],
  '10': [ '13', 89 ],
  '11': [ '14', 9 ] }
*/





Linux File Permissions

Let’s take a look at our ls -l output and look the first column of the listing:

touch myfile 
ls -l myfile
-rw-rw-r-- 1 hano hano 0 Apr 16 08:57 myfile
sudo chown hano.root myfile
ls -l myfile 
-rw-rw-r-- 1 hano root 0 Apr 16 08:57 myfile

The you have 3 access value bits in this case rw- for the owner in this case hano then rw- for the group which corresponds to the root group and then all other users r–.

rRead Access
wWrite Access
xExecute Access

To change the access use chmod (Change Mod)

chmod 754 myfile
ls -l myfile 
-rwxr-xr-- 1 hano root 0 Apr 16 08:57 myfile
ModeDecimal
rwx7
rw-6
r-w5
r–4
-wx3
-w-2
–x1
0

If you know binary you will notice that it is the decimal value of binary digits 001 = 1 010 = 2 100 = 4 so combinations will then be 101 = 4 + 1 = 5 corresponding to r-w

ES6 Arrow functions and correct using of bind(this)

function Desciples(firstname, attributes) {
  this.firstname = firstname;
  this.attributes = attributes;
  this.listAttributes = function() {
    return this.attributes.map(function (attrib)  {
      console.log(this.firstname + " did " + attrib);
    });
  };
}

let desciple1 = new Desciples("John", [
  "Author of Gospel of John ",
  "Author of Reveleations",
  "Desicple that Jesus loved"
]);

desciple1.listAttributes();

retuns

"undefined did Author of Gospel of John "
"undefined did Author of Reveleations"
"undefined did Desicple that Jesus loved"

But if we change it to

this.listAttributes = function() {
    return this.attributes.map(attrib => {
      console.log(this.firstname + " did " + attrib);
    });


"John did Author of Gospel of John "
"John did Author of Reveleations"
"John did Desicple that Jesus loved"

We can also create local variable to the function and assign this to it

 this.listAttributes = function() {
    self = this;
    return this.attributes.map(function (attrib)  {
      console.log(self.firstname + " did " + attrib);
    });
  };

We can also use the BIND on the function like:

  this.listAttributes = function() {
    return this.attributes.map(function (attrib)  {
      console.log(this.firstname + " did " + attrib);
    }.bind(this));
  };

Or the MAP has a second paramenter and we can pass this there that will also work

  this.listAttributes = function() {
    return this.attributes.map(function (attrib)  {
      console.log(this.firstname + " did " + attrib);
    },this);
  };

Build pdo_oci.so (or oci8.so) on SLES 11.4 and Oracle 12.1

I recently had to build pdo_oci.so and I could not find a proper guide to do it. Hopefully this one will help someone.

create a file on document root called test.php

vi /srv/www/htdocs/test.php
<?php
phpinfo();
?>

Browse to http://yourserverip/test.php, on top is the version mine is 5.3.17
Download the correct source http://php.net/releases/ I used this link http://museum.php.net/php5/php-5.3.17.tar.gz
Now copy the downloaded source to ~/php-build

tar -zxvf php-5.3.17.tar.gz
cd php-5.3.17

Oracle Server installed


I have Oracle 12.1 Server installed on the box so I have ensured the my ORACLE_HOME is correct. Read the docs if you want to use the instant client.

./configure  --with-apxs2=/usr/sbin/apxs2 --with-pdo-oci=shared --with-oci8=shared --with-config-file-path=/etc/php5/apache2/php.ini --with-config-file-scan-dir=/etc/php5/apache2  --with-ldap
make 
cd ext/pdo_oci
;edit the config.m4 file
vi config.m4

search for 11
and change

case $PDO_OCI_VERSION in
9.0|10.1|10.2|11.1|11.2)
PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
;;

to
case $PDO_OCI_VERSION in
9.0|10.1|10.2|11.1|11.2|12.1|12.2)
PHP_ADD_LIBRARY(clntsh, 1, PDO_OCI_SHARED_LIBADD)
;;

do a pwd make sure you are in ext/pdo_oci directory

phpize
./configure --enable-shared
make

Instant Client

For the instant client you might get error like:
checking Oracle Instant Client library version compatibility… configure: error: Link from /usr/lib/oracle/libclntsh.so to /usr/lib/oracle/libclntsh.so.*

cd /usr/lib/oracle
sudo ln libclntsh.so.12.1 libclntsh.so

Then configure like

./configure  --with-apxs2=/usr/bin/apxs2 --with-pdo-oci=shared,instantclient,/usr/lib/oracle --with-oci8=shared,instantclient,/usr/lib/oracle

To Build the shared object

cd ext/oci8/
phpize
./configure --enable-shared -with-oci8=instantclient,/usr/lib/oracle
make

OR PDO_OCI

cd ext/pdo_oci
phpize
./configure --enable-shared -with-pdo-oci=instantclient,/usr/lib/oracle
make
now there should be a folder modules cd modules ls -l ;should show a pdo_oci.so sudo cp pdo_oci.so /usr/lib64/php5/extensions cd /etc/php5/conf.d/ sudo vi pdo_oci.ini

Add the following to the file :

; comment out next line to disable pdo_oci extension in php
extension=pdo_oci.so

tail -f /var/log/apache2/error.log
if you get
Unable to load dynamic library ‘pdo_oci.so’ (tried: /usr/lib/php/[SOMENUMBER]/pdo_oci.so (libmql1.so: cannot open shared object file: No such file or directory)

Add Oracle libs to the LD_LIBRARY_PATH

cd /etc/ld.so.conf.d
#Create file oracle and add /usr/lib/oracle
sudo vi oracle

and if that does not work then

cd /etc/apache2/
sudo vi envvars 
#and add 
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/oracle
#to the end of the file

Restart the apache server or you can do
php -i | grep pdo
to see if its loaded.

Python update to fix Kodi on linux (Ubuntu and mint)

I had issues with my Kodi 17.6 on linux. I have noticed errors involving cryptography and ffi. Here is the procedure that I followed to fix it.
Open a Terminal and do the following:

 sudo apt-get install build-essential libssl-dev libffi-dev python-dev python-pip

After its done then install cryptography for python

pip install cryptography

It will prompt to install update pip BUT DONT DO THAT IT WILL BREAK THINGS.

PHP 7.0 to PHP 5.6 on Ubuntu / Linux Mint

Parse error: syntax error, unexpected ‘new’ (T_NEW) in /usr/share/php/MDB2/Driver/pgsql.php on line 925

The problem is that MDB2 is not supported for PHP7.0
It is better to use PDO PHP PDO but if you cant then here is how to downgrade to PHP5.6.

dpkg -l | grep php| awk '{print $2}' |tr "\n" " "

I like to do it manually so copy the above output to the command

sudo apt-get purge libapache2-mod-php7.0 php php-common php-gettext php-mdb2 php-pear php-pgsql php-phpseclib php-xml php7.0 php7.0-cli php7.0-common php7.0-dev php7.0-fpm php7.0-gd php7.0-intl php7.0-json php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-pgsql php7.0-readline php7.0-xml php7.0-zip 
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php5.6

Verify php 5.6

php -v

PHP 5.6.36-1+ubuntu16.04.1+deb.sury.org+1 (cli)
Then restart apache2.0

sudo /etc/init.d/apache2 restart

Dont forget to install PEAR again

sudo apt-get install php-mdb2
apt-cache search mdb2
sudo apt-get install php-mdb2-driver-pgsql
sudo apt-get install php5.6-pgsql

Javascript Essentials ES6

Object to String Array of the object keys

 const strArrKeys = Object.keys(jsObjectVariable); 

Object to Array with Key Value pairs

let theobj = { key1: "value1", key2: "value2" }
Object.entries(theobj).forEach(eachKeyVal =&amp;amp;amp;amp;amp;amp;gt;
   { console.log("Key is " + eachKeyVal[0] + " and value is " + eachKeyVal[1]) }); 

Array from object values

 jsObjectVariable = { one : 1, two: 2, three: { temp1: "t1", temp2: "t2" } } const strArr = Object.keys(jsObjectVariable) // Returns ARRAY of the keys .map(k =&amp;amp;amp;amp;gt; jsObjectVariable[k] ); // Retruns an Array of values console.log(strArr); const strArr2 = Object.keys(jsObjectVariable) // Returns ARRAY of the keys .map(k =&amp;amp;amp;amp;gt; { return jsObjectVariable[k] } ); // Retruns an Array of values console.log(strArr2); jsARRVariable = [(one =&amp;amp;amp;amp;gt; 1),(two =&amp;amp;amp;amp;gt; 2)]; const strArr3 = jsARRVariable.map((k,v) =&amp;amp;amp;amp;gt; {return v;} ); // Returns array of values console.log(strArr3); 

Make a copy of the object.

cont newObject = {...oldObject};

Make a copy of the array.

cont newArray = [...oldArray];

Add values to an Array immutably.
Dont use push use concat, it will make a copy of the array.
Add an integer called counter to an array called results:

cont newObject = {
    ...oldObject,
    results: oldObject.results.concat(oldObject.counter)
};

Delete values from an Array immutably.

const idToDelete = 4;
const newArray = [...oldArray];
newArray.splice(idToDelete,1);

OR

newArray = oldArray.filter((result,index) =&amp;amp;amp;amp;gt; index !== idToDelete);

You can also create dynamic object keys (Object Names based on variables) and use them

var example = {one : 'something', two: 'somethingElse'};
var test = {
    [example.one]: true,
    [example.two]: function () { console.log('This is SomethingElse function Result');},
};

console.log(test.something);
test.somethingElse();

IN ES5 we used to:

var key = 'dynamicKey';
var o = {};
o[key] = 'value';

But in ES6, when creating an object literal, you can do this:

Continue reading “Javascript Essentials ES6”

Java easily convert from char or string to ENUM

One of my popular ways to make my code more readable is to make use of an ENUM.

If you have some “if” or “switch” statement:

             char myChar = 'D';
             if (myChar=='H') {
              //SOME CODE
              } else if (myChar=='V') {
                //SOME OTHER CODE
                 }

it does not say much about the functionality thats why I use enums

public enum myType {
        HORIZONTAL("H"),
        VERTICAL("V");
        private char directionToken;
        
        public static myType fromChar(char searchchar) {
            for (myType st : myType.values()) {
                if (st.directionToken == searchchar) {
                    return st;
                }
            }
            return null;
        }
        
        private myType(String transToken) {
            this.directionToken = transToken.charAt(0);
        }

        public char directionToken() {
            return directionToken;
        }
    }    

.....

             myType currRecordType = myType.fromChar(linechar[43]);
             switch (currRecordType) {
                 case HORIZONTAL:{
                     ...
                     break;
                     }
                     
                 case  VERTICAL:{
                     ...
                     break;
                 }
             }