Friday, 6 December 2019

NPM update package

https://bytearcher.com/articles/using-npm-update-and-npm-outdated-to-update-dependencies/

Going for bigger update with @latest tag

Updating a version that is beyond semantic versioning range requires two parts. First, you ask npm to list which packages have newer versions available using npm outdated.
1 $ npm outdated
2 Package  Current  Wanted  Latest  Location
3 lodash    3.10.1  3.10.1  4.16.4  backend
Then you ask npm to install the latest version of a package. You can ask for the latest version with the @latest tag. You should also use the --save flag to update package.json.
1 $ npm install lodash@latest --save
Now npm installs version 4.16.4 under node_modules. Also, package.json is updated.
1 "dependencies": {
2     "lodash": "^4.16.4"
3 }

Tuesday, 3 December 2019

SQL server delete all data in database, and reset counter for all tables in database

// Reset counter for all tables in db
exec sp_MSforeachtable @command1 = 'DBCC CHECKIDENT(''?'', RESEED, 1)'
https://stackoverflow.com/questions/2364742/sql-server-reset-identity-increment-for-all-tables




// Delete all data in db
-- disable referential integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' 
GO 

EXEC sp_MSForEachTable 'DELETE FROM ?' 
GO 

-- enable referential integrity again 
EXEC sp_MSForEachTable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT ALL' 
GO
https://stackoverflow.com/questions/3687575/delete-all-data-in-sql-server-database

Monday, 2 December 2019

JS Object references && pass by copy && pass by copy and then pass by reference

// Try following on console
// Object pass by references

var a = {test:'hi'}
undefined
var b = a
undefined
{test: "hi"}
{test: "hi"}
b.test = 'hi!'
"hi!"
a
{test: "hi!"}
b
{test: "hi!"}

// Object pass by copy
var test = {test : 'hi'}
undefined
var test2 = {}
undefined
Object.assign(test2,test)
{test: "hi"}
test
{test: "hi"}
test2
{test: "hi"}
test2.test='hihi'
"hihi"
test
{test: "hi"}
test2
{test: "hihi"}

// Object pass by copy and then by reference
var dev = {test: 'hello'}
undefined
var dev2 = {}
undefined
var dev3 = Object.assign(dev2,dev)
undefined
dev
{test: "hello"}
dev2
{test: "hello"}
dev3
{test: "hello"}
dev2.test="helloworld"
"helloworld"
dev
{test: "hello"}
dev2
{test: "helloworld"}
dev3
{test: "helloworld"}

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

Friday, 29 November 2019

JS shift, unshift

https://www.hostingadvice.com/how-to/javascript-remove-element-array/

Removing One Element Using splice()

The splice() method is a versatile way of removing, replacing, and/or adding elements in an array. It works similarly to splice() functions in other languages. Basically, you take an array and selectively remove portions of it (aka “splice”). The inputs to the splice() function are the index point to start at and the number of elements to remove.
Also, remember that arrays are zero-indexed in JavaScript.
To remove one element from a specific index in an array:
The splice() call will return any removed elements, so you know what was actually removed.

Removing a Range of Elements Using splice()

Just to make sure you didn’t miss it in the previous example, it is worth calling out specifically that you can remove several consecutive elements with splice().
To remove several consecutive elements from an array:

Removing One Element Using pop()

The array methods push() and pop() work on the the end of an array. The terms push() and pop() come from the idea of a stack in memory from the early days of microprocessors. This implements the idea of a Last-In-First-Out data structure (LIFO). The push() method will ADD an element to the array and the pop() method will remove one.
To remove the last element of an array:

Removing One Element Using shift()

The array methods shift() and unshift() work on the beginning of an array instead of the end of an array, as is the case with push() and pop(). The shift() command will remove the first element of the array and the unshift() command will add an element to the beginning of the array.
To remove the first element of an array:

Searching and Removing a Specific Element by Value

The indexOf() command returns the first index at which a given element can be found in the array, or -1 if it is not present. This can be used along with splice() to search for an element and then remove it, even if you don’t know where it is in the array.
Let’s remove the “foo” element:

Removing Multiple Specific Elements

Let’s add an extra “foo” element to our array, and then remove all occurrences of “foo”:

Monday, 25 November 2019

PHP try catch finally



https://stackify.com/php-try-catch-php-exception-tutorial/

//
try {
    print "this is our try block n";
    throw new Exception();
} catch (Exception $e) {
    print "something went wrong, caught yah! n";
} finally {
    print "this part is always executed n";
}


// Handle different exceptions



class DivideByZeroException extends Exception {};
class DivideByNegativeException extends Exception {};

function process_divide($denominator)
{
    try
    {
        if ($denominator == 0)
        {
            throw new DivideByZeroException();
        }
        else if ($denominator < 0)
        {
            throw new DivideByNegativeException();
        }
        else
        {
            echo 100 / $denominator;
        }
    }
    catch (DivideByZeroException $ex)
    {
        echo "Divide by zero exception!";
    }
    catch (DivideByNegativeException $ex)
    {
        echo "Divide by negative number exception!";
    }
    catch (Exception $x)
    {
        echo "UNKNOWN EXCEPTION!";
    }
// Better catch  
    catch (Exception $e) {
        switch (TRUE) {
            case $e instanceof iException :
            break;
            case $e instanceof mException :
            break;
        }
    }

PHP magic getter and setter vs standard getter and setter, and why is a good idea to not use magic methods


a) Using __get and __set
class MyClass {
    private $firstField;
    private $secondField;

    public function __get($property) {
            if (property_exists($this, $property)) {
                return $this->$property;
            }
    }

    public function __set($property, $value) {
        if (property_exists($this, $property)) {
            $this->$property = $value;
        }
    }
}

$myClass = new MyClass();

$myClass->firstField = "This is a foo line";
$myClass->secondField = "This is a bar line";

echo $myClass->firstField;
echo $myClass->secondField;

/* Output:
    This is a foo line
    This is a bar line
 */
b) Using traditional setters and getters
class MyClass {

    private $firstField;
    private $secondField;

    public function getFirstField() {
        return $this->firstField;
    }

    public function setFirstField($firstField) {
        $this->firstField = $firstField;
    }

    public function getSecondField() {
        return $this->secondField;
    }

    public function setSecondField($secondField) {
        $this->secondField = $secondField;
    }

}

$myClass = new MyClass();

$myClass->setFirstField("This is a foo line");
$myClass->setSecondField("This is a bar line");

echo $myClass->getFirstField();
echo $myClass->getSecondField();

/* Output:
    This is a foo line
    This is a bar line
 */