Skip to content

Commit 531e09a

Browse files
authored
Merge pull request #30 from dreambo8563/release
1.2.0
2 parents 81f6eb4 + 6bb28c4 commit 531e09a

File tree

6 files changed

+179
-17
lines changed

6 files changed

+179
-17
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
<a name="1.2.0"></a>
2+
# [1.2.0](https://github.com/dreambo8563/vue-lazy-calc/compare/v1.1.6...v1.2.0) (2019-03-21)
3+
4+
5+
16
<a name="1.1.6"></a>
27
## [1.1.6](https://github.com/dreambo8563/vue-lazy-calc/compare/v1.1.5...v1.1.6) (2019-03-21)
38

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-lazy-calc",
3-
"version": "1.1.6",
3+
"version": "1.2.0",
44
"private": false,
55
"author": "dreambo8563",
66
"main": "dist/vue-lazy-calc.umd.min.js",

src/stream.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ class LazyStream {
1414
tmp.operators = operators;
1515
return tmp;
1616
}
17+
private isInvalid(x: number | string) {
18+
return Number.isNaN(+x) || !Number.isFinite(+x);
19+
}
1720
private createRound(methodName: CalcMethod, precision: number = 0) {
1821
const func = <operatorFunc>Math[methodName];
19-
return function(number: number | string): number {
22+
return (number: number | string): number => {
2023
// const _number = number.value()
21-
if (Number.isNaN(+number) || Number.isNaN(+precision)) {
24+
if (this.isInvalid(number) || this.isInvalid(precision)) {
2225
return NaN;
2326
}
2427
precision =
@@ -45,9 +48,9 @@ class LazyStream {
4548
}
4649

4750
add(y: LazyCalc): LazyStream {
48-
const operation = function(x: number | string) {
51+
const operation = (x: number | string) => {
4952
const _y = y.value();
50-
if (Number.isNaN(+x) || Number.isNaN(+_y)) {
53+
if (this.isInvalid(x) || this.isInvalid(_y)) {
5154
return NaN;
5255
}
5356
return +x + +_y;
@@ -57,8 +60,8 @@ class LazyStream {
5760

5861
subtract(y: LazyCalc): LazyStream {
5962
const _y = y.value();
60-
const operation = function(x: number | string) {
61-
if (Number.isNaN(+x) || Number.isNaN(+_y)) {
63+
const operation = (x: number | string) => {
64+
if (this.isInvalid(x) || this.isInvalid(_y)) {
6265
return NaN;
6366
}
6467
return +x - +_y;
@@ -67,8 +70,8 @@ class LazyStream {
6770
}
6871
multiply(y: LazyCalc): LazyStream {
6972
const _y = y.value();
70-
const operation = function(x: number | string) {
71-
if (Number.isNaN(+x) || Number.isNaN(+_y)) {
73+
const operation = (x: number | string) => {
74+
if (this.isInvalid(x) || this.isInvalid(_y)) {
7275
return NaN;
7376
}
7477
return +x * +_y;
@@ -77,8 +80,8 @@ class LazyStream {
7780
}
7881
divide(y: LazyCalc): LazyStream {
7982
const _y = y.value();
80-
const operation = function(x: number | string) {
81-
if (Number.isNaN(+x) || Number.isNaN(+_y)) {
83+
const operation = (x: number | string) => {
84+
if (this.isInvalid(x) || this.isInvalid(_y)) {
8285
return NaN;
8386
}
8487
return +x / +_y;
@@ -98,16 +101,16 @@ class LazyStream {
98101
const operation = this.createRound("floor", precision);
99102
return this.clone([operation, ...this.operators]);
100103
}
101-
do(fn: operatorFunc): LazyStream {
104+
do(fn: Function): LazyStream {
102105
const operation = function(y: number | string) {
103106
return fn(y);
104107
};
105108
// this.operators.unshift(operation);
106109
return this.clone([operation, ...this.operators]);
107110
}
108111
default(fallback: any): LazyStream {
109-
const operation = function(x: number | string) {
110-
return Number.isNaN(+x) ? fallback : +x;
112+
const operation = (x: number | string) => {
113+
return this.isInvalid(x) ? fallback : +x;
111114
};
112115
return this.clone([operation, ...this.operators]);
113116
}

tests/unit/stream.spec.ts

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import { LazyBase } from "../../src/main";
2+
3+
describe("stream class", () => {
4+
it("stream add - invalid", () => {
5+
const base1 = LazyBase.lazy(1).divide(0);
6+
const base2 = LazyBase.lazy(3);
7+
expect(
8+
LazyBase.stream(base1)
9+
.add(base2)
10+
.value()
11+
).toBeNaN();
12+
});
13+
14+
it("stream subtract - invalid", () => {
15+
const base1 = LazyBase.lazy(1).divide(0);
16+
const base2 = LazyBase.lazy(3);
17+
expect(
18+
LazyBase.stream(base1)
19+
.subtract(base2)
20+
.value()
21+
).toBeNaN();
22+
});
23+
24+
it("stream multiply - invalid", () => {
25+
const base1 = LazyBase.lazy(1).divide(0);
26+
const base2 = LazyBase.lazy(3);
27+
expect(
28+
LazyBase.stream(base1)
29+
.multiply(base2)
30+
.value()
31+
).toBeNaN();
32+
});
33+
it("stream multiply ", () => {
34+
const base1 = LazyBase.lazy(1).divide(4);
35+
const base2 = LazyBase.lazy(3);
36+
expect(
37+
LazyBase.stream(base1)
38+
.multiply(base2)
39+
.value()
40+
).toBe(0.75);
41+
});
42+
43+
it("stream divide - invalid", () => {
44+
const base1 = LazyBase.lazy(1).divide(0);
45+
const base2 = LazyBase.lazy(3);
46+
expect(
47+
LazyBase.stream(base1)
48+
.divide(base2)
49+
.value()
50+
).toBeNaN();
51+
});
52+
53+
it("stream divide", () => {
54+
const base1 = LazyBase.lazy(1).multiply(6);
55+
const base2 = LazyBase.lazy(3);
56+
expect(
57+
LazyBase.stream(base1)
58+
.divide(base2)
59+
.value()
60+
).toBe(2);
61+
});
62+
63+
it("stream round", () => {
64+
const base1 = LazyBase.lazy(1).divide(4);
65+
// const base2 = LazyBase.lazy(3);
66+
expect(
67+
LazyBase.stream(base1)
68+
.round(1)
69+
.value()
70+
).toBe(0.3);
71+
});
72+
73+
it("stream precision - invalid", () => {
74+
const base1 = LazyBase.lazy(1).divide(4);
75+
// const base2 = LazyBase.lazy(3);
76+
expect(
77+
LazyBase.stream(base1)
78+
.round(null)
79+
.value()
80+
).toBe(0);
81+
});
82+
83+
it("stream precision - invalid2", () => {
84+
const base1 = LazyBase.lazy(1).divide(4);
85+
// const base2 = LazyBase.lazy(3);
86+
expect(
87+
LazyBase.stream(base1)
88+
.round(NaN)
89+
.value()
90+
).toBeNaN();
91+
});
92+
93+
it("stream precision - minus", () => {
94+
const base1 = LazyBase.lazy(1).divide(4);
95+
// const base2 = LazyBase.lazy(3);
96+
expect(
97+
LazyBase.stream(base1)
98+
.round(-1)
99+
.value()
100+
).toBe(0);
101+
});
102+
103+
it("stream ceil", () => {
104+
const base1 = LazyBase.lazy(1).divide(4);
105+
// const base2 = LazyBase.lazy(3);
106+
expect(
107+
LazyBase.stream(base1)
108+
.ceil(1)
109+
.value()
110+
).toBe(0.3);
111+
});
112+
113+
it("stream floor", () => {
114+
const base1 = LazyBase.lazy(1).divide(4);
115+
// const base2 = LazyBase.lazy(3);
116+
expect(
117+
LazyBase.stream(base1)
118+
.floor(1)
119+
.value()
120+
).toBe(0.2);
121+
});
122+
123+
it("stream do", () => {
124+
const base1 = LazyBase.lazy(6);
125+
// const base2 = LazyBase.lazy(3);
126+
expect(
127+
LazyBase.stream(base1)
128+
.do(x => x + 10)
129+
.value()
130+
).toBe(16);
131+
});
132+
133+
it("stream default", () => {
134+
const base1 = LazyBase.lazy(1).divide(0);
135+
const base2 = LazyBase.lazy(3);
136+
expect(
137+
LazyBase.stream(base1)
138+
.divide(base2)
139+
.default(88)
140+
.value()
141+
).toBe(88);
142+
});
143+
144+
it("stream default - unused", () => {
145+
const base1 = LazyBase.lazy(1).multiply(8);
146+
const base2 = LazyBase.lazy(2);
147+
expect(
148+
LazyBase.stream(base1)
149+
.divide(base2)
150+
.default(88)
151+
.value()
152+
).toBe(4);
153+
});
154+
});

types/stream.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { operatorFunc } from "./main";
21
import { LazyCalc } from "./simple";
32
declare class LazyStream {
43
private operators;
54
private compose;
65
private clone(operators);
6+
private isInvalid(x);
77
private createRound(methodName, precision?);
88
constructor();
99
add(y: LazyCalc): LazyStream;
@@ -13,7 +13,7 @@ declare class LazyStream {
1313
round(precision?: number): LazyStream;
1414
ceil(precision?: number): LazyStream;
1515
floor(precision?: number): LazyStream;
16-
do(fn: operatorFunc): LazyStream;
16+
do(fn: Function): LazyStream;
1717
default(fallback: any): LazyStream;
1818
value(): any;
1919
}

0 commit comments

Comments
 (0)