Skip to content

Commit 7766330

Browse files
author
André Schreck
committed
issue #10: none operator
1 parent 32ae4df commit 7766330

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Gets a function that takes an element and returns a boolean. If an element is ev
5757
Some works similar to the `first`-operator except that not the element is returned, but the value `true` as soon as the passed function evaluates to true.
5858
* __every__<br />
5959
`every` also applies the passed function to each element. As soon as one element does not evaluate to `true` the pipe immediately returns `false`.
60+
* __none__<br />
61+
`none` also applies the passed function to each element. As soon as one element evaluates to `true` the pipe immediately returns `false`.
6062

6163
## When is it usefull?
6264

src/index.spec.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import './index';
2-
import { filter, map, flatMap, distinct, find, some, every } from './operators';
2+
import { filter, map, flatMap, distinct, find, some, every, none } from './operators';
33

44
describe('pipe', () => {
55

@@ -111,6 +111,26 @@ describe('pipe', () => {
111111
expect(result).toBe(false);
112112
});
113113

114+
it('should pipe and return true because all elements do not match criteria', () => {
115+
const result: number = ['3', '5', '7', '9', '11', '13', '15', '17', '19', '21']
116+
.pipe(
117+
map((n: string) => parseInt(n)),
118+
none((n: number) => n%2 === 0)
119+
);
120+
121+
expect(result).toBeTruthy();
122+
});
123+
124+
it('should pipe and return false because not all elements match criteria', () => {
125+
const result: number = ['3', '5', '7', '9', '12', '13', '15', '17', '19', '21']
126+
.pipe(
127+
map((n: string) => parseInt(n)),
128+
none((n: number) => n%2 === 0)
129+
);
130+
131+
expect(result).toBe(false);
132+
});
133+
114134
it('should pipe undefined and null values', () => {
115135
const undefinedMapper = (s: string) => {
116136
if (s === undefined || s === null) {

src/none.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { TerminalOperator, none } from './operators';
2+
3+
describe('none', () => {
4+
5+
it('should be terminal', () => {
6+
const operator: TerminalOperator<any, any> = none((n: number) => n%2 === 0);
7+
expect(operator.isTerminal()).toBeTruthy();
8+
});
9+
10+
it('should have a fallback value', () => {
11+
const operator: TerminalOperator<any, any> = none((n: number) => n%2 === 0);
12+
expect(operator.getFallbackValue()).toBeTruthy();
13+
});
14+
15+
it('should not match', () => {
16+
const operator: TerminalOperator<any, any> = none((n: number) => n%2 === 0);
17+
expect(operator.perform(4)).toEqual({
18+
value: false,
19+
skip: false,
20+
needsFlattening: false
21+
});
22+
});
23+
24+
it('should match', () => {
25+
const operator: TerminalOperator<any, any> = none((n: number) => n%2 === 0);
26+
expect(operator.perform(5)).toEqual({
27+
value: null,
28+
skip: true,
29+
needsFlattening: false
30+
});
31+
});
32+
});

src/operators.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,32 @@ class EveryOperator<T> extends TerminalOperator<T, boolean> {
9090
}
9191
}
9292

93+
class NoneOperator<T> extends TerminalOperator<T, boolean> {
94+
95+
constructor(private tester: Predicate<T>) {
96+
super();
97+
}
98+
99+
public getFallbackValue(): boolean {
100+
return true;
101+
}
102+
103+
public perform(from: T): OperatorResult<boolean> {
104+
if (this.tester(from)) {
105+
return {
106+
value: false,
107+
skip: false,
108+
needsFlattening: false
109+
};
110+
}
111+
return {
112+
value: null,
113+
skip: true,
114+
needsFlattening: false
115+
};
116+
}
117+
}
118+
93119
class FilterOperator<T> extends IntermediateOperator<T, T> {
94120

95121
constructor(private tester: Predicate<T>) {
@@ -205,3 +231,7 @@ export function some<T>(tester: Predicate<T>): TerminalOperator<T, boolean> {
205231
export function every<T>(tester: Predicate<T>): TerminalOperator<T, boolean> {
206232
return new EveryOperator<T>(tester);
207233
}
234+
235+
export function none<T>(tester: Predicate<T>): TerminalOperator<T, boolean> {
236+
return new NoneOperator<T>(tester);
237+
}

0 commit comments

Comments
 (0)