Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<a name="1.2.0"></a>
# [1.2.0](https://github.com/dreambo8563/vue-lazy-calc/compare/v1.1.6...v1.2.0) (2019-03-21)



<a name="1.1.6"></a>
## [1.1.6](https://github.com/dreambo8563/vue-lazy-calc/compare/v1.1.5...v1.1.6) (2019-03-21)

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-lazy-calc",
"version": "1.1.6",
"version": "1.2.0",
"private": false,
"author": "dreambo8563",
"main": "dist/vue-lazy-calc.umd.min.js",
Expand Down
29 changes: 16 additions & 13 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ class LazyStream {
tmp.operators = operators;
return tmp;
}
private isInvalid(x: number | string) {
return Number.isNaN(+x) || !Number.isFinite(+x);
}
private createRound(methodName: CalcMethod, precision: number = 0) {
const func = <operatorFunc>Math[methodName];
return function(number: number | string): number {
return (number: number | string): number => {
// const _number = number.value()
if (Number.isNaN(+number) || Number.isNaN(+precision)) {
if (this.isInvalid(number) || this.isInvalid(precision)) {
return NaN;
}
precision =
Expand All @@ -45,9 +48,9 @@ class LazyStream {
}

add(y: LazyCalc): LazyStream {
const operation = function(x: number | string) {
const operation = (x: number | string) => {
const _y = y.value();
if (Number.isNaN(+x) || Number.isNaN(+_y)) {
if (this.isInvalid(x) || this.isInvalid(_y)) {
return NaN;
}
return +x + +_y;
Expand All @@ -57,8 +60,8 @@ class LazyStream {

subtract(y: LazyCalc): LazyStream {
const _y = y.value();
const operation = function(x: number | string) {
if (Number.isNaN(+x) || Number.isNaN(+_y)) {
const operation = (x: number | string) => {
if (this.isInvalid(x) || this.isInvalid(_y)) {
return NaN;
}
return +x - +_y;
Expand All @@ -67,8 +70,8 @@ class LazyStream {
}
multiply(y: LazyCalc): LazyStream {
const _y = y.value();
const operation = function(x: number | string) {
if (Number.isNaN(+x) || Number.isNaN(+_y)) {
const operation = (x: number | string) => {
if (this.isInvalid(x) || this.isInvalid(_y)) {
return NaN;
}
return +x * +_y;
Expand All @@ -77,8 +80,8 @@ class LazyStream {
}
divide(y: LazyCalc): LazyStream {
const _y = y.value();
const operation = function(x: number | string) {
if (Number.isNaN(+x) || Number.isNaN(+_y)) {
const operation = (x: number | string) => {
if (this.isInvalid(x) || this.isInvalid(_y)) {
return NaN;
}
return +x / +_y;
Expand All @@ -98,16 +101,16 @@ class LazyStream {
const operation = this.createRound("floor", precision);
return this.clone([operation, ...this.operators]);
}
do(fn: operatorFunc): LazyStream {
do(fn: Function): LazyStream {
const operation = function(y: number | string) {
return fn(y);
};
// this.operators.unshift(operation);
return this.clone([operation, ...this.operators]);
}
default(fallback: any): LazyStream {
const operation = function(x: number | string) {
return Number.isNaN(+x) ? fallback : +x;
const operation = (x: number | string) => {
return this.isInvalid(x) ? fallback : +x;
};
return this.clone([operation, ...this.operators]);
}
Expand Down
154 changes: 154 additions & 0 deletions tests/unit/stream.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { LazyBase } from "../../src/main";

describe("stream class", () => {
it("stream add - invalid", () => {
const base1 = LazyBase.lazy(1).divide(0);
const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.add(base2)
.value()
).toBeNaN();
});

it("stream subtract - invalid", () => {
const base1 = LazyBase.lazy(1).divide(0);
const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.subtract(base2)
.value()
).toBeNaN();
});

it("stream multiply - invalid", () => {
const base1 = LazyBase.lazy(1).divide(0);
const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.multiply(base2)
.value()
).toBeNaN();
});
it("stream multiply ", () => {
const base1 = LazyBase.lazy(1).divide(4);
const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.multiply(base2)
.value()
).toBe(0.75);
});

it("stream divide - invalid", () => {
const base1 = LazyBase.lazy(1).divide(0);
const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.divide(base2)
.value()
).toBeNaN();
});

it("stream divide", () => {
const base1 = LazyBase.lazy(1).multiply(6);
const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.divide(base2)
.value()
).toBe(2);
});

it("stream round", () => {
const base1 = LazyBase.lazy(1).divide(4);
// const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.round(1)
.value()
).toBe(0.3);
});

it("stream precision - invalid", () => {
const base1 = LazyBase.lazy(1).divide(4);
// const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.round(null)
.value()
).toBe(0);
});

it("stream precision - invalid2", () => {
const base1 = LazyBase.lazy(1).divide(4);
// const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.round(NaN)
.value()
).toBeNaN();
});

it("stream precision - minus", () => {
const base1 = LazyBase.lazy(1).divide(4);
// const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.round(-1)
.value()
).toBe(0);
});

it("stream ceil", () => {
const base1 = LazyBase.lazy(1).divide(4);
// const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.ceil(1)
.value()
).toBe(0.3);
});

it("stream floor", () => {
const base1 = LazyBase.lazy(1).divide(4);
// const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.floor(1)
.value()
).toBe(0.2);
});

it("stream do", () => {
const base1 = LazyBase.lazy(6);
// const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.do(x => x + 10)
.value()
).toBe(16);
});

it("stream default", () => {
const base1 = LazyBase.lazy(1).divide(0);
const base2 = LazyBase.lazy(3);
expect(
LazyBase.stream(base1)
.divide(base2)
.default(88)
.value()
).toBe(88);
});

it("stream default - unused", () => {
const base1 = LazyBase.lazy(1).multiply(8);
const base2 = LazyBase.lazy(2);
expect(
LazyBase.stream(base1)
.divide(base2)
.default(88)
.value()
).toBe(4);
});
});
4 changes: 2 additions & 2 deletions types/stream.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { operatorFunc } from "./main";
import { LazyCalc } from "./simple";
declare class LazyStream {
private operators;
private compose;
private clone(operators);
private isInvalid(x);
private createRound(methodName, precision?);
constructor();
add(y: LazyCalc): LazyStream;
Expand All @@ -13,7 +13,7 @@ declare class LazyStream {
round(precision?: number): LazyStream;
ceil(precision?: number): LazyStream;
floor(precision?: number): LazyStream;
do(fn: operatorFunc): LazyStream;
do(fn: Function): LazyStream;
default(fallback: any): LazyStream;
value(): any;
}
Expand Down