Skip to content

Instantly share code, notes, and snippets.

View ziomarco's full-sized avatar

Marco Palmisano ziomarco

  • Milan
View GitHub Profile
@ziomarco
ziomarco / <entity>.module.ts
Last active September 1, 2023 08:13
Easy NestJS Mongoose Testing
/*
Merging all the times <entity>.module.ts and fake module instantation is boring for all, so let's help us using this structure.
*/
export const moduleConf = {
imports: [
TracksModule,
MongooseModule.forFeature([{ name: Entity.name, schema: EntitySchema }]),
MongooseModule.forFeature([
{ name: SecondEntity.name, schema: SecondEntitySchema },
@ziomarco
ziomarco / safeIterate.ts
Last active April 29, 2023 13:22
Need to iterate on functions when potentially multiple valid arguments without risking a RangeError exception?
/**
Do you need to handle a potential RangeError (Max call stack size exceeded) because of a recursive function?
Maybe the input should change in several ranges basing on random criterias?
Please don't try/catch things lot of times, use this :)
Example Usage:
[ this is my recursive function ] [these are my fn args], [this are values that will be tried instead of 'ITERATE' arg ]
const simplifiedTrack: FeatureCollection = handleSyncRecursion(simplifyGeoJsonWithMaxPointsLimit, [track, 50, 'ITERATE'], [0.001, 0.005, 0.008, 0.01]);
*/
@ziomarco
ziomarco / sideEffects.ts
Created November 29, 2021 14:02
NestJS util in order to obtain "side-effects"
/**
* Little helper for creating "side effects" with TypeScript
* @param effect {Promise<T> | Function} Function or promise to call independently
* @param caller {string} Name of side effect, useful for logging
*/
export function sideEffect(effect: Promise<any> | Function, caller: string): void {
if (effect instanceof Promise) {
effect
.then(() => console.log(`Side effect "${caller}" completed without errors.`))
@ziomarco
ziomarco / brokenJsonParser.ts
Last active July 6, 2021 18:10
fast_broken_json_parser
export class BrokenJSONParser {
static forceParseNested(input: string) {
if (!input) return null;
let stringToParse = input;
try {
const parsed = JSON.parse(stringToParse);
return parsed;
} catch (e) {
const errorSearchString = 'at position ';
const startIndex = e.message.indexOf(errorSearchString);