feat(utils): add utils library with a fake ramdon generator

This commit is contained in:
thomas
2022-12-15 17:04:07 +01:00
parent 0979a9f3db
commit c60791a530
13 changed files with 192 additions and 10 deletions

View File

@@ -20,7 +20,8 @@
"apps/rxjs-pipe-bug/src/assets" "apps/rxjs-pipe-bug/src/assets"
], ],
"styles": ["apps/rxjs-pipe-bug/src/styles.scss"], "styles": ["apps/rxjs-pipe-bug/src/styles.scss"],
"scripts": [] "scripts": [],
"allowedCommonJsDependencies": ["seedrandom"]
}, },
"configurations": { "configurations": {
"production": { "production": {

View File

@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/member-ordering */ /* eslint-disable @typescript-eslint/member-ordering */
import { randomError } from '@angular-challenges/shared/utils';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { randNumber } from '@ngneat/falso';
import { ComponentStore } from '@ngrx/component-store'; import { ComponentStore } from '@ngrx/component-store';
import { of } from 'rxjs'; import { of } from 'rxjs';
@@ -47,12 +47,12 @@ export class LocalDBService extends ComponentStore<DBState> {
}) })
); );
deleteOneTopic = (id: number) => { deleteOneTopic = (id: number) =>
const randomNumber = randNumber({ min: 0.1, max: 1, fraction: 2 }); randomError({
if (randomNumber > 0.5) { success: () => {
this.deleteOne(id); this.deleteOne(id);
return of(true); return of(true);
} },
return of(false); error: () => of(false),
}; });
} }

View File

@@ -0,0 +1,36 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts"],
"rules": {
"@angular-eslint/directive-selector": [
"error",
{
"type": "attribute",
"prefix": "angularChallenges",
"style": "camelCase"
}
],
"@angular-eslint/component-selector": [
"error",
{
"type": "element",
"prefix": "angular-challenges",
"style": "kebab-case"
}
]
},
"extends": [
"plugin:@nrwl/nx/angular",
"plugin:@angular-eslint/template/process-inline-templates"
]
},
{
"files": ["*.html"],
"extends": ["plugin:@nrwl/nx/angular-template"],
"rules": {}
}
]
}

View File

@@ -0,0 +1,7 @@
# shared-utils
This library was generated with [Nx](https://nx.dev).
## Running unit tests
Run `nx test shared-utils` to execute the unit tests.

View File

@@ -0,0 +1,22 @@
/* eslint-disable */
export default {
displayName: 'shared-utils',
preset: '../../../jest.preset.js',
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
stringifyContentPathRegex: '\\.(html|svg)$',
},
},
coverageDirectory: '../../../coverage/libs/shared/utils',
transform: {
'^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
},
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
snapshotSerializers: [
'jest-preset-angular/build/serializers/no-ng-attributes',
'jest-preset-angular/build/serializers/ng-snapshot',
'jest-preset-angular/build/serializers/html-comment',
],
};

View File

@@ -0,0 +1,28 @@
{
"name": "shared-utils",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"sourceRoot": "libs/shared/utils/src",
"prefix": "angular-challenges",
"targets": {
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/shared/utils/jest.config.ts",
"passWithNoTests": true
}
},
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"libs/shared/utils/**/*.ts",
"libs/shared/utils/**/*.html"
]
}
}
},
"tags": []
}

View File

@@ -0,0 +1 @@
export * from './lib/random-http-error.utils';

View File

@@ -0,0 +1,24 @@
import { randNumber } from '@ngneat/falso';
/**
* Execute success function if value is above threshold and error function otherwise
* Success and error function should return the same ReturnType
* @param success
* @param error
* @param threashold default to 0.5
*/
export const randomError = <T>({
success,
error,
threashold,
}: {
success: () => T;
error: () => T;
threashold?: number;
}): T => {
const randomNumber = randNumber({ min: 0.1, max: 1, fraction: 2 });
if (randomNumber > (threashold ?? 0.5)) {
return success();
}
return error();
};

View File

@@ -0,0 +1 @@
import 'jest-preset-angular/setup-jest';

View File

@@ -0,0 +1,29 @@
{
"compilerOptions": {
"target": "es2022",
"useDefineForClassFields": false,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"extends": "../../../tsconfig.base.json",
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}

View File

@@ -0,0 +1,17 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"declaration": true,
"declarationMap": true,
"inlineSources": true,
"types": []
},
"exclude": [
"src/test-setup.ts",
"src/**/*.spec.ts",
"jest.config.ts",
"src/**/*.test.ts"
],
"include": ["src/**/*.ts"]
}

View File

@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"files": ["src/test-setup.ts"],
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}

View File

@@ -21,6 +21,7 @@
"@angular-challenges/ngrx-notification/model": [ "@angular-challenges/ngrx-notification/model": [
"libs/ngrx-notification/model/src/index.ts" "libs/ngrx-notification/model/src/index.ts"
], ],
"@angular-challenges/shared/utils": ["libs/shared/utils/src/index.ts"],
"@tomalaforge/ngrx-callstate-store": [ "@tomalaforge/ngrx-callstate-store": [
"libs/shared/ngrx-callstate-store/src/index.ts" "libs/shared/ngrx-callstate-store/src/index.ts"
] ]