Skip to content

Instantly share code, notes, and snippets.

View ezzabuzaid's full-sized avatar
🎯
Focusing

ezzabuzaid ezzabuzaid

🎯
Focusing
View GitHub Profile
import {
BehaviorSubject,
Observable,
ObservableInput,
Subject,
debounceTime,
exhaustMap,
filter,
finalize,
fromEvent,
@ezzabuzaid
ezzabuzaid / inject-migration.ts
Last active August 27, 2023 18:50
Migrate Angular projects to use new inject fn
import { writeFileSync } from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
/**
* Extracts files from tsconfig.json
*
* @param tsconfigPath
* @returns
*/

I was able to use the ESM version with Angular 15 following these steps:

Note: I use nx, but it should work with other executers like Angular builder

  1. install monaco-editor-webpack-plugin
  2. change the build target to use an executer that supports modifying the webpack config. in case of nx 16, use "@nx/angular:webpack-browser"
  3. add webpack.config.js

angular.json/project.json

import { MonoTypeOperatorFunction, Observable } from 'rxjs';
import {
debounceTime,
distinctUntilChanged,
filter,
map
} from 'rxjs/operators';
interface ITypeaheadOperatorOptions {
minLength: number;
import { createCustomElement } from '@angular/elements';
import { createApplication } from '@angular/platform-browser';
(async () => {
const app = await createApplication({
providers: [],
});
const WebComponent = createCustomElement(YourComponent, {
injector: app.injector,
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);