@balena/balena-pricing 2.0.1 → 2.0.2

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,2 @@
1
- !function(){"use strict";var t,e={};var r=function(){if(t)return e;let r;if(t=1,Object.defineProperty(e,"__esModule",{value:!0}),e.TypedError=void 0,null!=Error.captureStackTrace){const t=Error.captureStackTrace;r=e=>{t(e,e.constructor)}}else r=(t,e)=>{e instanceof Error||(e=new Error(e)),null!=e.stack&&(t.stack=e.stack)};class i extends Error{constructor(t=""){super(),this.stack="",t instanceof Error?this.message=t.message:this.message=t,Object.setPrototypeOf(this,new.target.prototype),this.name=this.constructor.name,r(this,t)}}return e.TypedError=i,e}();class i extends r.TypedError{constructor(t){super(t)}}const s={"device:microservices":[{version:1,validFrom:new Date("2023-02-01T00:00:00Z"),firstDiscountPriceCents:149,discountRate:.33,discountThreshold:12e3,discountThresholdPriceCents:125},{version:2,validFrom:new Date("2023-03-08T00:00:00Z"),firstDiscountPriceCents:199,discountRate:.33,discountThreshold:12e3,discountThresholdPriceCents:150}]};function o(t,e){return e>=t.discountThresholdPriceCents?(e-t.firstDiscountPriceCents)*(t.discountThreshold-1)/(t.discountThresholdPriceCents-t.firstDiscountPriceCents)+1:t.discountThreshold*10**(Math.log10(e/t.discountThresholdPriceCents)/Math.log10(1-t.discountRate))}window.CreditPricing=class{constructor(t={}){var e,r;this.credits=function(t){const e={};for(const[r,i]of Object.entries(t))e[r]=i.sort(((t,e)=>e.validFrom.getTime()-t.validFrom.getTime()));return e}(null!==(e=t.credits)&&void 0!==e?e:s),function(t){for(const[e,r]of Object.entries(t)){const t=new Set,s=new Set;for(const o of r){if(t.has(o.version))throw new i(`Duplicate version ${o.version} for feature ${e}`);const r=o.validFrom.toISOString();if(s.has(r))throw new i(`Duplicate validFrom ${r} for feature ${e}`);t.add(o.version),s.add(r)}}}(this.credits),this.target=null!==(r=t.target)&&void 0!==r?r:"current"}getDefinition(t){if(null==this.credits[t])throw new i(`Feature ${t} not supported for credits`);let e;return"latest"===this.target?this.credits[t][0]:"number"==typeof this.target?(e=this.credits[t].find((t=>t.version===this.target)),e):this.target instanceof Date?this.credits[t].find((t=>t.validFrom<=this.target)):this.credits[t].find((t=>t.validFrom<=new Date))}fixRange(t,e,r){let i=r,s=this.getCreditPrice(t,0,i);for(;s>e;)i+=1,s=this.getCreditPrice(t,0,i);for(;s===e;)i-=1,s=this.getCreditPrice(t,0,i);return i}getCreditRange(t,e,r=0){if(!Number.isInteger(e))throw new i("Unit cost must be a whole number");if(e<=0)throw new i("Unit cost must be greater than 0");if(!Number.isInteger(r))throw new i("Available credits must be a whole number");if(r<0)throw new i("Available credits must be greater than or equal to 0");const s=this.getDefinition(t);if(null==s)throw new i("Requested feature not allowed for credit usage");if(e>s.firstDiscountPriceCents)throw new i(`Unit cost cannot be greater than ${s.firstDiscountPriceCents}`);const n={from:e===s.firstDiscountPriceCents?1:Math.ceil(o(s,e+.5))};if(e>1&&(n.to=Math.floor(o(s,e-.5))),n.from>1&&(this.getCreditPrice(t,0,n.from)!==e||this.getCreditPrice(t,0,n.from-1)!==e+1)&&(n.from=this.fixRange(t,e,n.from)+1),!n.to||this.getCreditPrice(t,0,n.to)===e&&this.getCreditPrice(t,0,n.to+1)===e-1||(n.to=this.fixRange(t,e-1,n.to)),r>0&&(n.from=Math.max(n.from-r,0)),n.to&&r>0&&(n.to=Math.max(n.to-r,0)),0===n.from&&(0===n.to||null==n.to))throw new Error("Requested unit cost is too high");return n}getCreditPrice(t,e,r){if(!Number.isInteger(e))throw new i("Available credits must be a whole number");if(!Number.isInteger(r))throw new i("Credit purchase amount must be a whole number");if(e<0)throw new i("Available credits must be greater than or equal to 0");if(r<=0)throw new i("Credit purchase amount must be greater than 0");const s=this.getDefinition(t);if(null==s)throw new i("Requested feature not allowed for credit usage");const o=e+r;if(0===r||0===o)return 0;if(o<=s.discountThreshold)return Math.round(s.firstDiscountPriceCents+(s.discountThresholdPriceCents-s.firstDiscountPriceCents)/(s.discountThreshold-1)*(o-1));const n=Math.round(s.discountThresholdPriceCents*Math.pow(1-s.discountRate,Math.log10(o/s.discountThreshold)));if(n<=0)throw new i("The provided quantity surpasses the maximum supported amount of credits");return n}getCreditTotalPrice(t,e,r){return Math.round(this.getCreditPrice(t,e,r)*r)}getDiscountOverDynamic(t,e,r,i){return Math.round((i-this.getCreditPrice(t,e,r))/i*100)}getTotalSavings(t,e,r,i){return Math.round(r*(i-this.getCreditPrice(t,e,r)))}}}();
1
+ !function(){"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=require("./index");window.CreditPricing=e.CreditPricing}();
2
+ //# sourceMappingURL=browser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"browser.js","sources":["../src/browser.ts"],"sourcesContent":[null],"names":["index_1","require","window","CreditPricing"],"mappings":"gFAAA,MAAwCA,EAAAC,QAAA,WAGvCC,OAAeC,cAAgBH,EAAAG"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@balena/balena-pricing",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Balena pricing/savings calculators",
5
5
  "homepage": "https://github.com/balena-io-modules/balena-pricing#readme",
6
6
  "main": "dist",
@@ -51,8 +51,9 @@
51
51
  "lint-staged": "^15.4.3",
52
52
  "mocha": "^11.1.0",
53
53
  "rimraf": "^6.0.1",
54
- "ts-node": "^10.9.2",
55
54
  "rollup": "^4.34.6",
55
+ "ts-node": "^10.9.2",
56
+ "tslib": "^2.8.1",
56
57
  "typescript": "^5.7.3"
57
58
  },
58
59
  "lint-staged": {
@@ -67,7 +68,7 @@
67
68
  "_": "test/*.spec.ts"
68
69
  },
69
70
  "versionist": {
70
- "publishedAt": "2025-02-14T12:58:27.507Z"
71
+ "publishedAt": "2025-06-08T00:02:02.193Z"
71
72
  },
72
73
  "dependencies": {
73
74
  "typed-error": "^3.2.2"