Skip to content

Instantly share code, notes, and snippets.

View ostgals's full-sized avatar

Andrejs Kuzmins ostgals

  • Riga, LV
View GitHub Profile
@ostgals
ostgals / ko.ext.lazy.js
Created October 10, 2015 17:55
Knockout - Lazy data loading into observables
// thanks to rniemeyer!
// http://www.knockmeout.net/2011/06/lazy-loading-observable-in-knockoutjs.html
ko.extenders.lazy = function(target, callback){
var result = ko.computed({
read: function(){
if (!result.loaded()) callback.call(context);
return target();
},
@ostgals
ostgals / snippet01.js
Last active October 10, 2015 18:06
Snippet - Multi-thread queue with jq's promises
{
// .....
enqueue: function(items, get_promise, num_threads){
num_threads = num_threads || 1;
var dfd = $.Deferred(), i = 0, num_total = items.length, num_active = 0, num_done = 0,
next = function(){
while (num_active < num_threads && i < num_total) {
num_active++;
get_promise(items[i++]).done(function(){
<?
$arSections = array_reduce($arResult["SECTIONS"], function($carry, $section){
if (empty($section["IBLOCK_SECTION_ID"])) { // depth 1
$section["LEVEL2_ITEMS"] = [];
$carry[$section["ID"]] = $section;
} else { // level
@ostgals
ostgals / dist.js
Created March 20, 2019 10:22
Spherical distance on Earth
// credits: http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates
function dist(a, b) {
const [a0, a1, b0, b1] = [...a, ...b].map(x => x / 180 * Math.PI);
const { acos, sin, cos } = Math;
return acos(sin(a0) * sin(b0) + cos(a0) * cos(b0) * cos(a1 - b1)) * 6371;
}