Skip to content

Instantly share code, notes, and snippets.

Avatar

Alexander cyberduck0

  • Softx s.r.o
  • Prague, Czech Republic
View GitHub Profile
@cyberduck0
cyberduck0 / string-to-color.js
Created Jan 23, 2020
convert any type of string into a hex color
View string-to-color.js
function hashCode(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
}
function intToRGB(i) {
const c = (i & 0x00ffffff).toString(16).toUpperCase();
@cyberduck0
cyberduck0 / ng-for-filter.pipe.ts
Created Oct 7, 2019
Angular ngFor filter pipe
View ng-for-filter.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'ngForFilter',
pure: false,
})
export class NgForFilterPipe implements PipeTransform {
isStrict: boolean;
transform(
@cyberduck0
cyberduck0 / object.ts
Created Sep 24, 2019
Get field of object which contains value
View object.ts
export function getKeyByValue(structure: object, value: string | number) {
return Object.keys(structure).find((keyItem: keyof object) => structure[keyItem] === value);
}
@cyberduck0
cyberduck0 / files-over-http.service.ts
Last active Sep 19, 2019
Angular 2+ Serve and observe file progress with percentage over HTTP request
View files-over-http.service.ts
import { Injectable } from '@angular/core';
import {
HttpClient,
HttpEvent,
HttpEventType,
HttpHeaders,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { defaults } from 'lodash';
@cyberduck0
cyberduck0 / ajax_request.ts
Created Sep 19, 2019
XMLHttpRequest used in observable
View ajax_request.ts
export interface FileUploadSchema {
done: boolean;
response: string;
status: number;
progress: number;
}
export class Request {
request(method: string, URL: string, body: any) {
return new Observable<FileUploadSchema>(subscriber => {
@cyberduck0
cyberduck0 / treeflat.ts
Created Sep 2, 2019
A simple way to find an index of an element in an array nested tree without doing deep search
View treeflat.ts
export interface DisassembledTreeNodeSchema {
index: string;
}
export interface TreeNodeItem {
value: DisassembledTreeNodeSchema;
children: TreeNodeItem[];
}
const tree: TreeNodeItem[] = [nested tree data];
@cyberduck0
cyberduck0 / counter.js
Last active Aug 26, 2019
Simple counter with private variables
View counter.js
// First example
function Counter () {
let val = 0;
return function () {
return ++val;
}
}
const b = new Counter();
@cyberduck0
cyberduck0 / utils.ts
Created Aug 26, 2019
Check if value is empty
View utils.ts
export class Utils {
/**
* @static
* @param {*} value
* @param {*} [strict=false]
* @returns {boolean}
* @memberof Utils
*
* Under the rule of strict the values of object and arrays will be checked as well
* Also Number 0 under strict will return false
@cyberduck0
cyberduck0 / ifile.to.file.ts
Created Mar 30, 2018
Convert IFile from ionic to regular File type
View ifile.to.file.ts
export class Helpers {
// For some reason uploading files from ionic image picker sometimes loses type and sets it to plain/text
// This basically is a hack but works well
IFlieToFile(file: any): Promise<File> {
return new Promise((resolve, reject) => {
if (file.localURL === undefined) {
return resolve(<File> file);
}
let fr = new FileReader();
fr.readAsArrayBuffer(file);
@cyberduck0
cyberduck0 / refresh-image.js
Created Apr 2, 2017
Refresh image on screen in view good for refreshing gif animation that has no loop
View refresh-image.js
var image = $('#mainImageFeature');
if( image.length > 0 ){
var displayed = 0;
$(window).on("scroll",function(){
console.log(document.body.scrollTop + ' = ' + image.offset().top);
if(document.body.scrollTop < image.offset().top + image.height()){
if(displayed == 0){
image.attr('src',image.attr('src'));
displayed = 1;