Skip to content

Instantly share code, notes, and snippets.

View jagomf's full-sized avatar

Jago jagomf

View GitHub Profile
@jagomf
jagomf / simple-calendar.ts
Last active February 6, 2020 10:42
Generate a simple calendar which is an array of arrays
/**
* Generates a simple Calendar formatted as an array of arrays
* @param year Year for which calendar month is generated
* @param month Calendar month to be generated
* @param startWeekMonday Optional offset -- defaults to false (week starts on Sunday)
* @returns Main array (month) of arrays (weeks) of days
* @see Based on algorithm by github:lukeed/calendarize
*/
private generateMonthCalendar(year: number, month: number, startWeekMonday = false) {
const res: number[][] = [];
@jagomf
jagomf / locales.js
Last active February 6, 2020 10:22
Generate constants with months/weekdays names
// More info on locales and codes for names on MDN 'toLocaleString'
weekdaysArray(locale = 'en-us', startMonday = false) {
const initYear = 2006; // Year that started on the first day of week
return Array.from({ length: 7 }, (v, k) => k)
.map(weekday => new Date(initYear, 0, weekday + (startMonday ? 1 : 0) + 1)
.toLocaleString(locale, { weekday: 'narrow' }));
}
@jagomf
jagomf / variable_sized_grid_view.dart
Created January 27, 2020 14:18 — forked from letsar/variable_sized_grid_view.dart
VariableSizedGridView for Flutter (Masonry style)
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
/// Signature for a function that creates a [TileSize] for a given index.
typedef TileSize IndexedTileSizeBuilder(int index);
/// Creates grid layouts with a fixed number of spans in the cross axis.
@jagomf
jagomf / promise-function.js
Last active June 5, 2019 11:22
Promises to async/await
// Function with a promise
const doAsyncStuff = () => {
return new Promise((resolve, reject) => {
if (stuff) {
resolve('cool');
}
reject('bad');
});
};
@jagomf
jagomf / appendRoute.ts
Created October 23, 2018 14:17
Angular: Append a query parameter to current route without navigating
import { ActivatedRoute, Router, NavigationExtras } from '@angular/router';
...
const extras: NavigationExtras = {
relativeTo: this.route,
queryParams: { name: value },
queryParamsHandling: 'merge'
};
this.router.navigate([], extras).then(() => { /* continue here */ });
@jagomf
jagomf / manual-request.ts
Last active October 8, 2018 09:08
Manually create XHR request for Angular
requestName(): Observable<Call> {
return new Observable(observer => {
const endpoint = 'endpoint';
const data = {};
// #region:multipart
// In case of multipart
const formData: FormData = new FormData();
Object.keys(data).forEach((key) => {
formData.append(key, data[key]);
@jagomf
jagomf / observable.ts
Created September 25, 2018 09:33
Create a Observable just like a Promise
returnObservable(): Observable<any> {
return new Observable(observer => {
if (/* whatever */) {
return observer.next(body.data);
} else {
return observer.error(body.data);
}
return { unsubscribe() {} };
});
@jagomf
jagomf / clone.js
Created September 18, 2018 10:45
Simplest way to deep clone (and get a plain object)
const cloned = JSON.parse(JSON.stringify(original));
@jagomf
jagomf / app.ts
Created August 21, 2018 09:52
Import JSON to TypeScript
...
import * as data from './example.json';
...
const word = (<any>data).name;
console.log(word); // output 'testing'
@jagomf
jagomf / example-method.service.ts
Last active September 29, 2018 17:10
Alternate method of returning a subscription in an Angular service
getResultFromRemote(): Subject<string> {
const subs = new Subject<string>();
this.http.get('http://localhost/service')
.subscribe((res) => {
subs.next(res);
}, (err) => {
subs.error(res);
});
return subs;
}