Skip to content

Instantly share code, notes, and snippets.

View ezzabuzaid's full-sized avatar
🎯
Focusing

ezzabuzaid ezzabuzaid

🎯
Focusing
View GitHub Profile
type TestId = string;
function itRenders(
testCase: [DOMSelector | TestId, string, string?][]
): void {
describe.each(testCase)('', (selector, text, report) => {
it(report ?? `renders ${text}`, () => {
spectator.detectChanges();
const element = spectator.query(
selector instanceof DOMSelector ? selector : byTestId(selector)
);
@ezzabuzaid
ezzabuzaid / fast_write.ts
Created August 8, 2021 22:58
Append array to json file without loading it.
async function appendArray(path: string, data: any[]) {
if (!data.length) {
return;
}
const handle = await require('fs').promises.open(path, "r+");
const stat = await handle.stat();
const cursor = stat.size - 1;
const json = JSON.stringify(data);
const buffer = Buffer.from(',' + json.substr(1));
try {
@ezzabuzaid
ezzabuzaid / page_object.ts
Last active November 29, 2021 19:40
Angular Page Object
import { ComponentType } from '@angular/cdk/portal';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { DebugElement, InjectionToken } from '@angular/core';
import { ComponentFixture, TestBed, TestModuleMetadata } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { Subject } from 'rxjs';
export class Page<T> {
private _fixture: ComponentFixture<T>;
constructor(
@ezzabuzaid
ezzabuzaid / path-resolver.js
Created March 26, 2021 22:10
Node.js TypeScript path alias resolver
const tsConfig = require('../tsconfig.json');
const tsConfigPaths = require('tsconfig-paths');
tsConfigPaths.register({
baseUrl: __dirname,
paths: tsConfig.compilerOptions.paths,
});
@ezzabuzaid
ezzabuzaid / singularize.pipe.ts
Created December 8, 2020 10:31
Angular Pipe to Singularize or pluralize word
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'singularize'
})
export class SingularizePipe implements PipeTransform {
public transform(value: any, count: number = 1): string {
return count > 1 ? plural(value) : singular(value);
/**
*
* Wrap database calls within transaction
*
* @param computation an action function with {EntityManager} argument that will be executed after starting the transaction
*
*/
export async function useTransaction<TResult>(computation: (manager: EntityManager) => Promise<TResult>) {
let queryRunner = getConnection().createQueryRunner();
@ezzabuzaid
ezzabuzaid / compute.js
Last active April 17, 2024 05:49
Simple inline web worker helper. compute version of Dart in JavaScript.
function compute(computation, ...message) {
const delegate = () => {
onmessage = ({ data: { computation, message } }) => {
const wrapper = (fn) => Function('"use strict"; return (' + fn.toString() + ')')();
const result = wrapper(computation)(...message);
postMessage(result);
};
}
const functionBody = delegate.toString().replace(/^[^{]*{\s*/, '').replace(/\s*}[^}]*$/, '');
return new Promise((resolve, reject) => {
@ezzabuzaid
ezzabuzaid / eventemitter.js
Created May 28, 2020 22:25
Custom EventEmitter
class EventEmitter {
constructor() {
this.callbacks = {};
this.noEventYet = [];
}
addEventListener(eventName, callback) {
const callbacks = this.callbacks[eventName];
@ezzabuzaid
ezzabuzaid / automapper.dart
Created May 19, 2020 19:18
Dart automapper to serialize and deserialize JSON
import 'dart:mirrors';
class Person {
String firstname;
String lastname;
}
main() {
final json = {"firstname": "ezz", "lastname": "abuzaid"};
Person person = deserialize(json, Person);
@ezzabuzaid
ezzabuzaid / ftp-upload.js
Created January 26, 2020 09:20
Upload your static site over FTP protocol using on command `node ftp-upload.js --from="localFolder" --to="serverFolder"`
const fs = require('fs');
const path = require('path');
const Client = require('ftp');
const program = require('commander');
const client = new Client();
client.connect({
user: "",
password: "",