Skip to content

Instantly share code, notes, and snippets.

View space11's full-sized avatar
🐧

Borys space11

🐧
View GitHub Profile
// Source: https://github.com/typeorm/typeorm/issues/508
// Note: when we are referencing post we want to via non primary key, but by `postId`
@OneToOne(() => PostInfo, (entity) => entity.postId)
@JoinColumn({ name: 'postId', referencedColumnName: 'postId' })
post: PostInfo;
@space11
space11 / component.html
Last active October 21, 2022 11:04
Angular refresh slick with slickItem width = 0 issue
<ngx-slick-carousel class="carousel" [config]="carouselConfig" #slickCarousel>
<div ngxSlickItem *ngFor="let item of vm.item; let i = index"
class="py-5 text-center cursor-pointer">
{{ item.content }}
</div>
</label>
</ngx-slick-carousel>
@space11
space11 / index.html
Created October 19, 2022 08:22
Example of restricting the typing of negative numbers
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<input type="number" name="test_name" min="0" oninput="validity.valid||(value='');">
</body>
</html>
@space11
space11 / renderer2.service.ts
Created October 11, 2022 20:12
How to inject Renderer2 into custom service in Angular
import { Renderer2, RendererFactory2 } from '@angular/core';
@Injectable()
class Service {
private renderer: Renderer2;
constructor(rendererFactory: RendererFactory2) {
this.renderer = rendererFactory.createRenderer(null, null);
}
}
@space11
space11 / resize-observer.directive.ts
Last active October 10, 2022 15:53
Angular component resize observer.
import {
Directive,
ElementRef,
EventEmitter,
OnDestroy,
Output
} from '@angular/core';
import ResizeObserver from 'resize-observer-polyfill';
const entriesMap = new WeakMap();
@space11
space11 / exhaustive-switch-check.ts
Created September 14, 2022 05:31
How do I check that a switch block is exhaustive in TypeScript?
enum Color {
Red,
Green,
Blue
}
function getColorName(c: Color): string {
switch (c) {
case Color.Red:
return "red";
@space11
space11 / at-least-one.validator.ts
Created August 8, 2022 16:23
Angular Validator: At least one control has to pass validation function.
import { FormGroup, ValidationErrors, ValidatorFn } from '@angular/forms';
/**
* Validates that at least one of the form controls is valid against `validator` function passed as the parameter.
* @param validator Validation function, in example `Validators.required`.
* @param controls Controls names.
* @returns ValidationErrors or null if at least control is valid.
*
* @example
* ```JS
@space11
space11 / regex-match-precision.md
Created July 29, 2022 06:00
RegEx to match number to exact decimal places
const precision = '4';
const re = new RegExp('^[0-9]*\.[0-9]{' + precision + '}$');


//returns true
re.test('10.50')

//returns false
re.test('-120')
@space11
space11 / ngx-datatable-demo.ts
Created July 28, 2022 19:22
Server Side sorted, paginated and filter ngx-datatable.
/*
credit to: https://stackoverflow.com/questions/46040077/how-to-implement-server-side-pagination-server-side-sorting-with-ngx-datatable
The best way to handle both server-side pagination AND server-side sorting consist of:
- having a page object which holds all the pagination and sorting information
(like the order column, order direction, page number, page size, ...) which will be bound to the table
- having a single function reloadTable() which calls the API to get the data using the data stored in the page object
as parameters, automaticaly re-rendering the table
- having a pageCallback which only updates the data contained in page relative to the pagination and then calls reloadTable()
- having a sortCallback which only updates the data contained in page relative to the sorting and then calls reloadTable()
*/
@space11
space11 / js-array-of-arrays.md
Created July 11, 2022 19:37
Map an array of arrays

Working with nested arrays in JavaScript / TS.

  1. Map array of arrays to single array - flat. [].concat(...this.array1.map(ap =&gt; ap.subArray))