swantzter / kontonummer

Parse, validate and format Swedish bank account numbers
https://kontonummer.se
MIT License
5 stars 2 forks source link

Drop-in replacement of jop-io/kontonummer.js #31

Closed ariselseng closed 1 year ago

ariselseng commented 1 year ago

Hi, Thank you for this project. It would be nice to be able to get a single file drop in replacement for kontonummer.js I am not very familiar with your tooling, but maybe it is not that hard to achieve?

swantzter commented 1 year ago

Hi,

I'd assume it'd look something like this:

function kontonummer (number: string) {
  try {
    const k = new Kontonummer(number)
    return {
      bank_name: k.bankName,
      clearing_number: k.sortingCode, 
      account_number: k.accountNumber
    }
  } catch {
    return false
  }
}

However I'm not sure if it's right to place it in this library, or if it's better to add that snippet to your own codebase to simplify migration

ariselseng commented 1 year ago

Thanks. In my project there is no npm/yarn. We are primarily a backend service with some very simple html forms. If I could choose, I would want a simple js file I could include.

swantzter commented 1 year ago

Fair, I'll havw to test this when I have a computer at hand, but I think you could do something like

<script type="module">
import Kontonummer from 'https://cdn.jsdelivr.net/npm/kontonummer@4.0.0/dist/esm/index.min.js'

function kontonummer (number) {
  try {
    const k = new Kontonummer(number)
    return {
      bank_name: k.bankName,
      clearing_number: k.sortingCode, 
      account_number: k.accountNumber
    }
  } catch {
    return false
  }
}
</script>
ariselseng commented 1 year ago

Thank you so much for following up on this. The example is a great start, but I am afraid the dependencies When it is importing the other files it cannot find them due to the missing extension in the url: image I tried building from source manually and add ".js" on all the imports and then it worked.

swantzter commented 1 year ago

Yeah I was afraid that would happen. I'll see if I can add that to make sure it works when imported in browsers, and push out a new patch version

swantzter commented 1 year ago

All right, 4.0.1 should do the trick, I've also added the above snippet as an example to the release notes

ariselseng commented 1 year ago

Thanks, it works! To actually get kontonumber function available in my code I needed to add it to window:


 <script type="module">
        import Kontonummer from 'https://cdn.jsdelivr.net/npm/kontonummer@4.0.1/dist/esm/index.min.js'

        window.kontonummer = function (number) {
            try {
                const k = new Kontonummer(number)
                return {
                    bank_name: k.bankName,
                    clearing_number: k.sortingCode,
                    account_number: k.accountNumber
                }
            } catch {
                return false
            }
        }
    </script>