UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 724 B
/*! Copyright 2023 the gnablib contributors MPL-1.1 */ import{hex as t}from"../codec/index.js";export class Assert{static inClosedOpen(t,e,n){if(!(t>=e&&t<n))throw new Error(`Number '${t}' not in range (${e} <= n < ${n})`)}static equalish(t,e,n,r="Number"){let o=e,c="expect",s=e,a="expect";if(n.amount){const t=e-n.amount,r=e+n.amount,i="amt";t<o&&(o=t,c=i),r>s&&(s=r,a=i)}if(n.percent){const t=e*(1-n.percent/100),r=e*(1+n.percent/100),i="%";t<o&&(o=t,c=i),r>s&&(s=r,a=i)}const i=Math.abs(t-e);if(!(t>=o&&t<=s))throw new Error(`${r} '${t}' not in range (${o} (${c}) <= ${e} <= ${s} (${a})) ${i}/${i/e*100}% off`)}static bytesMatchHex(e,n){const r=t.fromBytes(e);if(r!==n)throw new Error(`Expect: ${n}\n Found : ${r}`)}}