Skip to content

Commit db0701b

Browse files
authored
Merge pull request #15 from dreambo8563/release
stream
2 parents f1687ea + 8b4aace commit db0701b

File tree

9 files changed

+157
-64
lines changed

9 files changed

+157
-64
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
<a name="1.0.6"></a>
2+
## [1.0.6](https://github.com/dreambo8563/vue-lazy-calc/compare/v1.0.5...v1.0.6) (2019-03-19)
3+
4+
5+
6+
<a name="1.0.5"></a>
7+
## [1.0.5](https://github.com/dreambo8563/vue-lazy-calc/compare/v1.0.4...v1.0.5) (2019-03-19)
8+
9+
10+
### Features
11+
12+
* **stream:** basic stream class with add/default operator ([9ccbc2d](https://github.com/dreambo8563/vue-lazy-calc/commit/9ccbc2d))
13+
14+
15+
116
<a name="1.0.4"></a>
217
## [1.0.4](https://github.com/dreambo8563/vue-lazy-calc/compare/v1.0.3...v1.0.4) (2019-03-19)
318

README.md

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ this is a just support simple calculation in lazy way.
2121
- lazy evaluation
2222
- chaining methods
2323

24+
### TODO
25+
26+
- [] seperate simple lazy class and base class
27+
- [] support more operator in stream api
28+
2429
### Install
2530

2631
```cmd
@@ -42,27 +47,55 @@ Vue.use(lzCalc)
4247
### API list
4348

4449
```ts
45-
interface ILazyCalc {
46-
lazy(init?: number): ILazyCalc
47-
add(number: number): ILazyCalc
48-
subtract(number: number): ILazyCalc
49-
divide(y: number): ILazyCalc
50-
multiply(y: number): ILazyCalc
51-
round(precision?: number): ILazyCalc
52-
floor(precision?: number): ILazyCalc
53-
ceil(precision?: number): ILazyCalc
54-
do(fn: operatorFunc): ILazyCalc
55-
default(fallback: any): ILazyCalc
56-
value(fallback?: any): any
50+
interface LazyCalc {
51+
lazy(init?: number): LazyCalc
52+
add(y: number): LazyCalc
53+
divide(y: number): LazyCalc
54+
subtract(y: number): LazyCalc
55+
multiply(y: number): LazyCalc
56+
do(fn: operatorFunc): LazyCalc
57+
ceil(precision?: number): LazyCalc
58+
floor(precision?: number): LazyCalc
59+
round(precision?: number): LazyCalc
60+
stream(s: LazyCalc): LazyStream
61+
default(fallback: any): LazyCalc
62+
value(): any
5763
}
5864
```
5965

6066
- lazy => init a new instance with optional initValue
61-
- add/subtract/divide/multiple => + - \* / (simple calculation)
67+
- add/subtract/divide/multiple => + - \* / (simple calculation) between numbers
6268
- round/floor/ceil => deal with precision of the float number
63-
- value => excute the declared method chain with optional fallBack value(if the result is NaN)
69+
- value => excute the declared method chain
6470
- default => set default value if previous operations get NaN
6571
- do => accept a custormized function for the number
72+
- stream => init a stream to operate between multiple lazy instance with optional init instantce
73+
74+
#### Stream
75+
76+
```ts
77+
declare class LazyStream {
78+
add(y: LazyCalc): LazyStream
79+
default(fallback: any): LazyStream
80+
value(): any
81+
}
82+
```
83+
84+
- add
85+
86+
```js
87+
const result = this.$lzCalc
88+
.lazy(1)
89+
.add(3)
90+
.multiply(2)
91+
.divide(3)
92+
.round(2)
93+
94+
const tmp = this.$lzCalc.lazy(2).add(3)
95+
const s = this.$lzCalc.stream(result).add(tmp)
96+
97+
console.log(s.value()) // 2.67 + 5 => 7.67
98+
```
6699

67100
### Examples
68101

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.0.4",
3+
"version": "1.0.6",
44
"private": false,
55
"author": "dreambo8563",
66
"main": "dist/vue-lazy-calc.umd.min.js",

src/main.ts

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import Vue, { VueConstructor } from "vue";
2+
import LazyStream from "./stream";
23
export type operatorFunc = (i: number | string) => number;
34
type CalcMethod = "ceil" | "floor" | "round";
4-
class LazyCalc {
5+
export class LazyCalc {
56
initValue: number = 0;
67
operators: operatorFunc[];
78
private compose = (fns: operatorFunc[]) =>
@@ -59,13 +60,6 @@ class LazyCalc {
5960

6061
return this.clone([operation, ...this.operators]);
6162
}
62-
do(fn: operatorFunc): LazyCalc {
63-
const operation = function(y: number | string) {
64-
return fn(+y);
65-
};
66-
// this.operators.unshift(operation);
67-
return this.clone([operation, ...this.operators]);
68-
}
6963
divide(y: number): LazyCalc {
7064
const operation = function(x: number | string) {
7165
if (Number.isNaN(+x) || Number.isNaN(+y)) {
@@ -97,54 +91,46 @@ class LazyCalc {
9791
// this.operators.unshift(operation);
9892
return this.clone([operation, ...this.operators]);
9993
}
94+
do(fn: operatorFunc): LazyCalc {
95+
const operation = function(y: number | string) {
96+
return fn(+y);
97+
};
98+
// this.operators.unshift(operation);
99+
return this.clone([operation, ...this.operators]);
100+
}
100101
ceil(precision: number = 0): LazyCalc {
101102
const operation = this.createRound("ceil", precision);
102-
// this.operators.unshift(operation);
103103
return this.clone([operation, ...this.operators]);
104104
}
105105
floor(precision: number = 0): LazyCalc {
106106
const operation = this.createRound("floor", precision);
107-
// this.operators.unshift(operation);
108107
return this.clone([operation, ...this.operators]);
109108
}
110109
round(precision: number = 0): LazyCalc {
111110
const operation = this.createRound("round", precision);
112-
// this.operators.unshift(operation);
113111
return this.clone([operation, ...this.operators]);
114112
}
115-
default(fallback: any) {
113+
stream(s: LazyCalc): LazyStream {
114+
return new LazyStream().add(s);
115+
}
116+
default(fallback: any): LazyCalc {
116117
const operation = function(x: number | string) {
117118
return Number.isNaN(+x) ? fallback : x;
118119
};
119120
return this.clone([operation, ...this.operators]);
120121
}
121-
value(fallback: any = 0) {
122-
const result = this.compose(this.operators)(this.initValue);
123-
this.initValue = 0;
124-
return Number.isNaN(result) ? fallback : result;
122+
value(): any {
123+
return this.compose(this.operators)(this.initValue);
125124
}
126125
}
127126

128-
interface ILazyCalc {
129-
lazy(init?: number): ILazyCalc;
130-
add(number: number): ILazyCalc;
131-
subtract(number: number): ILazyCalc;
132-
divide(y: number): ILazyCalc;
133-
multiply(y: number): ILazyCalc;
134-
round(precision?: number): ILazyCalc;
135-
floor(precision?: number): ILazyCalc;
136-
ceil(precision?: number): ILazyCalc;
137-
do(fn: operatorFunc): ILazyCalc;
138-
default(fallback: any): ILazyCalc;
139-
value(fallback?: any): any;
140-
}
141127
export type LzCalcPlugin = {
142128
install(vue: VueConstructor<Vue>, options?: any): void;
143129
};
144130
const instantce: LzCalcPlugin = {
145131
install(vue, options) {
146132
let alias = "$lzCalc";
147-
const p = new LazyCalc(options) as ILazyCalc;
133+
const p = new LazyCalc(options);
148134
vue.prototype[alias] = p;
149135
Object.defineProperty(Vue, `${alias}`, {
150136
get() {
@@ -154,4 +140,3 @@ const instantce: LzCalcPlugin = {
154140
}
155141
};
156142
export default instantce;
157-
export { ILazyCalc };

src/stream.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { LazyCalc, operatorFunc } from "./main";
2+
class LazyStream {
3+
private operators: operatorFunc[];
4+
private compose = (fns: operatorFunc[]) =>
5+
fns.reduceRight(
6+
(prevFn: Function, nextFn: Function) => (...args: any[]) =>
7+
nextFn(prevFn(...args)),
8+
(i: any) => i
9+
);
10+
private clone(operators: operatorFunc[]) {
11+
let tmp = new LazyStream();
12+
tmp.operators = operators;
13+
return tmp;
14+
}
15+
constructor() {
16+
this.operators = [];
17+
return this;
18+
}
19+
20+
add(y: LazyCalc): LazyStream {
21+
const operation = function(x: number | string) {
22+
const _y = y.value();
23+
if (Number.isNaN(+x) || Number.isNaN(+_y)) {
24+
return NaN;
25+
}
26+
return +x + +_y;
27+
};
28+
return this.clone([operation, ...this.operators]);
29+
}
30+
default(fallback: any): LazyStream {
31+
const operation = function(x: number | string) {
32+
return Number.isNaN(+x) ? fallback : +x;
33+
};
34+
return this.clone([operation, ...this.operators]);
35+
}
36+
37+
value(): any {
38+
return this.compose(this.operators)(0);
39+
}
40+
}
41+
42+
export default LazyStream;

types/main.d.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
import Vue, { VueConstructor } from "./vue";
2+
import LazyStream from "./stream";
23
export declare type operatorFunc = (i: number | string) => number;
3-
interface ILazyCalc {
4-
lazy(init?: number): ILazyCalc;
5-
add(number: number): ILazyCalc;
6-
subtract(number: number): ILazyCalc;
7-
divide(y: number): ILazyCalc;
8-
multiply(y: number): ILazyCalc;
9-
round(precision?: number): ILazyCalc;
10-
floor(precision?: number): ILazyCalc;
11-
ceil(precision?: number): ILazyCalc;
12-
do(fn: operatorFunc): ILazyCalc;
13-
default(fallback: any): ILazyCalc;
14-
value(fallback?: any): any;
4+
export declare class LazyCalc {
5+
initValue: number;
6+
operators: operatorFunc[];
7+
private compose;
8+
private createRound(methodName, precision?);
9+
constructor(init?: number);
10+
private clone(operators);
11+
lazy(init?: number): LazyCalc;
12+
add(y: number): LazyCalc;
13+
divide(y: number): LazyCalc;
14+
subtract(y: number): LazyCalc;
15+
multiply(y: number): LazyCalc;
16+
do(fn: operatorFunc): LazyCalc;
17+
ceil(precision?: number): LazyCalc;
18+
floor(precision?: number): LazyCalc;
19+
round(precision?: number): LazyCalc;
20+
stream(s: LazyCalc): LazyStream;
21+
default(fallback: any): LazyCalc;
22+
value(): any;
1523
}
1624
export declare type LzCalcPlugin = {
1725
install(vue: VueConstructor<Vue>, options?: any): void;
1826
};
1927
declare const instantce: LzCalcPlugin;
2028
export default instantce;
21-
export { ILazyCalc };

types/stream.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { LazyCalc } from "./main";
2+
declare class LazyStream {
3+
private operators;
4+
private compose;
5+
private clone(operators);
6+
constructor();
7+
add(y: LazyCalc): LazyStream;
8+
default(fallback: any): LazyStream;
9+
value(): any;
10+
}
11+
export default LazyStream;

types/vue.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import Vue, { VueConstructor } from "vue";
2-
import { ILazyCalc } from "./main";
2+
import { LazyCalc } from "./main";
33
declare module "vue/types/vue" {
44
interface Vue {
5-
$lzCalc: ILazyCalc;
5+
$lzCalc: LazyCalc;
66
}
77
interface VueConstructor {
8-
$lzCalc: ILazyCalc;
8+
$lzCalc: LazyCalc;
99
}
1010
}
1111

1212
declare module "vue/types/options" {
1313
interface ComponentOptions<V extends Vue> {
14-
$lzCalc?: ILazyCalc;
14+
$lzCalc?: LazyCalc;
1515
}
1616
}
1717

0 commit comments

Comments
 (0)