Categories

## How to Implement a Graph Class in Javascript

The following code snippet shows an implementation of a Graph in JS

``````class Graph {
verticies = {};

if (!this.verticies[vertix]) this.verticies[vertix] = new Set();

}
}
}``````
Categories

## How to Implement a Union Find Class in Javascript

Use the code below to implement a union-find class in Javascript:
1. Array Version

``````class UnionFind {
constructor(n) {
this.root = new Array(n).fill(null).map((x, idx) => idx);
this.rank = new Array(n).fill(1);
this.vertexCount = n;
}
find(v) {
if (v === this.root[v]) {
return v;
}
this.root[v] = this.find(this.root[v]);
return this.root[v];
}
union(v1, v2) {
const r1 = this.find(v1),
r2 = this.find(v2);

if (r1 === r2) {
return;
}
if (this.rank[r1] > this.rank[r2]) {
this.root[r2] = r1;
} else if (this.rank[r1] < this.rank[r2]) {
this.root[r1] = r2;
} else {
this.root[r2] = r1;
this.rank[r1] += 1;
}
}
disjointSets() {
const map = new Map();
for (let i = 0; i < this.vertexCount; ++i) {
const root = this.find(i);
const vtx = map.has(root) ? map.get(root) : [];
vtx.push(i);
map.set(root, vtx);
}
return map;
}
}``````

2. Object version:

``````class UnionFind {
constructor(nums) {
this.root = new Map();
this.rank = new Map();
for (const num of nums) {
this.root.set(num, num);
this.rank.set(num, 1);
}
this.nums = nums;
}
find(v) {
if (v === this.root.get(v)) {
return v;
}
this.root.set(v, this.find(this.root.get(v)));
return this.root.get(v);
}
union(v1, v2) {
const r1 = this.find(v1),
r2 = this.find(v2);

if (r1 === r2) {
return;
}
if (this.rank.get(r1) > this.rank.get(r2)) {
this.root.set(r2, r1);
} else if (this.rank.get(r1) < this.rank.get(r2)) {
this.root.set(r1, r2);
} else {
this.root.set(r2, r1);
this.rank.set(r1, this.rank.get(r1) + 1);
}
}
disjointSets() {
const map = new Map();
for (let i = 0; i < this.nums.length; i++) {
const root = this.find(this.nums[i]);
const vtx = map.has(root) ? map.get(root) : [];
vtx.push(this.nums[i]);
map.set(root, vtx);
}
return map;
}
}``````
Categories

## Change DB Connection at Runtime in Laravel

`Use the following code to change Laravel Database at runtime.`
``````\Config::set("database.connections.mysql", [
"host" => "your-host.com",
"driver" => "mysql",
"database" => 'database-name',
]);
\DB::purge('mysql');``````

Categories

## PhpSpreadsheet Issue With Vertical Tabs (0x0B)

``Failed to load path_to_project/storage/framework/laravel-excel/laravel-excel-eTdm3TlUf1neh8QbolfXmodtigO50x3x.html as a DOM Document``

Recently our PHP excel parser (PhpSpreadsheet) was failing when parsing some rows fetched from the database. With a series of trial and error tests, I found the row that was causing the failure.

So, I copied the sentence in Sublime Text and saw that there was a hex character (0x0b) in the sentence. This character is known as a “Vertical Space” and is visually invisible in most editors. I couldn’t see this character in Mysql Workbench so I copied the whole row in Sublime text and saw the hex representation of this character. Below are some other representations of this character:

• 0x0b
• “\v”

To fix the issue, I ran the following SQL query to find all sentences containing this character and then removed them from the database:

``SELECT text FROM messages WHERE text LIKE CONCAT('%',0x0b,'%');``

It’s a good practice to filter your inputs and make sure you’re not storing unwanted characters like these in the database, they might bite you one day!

Categories

## How to Get All Redis Cache Keys in Laravel

TL;DR:

``Cache::getRedis()->keys("*")``

If you’ve set up your Laravel application with Redis and you want to see all the keys registered in your Redis, simply open Laravel Tinker using the following command:

``php artisan tinker``

And then run the following code snippet to get all the keys available in your redis:

``Cache::getRedis()->keys("*")``
Categories