Last active
March 18, 2023 17:37
-
-
Save wirwolf/ec023ad8f6716c1ebbf9a5721bf3d621 to your computer and use it in GitHub Desktop.
Revisions
-
wirwolf revised this gist
Mar 18, 2023 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,11 @@ function findSuffix(quantity: string): string { let ix = quantity.length - 1; while (ix >= 0 && !/[\.0-9]/.test(quantity.charAt(ix))) { ix--; } return ix === -1 ? '' : quantity.substring(ix + 1); } function quantityToScalar(quantity: string): number | bigint { if (!quantity) { return 0; -
wirwolf revised this gist
Mar 16, 2023 . 1 changed file with 0 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,14 +2,6 @@ function quantityToScalar(quantity: string): number | bigint { if (!quantity) { return 0; } switch (suffix) { case 'n': return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000_000.0; -
wirwolf created this gist
Mar 16, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ function quantityToScalar(quantity: string): number | bigint { if (!quantity) { return 0; } const suffix = findSuffix(quantity); if (suffix === '') { const num = Number(quantity).valueOf(); if (isNaN(num)) { throw new Error('Unknown quantity ' + quantity); } return num; } switch (suffix) { case 'n': return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000_000.0; case 'u': return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1_000_000.0; case 'm': return Number(quantity.substr(0, quantity.length - 1)).valueOf() / 1000.0; case 'k': return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000); case 'M': return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000); case 'G': return BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000); case 'T': return ( BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000) ); case 'P': return ( BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000 * 1000) ); case 'E': return ( BigInt(quantity.substr(0, quantity.length - 1)) * BigInt(1000 * 1000 * 1000) * BigInt(1000 * 1000 * 1000) ); case 'Ki': return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024); case 'Mi': return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024); case 'Gi': return BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024); case 'Ti': return ( BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024) ); case 'Pi': return ( BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024 * 1024) ); case 'Ei': return ( BigInt(quantity.substr(0, quantity.length - 2)) * BigInt(1024 * 1024 * 1024) * BigInt(1024 * 1024 * 1024) ); default: throw new Error(`Unknown suffix: ${suffix}`); } }