Skip to content

Instantly share code, notes, and snippets.

View KEIII's full-sized avatar
😎
show me the code

Ivan Kasenkov KEIII

😎
show me the code
View GitHub Profile
@KEIII
KEIII / findAll.js
Last active November 12, 2016 10:39
MongoDB JavaScript function that will return an array of all records matching specified criteria, searching across all collections in the current database. Original from https://stackoverflow.com/a/32790853/1847657
/**
* Find into all collections.
*
* @param {{}} query
* @param {[]} fields
* @param {{}} sort
*
* @return {Array}
*/
function findAll(query, fields, sort) {
const createSelector = (...selectors) => {
let last = selectors.pop();
return state => {
let args = selectors.map(selector => selector(state));
return last.apply(null, args);
}
};
@KEIII
KEIII / ng-var.directive.ts
Last active November 24, 2021 08:36
Missed Angular *ngVar directive (from https://stackoverflow.com/a/43172992/1847657)
// tslint:disable:no-any directive-selector
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
/**
* Declare a variable in the template.
* Eg. <i *ngVar="false as variable">{{ variable | json }}</i>
*/
@Directive({selector: '[ngVar]'})
export class NgVarDirective {
@KEIII
KEIII / ls.js
Last active February 16, 2018 21:54
alert('I am stealing your local storage... ha-ha');
console.log(localStorage);
<canvas id=canvas width=360 height=360>
@KEIII
KEIII / copy-to-clipboard.ts
Created May 19, 2018 12:41
copy to clipboard
// The core function is ported from clipboard.js
// https://github.com/zenorocha/clipboard.js/
const isCopySupported = (doc: Document): boolean => {
return Boolean(doc.queryCommandSupported && doc.queryCommandSupported('copy'));
};
/**
* Creates a fake textarea element, sets its value from `text` property,
* and makes a selection on it.
@KEIII
KEIII / rx-form-control.ts
Last active December 1, 2018 08:45
rx-form-control.ts
import { FormControl } from '@angular/forms';
import { concat, of, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
type Nullable<T> = T | null;
/**
* @see AbstractControl.status
*/
export enum RxFormControlStatus {
@KEIII
KEIII / zoom-level-to-range.ts
Last active June 29, 2018 09:53
Convert zoom level to range
/**
* Convert zoom level to range.
*
* @param {number} zoomLevel The zoom level in meters
* @param {number} latitude The latitude in radians
* @param {number} canvasWidth The canvas width in px
* @param {number} scaleFactor The scale factor (e.g. window.devicePixelRatio)
* @param {number} fov The FOV in degrees (e.g. 60)
* @param {number} globeRadius The globe radius in meters (e.g. 6378137)
*
@KEIII
KEIII / dt-local-iso.ts
Created November 29, 2018 16:02
ISO8601 in local time zone
const zeroPad = (n: number) => n < 10 ? '0' + n : String(n);
const getTimezoneStr = (date: Date): string => {
const tz_offset_min = date.getTimezoneOffset();
const offset_hrs = Math.abs(tz_offset_min / 60);
const offset_min = Math.abs(tz_offset_min % 60);
return tz_offset_min === 0
? 'Z'
: `${tz_offset_min > 0 ? '-' : '+'}${zeroPad(offset_hrs)}:${zeroPad(offset_min)}`;
import { BehaviorSubject, Observable, Subscription } from 'rxjs';
export class RxRpc<P, R> {
constructor(private readonly cmd: (params: P) => Observable<R>) {}
private subscription: Subscription | null = null;
private readonly _pending$ = new BehaviorSubject(false);
public readonly pending$ = this._pending$.asObservable();