Skip to content

Instantly share code, notes, and snippets.

View benqus's full-sized avatar
🛫

Bence Kormos benqus

🛫
View GitHub Profile
import { ErrorController } from './types';
function throwIfConditionIsTrue(error: Error | string, condition: boolean): void {
if (condition) throw error instanceof Error ? error : new Error(error);
}
export function throwError(error: Error | string): ErrorController {
const when = {
isNull: (arg: unknown): void => throwIfConditionIsTrue(error, arg === null),
isUndefined: (arg: unknown): void => throwIfConditionIsTrue(error, typeof arg === 'undefined'),
async function render(...args: Array<any>) {
const strings = args[0];
const params = args.slice(1);
const results = await Promise.allSettled(params.map(param => typeof param === 'function' ? param() : param));
return strings.map((str: string, i: number): string => {
const result = results[i];
if (result == null) return str;
return str + (result.status === 'fulfilled' ? result.value : result.reason);
}).join('');
}
@benqus
benqus / counter-component.tsx
Created February 20, 2024 23:39
Why TF is this not working ...
import { JSX } from 'solid-js';
import { CounterContextProvider, useCounterContext } from '../contexts/counter';
export function WithContext(): JSX.Element {
const [ { count }, { increment, decrement } ] = useCounterContext();
return (
<CounterContextProvider>
<span>{count}</span>
<button onClick={increment}>+</button>
@benqus
benqus / pubsub.js
Last active August 12, 2021 07:48
export class PubSub {
constructor() {
this._subs = new Map()
}
subscribe(action, fn) {
if (!this._subs.has(action)) {
this._subs.set(action, new Set())
}
/**
* @class
* @classdesc The Job model class
*/
var Job = Model.extend({
idAttribute: 'resource_uri',
defaults: {
name: 'n/a'
@benqus
benqus / Sibling.js
Created November 11, 2014 15:47
Simple Mediator
function Sibling() {
mediator.add(this);
this.id = Sibling.getUniqueID();
}
Sibling.getUniqueID = (function () {
var id = 0;
return function () {
return id++;
@benqus
benqus / Observable.js
Last active August 29, 2015 14:09
Observable - Pub/Sub
/**
* Provides basic Pub/Sub interface and functionality
*
* @class Observable
*/
function Observable() {
this.subscribers = [];
}
/**
@benqus
benqus / fjs
Last active August 29, 2015 14:05
fjs - idea
http://pastebin.com/zKpJY9ck - copy from there
# single line comment
##
multi line comment
##
# variable assignment
a = 7.
// Backbone.js 2.0.0
// (c) 2010-2011 Jeremy Ashkenas, DocumentCloud Inc.
// (c) 2011-2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
// Backbone may be freely distributed under the MIT license.
// For all details and documentation:
// http://backbonejs.org
(function () {
@benqus
benqus / js_inheritance
Created January 8, 2013 12:20
JavaScript inheritance behaviour testing
var prototypeTest = (function () {
/**
* Instance class definition
* inherits from Base
*/
function Instance() {
//super class constructor
Base.apply(this, arguments);
}