npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-currency-directive

v1.5.5

Published

Simple, quick custom directive for handling currency format inside text inputs.

Downloads

150

Readme

Vue-currency-directive

Simple, quick custom directive for handling currency format inside text inputs.

Build Status Version Downloads License

Compatible with Vue 2.x

  • Demo/Playground
  • Installation
  • Usage
  • Global registration
  • Local registration
  • Dynamic arguments
  • Examples

Installation

npm i vue-currency-directive || yarn add vue-currency-directive

Usage

  • Register in your data() a main state object e.g. amount and inside it 2 main properties value and formatted.
  • You mainly get 2 outputs: one for the unformatted/original value and the other for the formatted value.
  • Valid values for currency USD, EUR, GBP, EGP, SAR, for more Currency Codes (ISO 4217 Standard).
  • Valid values for locale en-US, de-DE, fr-FR, ar-EG, ar-SA, for more List of locales.

In DOM/Single-file-component

<input v-currency:<currency?>|[<minimumFractionDigits?>,<maximumFractionDigits?> ,<locale?>]="<bindingExpression>">

v-currency="amount.value"
v-currency:EUR="amount.value"
v-currency:EUR|[1,2,(en-GB)]="amount.value"

For example:

<template>
  <input v-currency="amount.value">
  <input v-currency="foo.value">
  <input v-currency="bar.value">
</template>

<script>
...
export default {
  data(){
    return {
      amount: { // naming is not strict 'amount, foo, bar, ...etc'
        value: '', 
        formatted: '' // Better to be empty
      }, 

      foo: {
        value: '',
        formatted: '' // Better to be empty
      },

      bar: {
        value: '',
        formatted: '' // Better to be empty
      }
    }
  }
}
...
</script>

With a Component/controlled input

const CurrencyInput = {
  template: '<input />',
}

// In DOM/Single-file-component
<CurrencyInput v-currency="amount.value">

Note: in case you are using a multiple or a group of different inputs with different types and you are not sure that the currency input is going to be indexed as the first item then you could pass a class name .v-currency-input other than that if it's going to be the only or the first input in the group then you don't have to.

Global registration

import Vue from 'vue';
import vueCurrencyDirective from 'vue-currency-directive';

Vue.directive('currency', vueCurrencyDirective);

Local registration

<script>
import vueCurrencyDirective from 'vue-currency-directive';
export default {
  ...
  data(){
    amount: {
      value: '', 
      formatted: '' // Better to be empty
    }, 
  },
  directives: {
    currency: vueCurrencyDirective
  },
  ...
}
</script>

Dynamic arguments

In case you want to handle arguments in more dynamic way based on data changes and not sticking with a specific currency and locale, just add 2 more state inputs currency and locale inside the parent object e.g. amount in our case and remove any directive args e.g.:EUR[de-DE] from the component and vice-versa:

<template>
  <input v-currency="amount.value" />

  //Currency selector
  <select v-model="amount.currency">
    <option value="USD">USD</option>
    <option value="EUR">EUR</option>
    <option value="GBP">GBP</option>
  </select>

  //Locale selector
  <select v-model="amount.locale">
    <option value="en-US">US</option>
    <option value="de-DE">DE</option>
    <option value="en-GB">GB</option>
  </select>
  
</template>

<script>
...
export default {
  data(){
    return {
      amount: { // naming is not strict 'amount, foo, bar, ...etc'
        value: '', 
        currency: '',
        locale: '',
        formatted: '' // Better to be empty
      }
    }
  }
}
...
</script>

Examples

Passing no arguments will reflect to USD currency by default and for locale it will use the configured browser language.

<input v-currency="amount.value"> // amount.value = 3244
//Output: $3,244.00

Passing currency argument only without locale.

<input v-currency:EUR="amount.value"> // amount.value = 100234
//Output: €100,234.00

Passing with locale argument and different currency.

<input v-currency:EGP[ar-EG]="amount.value"> // amount.value = 554342
//Output: ٥٥٤٬٣٤٢٫٠٠ ج.م.‏