Best JavaScript code snippet using fast-check-monorepo
service.ts
Source:service.ts
1import { BehaviorSubject, combineLatest, Observable, of, Subscription } from "rxjs";2import { distinctUntilChanged, filter, switchMap } from "rxjs/operators";3import { Service } from "@/core/service";4import { isNil } from "@/utils/common";5import { container } from "@/core/container";6export interface SeriesItem {7 title?: string;8 path?: string;9 children?: Array<SeriesItem>;10 disable?: boolean;11}12export interface Series {13 name: string;14 prefix?: string;15 data: Array<SeriesItem>;16}17export interface NormalizedSeriesItem {18 title?: string;19 path?: string;20 level: number;21 match: boolean;22 disable: boolean;23}24export interface NormalizedSeries {25 name: string;26 prefix?: string;27 data: Array<NormalizedSeriesItem>;28}29export class SeriesService implements Service {30 public currentPath$ = new BehaviorSubject<string | null>(null);31 public series$ = new BehaviorSubject<Series | null>(null);32 public normalizedSeries$ = new BehaviorSubject<NormalizedSeries | null>(null);33 public currentSeries$ = combineLatest([this.currentPath$, this.normalizedSeries$]).pipe(34 switchMap(([currentPath, normalizedSeries]) => {35 if (isNil(currentPath)) {36 return of(null);37 }38 const series = normalizedSeries?.data.find(39 (series) => !isNil(series.path) && this.isSlugEqual(series.path, currentPath)40 );41 return of(series ?? null);42 })43 );44 private subscriptions: Array<Subscription> = [];45 public constructor() {46 this.subscriptions.push(47 this.series$48 .pipe(49 filter((series) => !isNil(series)),50 distinctUntilChanged()51 )52 .subscribe((series) => {53 const normalized = this.normalizeSeries(series!);54 this.normalizedSeries$.next(normalized);55 })56 );57 this.subscriptions.push(58 this.currentPath$.subscribe((currentPath) => {59 const normalized = this.normalizedSeries$.value;60 console.debug("[series.service]", "[subscription currentPath$]", {61 currentPath,62 normalizedCurrentPath: currentPath && this.normalizeSlug(currentPath),63 });64 if (!isNil(normalized)) {65 normalized.data.forEach((item) => {66 item.match = this.isSlugEqual(currentPath, item.path);67 });68 this.normalizedSeries$.next({69 ...normalized,70 });71 }72 })73 );74 }75 public destroy() {76 this.subscriptions.forEach((subscription) => subscription.unsubscribe());77 }78 private normalizeSeries(series: Series): NormalizedSeries {79 const normalizedItems: Array<NormalizedSeriesItem> = [];80 const currentPath = this.currentPath$.value;81 const handleSeriesItems = (items: SeriesItem[], level = 1) => {82 for (const item of items) {83 const normalizedItemPath = !isNil(item.path) ? this.normalizeSlug(series.prefix ?? "", item.path) : undefined;84 normalizedItems.push({85 title: item.title,86 path: normalizedItemPath,87 level,88 match: this.isSlugEqual(currentPath, normalizedItemPath),89 disable: Boolean(item.disable),90 });91 if (item.children && item.children.length > 0) {92 handleSeriesItems(item.children, level + 1);93 }94 }95 };96 handleSeriesItems(series.data);97 return {98 name: series.name,99 prefix: series.prefix,100 data: normalizedItems,101 };102 }103 private normalizeSlug(...paths: string[]) {104 return paths.join("/").split("/").filter(Boolean).join("/") + "/";105 }106 private isSlugEqual(a: string | undefined | null, b: string | undefined | null) {107 if (isNil(a) || isNil(b)) {108 return false;109 }110 return this.normalizeSlug(a) === this.normalizeSlug(b);111 }112}113container.register({114 class: SeriesService,115 constructorArgClasses: [],...
packaged.ts
Source:packaged.ts
1import { promises as fs } from 'fs';2import { tarball } from 'pacote';3import * as path from 'path';4import { list } from 'tar';5/**6 * Compute the list of all files that will be part of the package if published7 * @param packageRoot - The path to the root of the package, eg.: .8 */9export async function computePublishedFiles(packageRoot: string): Promise<string[]> {10 const publishedFiles: string[] = [];11 const tarBuffer = await tarball(`file:${packageRoot}`, { dryRun: true });12 const stream = list({13 onentry: (entry) => {14 const entryPath: string = entry.path as any;15 publishedFiles.push(entryPath.substring(8)); // dropping 'package/'16 },17 });18 stream.end(tarBuffer);19 return publishedFiles;20}21/**22 * Remove from the filesystem any file that will not be published23 * @param packageRoot - The path to the root of the package, eg.: .24 */25export async function removeNonPublishedFiles(26 packageRoot: string,27 opts: { dryRun?: boolean; keepNodeModules?: boolean } = {}28): Promise<{ kept: string[]; removed: string[] }> {29 const kept: string[] = [];30 const removed: string[] = [];31 const publishedFiles = await computePublishedFiles(packageRoot);32 const normalizedPublishedFiles = new Set(33 publishedFiles.map((filename) => path.normalize(path.join(packageRoot, filename)))34 );35 const rootNodeModulesPath = path.join(packageRoot, 'node_modules');36 async function traverse(currentPath: string): Promise<boolean> {37 const content = await fs.readdir(currentPath);38 let numRemoved = 0;39 await Promise.all(40 content.map(async (itemName) => {41 const itemPath = path.join(currentPath, itemName);42 const normalizedItemPath = path.normalize(itemPath);43 const itemDetails = await fs.stat(itemPath);44 if (opts.keepNodeModules && itemPath === rootNodeModulesPath) {45 kept.push(normalizedItemPath);46 } else if (itemDetails.isDirectory()) {47 const fullyCleaned = await traverse(itemPath);48 if (!fullyCleaned) {49 kept.push(normalizedItemPath);50 } else {51 ++numRemoved;52 removed.push(normalizedItemPath);53 if (!opts.dryRun) {54 await fs.rmdir(itemPath);55 }56 }57 } else if (itemDetails.isFile()) {58 if (normalizedPublishedFiles.has(normalizedItemPath)) {59 kept.push(normalizedItemPath);60 } else {61 ++numRemoved;62 removed.push(normalizedItemPath);63 if (!opts.dryRun) {64 await fs.rm(itemPath);65 }66 }67 }68 })69 );70 return content.length === numRemoved;71 }72 await traverse(packageRoot);73 return { kept, removed };...
Using AI Code Generation
1const {normalizedItemPath} = require('fast-check-monorepo');2console.log(normalizedItemPath('test'));3console.log(normalizedItemPath('test/'));4console.log(normalizedItemPath('test/./'));5console.log(normalizedItemPath('test/../'));6console.log(normalizedItemPath('test/../test'));7console.log(normalizedItemPath('test/../test/'));8console.log(normalizedItemPath('test/../test/./'));9console.log(normalizedItemPath('test/../test/../'));10console.log(normalizedItemPath('test/../test/../test'));11console.log(normalizedItemPath('test/../test/../test/'));12console.log(normalizedItemPath('test/../test/../test/./'));13console.log(normalizedItemPath('test/../test/../test
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!