Skip to content

Commit 33267e2

Browse files
author
Tane Piper
committed
commit example library
1 parent 8044051 commit 33267e2

28 files changed

+8192
-24309
lines changed

.eslintrc.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"root": true,
3+
"ignorePatterns": ["**/*"],
4+
"plugins": ["@nrwl/nx"],
5+
"overrides": [
6+
{
7+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
8+
"rules": {
9+
"@nrwl/nx/enforce-module-boundaries": [
10+
"error",
11+
{
12+
"enforceBuildableLibDependency": true,
13+
"allow": [],
14+
"depConstraints": [{ "sourceTag": "*", "onlyDependOnLibsWithTags": ["*"] }]
15+
}
16+
]
17+
}
18+
},
19+
{
20+
"files": ["*.ts", "*.tsx"],
21+
"extends": ["plugin:@nrwl/nx/typescript"],
22+
"parserOptions": { "project": "./tsconfig.*?.json" },
23+
"rules": {}
24+
},
25+
{
26+
"files": ["*.js", "*.jsx"],
27+
"extends": ["plugin:@nrwl/nx/javascript"],
28+
"rules": {}
29+
}
30+
]
31+
}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ testem.log
3737
# System Files
3838
.DS_Store
3939
Thumbs.db
40+
/docs/

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
/dist
44
/coverage
5+
/docs

.prettierrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"singleQuote": true
2+
"singleQuote": true,
3+
"printWidth": 120,
4+
"trailingComma": "all"
35
}

.vscode/extensions.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"recommendations": [
33
"ms-vscode.vscode-typescript-tslint-plugin",
4-
"esbenp.prettier-vscode"
4+
"esbenp.prettier-vscode",
5+
"firsttris.vscode-jest-runner"
56
]
67
}

babel.config.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@nrwl/web/babel"],
3+
"babelrcRoots": ["*"]
4+
}

jest.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
projects: ['<rootDir>/libs/rxjs/example-lib'],
3+
};

jest.preset.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const nxPreset = require('@nrwl/jest/preset');
2+
3+
module.exports = { ...nxPreset };

libs/rxjs/example-lib/.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@nrwl/web/babel"],
3+
"plugins": []
4+
}

libs/rxjs/example-lib/.eslintrc.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "../../../.eslintrc.json",
3+
"ignorePatterns": ["!**/*"],
4+
"rules": {}
5+
}

libs/rxjs/example-lib/CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Added
11+
12+
- `fizzBuzz` operator that will return a [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz) based
13+
on an observable stream of integer numbers.

libs/rxjs/example-lib/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# rxjs-example-lib
2+
3+
This library was generated with [Nx](https://nx.dev).
4+
5+
## Running unit tests
6+
7+
Run `nx test rxjs-example-lib` to execute the unit tests via [Jest](https://jestjs.io).

libs/rxjs/example-lib/jest.config.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
displayName: 'rxjs-example-lib',
3+
preset: '../../../jest.preset.js',
4+
globals: {
5+
'ts-jest': {
6+
tsConfig: '<rootDir>/tsconfig.spec.json',
7+
},
8+
},
9+
transform: {
10+
'^.+\\.[tj]sx?$': 'ts-jest',
11+
},
12+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
13+
coverageDirectory: '../../../coverage/libs/rxjs/example-lib',
14+
};

libs/rxjs/example-lib/package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "@rxjs-ninja/example-lib",
3+
"description": "An example of an RxJS Typescript library for the web",
4+
"version": "0.0.1",
5+
"keywords": [
6+
"Rx",
7+
"RxJS",
8+
"ReactiveX",
9+
"ReactiveExtensions",
10+
"Streams",
11+
"Observables",
12+
"Observable",
13+
"Stream",
14+
"Typescript",
15+
"Reactive",
16+
"Utility",
17+
"FizzBuzz"
18+
],
19+
"homepage": "https://rxjs.ninja",
20+
"bugs": {
21+
"url": "https://github.com/rxjs-ninja/nx-library-starter/issues"
22+
},
23+
"license": "MIT",
24+
"author": {
25+
"name": "Tane Piper",
26+
"url": "https://tane.dev"
27+
},
28+
"repository": {
29+
"type": "git",
30+
"url": "https://github.com/rxjs-ninja/nx-library-starter.git"
31+
},
32+
"peerDependencies": {
33+
"@rxjs-ninja/rxjs-utility": "~2.1.2",
34+
"rxjs": ">=6.5.0"
35+
}
36+
}

libs/rxjs/example-lib/src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @packageDocumentation
3+
* @module array
4+
* @ignore
5+
*/
6+
/* istanbul ignore file */
7+
export { fizzbuzz } from './lib/fizzbuzz';
8+
export { fromFizzbuzz } from './lib/from-fizzbuzz';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { marbles } from 'rxjs-marbles/jest';
2+
import { fizzbuzz } from '@rxjs-ninja/example-lib';
3+
4+
describe('fizzbuzz', () => {
5+
it(
6+
'should return Fizz for 3, Buzz for 5, FizzBuzz for 15 or number if any other number',
7+
marbles((m) => {
8+
const input = m.hot('-a-b-c-d-e-f-g-|', { a: 1, b: 2, c: 3, d: 4, e: 5, f: 15, g: 16 });
9+
const subs = '^--------------!';
10+
const expected = m.cold('-t-u-v-w-x-y-z-|', { t: 1, u: 2, v: 'Fizz', w: 4, x: 'Buzz', y: 'FizzBuzz', z: 16 });
11+
m.expect(input.pipe(fizzbuzz())).toBeObservable(expected);
12+
m.expect(input).toHaveSubscriptions(subs);
13+
}),
14+
);
15+
});
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @packageDocumentation
3+
* @module fizzbuzz
4+
*/
5+
import { Observable, OperatorFunction } from 'rxjs';
6+
import { mapIfSource } from '@rxjs-ninja/rxjs-utility';
7+
8+
/**
9+
* The `fizzBuzz` operator takes an observable source of numbers and returns
10+
* either the number, or if the number is a modulus of `3, 5 and 15` it will instead return `Fizz`,
11+
* `Buzz` or `FizzBuzz`
12+
*
13+
* @example
14+
* ```ts
15+
* timer(1000).pipe(fizzbuzz()).subscribe()
16+
* // 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz
17+
* ```
18+
*
19+
* @returns Observable of a FizzBuzz sequence of numbers
20+
* @category Fizzbuzz Operators
21+
*/
22+
export function fizzbuzz(): OperatorFunction<number, string | number> {
23+
return (source: Observable<number>) =>
24+
source.pipe(
25+
mapIfSource<number, string, number>(
26+
(value) => value % 15 == 0 || value % 3 == 0 || value % 5 == 0,
27+
(value) => (value % 15 == 0 ? `FizzBuzz` : value % 3 === 0 ? 'Fizz' : 'Buzz'),
28+
(value) => value,
29+
),
30+
);
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { observe } from 'rxjs-marbles/jest';
2+
import { reduce, tap } from 'rxjs/operators';
3+
import { fromFizzbuzz } from '@rxjs-ninja/example-lib';
4+
5+
describe('fromFizzbuzz', () => {
6+
it(
7+
'should create observable fibonacci sequence number up to the max number of iterations',
8+
observe(() =>
9+
fromFizzbuzz(20, 0).pipe(
10+
reduce(
11+
(acc, val) =>
12+
typeof val === 'number'
13+
? { ...acc, number: [...acc.number, val] }
14+
: {
15+
...acc,
16+
string: [...acc.string, val],
17+
},
18+
{ number: [], string: [] },
19+
),
20+
tap((value) => expect(value.string).toHaveLength(8)),
21+
),
22+
),
23+
);
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @packageDocumentation
3+
* @module fizzbuzz
4+
*/
5+
6+
import { Observable, Subscriber, timer } from 'rxjs';
7+
import { finalize, skip, takeWhile, tap } from 'rxjs/operators';
8+
import { mapIfSource } from '@rxjs-ninja/rxjs-utility';
9+
10+
/**
11+
* The `fromFizzbuzz` operator is used to create an observable [FizzBuzz](https://en.wikipedia.org/wiki/Fizz_buzz)
12+
* sequence.
13+
*
14+
* @param iterations The number of iterations to emit
15+
* @param emitDelay If set the observable will emit per millisecond set, by default this is 0
16+
*
17+
* @example
18+
* ```ts
19+
* fromFizzbuzz(15).pipe(tap(console.log)).subscribe()
20+
* // 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz Buzz
21+
* ```
22+
*
23+
* @returns Observable of a FizzBuzz sequence of numbers
24+
* @category Fizzbuzz Observables
25+
*/
26+
export function fromFizzbuzz(iterations: number, emitDelay = 0): Observable<string | number> {
27+
return new Observable((subscriber: Subscriber<number>) => {
28+
timer(0, emitDelay)
29+
.pipe(
30+
takeWhile((value) => !subscriber.closed && value < iterations),
31+
skip(1),
32+
mapIfSource<number, string, number>(
33+
(value) => value % 15 == 0 || value % 3 == 0 || value % 5 == 0,
34+
(value) => (value % 15 == 0 ? `FizzBuzz` : value % 3 === 0 ? 'Fizz' : 'Buzz'),
35+
(value) => value,
36+
),
37+
tap((value) => subscriber.next(value as any)),
38+
finalize(() => subscriber.complete()),
39+
)
40+
.subscribe();
41+
});
42+
}

libs/rxjs/example-lib/tsconfig.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"files": [],
4+
"include": [],
5+
"references": [
6+
{
7+
"path": "./tsconfig.lib.json"
8+
},
9+
{
10+
"path": "./tsconfig.spec.json"
11+
}
12+
]
13+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "es2015",
5+
"outDir": "../../../dist/out-tsc",
6+
"declaration": true,
7+
"types": ["node"]
8+
},
9+
"exclude": ["**/*.spec.ts"],
10+
"include": ["**/*.ts"]
11+
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "../../../dist/out-tsc",
5+
"module": "commonjs",
6+
"types": ["jest", "node"]
7+
},
8+
"include": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js", "**/*.spec.jsx", "**/*.d.ts"]
9+
}

nx.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,9 @@
2222
}
2323
}
2424
},
25-
"projects": {}
25+
"projects": {
26+
"rxjs-example-lib": {
27+
"tags": []
28+
}
29+
}
2630
}

0 commit comments

Comments
 (0)