@panyam/tsutils 0.0.64 → 0.0.65

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,oDAAoC;AACpC,yDAAyC;AACzC,yDAAyC;AACzC,gDAAgC;AAChC,+CAA+B;AAC/B,qDAAqC;AACrC,6CAA6B;AAC7B,+CAA+B;AAC/B,qDAAqC;AACrC,oDAAoC;AAEpC,6CAA6B;AAC7B,qCAAoC;AAA3B,kGAAA,OAAO,OAAA","sourcesContent":["export * from \"./types\";\nexport * as Api from \"./apis/index\";\nexport * as Constants from \"./constants\";\nexport * as Events from \"./comms/events\";\nexport * as Lists from \"./list\";\nexport * as Misc from \"./misc\";\nexport * as Streams from \"./streams\";\nexport * as DOM from \"./dom\";\nexport * as Geom from \"./geom\";\nexport * as Num from \"./numberutils\";\nexport * as Time from \"./timeutils\";\n// export * as Properties from \"./properties\";\nexport * as DAL from \"./dal\";\nexport { Browser } from \"./browser\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,oDAAoC;AACpC,yDAAyC;AACzC,yDAAyC;AACzC,gDAAgC;AAChC,+CAA+B;AAC/B,qDAAqC;AACrC,6CAA6B;AAC7B,+CAA+B;AAC/B,qDAAqC;AACrC,oDAAoC;AACpC,6CAA6B;AAC7B,qCAAoC;AAA3B,kGAAA,OAAO,OAAA","sourcesContent":["export * from \"./types\";\nexport * as Api from \"./apis/index\";\nexport * as Constants from \"./constants\";\nexport * as Events from \"./comms/events\";\nexport * as Lists from \"./list\";\nexport * as Misc from \"./misc\";\nexport * as Streams from \"./streams\";\nexport * as DOM from \"./dom\";\nexport * as Geom from \"./geom\";\nexport * as Num from \"./numberutils\";\nexport * as Time from \"./timeutils\";\nexport * as DAL from \"./dal\";\nexport { Browser } from \"./browser\";\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"numberutils.js","sourceRoot":"","sources":["../../src/numberutils.ts"],"names":[],"mappings":";;;AAEA,SAAgB,KAAK,CAAC,KAAa,EAAE,MAAwB,IAAI,EAAE,OAAyB,CAAC;IAC3F,IAAI,GAAG,IAAI,IAAI,EAAE;QACf,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjC,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC7C,IAAI,KAAK,IAAI,CAAC,EAAE;YACd,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;SAC7B;aAAM;YACL,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC;SAC9C;KACF;IACD,MAAM,GAAG,GAAa,EAAE,CAAC;IACzB,IAAI,IAAI,IAAI,IAAI,EAAE;QAChB,IAAI,GAAG,CAAC,CAAC;KACV;IACD,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACtB,IAAI,KAAK,KAAK,GAAG,EAAE;QACjB,IAAI,KAAK,GAAG,GAAG,EAAE;YACf,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE;gBACvC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aACb;SACF;aAAM;YACL,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE;gBACvC,GAAG,CAAC,IAA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MH,4BA4MC;AAxMiB,aAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;AACtB,YAAG,GAAG,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACzB,iBAAQ,GAAG,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAyMzC,MAAM,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,UAAU,GAAG,KAAK,EAAY,EAAE,CAAC,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;AAAtF,QAAA,IAAI,QAAkF","sourcesContent":["import { Nullable } from \"./types\";\n\nexport function range(start: number, end: Nullable<number> = null, incr: Nullable<number> = 1): number[] {\n if (end == null) {\n const absStart = Math.abs(start);\n const arr = Array.from({ length: absStart });\n if (start >= 0) {\n return arr.map((x, i) => i);\n } else {\n return arr.map((x, i) => i - (absStart - 1));\n }\n }\n const out: number[] = [];\n if (incr == null) {\n incr = 1;\n }\n incr = Math.abs(incr);\n if (start !== end) {\n if (start < end) {\n for (let i = start; i <= end; i += incr) {\n out.push(i);\n }\n } else {\n for (let i = start; i >= end; i -= incr) {\n out.push(i);\n }\n }\n }\n return out;\n}\n\n/*\nexport function applyMixins(derivedCtor: any, baseCtors: any[]) {\n baseCtors.forEach(baseCtor => {\n Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {\n Object.defineProperty(derivedCtor.prototype, name,\n Object.getOwnPropertyDescriptor(baseCtor.prototype, name));\n });\n });\n}\n*/\n\nexport function gcdof(x: number, y: number): number {\n x = Math.abs(x);\n y = Math.abs(y);\n while (y > 0) {\n const t = y;\n y = x % y;\n x = t;\n }\n return x;\n}\n\nexport class Fraction {\n readonly num: number;\n readonly den: number;\n\n static readonly ZERO = new Fraction();\n static readonly ONE = new Fraction(1, 1);\n static readonly INFINITY = new Fraction(1, 0);\n\n constructor(num = 0, den = 1, factorized = false) {\n if (isNaN(num) || isNaN(den)) {\n throw new Error(`Invalid numerator(${num}) or denminator(${den})`);\n }\n if (factorized) {\n const gcd = gcdof(num, den);\n num /= gcd;\n den /= gcd;\n }\n this.num = num;\n this.den = den;\n }\n\n static parse(val: string, factorized = false): Fraction {\n const parts = val\n .trim()\n .split(\"/\")\n .map((x) => x.trim());\n let num = 1;\n let den = 1;\n if (parts.length == 1) num = parseInt(parts[0]);\n else if (parts.length != 2) {\n throw new Error(\"Invalid fraction string: \" + val);\n } else {\n if (parts[0].length > 0) {\n num = parseInt(parts[0]);\n }\n if (parts[1].length > 0) {\n den = parseInt(parts[1]);\n }\n }\n if (isNaN(num) || isNaN(den)) {\n throw new Error(\"Invalid fraction string: \" + val);\n }\n return new Fraction(num, den, factorized);\n }\n\n get isWhole(): boolean {\n return this.num % this.den == 0;\n }\n\n get isZero(): boolean {\n return this.num == 0;\n }\n\n get isInfinity(): boolean {\n return this.den == 0;\n }\n\n get isOne(): boolean {\n return this.num == this.den;\n }\n\n get ceil(): number {\n if (this.num % this.den == 0) {\n return this.num / this.den;\n } else {\n return 1 + Math.floor(this.num / this.den);\n }\n }\n\n get floor(): number {\n if (this.num % this.den == 0) {\n return this.num / this.den;\n } else {\n return Math.floor(this.num / this.den);\n }\n }\n\n plus(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den + this.den * another.num, this.den * another.den, factorized);\n }\n\n plusNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num + this.den * another, this.den, factorized);\n }\n\n minus(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den - this.den * another.num, this.den * another.den, factorized);\n }\n\n minusNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num - this.den * another, this.den, factorized);\n }\n\n times(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.num, this.den * another.den, factorized);\n }\n\n timesNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num * another, this.den, factorized);\n }\n\n divby(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den, this.den * another.num, factorized);\n }\n\n divbyNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num, this.den * another, factorized);\n }\n\n /**\n * Returns another / this.\n */\n numDivby(another: number, factorized = false): Fraction {\n return new Fraction(this.den * another, this.num, factorized);\n }\n\n /**\n * Returns this % another\n */\n mod(another: Fraction): Fraction {\n // a (mod b) = a − b ⌊a / b⌋\n const d = this.divby(another);\n const floorOfD = Math.floor(d.num / d.den);\n return this.minus(another.timesNum(floorOfD));\n }\n\n /*\n * Returns this % another\n */\n modNum(another: number): Fraction {\n // a (mod b) = a − b ⌊a / b⌋\n const d = this.divbyNum(another);\n const floorOfD = Math.floor(d.num / d.den);\n return this.minusNum(another * floorOfD);\n }\n\n get inverse(): Fraction {\n return new Fraction(this.den, this.num);\n }\n\n get factorized(): Fraction {\n const gcd = gcdof(this.num, this.den);\n return new Fraction(this.num / gcd, this.den / gcd);\n }\n\n equals(another: Fraction): boolean {\n return this.num * another.den == this.den * another.num;\n }\n\n equalsNum(another: number): boolean {\n return this.num == this.den * another;\n }\n\n cmp(another: Fraction): number {\n return this.num * another.den - this.den * another.num;\n }\n\n cmpNum(another: number): number {\n return this.num - this.den * another;\n }\n\n isLT(another: Fraction): boolean {\n return this.cmp(another) < 0;\n }\n\n isLTE(another: Fraction): boolean {\n return this.cmp(another) <= 0;\n }\n\n isLTNum(another: number): boolean {\n return this.cmpNum(another) < 0;\n }\n\n isLTENum(another: number): boolean {\n return this.cmpNum(another) <= 0;\n }\n\n isGT(another: Fraction): boolean {\n return this.cmp(another) > 0;\n }\n\n isGTE(another: Fraction): boolean {\n return this.cmp(another) >= 0;\n }\n\n isGTNum(another: number): boolean {\n return this.cmpNum(another) > 0;\n }\n\n isGTENum(another: number): boolean {\n return this.cmpNum(another) >= 0;\n }\n\n toString(): string {\n return this.num + \"/\" + this.den;\n }\n\n static max(f1: Fraction, f2: Fraction): Fraction {\n return f1.cmp(f2) > 0 ? f1 : f2;\n }\n\n static min(f1: Fraction, f2: Fraction): Fraction {\n return f1.cmp(f2) < 0 ? f1 : f2;\n }\n}\n\n// Shortcut helper\nexport const Frac = (a = 0, b = 1, factorized = false): Fraction => new Fraction(a, b, factorized);\n"]}
1
+ {"version":3,"file":"numberutils.js","sourceRoot":"","sources":["../../src/numberutils.ts"],"names":[],"mappings":";;;AAEA,SAAgB,KAAK,CAAC,KAAa,EAAE,MAAwB,IAAI,EAAE,OAAyB,CAAC;IAC3F,IAAI,GAAG,IAAI,IAAI,EAAE;QACf,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjC,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC7C,IAAI,KAAK,IAAI,CAAC,EAAE;YACd,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;SAC7B;aAAM;YACL,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC;SAC9C;KACF;IACD,MAAM,GAAG,GAAa,EAAE,CAAC;IACzB,IAAI,IAAI,IAAI,IAAI,EAAE;QAChB,IAAI,GAAG,CAAC,CAAC;KACV;IACD,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACtB,IAAI,KAAK,KAAK,GAAG,EAAE;QACjB,IAAI,KAAK,GAAG,GAAG,EAAE;YACf,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE;gBACvC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aACb;SACF;aAAM;YACL,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE;gBACv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IAClC,CAAC;;AA3MH,4BA4MC;AAxMiB,aAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;AACtB,YAAG,GAAG,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACzB,iBAAQ,GAAG,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAyMzC,MAAM,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,UAAU,GAAG,KAAK,EAAY,EAAE,CAAC,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC;AAAtF,QAAA,IAAI,QAAkF","sourcesContent":["import { Nullable } from \"./types\";\n\nexport function range(start: number, end: Nullable<number> = null, incr: Nullable<number> = 1): number[] {\n if (end == null) {\n const absStart = Math.abs(start);\n const arr = Array.from({ length: absStart });\n if (start >= 0) {\n return arr.map((x, i) => i);\n } else {\n return arr.map((x, i) => i - (absStart - 1));\n }\n }\n const out: number[] = [];\n if (incr == null) {\n incr = 1;\n }\n incr = Math.abs(incr);\n if (start !== end) {\n if (start < end) {\n for (let i = start; i <= end; i += incr) {\n out.push(i);\n }\n } else {\n for (let i = start; i >= end; i -= incr) {\n out.push(i);\n }\n }\n }\n return out;\n}\n\nexport function gcdof(x: number, y: number): number {\n x = Math.abs(x);\n y = Math.abs(y);\n while (y > 0) {\n const t = y;\n y = x % y;\n x = t;\n }\n return x;\n}\n\nexport class Fraction {\n readonly num: number;\n readonly den: number;\n\n static readonly ZERO = new Fraction();\n static readonly ONE = new Fraction(1, 1);\n static readonly INFINITY = new Fraction(1, 0);\n\n constructor(num = 0, den = 1, factorized = false) {\n if (isNaN(num) || isNaN(den)) {\n throw new Error(`Invalid numerator(${num}) or denminator(${den})`);\n }\n if (factorized) {\n const gcd = gcdof(num, den);\n num /= gcd;\n den /= gcd;\n }\n this.num = num;\n this.den = den;\n }\n\n static parse(val: string, factorized = false): Fraction {\n const parts = val\n .trim()\n .split(\"/\")\n .map((x) => x.trim());\n let num = 1;\n let den = 1;\n if (parts.length == 1) num = parseInt(parts[0]);\n else if (parts.length != 2) {\n throw new Error(\"Invalid fraction string: \" + val);\n } else {\n if (parts[0].length > 0) {\n num = parseInt(parts[0]);\n }\n if (parts[1].length > 0) {\n den = parseInt(parts[1]);\n }\n }\n if (isNaN(num) || isNaN(den)) {\n throw new Error(\"Invalid fraction string: \" + val);\n }\n return new Fraction(num, den, factorized);\n }\n\n get isWhole(): boolean {\n return this.num % this.den == 0;\n }\n\n get isZero(): boolean {\n return this.num == 0;\n }\n\n get isInfinity(): boolean {\n return this.den == 0;\n }\n\n get isOne(): boolean {\n return this.num == this.den;\n }\n\n get ceil(): number {\n if (this.num % this.den == 0) {\n return this.num / this.den;\n } else {\n return 1 + Math.floor(this.num / this.den);\n }\n }\n\n get floor(): number {\n if (this.num % this.den == 0) {\n return this.num / this.den;\n } else {\n return Math.floor(this.num / this.den);\n }\n }\n\n plus(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den + this.den * another.num, this.den * another.den, factorized);\n }\n\n plusNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num + this.den * another, this.den, factorized);\n }\n\n minus(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den - this.den * another.num, this.den * another.den, factorized);\n }\n\n minusNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num - this.den * another, this.den, factorized);\n }\n\n times(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.num, this.den * another.den, factorized);\n }\n\n timesNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num * another, this.den, factorized);\n }\n\n divby(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den, this.den * another.num, factorized);\n }\n\n divbyNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num, this.den * another, factorized);\n }\n\n /**\n * Returns another / this.\n */\n numDivby(another: number, factorized = false): Fraction {\n return new Fraction(this.den * another, this.num, factorized);\n }\n\n /**\n * Returns this % another\n */\n mod(another: Fraction): Fraction {\n // a (mod b) = a − b ⌊a / b⌋\n const d = this.divby(another);\n const floorOfD = Math.floor(d.num / d.den);\n return this.minus(another.timesNum(floorOfD));\n }\n\n /*\n * Returns this % another\n */\n modNum(another: number): Fraction {\n // a (mod b) = a − b ⌊a / b⌋\n const d = this.divbyNum(another);\n const floorOfD = Math.floor(d.num / d.den);\n return this.minusNum(another * floorOfD);\n }\n\n get inverse(): Fraction {\n return new Fraction(this.den, this.num);\n }\n\n get factorized(): Fraction {\n const gcd = gcdof(this.num, this.den);\n return new Fraction(this.num / gcd, this.den / gcd);\n }\n\n equals(another: Fraction): boolean {\n return this.num * another.den == this.den * another.num;\n }\n\n equalsNum(another: number): boolean {\n return this.num == this.den * another;\n }\n\n cmp(another: Fraction): number {\n return this.num * another.den - this.den * another.num;\n }\n\n cmpNum(another: number): number {\n return this.num - this.den * another;\n }\n\n isLT(another: Fraction): boolean {\n return this.cmp(another) < 0;\n }\n\n isLTE(another: Fraction): boolean {\n return this.cmp(another) <= 0;\n }\n\n isLTNum(another: number): boolean {\n return this.cmpNum(another) < 0;\n }\n\n isLTENum(another: number): boolean {\n return this.cmpNum(another) <= 0;\n }\n\n isGT(another: Fraction): boolean {\n return this.cmp(another) > 0;\n }\n\n isGTE(another: Fraction): boolean {\n return this.cmp(another) >= 0;\n }\n\n isGTNum(another: number): boolean {\n return this.cmpNum(another) > 0;\n }\n\n isGTENum(another: number): boolean {\n return this.cmpNum(another) >= 0;\n }\n\n toString(): string {\n return this.num + \"/\" + this.den;\n }\n\n static max(f1: Fraction, f2: Fraction): Fraction {\n return f1.cmp(f2) > 0 ? f1 : f2;\n }\n\n static min(f1: Fraction, f2: Fraction): Fraction {\n return f1.cmp(f2) < 0 ? f1 : f2;\n }\n}\n\n// Shortcut helper\nexport const Frac = (a = 0, b = 1, factorized = false): Fraction => new Fraction(a, b, factorized);\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;uBACH,cAAc;kBAAvB,GAAG;6BACY,aAAa;wBAA5B,SAAS;0BACG,gBAAgB;qBAA5B,MAAM;yBACK,QAAQ;oBAAnB,KAAK;wBACK,QAAQ;mBAAlB,IAAI;2BACS,WAAW;sBAAxB,OAAO;uBACE,OAAO;kBAAhB,GAAG;wBACO,QAAQ;mBAAlB,IAAI;uBACK,eAAe;kBAAxB,GAAG;wBACO,aAAa;mBAAvB,IAAI;uBAEK,OAAO;kBAAhB,GAAG;AACf,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC","sourcesContent":["export * from \"./types\";\nexport * as Api from \"./apis/index\";\nexport * as Constants from \"./constants\";\nexport * as Events from \"./comms/events\";\nexport * as Lists from \"./list\";\nexport * as Misc from \"./misc\";\nexport * as Streams from \"./streams\";\nexport * as DOM from \"./dom\";\nexport * as Geom from \"./geom\";\nexport * as Num from \"./numberutils\";\nexport * as Time from \"./timeutils\";\n// export * as Properties from \"./properties\";\nexport * as DAL from \"./dal\";\nexport { Browser } from \"./browser\";\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;uBACH,cAAc;kBAAvB,GAAG;6BACY,aAAa;wBAA5B,SAAS;0BACG,gBAAgB;qBAA5B,MAAM;yBACK,QAAQ;oBAAnB,KAAK;wBACK,QAAQ;mBAAlB,IAAI;2BACS,WAAW;sBAAxB,OAAO;uBACE,OAAO;kBAAhB,GAAG;wBACO,QAAQ;mBAAlB,IAAI;uBACK,eAAe;kBAAxB,GAAG;wBACO,aAAa;mBAAvB,IAAI;uBACK,OAAO;kBAAhB,GAAG;AACf,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC","sourcesContent":["export * from \"./types\";\nexport * as Api from \"./apis/index\";\nexport * as Constants from \"./constants\";\nexport * as Events from \"./comms/events\";\nexport * as Lists from \"./list\";\nexport * as Misc from \"./misc\";\nexport * as Streams from \"./streams\";\nexport * as DOM from \"./dom\";\nexport * as Geom from \"./geom\";\nexport * as Num from \"./numberutils\";\nexport * as Time from \"./timeutils\";\nexport * as DAL from \"./dal\";\nexport { Browser } from \"./browser\";\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"numberutils.js","sourceRoot":"","sources":["../../src/numberutils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,KAAK,CAAC,KAAa,EAAE,MAAwB,IAAI,EAAE,OAAyB,CAAC;IAC3F,IAAI,GAAG,IAAI,IAAI,EAAE;QACf,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjC,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC7C,IAAI,KAAK,IAAI,CAAC,EAAE;YACd,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;SAC7B;aAAM;YACL,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC;SAC9C;KACF;IACD,MAAM,GAAG,GAAa,EAAE,CAAC;IACzB,IAAI,IAAI,IAAI,IAAI,EAAE;QAChB,IAAI,GAAG,CAAC,CAAC;KACV;IACD,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACtB,IAAI,KAAK,KAAK,GAAG,EAAE;QACjB,IAAI,KAAK,GAAG,GAAG,EAAE;YACf,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE;gBACvC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aACb;SACF;aAAM;YACL,KAAK,IAAI,CAAC,GAAG,KAA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AAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IAClC,CAAC;;AAvMe,aAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;AACtB,YAAG,GAAG,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACzB,iBAAQ,GAAG,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAyMhD,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,UAAU,GAAG,KAAK,EAAY,EAAE,CAAC,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC","sourcesContent":["import { Nullable } from \"./types\";\n\nexport function range(start: number, end: Nullable<number> = null, incr: Nullable<number> = 1): number[] {\n if (end == null) {\n const absStart = Math.abs(start);\n const arr = Array.from({ length: absStart });\n if (start >= 0) {\n return arr.map((x, i) => i);\n } else {\n return arr.map((x, i) => i - (absStart - 1));\n }\n }\n const out: number[] = [];\n if (incr == null) {\n incr = 1;\n }\n incr = Math.abs(incr);\n if (start !== end) {\n if (start < end) {\n for (let i = start; i <= end; i += incr) {\n out.push(i);\n }\n } else {\n for (let i = start; i >= end; i -= incr) {\n out.push(i);\n }\n }\n }\n return out;\n}\n\n/*\nexport function applyMixins(derivedCtor: any, baseCtors: any[]) {\n baseCtors.forEach(baseCtor => {\n Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {\n Object.defineProperty(derivedCtor.prototype, name,\n Object.getOwnPropertyDescriptor(baseCtor.prototype, name));\n });\n });\n}\n*/\n\nexport function gcdof(x: number, y: number): number {\n x = Math.abs(x);\n y = Math.abs(y);\n while (y > 0) {\n const t = y;\n y = x % y;\n x = t;\n }\n return x;\n}\n\nexport class Fraction {\n readonly num: number;\n readonly den: number;\n\n static readonly ZERO = new Fraction();\n static readonly ONE = new Fraction(1, 1);\n static readonly INFINITY = new Fraction(1, 0);\n\n constructor(num = 0, den = 1, factorized = false) {\n if (isNaN(num) || isNaN(den)) {\n throw new Error(`Invalid numerator(${num}) or denminator(${den})`);\n }\n if (factorized) {\n const gcd = gcdof(num, den);\n num /= gcd;\n den /= gcd;\n }\n this.num = num;\n this.den = den;\n }\n\n static parse(val: string, factorized = false): Fraction {\n const parts = val\n .trim()\n .split(\"/\")\n .map((x) => x.trim());\n let num = 1;\n let den = 1;\n if (parts.length == 1) num = parseInt(parts[0]);\n else if (parts.length != 2) {\n throw new Error(\"Invalid fraction string: \" + val);\n } else {\n if (parts[0].length > 0) {\n num = parseInt(parts[0]);\n }\n if (parts[1].length > 0) {\n den = parseInt(parts[1]);\n }\n }\n if (isNaN(num) || isNaN(den)) {\n throw new Error(\"Invalid fraction string: \" + val);\n }\n return new Fraction(num, den, factorized);\n }\n\n get isWhole(): boolean {\n return this.num % this.den == 0;\n }\n\n get isZero(): boolean {\n return this.num == 0;\n }\n\n get isInfinity(): boolean {\n return this.den == 0;\n }\n\n get isOne(): boolean {\n return this.num == this.den;\n }\n\n get ceil(): number {\n if (this.num % this.den == 0) {\n return this.num / this.den;\n } else {\n return 1 + Math.floor(this.num / this.den);\n }\n }\n\n get floor(): number {\n if (this.num % this.den == 0) {\n return this.num / this.den;\n } else {\n return Math.floor(this.num / this.den);\n }\n }\n\n plus(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den + this.den * another.num, this.den * another.den, factorized);\n }\n\n plusNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num + this.den * another, this.den, factorized);\n }\n\n minus(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den - this.den * another.num, this.den * another.den, factorized);\n }\n\n minusNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num - this.den * another, this.den, factorized);\n }\n\n times(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.num, this.den * another.den, factorized);\n }\n\n timesNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num * another, this.den, factorized);\n }\n\n divby(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den, this.den * another.num, factorized);\n }\n\n divbyNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num, this.den * another, factorized);\n }\n\n /**\n * Returns another / this.\n */\n numDivby(another: number, factorized = false): Fraction {\n return new Fraction(this.den * another, this.num, factorized);\n }\n\n /**\n * Returns this % another\n */\n mod(another: Fraction): Fraction {\n // a (mod b) = a − b ⌊a / b⌋\n const d = this.divby(another);\n const floorOfD = Math.floor(d.num / d.den);\n return this.minus(another.timesNum(floorOfD));\n }\n\n /*\n * Returns this % another\n */\n modNum(another: number): Fraction {\n // a (mod b) = a − b ⌊a / b⌋\n const d = this.divbyNum(another);\n const floorOfD = Math.floor(d.num / d.den);\n return this.minusNum(another * floorOfD);\n }\n\n get inverse(): Fraction {\n return new Fraction(this.den, this.num);\n }\n\n get factorized(): Fraction {\n const gcd = gcdof(this.num, this.den);\n return new Fraction(this.num / gcd, this.den / gcd);\n }\n\n equals(another: Fraction): boolean {\n return this.num * another.den == this.den * another.num;\n }\n\n equalsNum(another: number): boolean {\n return this.num == this.den * another;\n }\n\n cmp(another: Fraction): number {\n return this.num * another.den - this.den * another.num;\n }\n\n cmpNum(another: number): number {\n return this.num - this.den * another;\n }\n\n isLT(another: Fraction): boolean {\n return this.cmp(another) < 0;\n }\n\n isLTE(another: Fraction): boolean {\n return this.cmp(another) <= 0;\n }\n\n isLTNum(another: number): boolean {\n return this.cmpNum(another) < 0;\n }\n\n isLTENum(another: number): boolean {\n return this.cmpNum(another) <= 0;\n }\n\n isGT(another: Fraction): boolean {\n return this.cmp(another) > 0;\n }\n\n isGTE(another: Fraction): boolean {\n return this.cmp(another) >= 0;\n }\n\n isGTNum(another: number): boolean {\n return this.cmpNum(another) > 0;\n }\n\n isGTENum(another: number): boolean {\n return this.cmpNum(another) >= 0;\n }\n\n toString(): string {\n return this.num + \"/\" + this.den;\n }\n\n static max(f1: Fraction, f2: Fraction): Fraction {\n return f1.cmp(f2) > 0 ? f1 : f2;\n }\n\n static min(f1: Fraction, f2: Fraction): Fraction {\n return f1.cmp(f2) < 0 ? f1 : f2;\n }\n}\n\n// Shortcut helper\nexport const Frac = (a = 0, b = 1, factorized = false): Fraction => new Fraction(a, b, factorized);\n"]}
1
+ {"version":3,"file":"numberutils.js","sourceRoot":"","sources":["../../src/numberutils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,KAAK,CAAC,KAAa,EAAE,MAAwB,IAAI,EAAE,OAAyB,CAAC;IAC3F,IAAI,GAAG,IAAI,IAAI,EAAE;QACf,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACjC,MAAM,GAAG,GAAG,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;QAC7C,IAAI,KAAK,IAAI,CAAC,EAAE;YACd,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;SAC7B;aAAM;YACL,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC;SAC9C;KACF;IACD,MAAM,GAAG,GAAa,EAAE,CAAC;IACzB,IAAI,IAAI,IAAI,IAAI,EAAE;QAChB,IAAI,GAAG,CAAC,CAAC;KACV;IACD,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACtB,IAAI,KAAK,KAAK,GAAG,EAAE;QACjB,IAAI,KAAK,GAAG,GAAG,EAAE;YACf,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE;gBACvC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aACb;SACF;aAAM;YACL,KAAK,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,GAAG,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,CAAC,EAAE,CAAC;IAClC,CAAC;;AAvMe,aAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;AACtB,YAAG,GAAG,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACzB,iBAAQ,GAAG,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAyMhD,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,UAAU,GAAG,KAAK,EAAY,EAAE,CAAC,IAAI,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,CAAC","sourcesContent":["import { Nullable } from \"./types\";\n\nexport function range(start: number, end: Nullable<number> = null, incr: Nullable<number> = 1): number[] {\n if (end == null) {\n const absStart = Math.abs(start);\n const arr = Array.from({ length: absStart });\n if (start >= 0) {\n return arr.map((x, i) => i);\n } else {\n return arr.map((x, i) => i - (absStart - 1));\n }\n }\n const out: number[] = [];\n if (incr == null) {\n incr = 1;\n }\n incr = Math.abs(incr);\n if (start !== end) {\n if (start < end) {\n for (let i = start; i <= end; i += incr) {\n out.push(i);\n }\n } else {\n for (let i = start; i >= end; i -= incr) {\n out.push(i);\n }\n }\n }\n return out;\n}\n\nexport function gcdof(x: number, y: number): number {\n x = Math.abs(x);\n y = Math.abs(y);\n while (y > 0) {\n const t = y;\n y = x % y;\n x = t;\n }\n return x;\n}\n\nexport class Fraction {\n readonly num: number;\n readonly den: number;\n\n static readonly ZERO = new Fraction();\n static readonly ONE = new Fraction(1, 1);\n static readonly INFINITY = new Fraction(1, 0);\n\n constructor(num = 0, den = 1, factorized = false) {\n if (isNaN(num) || isNaN(den)) {\n throw new Error(`Invalid numerator(${num}) or denminator(${den})`);\n }\n if (factorized) {\n const gcd = gcdof(num, den);\n num /= gcd;\n den /= gcd;\n }\n this.num = num;\n this.den = den;\n }\n\n static parse(val: string, factorized = false): Fraction {\n const parts = val\n .trim()\n .split(\"/\")\n .map((x) => x.trim());\n let num = 1;\n let den = 1;\n if (parts.length == 1) num = parseInt(parts[0]);\n else if (parts.length != 2) {\n throw new Error(\"Invalid fraction string: \" + val);\n } else {\n if (parts[0].length > 0) {\n num = parseInt(parts[0]);\n }\n if (parts[1].length > 0) {\n den = parseInt(parts[1]);\n }\n }\n if (isNaN(num) || isNaN(den)) {\n throw new Error(\"Invalid fraction string: \" + val);\n }\n return new Fraction(num, den, factorized);\n }\n\n get isWhole(): boolean {\n return this.num % this.den == 0;\n }\n\n get isZero(): boolean {\n return this.num == 0;\n }\n\n get isInfinity(): boolean {\n return this.den == 0;\n }\n\n get isOne(): boolean {\n return this.num == this.den;\n }\n\n get ceil(): number {\n if (this.num % this.den == 0) {\n return this.num / this.den;\n } else {\n return 1 + Math.floor(this.num / this.den);\n }\n }\n\n get floor(): number {\n if (this.num % this.den == 0) {\n return this.num / this.den;\n } else {\n return Math.floor(this.num / this.den);\n }\n }\n\n plus(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den + this.den * another.num, this.den * another.den, factorized);\n }\n\n plusNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num + this.den * another, this.den, factorized);\n }\n\n minus(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den - this.den * another.num, this.den * another.den, factorized);\n }\n\n minusNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num - this.den * another, this.den, factorized);\n }\n\n times(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.num, this.den * another.den, factorized);\n }\n\n timesNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num * another, this.den, factorized);\n }\n\n divby(another: Fraction, factorized = false): Fraction {\n return new Fraction(this.num * another.den, this.den * another.num, factorized);\n }\n\n divbyNum(another: number, factorized = false): Fraction {\n return new Fraction(this.num, this.den * another, factorized);\n }\n\n /**\n * Returns another / this.\n */\n numDivby(another: number, factorized = false): Fraction {\n return new Fraction(this.den * another, this.num, factorized);\n }\n\n /**\n * Returns this % another\n */\n mod(another: Fraction): Fraction {\n // a (mod b) = a − b ⌊a / b⌋\n const d = this.divby(another);\n const floorOfD = Math.floor(d.num / d.den);\n return this.minus(another.timesNum(floorOfD));\n }\n\n /*\n * Returns this % another\n */\n modNum(another: number): Fraction {\n // a (mod b) = a − b ⌊a / b⌋\n const d = this.divbyNum(another);\n const floorOfD = Math.floor(d.num / d.den);\n return this.minusNum(another * floorOfD);\n }\n\n get inverse(): Fraction {\n return new Fraction(this.den, this.num);\n }\n\n get factorized(): Fraction {\n const gcd = gcdof(this.num, this.den);\n return new Fraction(this.num / gcd, this.den / gcd);\n }\n\n equals(another: Fraction): boolean {\n return this.num * another.den == this.den * another.num;\n }\n\n equalsNum(another: number): boolean {\n return this.num == this.den * another;\n }\n\n cmp(another: Fraction): number {\n return this.num * another.den - this.den * another.num;\n }\n\n cmpNum(another: number): number {\n return this.num - this.den * another;\n }\n\n isLT(another: Fraction): boolean {\n return this.cmp(another) < 0;\n }\n\n isLTE(another: Fraction): boolean {\n return this.cmp(another) <= 0;\n }\n\n isLTNum(another: number): boolean {\n return this.cmpNum(another) < 0;\n }\n\n isLTENum(another: number): boolean {\n return this.cmpNum(another) <= 0;\n }\n\n isGT(another: Fraction): boolean {\n return this.cmp(another) > 0;\n }\n\n isGTE(another: Fraction): boolean {\n return this.cmp(another) >= 0;\n }\n\n isGTNum(another: number): boolean {\n return this.cmpNum(another) > 0;\n }\n\n isGTENum(another: number): boolean {\n return this.cmpNum(another) >= 0;\n }\n\n toString(): string {\n return this.num + \"/\" + this.den;\n }\n\n static max(f1: Fraction, f2: Fraction): Fraction {\n return f1.cmp(f2) > 0 ? f1 : f2;\n }\n\n static min(f1: Fraction, f2: Fraction): Fraction {\n return f1.cmp(f2) < 0 ? f1 : f2;\n }\n}\n\n// Shortcut helper\nexport const Frac = (a = 0, b = 1, factorized = false): Fraction => new Fraction(a, b, factorized);\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panyam/tsutils",
3
- "version": "0.0.64",
3
+ "version": "0.0.65",
4
4
  "author": "Sriram Panyam",
5
5
  "description": "Some basic TS utils for personal use",
6
6
  "homepage": "https://github.com/panyam/tsutils#readme",
@@ -1,54 +0,0 @@
1
- import { Nullable } from "./types";
2
- export declare class PropertySchema {
3
- name: string;
4
- units: string;
5
- defaultValue: any;
6
- minValue: Nullable<number>;
7
- maxValue: Nullable<number>;
8
- beforeSetterName: Nullable<string>;
9
- constructor(name: string, config: any);
10
- validate(newValue: number): boolean;
11
- }
12
- export declare class Property {
13
- readonly schema: PropertySchema;
14
- currentValue: any;
15
- beforeSetterFunc: any;
16
- constructor(schema: PropertySchema);
17
- clone(): Property;
18
- get value(): any;
19
- setValue(instance: any, value: any): boolean;
20
- }
21
- export declare class PropertyStore {
22
- readonly __properties__: {
23
- [key: string]: Property;
24
- };
25
- private eventBus;
26
- getPropertyNames(): string[];
27
- getProperty(name: string): Nullable<Property>;
28
- setProperty(name: string, value: any): void;
29
- }
30
- export declare const mixinPropertyStore: (target: any) => {
31
- new (): {
32
- [x: string]: any;
33
- readonly __properties__: {
34
- [key: string]: Property;
35
- };
36
- getProperties(): {
37
- [key: string]: Property;
38
- };
39
- getProperty(name: string): Property;
40
- setProperty(name: string, value: any): void;
41
- };
42
- [x: string]: any;
43
- };
44
- interface PropertyParams<T> {
45
- defaultValue?: Nullable<T>;
46
- propertiesName?: string;
47
- beforeSetterName?: Nullable<string>;
48
- units?: string;
49
- minValue?: Nullable<number>;
50
- maxValue?: Nullable<number>;
51
- }
52
- export declare function property<T>({ propertiesName, defaultValue, beforeSetterName, units, minValue, maxValue, }?: PropertyParams<T>): (target: any, propertyKey: string) => void;
53
- export declare function propertyValidator<T>(propertyName: string, propertiesName?: string): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
54
- export {};
@@ -1,158 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.propertyValidator = exports.property = exports.mixinPropertyStore = exports.PropertyStore = exports.Property = exports.PropertySchema = void 0;
4
- const bus_1 = require("./comms/bus");
5
- const misc_1 = require("./misc");
6
- class PropertySchema {
7
- constructor(name, config) {
8
- this.units = "";
9
- this.defaultValue = null;
10
- this.minValue = null;
11
- this.maxValue = null;
12
- this.beforeSetterName = null;
13
- this.name = name;
14
- this.beforeSetterName = misc_1.ifDefined(config.beforeSetterName);
15
- this.units = misc_1.ifDefined(config.units);
16
- this.defaultValue = misc_1.ifDefined(config.defaultValue);
17
- this.minValue = misc_1.ifDefined(config.minValue);
18
- this.maxValue = misc_1.ifDefined(config.maxValue);
19
- }
20
- validate(newValue) {
21
- if (this.minValue != null && newValue < this.minValue)
22
- return false;
23
- if (this.maxValue != null && newValue > this.maxValue)
24
- return false;
25
- return true;
26
- }
27
- }
28
- exports.PropertySchema = PropertySchema;
29
- class Property {
30
- constructor(schema) {
31
- this.currentValue = null;
32
- this.beforeSetterFunc = null;
33
- this.schema = schema;
34
- }
35
- clone() {
36
- const out = new Property(this.schema);
37
- out.currentValue = this.currentValue;
38
- return out;
39
- }
40
- get value() {
41
- return this.currentValue;
42
- }
43
- setValue(instance, value) {
44
- if (this.schema.beforeSetterName != null) {
45
- if (this.beforeSetterFunc == null) {
46
- const beforeSetterFunc = instance[this.schema.beforeSetterName];
47
- if (beforeSetterFunc == null) {
48
- throw new Error("Cannot find beforeSetter function: " + this.schema.beforeSetterName);
49
- }
50
- this.beforeSetterFunc = beforeSetterFunc.bind(instance);
51
- }
52
- if (this.beforeSetterFunc(value) == false) {
53
- return false;
54
- }
55
- }
56
- if (!this.schema.validate(value)) {
57
- return false;
58
- }
59
- this.currentValue = value;
60
- return true;
61
- }
62
- }
63
- exports.Property = Property;
64
- class PropertyStore {
65
- constructor() {
66
- this.__properties__ = {};
67
- this.eventBus = bus_1.EventBus.getInstance();
68
- }
69
- getPropertyNames() {
70
- return Object.keys(this.__properties__);
71
- }
72
- getProperty(name) {
73
- this[name];
74
- return this.__properties__[name] || null;
75
- }
76
- setProperty(name, value) {
77
- if (this.__properties__[name]) {
78
- const old = this.__properties__[name].value;
79
- this.__properties__[name].setValue(this, value);
80
- this.eventBus.emit("PropertyChange", {
81
- name: name,
82
- old: old,
83
- current: value,
84
- });
85
- }
86
- }
87
- }
88
- exports.PropertyStore = PropertyStore;
89
- const mixinPropertyStore = (target) => {
90
- return class extends target {
91
- constructor() {
92
- super(...arguments);
93
- this.__properties__ = {};
94
- }
95
- getProperties() {
96
- return this.__properties__;
97
- }
98
- getProperty(name) {
99
- return this.__properties__[name];
100
- }
101
- setProperty(name, value) {
102
- if (this.__properties__[name]) {
103
- this.__properties__[name].setValue(this, value);
104
- }
105
- }
106
- };
107
- };
108
- exports.mixinPropertyStore = mixinPropertyStore;
109
- function ensureProperty(schema, instance, propertyKey, propertiesName = "__properties__") {
110
- if (!instance[propertiesName]) {
111
- throw new Error(`Property store (${propertiesName}) does not exist in target.`);
112
- }
113
- const propertiesMap = instance[propertiesName];
114
- if (propertyKey in propertiesMap) {
115
- return propertiesMap[propertyKey];
116
- }
117
- const property = (propertiesMap[propertyKey] = new Property(schema));
118
- property.setValue(instance, schema.defaultValue);
119
- return property;
120
- }
121
- function property({ propertiesName = "__properties__", defaultValue = null, beforeSetterName = null, units = "", minValue = null, maxValue = null, } = {}) {
122
- return function (target, propertyKey) {
123
- const root = target;
124
- if (!(propertiesName in root)) {
125
- root[propertiesName] = {};
126
- }
127
- const propertySchemas = root[propertiesName];
128
- const newSchema = new PropertySchema(propertyKey, {
129
- units: units,
130
- beforeSetterName: beforeSetterName,
131
- defaultValue: defaultValue,
132
- minValue: minValue,
133
- maxValue: maxValue,
134
- });
135
- if (propertyKey in propertySchemas) {
136
- }
137
- propertySchemas[propertyKey] = newSchema;
138
- Object.defineProperty(target, propertyKey, {
139
- get() {
140
- const property = ensureProperty(newSchema, this, propertyKey, propertiesName);
141
- return property.value;
142
- },
143
- set(newValue) {
144
- const property = ensureProperty(newSchema, this, propertyKey, propertiesName);
145
- property.setValue(this, newValue);
146
- },
147
- enumerable: true,
148
- configurable: false,
149
- });
150
- };
151
- }
152
- exports.property = property;
153
- function propertyValidator(propertyName, propertiesName = "__properties__") {
154
- return function (target, propertyKey, descriptor) {
155
- };
156
- }
157
- exports.propertyValidator = propertyValidator;
158
- //# sourceMappingURL=properties.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"properties.js","sourceRoot":"","sources":["../../src/properties.ts"],"names":[],"mappings":";;;AACA,qCAAuC;AACvC,iCAAmC;AAEnC,MAAa,cAAc;IASzB,YAAY,IAAY,EAAE,MAAW;QAN9B,UAAK,GAAG,EAAE,CAAC;QACX,iBAAY,GAAQ,IAAI,CAAC;QACzB,aAAQ,GAAqB,IAAI,CAAC;QAClC,aAAQ,GAAqB,IAAI,CAAC;QAClC,qBAAgB,GAAqB,IAAI,CAAC;QAG/C,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,gBAAgB,GAAG,gBAAS,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;QAC3D,IAAI,CAAC,KAAK,GAAG,gBAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACrC,IAAI,CAAC,YAAY,GAAG,gBAAS,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QACnD,IAAI,CAAC,QAAQ,GAAG,gBAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAC3C,IAAI,CAAC,QAAQ,GAAG,gBAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC7C,CAAC;IAED,QAAQ,CAAC,QAAgB;QACvB,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,IAAI,QAAQ,GAAG,IAAI,CAAC,QAAQ;YAAE,OAAO,KAAK,CAAC;QACpE,IAAI,IAAI,CAAC,QAAQ,IAAI,IAAI,IAAI,QAAQ,GAAG,IAAI,CAAC,QAAQ;YAAE,OAAO,KAAK,CAAC;QAEpE,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAxBD,wCAwB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ACH,IAAI,WAAW,IAAI,eAAe,EAAE;SAEnC;QACD,eAAe,CAAC,WAAW,CAAC,GAAG,SAAS,CAAC;QAEzC,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE;YACzC,GAAG;gBACD,MAAM,QAAQ,GAAG,cAAc,CAAC,SAAS,EAAE,IAAI,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC;gBAC9E,OAAO,QAAQ,CAAC,KAAK,CAAC;YACxB,CAAC;YACD,GAAG,CAAC,QAAa;gBACf,MAAM,QAAQ,GAAG,cAAc,CAAC,SAAS,EAAE,IAAI,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC;gBAC9E,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YACpC,CAAC;YACD,UAAU,EAAE,IAAI;YAChB,YAAY,EAAE,KAAK;SACpB,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAvCD,4BAuCC;AAED,SAAgB,iBAAiB,CAAI,YAAoB,EAAE,cAAc,GAAG,gBAAgB;IAC1F,OAAO,UAAU,MAAW,EAAE,WAAmB,EAAE,UAA8B;IAGjF,CAAC,CAAC;AACJ,CAAC;AALD,8CAKC","sourcesContent":["import { Nullable } from \"./types\";\nimport { EventBus } from \"./comms/bus\";\nimport { ifDefined } from \"./misc\";\n\nexport class PropertySchema {\n // Name of the property\n public name: string;\n public units = \"\";\n public defaultValue: any = null;\n public minValue: Nullable<number> = null;\n public maxValue: Nullable<number> = null;\n public beforeSetterName: Nullable<string> = null;\n\n constructor(name: string, config: any) {\n this.name = name;\n this.beforeSetterName = ifDefined(config.beforeSetterName);\n this.units = ifDefined(config.units);\n this.defaultValue = ifDefined(config.defaultValue);\n this.minValue = ifDefined(config.minValue);\n this.maxValue = ifDefined(config.maxValue);\n }\n\n validate(newValue: number): boolean {\n if (this.minValue != null && newValue < this.minValue) return false;\n if (this.maxValue != null && newValue > this.maxValue) return false;\n // TODO - other validations\n return true;\n }\n}\n\n/**\n * Proprties are values required to fully resolve a component.\n * Like disk needs seek latency or capacity.\n */\nexport class Property {\n // Value of the property\n readonly schema: PropertySchema;\n public currentValue: any = null;\n public beforeSetterFunc: any = null;\n\n constructor(schema: PropertySchema) {\n this.schema = schema;\n }\n\n clone(): Property {\n const out = new Property(this.schema);\n out.currentValue = this.currentValue;\n return out;\n }\n\n get value() {\n return this.currentValue;\n }\n\n setValue(instance: any, value: any): boolean {\n if (this.schema.beforeSetterName != null) {\n if (this.beforeSetterFunc == null) {\n const beforeSetterFunc = instance[this.schema.beforeSetterName];\n if (beforeSetterFunc == null) {\n throw new Error(\"Cannot find beforeSetter function: \" + this.schema.beforeSetterName);\n }\n this.beforeSetterFunc = beforeSetterFunc.bind(instance);\n }\n if (this.beforeSetterFunc(value) == false) {\n // validation failed\n return false;\n }\n }\n if (!this.schema.validate(value)) {\n return false;\n }\n this.currentValue = value;\n return true;\n }\n}\n\nexport class PropertyStore {\n /**\n * The properties for this Element\n */\n readonly __properties__: { [key: string]: Property } = {};\n\n private eventBus = EventBus.getInstance();\n\n getPropertyNames(): string[] {\n // return Object.keys((this.constructor as any).__properties__);\n return Object.keys(this.__properties__);\n }\n\n getProperty(name: string): Nullable<Property> {\n (this as any)[name];\n return this.__properties__[name] || null;\n }\n\n setProperty(name: string, value: any): void {\n if (this.__properties__[name]) {\n const old = this.__properties__[name].value;\n this.__properties__[name].setValue(this, value);\n // TODO: Maybe need a fully qualified name here.\n this.eventBus.emit(\"PropertyChange\", {\n name: name,\n old: old,\n current: value,\n });\n }\n }\n}\n\nexport const mixinPropertyStore = (target: any) => {\n return class extends target {\n /**\n * The properties for this Element\n */\n readonly __properties__: { [key: string]: Property } = {};\n\n getProperties(): { [key: string]: Property } {\n return this.__properties__;\n }\n\n getProperty(name: string): Property {\n return this.__properties__[name];\n }\n\n setProperty(name: string, value: any): void {\n if (this.__properties__[name]) {\n this.__properties__[name].setValue(this, value);\n }\n }\n };\n};\n\ninterface PropertyParams<T> {\n defaultValue?: Nullable<T>;\n propertiesName?: string;\n beforeSetterName?: Nullable<string>;\n units?: string;\n minValue?: Nullable<number>;\n maxValue?: Nullable<number>;\n}\n\nfunction ensureProperty<T>(\n schema: PropertySchema,\n instance: any,\n propertyKey: string,\n propertiesName = \"__properties__\",\n): Property {\n if (!instance[propertiesName]) {\n throw new Error(`Property store (${propertiesName}) does not exist in target.`);\n }\n const propertiesMap = instance[propertiesName];\n if (propertyKey in propertiesMap) {\n return propertiesMap[propertyKey];\n }\n const property = (propertiesMap[propertyKey] = new Property(schema));\n property.setValue(instance, schema.defaultValue);\n return property;\n}\n\n/**\n * property is a decorator factory. It returns a decorator that can be applied to a property.\n * This decorator allows us to have some properties of an Element be treated specially (so it\n * can be reflected, configured/enumerated via UI). This decorator in a way makes our Element\n * attributes metadata-able so we dont have to duplicate a property object and an attribute\n * manually.\n */\nexport function property<T>({\n propertiesName = \"__properties__\",\n defaultValue = null,\n beforeSetterName = null,\n units = \"\",\n minValue = null,\n maxValue = null,\n}: PropertyParams<T> = {}) {\n return function (target: any, propertyKey: string) {\n const root = target; // .constructor;\n if (!(propertiesName in root)) {\n root[propertiesName] = {};\n }\n const propertySchemas = root[propertiesName];\n const newSchema = new PropertySchema(propertyKey, {\n units: units,\n beforeSetterName: beforeSetterName,\n defaultValue: defaultValue,\n minValue: minValue,\n maxValue: maxValue,\n });\n if (propertyKey in propertySchemas) {\n // throw new Error(\"Duplicate property: \" + propertyKey);\n }\n propertySchemas[propertyKey] = newSchema;\n\n Object.defineProperty(target, propertyKey, {\n get() {\n const property = ensureProperty(newSchema, this, propertyKey, propertiesName);\n return property.value;\n },\n set(newValue: any) {\n const property = ensureProperty(newSchema, this, propertyKey, propertiesName);\n property.setValue(this, newValue);\n },\n enumerable: true,\n configurable: false,\n });\n };\n}\n\nexport function propertyValidator<T>(propertyName: string, propertiesName = \"__properties__\") {\n return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {\n // const property = ensureProperty(this, propertyKey, null, propertiesName);\n // property.setValue(\n };\n}\n"]}
@@ -1,54 +0,0 @@
1
- import { Nullable } from "./types";
2
- export declare class PropertySchema {
3
- name: string;
4
- units: string;
5
- defaultValue: any;
6
- minValue: Nullable<number>;
7
- maxValue: Nullable<number>;
8
- beforeSetterName: Nullable<string>;
9
- constructor(name: string, config: any);
10
- validate(newValue: number): boolean;
11
- }
12
- export declare class Property {
13
- readonly schema: PropertySchema;
14
- currentValue: any;
15
- beforeSetterFunc: any;
16
- constructor(schema: PropertySchema);
17
- clone(): Property;
18
- get value(): any;
19
- setValue(instance: any, value: any): boolean;
20
- }
21
- export declare class PropertyStore {
22
- readonly __properties__: {
23
- [key: string]: Property;
24
- };
25
- private eventBus;
26
- getPropertyNames(): string[];
27
- getProperty(name: string): Nullable<Property>;
28
- setProperty(name: string, value: any): void;
29
- }
30
- export declare const mixinPropertyStore: (target: any) => {
31
- new (): {
32
- [x: string]: any;
33
- readonly __properties__: {
34
- [key: string]: Property;
35
- };
36
- getProperties(): {
37
- [key: string]: Property;
38
- };
39
- getProperty(name: string): Property;
40
- setProperty(name: string, value: any): void;
41
- };
42
- [x: string]: any;
43
- };
44
- interface PropertyParams<T> {
45
- defaultValue?: Nullable<T>;
46
- propertiesName?: string;
47
- beforeSetterName?: Nullable<string>;
48
- units?: string;
49
- minValue?: Nullable<number>;
50
- maxValue?: Nullable<number>;
51
- }
52
- export declare function property<T>({ propertiesName, defaultValue, beforeSetterName, units, minValue, maxValue, }?: PropertyParams<T>): (target: any, propertyKey: string) => void;
53
- export declare function propertyValidator<T>(propertyName: string, propertiesName?: string): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void;
54
- export {};
@@ -1,149 +0,0 @@
1
- import { EventBus } from "./comms/bus";
2
- import { ifDefined } from "./misc";
3
- export class PropertySchema {
4
- constructor(name, config) {
5
- this.units = "";
6
- this.defaultValue = null;
7
- this.minValue = null;
8
- this.maxValue = null;
9
- this.beforeSetterName = null;
10
- this.name = name;
11
- this.beforeSetterName = ifDefined(config.beforeSetterName);
12
- this.units = ifDefined(config.units);
13
- this.defaultValue = ifDefined(config.defaultValue);
14
- this.minValue = ifDefined(config.minValue);
15
- this.maxValue = ifDefined(config.maxValue);
16
- }
17
- validate(newValue) {
18
- if (this.minValue != null && newValue < this.minValue)
19
- return false;
20
- if (this.maxValue != null && newValue > this.maxValue)
21
- return false;
22
- return true;
23
- }
24
- }
25
- export class Property {
26
- constructor(schema) {
27
- this.currentValue = null;
28
- this.beforeSetterFunc = null;
29
- this.schema = schema;
30
- }
31
- clone() {
32
- const out = new Property(this.schema);
33
- out.currentValue = this.currentValue;
34
- return out;
35
- }
36
- get value() {
37
- return this.currentValue;
38
- }
39
- setValue(instance, value) {
40
- if (this.schema.beforeSetterName != null) {
41
- if (this.beforeSetterFunc == null) {
42
- const beforeSetterFunc = instance[this.schema.beforeSetterName];
43
- if (beforeSetterFunc == null) {
44
- throw new Error("Cannot find beforeSetter function: " + this.schema.beforeSetterName);
45
- }
46
- this.beforeSetterFunc = beforeSetterFunc.bind(instance);
47
- }
48
- if (this.beforeSetterFunc(value) == false) {
49
- return false;
50
- }
51
- }
52
- if (!this.schema.validate(value)) {
53
- return false;
54
- }
55
- this.currentValue = value;
56
- return true;
57
- }
58
- }
59
- export class PropertyStore {
60
- constructor() {
61
- this.__properties__ = {};
62
- this.eventBus = EventBus.getInstance();
63
- }
64
- getPropertyNames() {
65
- return Object.keys(this.__properties__);
66
- }
67
- getProperty(name) {
68
- this[name];
69
- return this.__properties__[name] || null;
70
- }
71
- setProperty(name, value) {
72
- if (this.__properties__[name]) {
73
- const old = this.__properties__[name].value;
74
- this.__properties__[name].setValue(this, value);
75
- this.eventBus.emit("PropertyChange", {
76
- name: name,
77
- old: old,
78
- current: value,
79
- });
80
- }
81
- }
82
- }
83
- export const mixinPropertyStore = (target) => {
84
- return class extends target {
85
- constructor() {
86
- super(...arguments);
87
- this.__properties__ = {};
88
- }
89
- getProperties() {
90
- return this.__properties__;
91
- }
92
- getProperty(name) {
93
- return this.__properties__[name];
94
- }
95
- setProperty(name, value) {
96
- if (this.__properties__[name]) {
97
- this.__properties__[name].setValue(this, value);
98
- }
99
- }
100
- };
101
- };
102
- function ensureProperty(schema, instance, propertyKey, propertiesName = "__properties__") {
103
- if (!instance[propertiesName]) {
104
- throw new Error(`Property store (${propertiesName}) does not exist in target.`);
105
- }
106
- const propertiesMap = instance[propertiesName];
107
- if (propertyKey in propertiesMap) {
108
- return propertiesMap[propertyKey];
109
- }
110
- const property = (propertiesMap[propertyKey] = new Property(schema));
111
- property.setValue(instance, schema.defaultValue);
112
- return property;
113
- }
114
- export function property({ propertiesName = "__properties__", defaultValue = null, beforeSetterName = null, units = "", minValue = null, maxValue = null, } = {}) {
115
- return function (target, propertyKey) {
116
- const root = target;
117
- if (!(propertiesName in root)) {
118
- root[propertiesName] = {};
119
- }
120
- const propertySchemas = root[propertiesName];
121
- const newSchema = new PropertySchema(propertyKey, {
122
- units: units,
123
- beforeSetterName: beforeSetterName,
124
- defaultValue: defaultValue,
125
- minValue: minValue,
126
- maxValue: maxValue,
127
- });
128
- if (propertyKey in propertySchemas) {
129
- }
130
- propertySchemas[propertyKey] = newSchema;
131
- Object.defineProperty(target, propertyKey, {
132
- get() {
133
- const property = ensureProperty(newSchema, this, propertyKey, propertiesName);
134
- return property.value;
135
- },
136
- set(newValue) {
137
- const property = ensureProperty(newSchema, this, propertyKey, propertiesName);
138
- property.setValue(this, newValue);
139
- },
140
- enumerable: true,
141
- configurable: false,
142
- });
143
- };
144
- }
145
- export function propertyValidator(propertyName, propertiesName = "__properties__") {
146
- return function (target, propertyKey, descriptor) {
147
- };
148
- }
149
- //# sourceMappingURL=properties.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"properties.js","sourceRoot":"","sources":["../../src/properties.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAC;AAEnC,MAAM,OA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AAS,EAAE,IAAI,EAAE,WAAW,EAAE,cAAc,CAAC,CAAC;gBAC9E,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;YACpC,CAAC;YACD,UAAU,EAAE,IAAI;YAChB,YAAY,EAAE,KAAK;SACpB,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAI,YAAoB,EAAE,cAAc,GAAG,gBAAgB;IAC1F,OAAO,UAAU,MAAW,EAAE,WAAmB,EAAE,UAA8B;IAGjF,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { Nullable } from \"./types\";\nimport { EventBus } from \"./comms/bus\";\nimport { ifDefined } from \"./misc\";\n\nexport class PropertySchema {\n // Name of the property\n public name: string;\n public units = \"\";\n public defaultValue: any = null;\n public minValue: Nullable<number> = null;\n public maxValue: Nullable<number> = null;\n public beforeSetterName: Nullable<string> = null;\n\n constructor(name: string, config: any) {\n this.name = name;\n this.beforeSetterName = ifDefined(config.beforeSetterName);\n this.units = ifDefined(config.units);\n this.defaultValue = ifDefined(config.defaultValue);\n this.minValue = ifDefined(config.minValue);\n this.maxValue = ifDefined(config.maxValue);\n }\n\n validate(newValue: number): boolean {\n if (this.minValue != null && newValue < this.minValue) return false;\n if (this.maxValue != null && newValue > this.maxValue) return false;\n // TODO - other validations\n return true;\n }\n}\n\n/**\n * Proprties are values required to fully resolve a component.\n * Like disk needs seek latency or capacity.\n */\nexport class Property {\n // Value of the property\n readonly schema: PropertySchema;\n public currentValue: any = null;\n public beforeSetterFunc: any = null;\n\n constructor(schema: PropertySchema) {\n this.schema = schema;\n }\n\n clone(): Property {\n const out = new Property(this.schema);\n out.currentValue = this.currentValue;\n return out;\n }\n\n get value() {\n return this.currentValue;\n }\n\n setValue(instance: any, value: any): boolean {\n if (this.schema.beforeSetterName != null) {\n if (this.beforeSetterFunc == null) {\n const beforeSetterFunc = instance[this.schema.beforeSetterName];\n if (beforeSetterFunc == null) {\n throw new Error(\"Cannot find beforeSetter function: \" + this.schema.beforeSetterName);\n }\n this.beforeSetterFunc = beforeSetterFunc.bind(instance);\n }\n if (this.beforeSetterFunc(value) == false) {\n // validation failed\n return false;\n }\n }\n if (!this.schema.validate(value)) {\n return false;\n }\n this.currentValue = value;\n return true;\n }\n}\n\nexport class PropertyStore {\n /**\n * The properties for this Element\n */\n readonly __properties__: { [key: string]: Property } = {};\n\n private eventBus = EventBus.getInstance();\n\n getPropertyNames(): string[] {\n // return Object.keys((this.constructor as any).__properties__);\n return Object.keys(this.__properties__);\n }\n\n getProperty(name: string): Nullable<Property> {\n (this as any)[name];\n return this.__properties__[name] || null;\n }\n\n setProperty(name: string, value: any): void {\n if (this.__properties__[name]) {\n const old = this.__properties__[name].value;\n this.__properties__[name].setValue(this, value);\n // TODO: Maybe need a fully qualified name here.\n this.eventBus.emit(\"PropertyChange\", {\n name: name,\n old: old,\n current: value,\n });\n }\n }\n}\n\nexport const mixinPropertyStore = (target: any) => {\n return class extends target {\n /**\n * The properties for this Element\n */\n readonly __properties__: { [key: string]: Property } = {};\n\n getProperties(): { [key: string]: Property } {\n return this.__properties__;\n }\n\n getProperty(name: string): Property {\n return this.__properties__[name];\n }\n\n setProperty(name: string, value: any): void {\n if (this.__properties__[name]) {\n this.__properties__[name].setValue(this, value);\n }\n }\n };\n};\n\ninterface PropertyParams<T> {\n defaultValue?: Nullable<T>;\n propertiesName?: string;\n beforeSetterName?: Nullable<string>;\n units?: string;\n minValue?: Nullable<number>;\n maxValue?: Nullable<number>;\n}\n\nfunction ensureProperty<T>(\n schema: PropertySchema,\n instance: any,\n propertyKey: string,\n propertiesName = \"__properties__\",\n): Property {\n if (!instance[propertiesName]) {\n throw new Error(`Property store (${propertiesName}) does not exist in target.`);\n }\n const propertiesMap = instance[propertiesName];\n if (propertyKey in propertiesMap) {\n return propertiesMap[propertyKey];\n }\n const property = (propertiesMap[propertyKey] = new Property(schema));\n property.setValue(instance, schema.defaultValue);\n return property;\n}\n\n/**\n * property is a decorator factory. It returns a decorator that can be applied to a property.\n * This decorator allows us to have some properties of an Element be treated specially (so it\n * can be reflected, configured/enumerated via UI). This decorator in a way makes our Element\n * attributes metadata-able so we dont have to duplicate a property object and an attribute\n * manually.\n */\nexport function property<T>({\n propertiesName = \"__properties__\",\n defaultValue = null,\n beforeSetterName = null,\n units = \"\",\n minValue = null,\n maxValue = null,\n}: PropertyParams<T> = {}) {\n return function (target: any, propertyKey: string) {\n const root = target; // .constructor;\n if (!(propertiesName in root)) {\n root[propertiesName] = {};\n }\n const propertySchemas = root[propertiesName];\n const newSchema = new PropertySchema(propertyKey, {\n units: units,\n beforeSetterName: beforeSetterName,\n defaultValue: defaultValue,\n minValue: minValue,\n maxValue: maxValue,\n });\n if (propertyKey in propertySchemas) {\n // throw new Error(\"Duplicate property: \" + propertyKey);\n }\n propertySchemas[propertyKey] = newSchema;\n\n Object.defineProperty(target, propertyKey, {\n get() {\n const property = ensureProperty(newSchema, this, propertyKey, propertiesName);\n return property.value;\n },\n set(newValue: any) {\n const property = ensureProperty(newSchema, this, propertyKey, propertiesName);\n property.setValue(this, newValue);\n },\n enumerable: true,\n configurable: false,\n });\n };\n}\n\nexport function propertyValidator<T>(propertyName: string, propertiesName = \"__properties__\") {\n return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {\n // const property = ensureProperty(this, propertyKey, null, propertiesName);\n // property.setValue(\n };\n}\n"]}