Skip to content

Instantly share code, notes, and snippets.

View mohammadhzp's full-sized avatar
🎯
Focusing

Mohammad Puyandeh mohammadhzp

🎯
Focusing
View GitHub Profile
@mohammadhzp
mohammadhzp / dummy.js
Created February 6, 2023 09:25
Singleton Impl in Javascript. `singleton.js` is the file you really need. the rest is there to make a running exmaple.
class Dummy extends Singleton {
constructor() {
super();
this.i = 0;
}
incr() { ++this.i; }
}
@mohammadhzp
mohammadhzp / number_format.php
Last active July 4, 2021 14:18
php compact_number_format keep decimals as needed
<?php
function compact_number_format(float $num , int $decimals = 2 , ?string $decimal_separator = '.' , ?string $thousands_separator = ','): string {
$r = number_format($num, $decimals, $decimal_separator, $thousands_separator);
if ($decimals === 0) {
return $r;
}
return rtrim(rtrim($r, '0'), $decimal_separator);
}
@mohammadhzp
mohammadhzp / utility_helper.php
Last active May 22, 2022 08:13
get_instance in codeigniter 4
# 1- Make a helper file and put following code in there
# 2- Auto-load it
# 3- call `register_ci_instance($this)` at the very beginning in your base controller
# 4- use `$ci = &get_instance()` anywhere you want. FYI: I'm not sure if `&` is even necessary when calling `get_instance`.
# Also, you may change `\App\Controllers\BaseController` for better IDE autocomplete support.
# Tested on PHP8
<?php
$CI_INSTANCE = []; # It keeps a ref to global CI instance