Skip to content

Instantly share code, notes, and snippets.

@minedun6
minedun6 / some.php
Created December 3, 2018 10:54
Laravel "some" macro
<?php
Collection::macro('some', function ($callback) {
return !! $this->first(function ($value, $key) use ($callback) {
return $callback($value, $key);
});
});
// use case
collect([1, 2, 3, 4])->some(function ($value) {
@minedun6
minedun6 / string-utils.js
Created November 29, 2018 13:44 — forked from jonlabelle/string-utils.js
Useful collection of JavaScript string utilities.
// String utils
//
// resources:
// -- mout, https://github.com/mout/mout/tree/master/src/string
/**
* "Safer" String.toLowerCase()
*/
function lowerCase(str){
return str.toLowerCase();
@minedun6
minedun6 / isSunSup.php
Created November 27, 2018 13:23 — forked from bramus/isSunSup.php
PHP: Is The Sun Up (standalone version)?
<?php
function sunIsUp(\DateTime $when, $lat, $lon): bool {
$whenTimestamp = $when->getTimestamp();
$sunriseTimestamp = date_sunrise(
$whenTimestamp,
SUNFUNCS_RET_TIMESTAMP,
$lat,
$lon
@minedun6
minedun6 / macro.md
Created November 27, 2018 13:22 — forked from brunogaspar/macro.md
Recursive Laravel Collection Macros

What?

If a nested array is passed into a Laravel Collection, by default these will be threaded as normal arrays.

However, that's not always the ideal case and it would be nice if we could have nested collections in a cleaner way.

This is where this macro comes in handy.

Setup

@minedun6
minedun6 / file_size_format.php
Created November 9, 2018 15:08
format size (GO, MO, KB, ...)
<?php
function intWithStyle($n)
{
if ($n < 1000) return $n;
$suffix = ['','k','M','G','T','P','E','Z','Y'];
$power = floor(log($n, 1000));
return round($n/(1000**$power),1,PHP_ROUND_HALF_EVEN).$suffix[$power];
};
@minedun6
minedun6 / index.html
Last active November 9, 2018 09:24
Orbit spinner html/vue
<!--https://epic-spinners.epicmax.co/-->
<div class="orbit-spinner">
<div class="orbit"></div>
<div class="orbit"></div>
<div class="orbit"></div>
</div>
@minedun6
minedun6 / mime2ext.php
Created October 9, 2018 19:25 — forked from alexcorvi/mime2ext.php
converting mime types to extension in php
function mime2ext($mime){
$all_mimes = '{"png":["image\/png","image\/x-png"],"bmp":["image\/bmp","image\/x-bmp","image\/x-bitmap","image\/x-xbitmap","image\/x-win-bitmap","image\/x-windows-bmp","image\/ms-bmp","image\/x-ms-bmp","application\/bmp","application\/x-bmp","application\/x-win-bitmap"],"gif":["image\/gif"],"jpeg":["image\/jpeg","image\/pjpeg"],"xspf":["application\/xspf+xml"],"vlc":["application\/videolan"],"wmv":["video\/x-ms-wmv","video\/x-ms-asf"],"au":["audio\/x-au"],"ac3":["audio\/ac3"],"flac":["audio\/x-flac"],"ogg":["audio\/ogg","video\/ogg","application\/ogg"],"kmz":["application\/vnd.google-earth.kmz"],"kml":["application\/vnd.google-earth.kml+xml"],"rtx":["text\/richtext"],"rtf":["text\/rtf"],"jar":["application\/java-archive","application\/x-java-application","application\/x-jar"],"zip":["application\/x-zip","application\/zip","application\/x-zip-compressed","application\/s-compressed","multipart\/x-zip"],"7zip":["application\/x-compressed"],"xml":["application\/xml","text\/xml"],"svg":
@minedun6
minedun6 / localized-date.js
Created October 2, 2018 21:20
Localized date
// list of timezone
// https://gist.github.com/rxaviers/8481876
let nhr = new Date().toLocaleTimeString('en', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hour12: false,
@minedun6
minedun6 / lodash-find.js
Created September 23, 2018 07:32
`find` in lodash works like `first` in Laravel's Collection class, but with some extra super power shorthands`find` in lodash works like `first` in Laravel's Collection class, but with some extra super power shorthands
const episodes = [
{number: 68, published: true, listens: 5683, guest: "Jonathan Reinink", title: "Building interfaces with utility-first CSS"},
{number: 60, published: true, listens: 15812, guest: "Michelle Bu", title: "Engineering payments at Stripe"},
{number: 50, published: true, listens: 12198, guest: "Evan You", title: "What's coming in Vue.js 2.0"},
];
_(episodes).find(e => e.listends > 15000)
@minedun6
minedun6 / example-wait-and-catch.js
Created September 23, 2018 07:13
Promise.takeAtLeast
import { delay, waitAndCatch } from 'promise-wait-and-catch';
// mock something that takes some time
function doSomethingThatTakesAWhile() {
// a more useful example would be calling an API, but in this case we'll just arbitrarily
// run a 3000 millisecond task.
return delay(3000);
}
waitAndCatch(2000, doSomethingThatTakesAWhile)