this is a simple calculation plugin in lazy way. (inspired by lodash)
features
- vue friendly
- strong typed
- lazy evaluation
- chaining methods
- code coverage 100%
- seperate simple lazy class from base class
- support more operator in stream api
npm install vue-lazy-calc --saveimport lzCalc from "vue-lazy-calc" Vue.use(lzCalc)- this.$lzCalc in Component context.
- Vue.$lzCalc in global.
export declare class LazyBase { lazy(init?: number): LazyCalc stream(s?: LazyCalc): LazyStream }- lazy => init a new instance with optional initValue
- stream => init a stream to operate between multiple lazy instance with optional init instantce
export declare class LazyCalc { add(y: number): LazyCalc divide(y: number): LazyCalc subtract(y: number): LazyCalc multiply(y: number): LazyCalc do(fn: operatorFunc): LazyCalc ceil(precision?: number): LazyCalc floor(precision?: number): LazyCalc round(precision?: number): LazyCalc default(fallback: any): LazyCalc value(): any }- add/subtract/divide/multiple => + - * / (simple calculation) between numbers
- round/floor/ceil => deal with precision of the float number
- value => excute the declared method chain
- default => set default value if previous operations get NaN
- do => accept a custormized function for the number
(1+3)*2/3 with precision 2
const result = this.$lzCalc .lazy(1) .add(3) .multiply(2) .divide(3) .round(2) console.log(result.value()) // 2.67 const addThree = result.add(3) console.log(addThree.value()) // 2.67+ 3 =>5.67declare class LazyStream { add(y: LazyCalc): LazyStream subtract(y: LazyCalc): LazyStream multiply(y: LazyCalc): LazyStream divide(y: LazyCalc): LazyStream round(precision?: number): LazyStream ceil(precision?: number): LazyStream floor(precision?: number): LazyStream do(fn: operatorFunc): LazyStream default(fallback: any): LazyStream value(): any }const result = this.$lzCalc .lazy(1) .add(3) .multiply(2) .divide(3) .round(2) const tmp = this.$lzCalc.lazy(2).add(3) const s = this.$lzCalc.stream(result).add(tmp) console.log(s.value()) // 2.67 + 5 => 7.67- when declare the result variable, no calculation excuted until value()
- you can reuse the declare variable
Thanks goes to these wonderful people (emoji key):
Vincent Guo π» π π |
This project follows the all-contributors specification. Contributions of any kind welcome!
