Skip to content

Instantly share code, notes, and snippets.

@wirwolf
Last active March 18, 2023 17:37
Show Gist options
  • Select an option

  • Save wirwolf/ec023ad8f6716c1ebbf9a5721bf3d621 to your computer and use it in GitHub Desktop.

Select an option

Save wirwolf/ec023ad8f6716c1ebbf9a5721bf3d621 to your computer and use it in GitHub Desktop.

Revisions

  1. wirwolf revised this gist Mar 18, 2023. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions quantityToScalar.ts
    Original 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;
  2. wirwolf revised this gist Mar 16, 2023. 1 changed file with 0 additions and 8 deletions.
    8 changes: 0 additions & 8 deletions quantityToScalar.ts
    Original file line number Diff line number Diff line change
    @@ -2,14 +2,6 @@ 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;
  3. wirwolf created this gist Mar 16, 2023.
    67 changes: 67 additions & 0 deletions quantityToScalar.ts
    Original 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}`);
    }
    }